Skip to content

Commit

Permalink
If bad encoding show both b58 and hex errors
Browse files Browse the repository at this point in the history
  • Loading branch information
gammazero committed Jul 9, 2024
1 parent e294f32 commit 0a026f6
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 16 deletions.
24 changes: 8 additions & 16 deletions server/find/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,11 +179,15 @@ func (s *Server) findMultihash(w http.ResponseWriter, r *http.Request) {
stream := match == mediaTypeNDJson

mhVar := path.Base(r.URL.Path)
m, err := caseMHString(mhVar)
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 {
log.Errorw("error decoding multihash", "multihash", mhVar, "b58Err", err, "hexErr", hexErr)
httpserver.HandleError(w, err, "find")
return
}
}
s.getIndexes(w, []multihash.Multihash{m}, stream)
}
Expand Down Expand Up @@ -516,15 +520,3 @@ func createExtendedProviderResult(epInfo registry.ExtendedProviderInfo, iVal ind
},
}
}

func caseMHString(s string) (multihash.Multihash, error) {
mh, err := multihash.FromHexString(s)
if err != nil {
mh, err := multihash.FromB58String(s)
if err != nil {
return multihash.Multihash{}, err
}
return mh, nil
}
return mh, nil
}
6 changes: 6 additions & 0 deletions server/find/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,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

0 comments on commit 0a026f6

Please sign in to comment.