Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: subdomain redirect for dir CIDs #7165

Merged
merged 1 commit into from
Apr 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions core/corehttp/gateway_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,16 @@ func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request
BackLink: backLink,
Hash: hash,
}

// See statusResponseWriter.WriteHeader
// and https://github.com/ipfs/go-ipfs/issues/7164
// Note: this needs to occur before listingTemplate.Execute otherwise we get
// superfluous response.WriteHeader call from prometheus/client_golang
if w.Header().Get("Location") != "" {
w.WriteHeader(http.StatusMovedPermanently)
Stebalien marked this conversation as resolved.
Show resolved Hide resolved
return
}

err = listingTemplate.Execute(w, tplData)
if err != nil {
internalWebError(w, err)
Expand Down
12 changes: 11 additions & 1 deletion test/sharness/t0114-gateway-subdomains.sh
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ test_localhost_gateway_response_should_contain \
# payload directly, but redirect to URL with proper origin isolation

test_localhost_gateway_response_should_contain \
"request for localhost/ipfs/{CIDv1} returns status code HTTP 301" \
"request for localhost/ipfs/{CIDv1} returns HTTP 301 Moved Permanently" \
"http://localhost:$GWAY_PORT/ipfs/$CIDv1" \
"301 Moved Permanently"

Expand All @@ -154,6 +154,16 @@ test_localhost_gateway_response_should_contain \
"http://localhost:$GWAY_PORT/ipfs/$CIDv1" \
"Location: http://$CIDv1.ipfs.localhost:$GWAY_PORT/"

test_localhost_gateway_response_should_contain \
"request for localhost/ipfs/{DIR_CID} returns HTTP 301 Moved Permanently" \
"http://localhost:$GWAY_PORT/ipfs/$DIR_CID" \
"301 Moved Permanently"

test_localhost_gateway_response_should_contain \
"request for localhost/ipfs/{DIR_CID} returns Location HTTP header for subdomain redirect in browsers" \
"http://localhost:$GWAY_PORT/ipfs/$DIR_CID/" \
"Location: http://$DIR_CID.ipfs.localhost:$GWAY_PORT/"

# Responses to the root domain of subdomain gateway hostname should Clear-Site-Data
# https://github.com/ipfs/go-ipfs/issues/6975#issuecomment-597472477
test_localhost_gateway_response_should_contain \
Expand Down