fix: subdomain redirect for dir CIDs
Closes #7164 License: MIT Signed-off-by: Marcin Rataj <lidel@lidel.org>
Marcin Rataj committed
Apr 15, 2020 at 13:42 UTC
8290ec11c376d8246a7e592eb4e6472479833077
2 files changed
+21
-1
core/corehttp/gateway_handler.go
+10
@@ -342,6 +342,16 @@ func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request
342
BackLink: backLink,
343
Hash: hash,
344
}
345
+
346
+ // See statusResponseWriter.WriteHeader
347
+ // and https://github.com/ipfs/go-ipfs/issues/7164
348
+ // Note: this needs to occur before listingTemplate.Execute otherwise we get
349
+ // superfluous response.WriteHeader call from prometheus/client_golang
350
+ if w.Header().Get("Location") != "" {
351
+ w.WriteHeader(http.StatusMovedPermanently)
352
+ return
353
+ }
354
+
355
err = listingTemplate.Execute(w, tplData)
356
if err != nil {
357
internalWebError(w, err)
test/sharness/t0114-gateway-subdomains.sh
+11
-1
@@ -145,7 +145,7 @@ test_localhost_gateway_response_should_contain \
145
# payload directly, but redirect to URL with proper origin isolation
146
147
test_localhost_gateway_response_should_contain \
148
- "request for localhost/ipfs/{CIDv1} returns status code HTTP 301" \
148
+ "request for localhost/ipfs/{CIDv1} returns HTTP 301 Moved Permanently" \
149
"http://localhost:$GWAY_PORT/ipfs/$CIDv1" \
150
"301 Moved Permanently"
151
@@ -154,6 +154,16 @@ test_localhost_gateway_response_should_contain \
154
"http://localhost:$GWAY_PORT/ipfs/$CIDv1" \
155
"Location: http://$CIDv1.ipfs.localhost:$GWAY_PORT/"
156
157
+test_localhost_gateway_response_should_contain \
158
+ "request for localhost/ipfs/{DIR_CID} returns HTTP 301 Moved Permanently" \
159
+ "http://localhost:$GWAY_PORT/ipfs/$DIR_CID" \
160
+ "301 Moved Permanently"
161
+
162
+test_localhost_gateway_response_should_contain \
163
+ "request for localhost/ipfs/{DIR_CID} returns Location HTTP header for subdomain redirect in browsers" \
164
+ "http://localhost:$GWAY_PORT/ipfs/$DIR_CID/" \
165
+ "Location: http://$DIR_CID.ipfs.localhost:$GWAY_PORT/"
166
+
167
# Responses to the root domain of subdomain gateway hostname should Clear-Site-Data
168
# https://github.com/ipfs/go-ipfs/issues/6975#issuecomment-597472477
169
test_localhost_gateway_response_should_contain \