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

feat(gw): /ipfs/ipfs/{cid} → /ipfs/{cid} #7930

Merged
merged 6 commits into from
Mar 24, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
11 changes: 11 additions & 0 deletions core/corehttp/gateway_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,17 @@ func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request

parsedPath := ipath.New(urlPath)
if err := parsedPath.IsValid(); err != nil {
// Attempt to fix redundant /ipfs/ namespace as long resulting
lidel marked this conversation as resolved.
Show resolved Hide resolved
// 'intended' path is valid. This is in case gremlins were tickled
// wrong way and user ended up at /ipfs/ipfs/{cid} or /ipfs/ipns/{id}
// like in bafybeien3m7mdn6imm425vc2s22erzyhbvk5n3ofzgikkhmdkh5cuqbpbq
// :^))
intendedPath := ipath.New(strings.TrimPrefix(urlPath, "/ipfs"))
Stebalien marked this conversation as resolved.
Show resolved Hide resolved
if err2 := intendedPath.IsValid(); err2 == nil {
intendedURL := strings.Replace(r.URL.String(), urlPath, intendedPath.String(), 1)
http.Redirect(w, r, intendedURL, http.StatusMovedPermanently)
return
}
webError(w, "invalid ipfs path", err, http.StatusBadRequest)
return
}
Expand Down
11 changes: 11 additions & 0 deletions test/sharness/t0110-gateway.sh
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@ test_expect_success "GET IPFS nonexistent file returns code expected (404)" '
test_curl_resp_http_code "http://127.0.0.1:$port/ipfs/$HASH2/pleaseDontAddMe" "HTTP/1.1 404 Not Found"
'

test_expect_success "GET /ipfs/ipfs/{cid} returns redirect to the valid path" "
curl -sI -o response_with_double_ipfs_ns \"http://127.0.0.1:$port/ipfs/ipfs/bafkqaaa?query=to-remember\" &&
test_should_contain \"Location: /ipfs/bafkqaaa?query=to-remember\" response_with_double_ipfs_ns
"

test_expect_failure "GET IPNS path succeeds" '
ipfs name publish --allow-offline "$HASH" &&
PEERID=$(ipfs config Identity.PeerID) &&
Expand All @@ -95,6 +100,12 @@ test_expect_failure "GET IPNS path output looks good" '
test_cmp expected actual
'

test_expect_success "GET /ipfs/ipns/{peerid} returns redirect to the valid path" '
PEERID=$(ipfs config Identity.PeerID) &&
curl -sI -o response_with_ipfs_ipns_ns "http://127.0.0.1:$port/ipfs/ipns/${PEERID}?query=to-remember" &&
test_should_contain "Location: /ipns/${PEERID}?query=to-remember" response_with_ipfs_ipns_ns
'

test_expect_success "GET invalid IPFS path errors" '
test_must_fail curl -sf "http://127.0.0.1:$port/ipfs/12345"
'
Expand Down