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: decode multihash string for B58 and Hex #2630

Merged
merged 3 commits into from
Aug 9, 2024
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
13 changes: 9 additions & 4 deletions server/find/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (

"github.com/ipfs/go-cid"
logging "github.com/ipfs/go-log/v2"
indexer "github.com/ipni/go-indexer-core"
"github.com/ipni/go-indexer-core"
coremetrics "github.com/ipni/go-indexer-core/metrics"
"github.com/ipni/go-libipni/apierror"
"github.com/ipni/go-libipni/find/model"
Expand Down Expand Up @@ -181,9 +181,14 @@ func (s *Server) findMultihash(w http.ResponseWriter, r *http.Request) {
mhVar := path.Base(r.URL.Path)
m, err := multihash.FromB58String(mhVar)
if err != nil {
log.Errorw("error decoding multihash", "multihash", mhVar, "err", err)
httpserver.HandleError(w, err, "find")
return
var hexErr error
m, hexErr = multihash.FromHexString(mhVar)
if hexErr != nil {
msg := "find: input is not a valid base58 or hex encoded multihash"
log.Errorw(msg, "multihash", mhVar, "err", err, "hexErr", hexErr)
http.Error(w, msg, http.StatusBadRequest)
return
}
}
s.getIndexes(w, []multihash.Multihash{m}, stream)
}
Expand Down
17 changes: 17 additions & 0 deletions server/find/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,17 @@ func TestServer_CORSWithExpectedContentType(t *testing.T) {
reqUrl: "/multihash/" + mhs[0].B58String(),
wantContentType: "application/json",
},
{
reqMethod: http.MethodGet,
reqUrl: "/multihash/" + mhs[0].HexString(),
wantContentType: "application/json",
},
{
reqMethod: http.MethodGet,
reqUrl: "/multihash/" + "deadbeef",
wantContentType: "application/json",
statusCode: http.StatusBadRequest,
},
{
reqMethod: http.MethodPost,
reqUrl: "/multihash",
Expand All @@ -115,6 +126,12 @@ func TestServer_CORSWithExpectedContentType(t *testing.T) {
reqUrl: "/",
wantContentType: "text/html",
},
{
reqMethod: http.MethodGet,
reqUrl: "/multihash/1qaai0lO_aa^",
wantContentType: "application/json",
statusCode: http.StatusBadRequest,
},
}

cl := http.DefaultClient
Expand Down
Loading