@cryptotaxi247 / kubo / commits / 15e3732af

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

This will try to recover from invalid paths like /ipfs/ipfs/{cid} and redirect to proper one, when possible.

Marcin Rataj committed Feb 20, 2021 at 00:09 UTC 15e3732afd29b380f6a12b06303dce49abe46f82
2 files changed +22
core/corehttp/gateway_handler.go
+11
@@ -217,6 +217,17 @@ func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request
217
218 parsedPath := ipath.New(urlPath)
219 if err := parsedPath.IsValid(); err != nil {
220 + // Attempt to fix redundant /ipfs/ namespace as long resulting
221 + // 'intended' path is valid. This is in case gremlins were tickled
222 + // wrong way and user ended up at /ipfs/ipfs/{cid} or /ipfs/ipns/{id}
223 + // like in bafybeien3m7mdn6imm425vc2s22erzyhbvk5n3ofzgikkhmdkh5cuqbpbq
224 + // :^))
225 + intendedPath := ipath.New(strings.TrimPrefix(urlPath, "/ipfs"))
226 + if err2 := intendedPath.IsValid(); err2 == nil {
227 + intendedURL := strings.Replace(r.URL.String(), urlPath, intendedPath.String(), 1)
228 + http.Redirect(w, r, intendedURL, http.StatusMovedPermanently)
229 + return
230 + }
231 webError(w, "invalid ipfs path", err, http.StatusBadRequest)
232 return
233 }
test/sharness/t0110-gateway.sh
+11
@@ -84,6 +84,11 @@ test_expect_success "GET IPFS nonexistent file returns code expected (404)" '
84 test_curl_resp_http_code "http://127.0.0.1:$port/ipfs/$HASH2/pleaseDontAddMe" "HTTP/1.1 404 Not Found"
85 '
86
87 +test_expect_success "GET /ipfs/ipfs/{cid} returns redirect to the valid path" "
88 + curl -sI -o response_with_double_ipfs_ns \"http://127.0.0.1:$port/ipfs/ipfs/bafkqaaa?query=to-remember\" &&
89 + test_should_contain \"Location: /ipfs/bafkqaaa?query=to-remember\" response_with_double_ipfs_ns
90 +"
91 +
92 test_expect_failure "GET IPNS path succeeds" '
93 ipfs name publish --allow-offline "$HASH" &&
94 PEERID=$(ipfs config Identity.PeerID) &&
@@ -95,6 +100,12 @@ test_expect_failure "GET IPNS path output looks good" '
100 test_cmp expected actual
101 '
102
103 +test_expect_success "GET /ipfs/ipns/{peerid} returns redirect to the valid path" '
104 + PEERID=$(ipfs config Identity.PeerID) &&
105 + curl -sI -o response_with_ipfs_ipns_ns "http://127.0.0.1:$port/ipfs/ipns/${PEERID}?query=to-remember" &&
106 + test_should_contain "Location: /ipns/${PEERID}?query=to-remember" response_with_ipfs_ipns_ns
107 +'
108 +
109 test_expect_success "GET invalid IPFS path errors" '
110 test_must_fail curl -sf "http://127.0.0.1:$port/ipfs/12345"
111 '