Skip to content

Commit

Permalink
feat(gw): /ipfs/ipfs/{cid} → /ipfs/{cid}
Browse files Browse the repository at this point in the history
This will try to recover from invalid paths like  /ipfs/ipfs/{cid}
and redirect to proper one, when possible.


This commit was moved from ipfs/kubo@15e3732
  • Loading branch information
lidel committed Feb 19, 2021
1 parent e90fd62 commit 326de7a
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions gateway/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
// '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"))
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

0 comments on commit 326de7a

Please sign in to comment.