fix(gw): send 200 for empty files

Fixes #9238
This commit is contained in:
Jorropo 2022-08-31 17:58:41 +02:00
parent 426cb84255
commit df22205385
2 changed files with 17 additions and 0 deletions

View File

@ -37,6 +37,15 @@ func (i *gatewayHandler) serveFile(ctx context.Context, w http.ResponseWriter, r
return
}
if size == 0 {
// We override null files to 200 to avoid issues with fragment caching reverse proxies.
// Also whatever you are asking for, it's cheaper to just give you the complete file (nothing).
// TODO: remove this if clause once https://github.com/golang/go/issues/54794 is fixed in two latest releases of go
w.Header().Set("Content-Type", "text/plain")
w.WriteHeader(http.StatusOK)
return
}
// Lazy seeker enables efficient range-requests and HTTP HEAD responses
content := &lazySeeker{
size: size,

View File

@ -103,6 +103,14 @@ test_expect_success "GET IPFS inlined zero-length data object returns ok code (2
test_should_contain "Content-Length: 0" empty_ok_response
'
# https://github.com/ipfs/kubo/issues/9238
test_expect_success "GET IPFS inlined zero-length data object with byte range returns ok code (200)" '
curl -sD - "http://127.0.0.1:$port/ipfs/bafkqaaa" -H "Range: bytes=0-1048575" > empty_ok_response &&
test_should_contain "HTTP/1.1 200 OK" empty_ok_response &&
test_should_contain "Content-Length: 0" empty_ok_response &&
test_should_contain "Content-Type: text/plain" empty_ok_response
'
test_expect_success "GET /ipfs/ipfs/{cid} returns redirect to the valid path" '
curl -sD - "http://127.0.0.1:$port/ipfs/ipfs/bafkqaaa?query=to-remember" > response_with_double_ipfs_ns &&
test_should_contain "<meta http-equiv=\"refresh\" content=\"10;url=/ipfs/bafkqaaa?query=to-remember\" />" response_with_double_ipfs_ns &&