From 87e9397bc69814d1969ee491aeac57c49995840c Mon Sep 17 00:00:00 2001 From: gammazero Date: Mon, 24 Jun 2024 22:40:39 -0700 Subject: [PATCH 1/2] The rwriter package handled hex-formatted multihashes This will allow queries to dhstore to handle hex formatted multihashes. --- rwriter/response_writer.go | 9 +++++++-- rwriter/response_writer_test.go | 29 +++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/rwriter/response_writer.go b/rwriter/response_writer.go index 022bab7..8942947 100644 --- a/rwriter/response_writer.go +++ b/rwriter/response_writer.go @@ -1,6 +1,7 @@ package rwriter import ( + "encoding/hex" "encoding/json" "errors" "fmt" @@ -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) diff --git a/rwriter/response_writer_test.go b/rwriter/response_writer_test.go index e295bd2..60ee3a8 100644 --- a/rwriter/response_writer_test.go +++ b/rwriter/response_writer_test.go @@ -2,6 +2,7 @@ package rwriter_test import ( "bytes" + "encoding/hex" "io" "net/http" "net/http/httptest" @@ -9,6 +10,7 @@ import ( "testing" "github.com/ipni/go-libipni/rwriter" + "github.com/mr-tron/base58" "github.com/stretchr/testify/require" ) @@ -87,4 +89,31 @@ 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) + body, err = io.ReadAll(res.Body) + 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))) } From f1a4ea9748dfb9177bcd5e3345c9d9f8492e6634 Mon Sep 17 00:00:00 2001 From: gammazero <11790789+gammazero@users.noreply.github.com> Date: Mon, 8 Jul 2024 18:56:01 -0700 Subject: [PATCH 2/2] lint --- rwriter/response_writer_test.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/rwriter/response_writer_test.go b/rwriter/response_writer_test.go index 60ee3a8..0a54fc7 100644 --- a/rwriter/response_writer_test.go +++ b/rwriter/response_writer_test.go @@ -96,7 +96,8 @@ func TestResponseWriter(t *testing.T) { req.Header.Set("Accept", "application/json") res, err = cli.Do(req) require.NoError(t, err) - body, err = io.ReadAll(res.Body) + _, 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)