Skip to content

Commit

Permalink
The rwriter package handles hex-formatted multihashes (#209)
Browse files Browse the repository at this point in the history
* The rwriter package handled hex-formatted multihashes

This will allow queries to dhstore to handle hex formatted multihashes.

* lint

---------

Co-authored-by: gammazero <[email protected]>
  • Loading branch information
gammazero and gammazero authored Jul 9, 2024
1 parent 0b9fd7c commit 898a440
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
9 changes: 7 additions & 2 deletions rwriter/response_writer.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package rwriter

import (
"encoding/hex"
"encoding/json"
"errors"
"fmt"
Expand Down Expand Up @@ -84,9 +85,13 @@ func New(w http.ResponseWriter, r *http.Request, options ...Option) (*ResponseWr
}
switch pathType {
case opts.mhPathType:
b, err = base58.Decode(strings.TrimSpace(path.Base(r.URL.Path)))
mhStr := strings.TrimSpace(path.Base(r.URL.Path))
b, err = base58.Decode(mhStr)
if err != nil {
return nil, apierror.New(multihash.ErrInvalidMultihash, http.StatusBadRequest)
b, err = hex.DecodeString(mhStr)
if err != nil {
return nil, apierror.New(multihash.ErrInvalidMultihash, http.StatusBadRequest)
}
}
mh = multihash.Multihash(b)
cidKey = cid.NewCidV1(cid.Raw, mh)
Expand Down
30 changes: 30 additions & 0 deletions rwriter/response_writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ package rwriter_test

import (
"bytes"
"encoding/hex"
"io"
"net/http"
"net/http/httptest"
"strings"
"testing"

"github.com/ipni/go-libipni/rwriter"
"github.com/mr-tron/base58"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -87,4 +89,32 @@ func TestResponseWriter(t *testing.T) {
require.NoError(t, err)
require.Equal(t, http.StatusBadRequest, res.StatusCode)
require.Equal(t, "unsupported resource type", strings.TrimSpace(string(body)))

// Check that invalid multihash is handled.
req, err = http.NewRequest(http.MethodGet, ts.URL+"/multihash/aflk324vecr-903r-0", nil)
require.NoError(t, err)
req.Header.Set("Accept", "application/json")
res, err = cli.Do(req)
require.NoError(t, err)
_, err = io.Copy(io.Discard, res.Body)
require.NoError(t, err)
res.Body.Close()
require.NoError(t, err)
require.Equal(t, http.StatusBadRequest, res.StatusCode)

b, err := base58.Decode("2DrjgbM2tfcpUE5imXMv3HnzryEaxd1FKh8DWMDEgtFkL7MDvT")
require.NoError(t, err)

// Check that hex multihash is handled.
req, err = http.NewRequest(http.MethodGet, ts.URL+"/multihash/"+hex.EncodeToString(b), nil)
require.NoError(t, err)
req.Header.Set("Accept", "application/json")
res, err = cli.Do(req)
require.NoError(t, err)
body, err = io.ReadAll(res.Body)
res.Body.Close()
require.NoError(t, err)
require.Equal(t, http.StatusOK, res.StatusCode)
require.Equal(t, testHeaderVal, res.Header.Get(testHeaderKey))
require.Equal(t, "ok", strings.TrimSpace(string(body)))
}

0 comments on commit 898a440

Please sign in to comment.