Skip to content

Commit

Permalink
prefix paths with /ipfs/ prefix if they don't have any prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
hsanjuan committed Oct 13, 2023
1 parent 8f160d3 commit 46ef95f
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,17 @@ func setupGatewayHandler(nd *Node) (http.Handler, error) {
return handler, nil
}

// Prefixes with /ipfs/ unprefixed paths.
func prefixPath(p string) string {
if len(p) == 0 {
return p
}

Check warning on line 172 in handlers.go

View check run for this annotation

Codecov / codecov/patch

handlers.go#L171-L172

Added lines #L171 - L172 were not covered by tests
if p[0] != '/' {
return "/ipfs/" + p
}
return p // let NewPath handle whatever issues from here.
}

func newKuboRPCHandler(endpoints []string) http.Handler {
mux := http.NewServeMux()

Expand All @@ -174,7 +185,7 @@ func newKuboRPCHandler(endpoints []string) http.Handler {
// - https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/303
redirectToGateway := func(format string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
path, err := path.NewPath(r.URL.Query().Get("arg"))
path, err := path.NewPath(prefixPath(r.URL.Query().Get("arg")))
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(err.Error()))
Expand All @@ -194,7 +205,7 @@ func newKuboRPCHandler(endpoints []string) http.Handler {
mux.HandleFunc("/api/v0/dag/export", redirectToGateway("car"))
mux.HandleFunc("/api/v0/block/get", redirectToGateway("raw"))
mux.HandleFunc("/api/v0/dag/get", func(w http.ResponseWriter, r *http.Request) {
path, err := path.NewPath(r.URL.Query().Get("arg"))
path, err := path.NewPath(prefixPath(r.URL.Query().Get("arg")))
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(err.Error()))
Expand Down

0 comments on commit 46ef95f

Please sign in to comment.