fix(gw): send 200 for empty files
Fixes #9238
Jorropo committed
Aug 31, 2022 at 17:58 UTC
df222053856d3967ff0b4d6bc513bdb66ceedd6f
2 files changed
+17
core/corehttp/gateway_handler_unixfs_file.go
+9
@@ -37,6 +37,15 @@ func (i *gatewayHandler) serveFile(ctx context.Context, w http.ResponseWriter, r
37
return
38
}
39
40
+ if size == 0 {
41
+ // We override null files to 200 to avoid issues with fragment caching reverse proxies.
42
+ // Also whatever you are asking for, it's cheaper to just give you the complete file (nothing).
43
+ // TODO: remove this if clause once https://github.com/golang/go/issues/54794 is fixed in two latest releases of go
44
+ w.Header().Set("Content-Type", "text/plain")
45
+ w.WriteHeader(http.StatusOK)
46
+ return
47
+ }
48
+
49
// Lazy seeker enables efficient range-requests and HTTP HEAD responses
50
content := &lazySeeker{
51
size: size,
test/sharness/t0110-gateway.sh
+8
@@ -103,6 +103,14 @@ test_expect_success "GET IPFS inlined zero-length data object returns ok code (2
103
test_should_contain "Content-Length: 0" empty_ok_response
104
'
105
106
+# https://github.com/ipfs/kubo/issues/9238
107
+test_expect_success "GET IPFS inlined zero-length data object with byte range returns ok code (200)" '
108
+ curl -sD - "http://127.0.0.1:$port/ipfs/bafkqaaa" -H "Range: bytes=0-1048575" > empty_ok_response &&
109
+ test_should_contain "HTTP/1.1 200 OK" empty_ok_response &&
110
+ test_should_contain "Content-Length: 0" empty_ok_response &&
111
+ test_should_contain "Content-Type: text/plain" empty_ok_response
112
+'
113
+
114
test_expect_success "GET /ipfs/ipfs/{cid} returns redirect to the valid path" '
115
curl -sD - "http://127.0.0.1:$port/ipfs/ipfs/bafkqaaa?query=to-remember" > response_with_double_ipfs_ns &&
116
test_should_contain "<meta http-equiv=\"refresh\" content=\"10;url=/ipfs/bafkqaaa?query=to-remember\" />" response_with_double_ipfs_ns &&