From 555c402ba3b7ca48bf16f391539966287e54a4de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Magiera?= Date: Fri, 30 Jul 2021 13:03:31 +0200 Subject: [PATCH] rpcenc: Fix POST read --- lib/rpcenc/reader.go | 4 ++++ lib/rpcenc/reader_test.go | 20 ++++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/lib/rpcenc/reader.go b/lib/rpcenc/reader.go index a5edbe68b..c423bae55 100644 --- a/lib/rpcenc/reader.go +++ b/lib/rpcenc/reader.go @@ -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() { diff --git a/lib/rpcenc/reader_test.go b/lib/rpcenc/reader_test.go index fcaa0dae2..b425aec73 100644 --- a/lib/rpcenc/reader_test.go +++ b/lib/rpcenc/reader_test.go @@ -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" @@ -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) } @@ -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) } { @@ -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") }