diff --git a/core/corehttp/gateway_handler_unixfs_file.go b/core/corehttp/gateway_handler_unixfs_file.go index 5053558dc1e4..13f0ec57fb01 100644 --- a/core/corehttp/gateway_handler_unixfs_file.go +++ b/core/corehttp/gateway_handler_unixfs_file.go @@ -37,6 +37,13 @@ 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). + w.WriteHeader(http.StatusOK) + return + } + // Lazy seeker enables efficient range-requests and HTTP HEAD responses content := &lazySeeker{ size: size, diff --git a/test/sharness/t0110-gateway.sh b/test/sharness/t0110-gateway.sh index ba6ecb354cba..0d047f0232e2 100755 --- a/test/sharness/t0110-gateway.sh +++ b/test/sharness/t0110-gateway.sh @@ -103,6 +103,13 @@ 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 starting at 0 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_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 "" response_with_double_ipfs_ns &&