Skip to content

Commit

Permalink
rpcenc: Fix POST read
Browse files Browse the repository at this point in the history
  • Loading branch information
magik6k committed Jul 30, 2021
1 parent 0c809d3 commit 555c402
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/rpcenc/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,10 @@ func (w *rpcReader) Close() error {
}

func (w *rpcReader) redirect(to string) bool {
if w.postBody != nil {
return false
}

done := false

w.beginOnce.Do(func() {
Expand Down
20 changes: 20 additions & 0 deletions lib/rpcenc/reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/gorilla/mux"
"github.com/stretchr/testify/require"
"golang.org/x/xerrors"

"github.com/filecoin-project/go-jsonrpc"
sealing "github.com/filecoin-project/lotus/extern/storage-sealing"
Expand All @@ -23,6 +24,18 @@ func (h *ReaderHandler) ReadAllApi(ctx context.Context, r io.Reader) ([]byte, er
return h.readApi(ctx, r)
}

func (h *ReaderHandler) ReadStartAndApi(ctx context.Context, r io.Reader) ([]byte, error) {
n, err := r.Read([]byte{0})
if err != nil {
return nil, err
}
if n != 1 {
return nil, xerrors.Errorf("not one")
}

return h.readApi(ctx, r)
}

func (h *ReaderHandler) ReadAll(ctx context.Context, r io.Reader) ([]byte, error) {
return ioutil.ReadAll(r)
}
Expand Down Expand Up @@ -121,6 +134,7 @@ func TestReaderRedirect(t *testing.T) {

var redirClient struct {
ReadAllApi func(ctx context.Context, r io.Reader) ([]byte, error)
ReadStartAndApi func(ctx context.Context, r io.Reader) ([]byte, error)
}

{
Expand All @@ -143,7 +157,13 @@ func TestReaderRedirect(t *testing.T) {
defer closer()
}

// redirect
read, err := redirClient.ReadAllApi(context.TODO(), strings.NewReader("rediracted pooooootato"))
require.NoError(t, err)
require.Equal(t, "rediracted pooooootato", string(read), "potatoes weren't equal")

// proxy (because we started reading locally)
read, err = redirClient.ReadStartAndApi(context.TODO(), strings.NewReader("rediracted pooooootato"))
require.NoError(t, err)
require.Equal(t, "ediracted pooooootato", string(read), "otatoes weren't equal")
}

0 comments on commit 555c402

Please sign in to comment.