From 9f2cb5aad9e98931db2f8cecee8faf19ba9fb68f Mon Sep 17 00:00:00 2001 From: Marcin Wolny Date: Mon, 11 Dec 2023 16:13:19 +0100 Subject: [PATCH] fix: run gunzip only otherwise pass through --- .github/workflows/golang.yaml | 3 ++ Dockerfile | 2 +- internal/middleware/gzip.go | 43 ----------------- internal/middleware/gzip_test.go | 65 -------------------------- internal/proxy/proxy.go | 8 ++-- internal/rpcgateway/rpcgateway_test.go | 10 ++-- 6 files changed, 12 insertions(+), 119 deletions(-) delete mode 100644 internal/middleware/gzip.go delete mode 100644 internal/middleware/gzip_test.go diff --git a/.github/workflows/golang.yaml b/.github/workflows/golang.yaml index bb0407f..5432fa0 100644 --- a/.github/workflows/golang.yaml +++ b/.github/workflows/golang.yaml @@ -33,6 +33,9 @@ jobs: run: docker-compose up -d - name: Run tests run: go test -race -shuffle=on -v ./internal/... + env: + RPC_GATEWAY_NODE_URL_1: ${{ secrets.RPC_GATEWAY_NODE_URL_1 }} + RPC_GATEWAY_NODE_URL_2: ${{ secrets.RPC_GATEWAY_NODE_URL_2 }} - name: Print out docker containers' logs if: always() run: docker-compose logs diff --git a/Dockerfile b/Dockerfile index 7cf4777..f635dd9 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM golang:1.21-alpine3.18 AS builder +FROM golang:1.21-alpine3.19 AS builder RUN apk add --update-cache \ git \ diff --git a/internal/middleware/gzip.go b/internal/middleware/gzip.go deleted file mode 100644 index fd261ec..0000000 --- a/internal/middleware/gzip.go +++ /dev/null @@ -1,43 +0,0 @@ -package middleware - -import ( - "bytes" - "compress/gzip" - "io" - "net/http" - "strings" - - "github.com/go-http-utils/headers" -) - -func Gzip(next http.Handler) http.Handler { - fn := func(w http.ResponseWriter, r *http.Request) { - // Skip if compressed. - if strings.Contains(r.Header.Get(headers.ContentEncoding), "gzip") { - next.ServeHTTP(w, r) - - return - } - - body := &bytes.Buffer{} - g := gzip.NewWriter(body) - - if _, err := io.Copy(g, r.Body); err != nil { - http.Error(w, - http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) - } - - if err := g.Close(); err != nil { - http.Error(w, - http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) - } - - r.Header.Set(headers.ContentEncoding, "gzip") - r.Body = io.NopCloser(body) - r.ContentLength = int64(body.Len()) - - next.ServeHTTP(w, r) - } - - return http.HandlerFunc(fn) -} diff --git a/internal/middleware/gzip_test.go b/internal/middleware/gzip_test.go deleted file mode 100644 index d1bca41..0000000 --- a/internal/middleware/gzip_test.go +++ /dev/null @@ -1,65 +0,0 @@ -package middleware - -import ( - "bytes" - "compress/gzip" - "io" - "net/http" - "net/http/httptest" - "testing" - - "github.com/go-http-utils/headers" - "github.com/stretchr/testify/assert" -) - -func TestGzip(t *testing.T) { - t.Parallel() - - ethChainID := `{"jsonrpc":"2.0","id":1,"method":"eth_chainId","params":[]}` - t.Run("compressed HTTP request", func(t *testing.T) { - t.Parallel() - - tests := http.HandlerFunc(func(_ http.ResponseWriter, r *http.Request) { - body := &bytes.Buffer{} - - g := gzip.NewWriter(body) - nbytes, err := io.Copy(g, bytes.NewBufferString(ethChainID)) - assert.Nil(t, err) - assert.True(t, nbytes > 0) - assert.Nil(t, g.Close()) - - assert.Equal(t, int64(body.Len()), r.ContentLength) - assert.Equal(t, io.NopCloser(body), r.Body) - assert.Contains(t, r.Header.Get(headers.ContentEncoding), "gzip") - }) - - Gzip(tests). - ServeHTTP(httptest.NewRecorder(), - httptest.NewRequest(http.MethodPost, "http://localhost", bytes.NewBufferString(ethChainID)), - ) - }) - - t.Run("uncompressed HTTP request", func(t *testing.T) { - t.Parallel() - - tests := http.HandlerFunc(func(_ http.ResponseWriter, r *http.Request) { - body := &bytes.Buffer{} - - g, err := gzip.NewReader(r.Body) - assert.Nil(t, err) - - nbytes, err := io.Copy(body, g) // nolint:gosec - assert.Nil(t, err) - assert.True(t, nbytes > 0) - assert.Nil(t, g.Close()) - - assert.Equal(t, ethChainID, body.String()) - assert.Contains(t, r.Header.Get(headers.ContentEncoding), "gzip") - }) - - Gzip(tests). - ServeHTTP(httptest.NewRecorder(), - httptest.NewRequest(http.MethodPost, "http://localhost", bytes.NewBufferString(ethChainID)), - ) - }) -} diff --git a/internal/proxy/proxy.go b/internal/proxy/proxy.go index bd0ba7f..f8c5709 100644 --- a/internal/proxy/proxy.go +++ b/internal/proxy/proxy.go @@ -5,9 +5,11 @@ import ( "io" "net/http" "net/http/httputil" + "strings" "time" "github.com/0xProject/rpc-gateway/internal/middleware" + "github.com/go-http-utils/headers" "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/promauto" ) @@ -113,10 +115,10 @@ func (h *Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) { pw := NewResponseWriter() r.Body = io.NopCloser(bytes.NewBuffer(body.Bytes())) - if target.Config.Connection.HTTP.Compression { - middleware.Gzip(target.Proxy).ServeHTTP(pw, r) - } else { + if !target.Config.Connection.HTTP.Compression && strings.Contains(r.Header.Get(headers.ContentEncoding), "gzip") { middleware.Gunzip(target.Proxy).ServeHTTP(pw, r) + } else { + target.Proxy.ServeHTTP(pw, r) } if h.HasNodeProviderFailed(pw.statusCode) { diff --git a/internal/rpcgateway/rpcgateway_test.go b/internal/rpcgateway/rpcgateway_test.go index 6945fa6..79d5733 100644 --- a/internal/rpcgateway/rpcgateway_test.go +++ b/internal/rpcgateway/rpcgateway_test.go @@ -118,9 +118,7 @@ func TestRpcGatewayFailover(t *testing.T) { MaxConnsPerHost: 1, } - t.Logf("gateway serving from: %s", gs.URL) - - req, _ := http.NewRequest("POST", gs.URL, bytes.NewBufferString(``)) + req, _ := http.NewRequest("POST", gs.URL, bytes.NewBufferString(rpcRequestBody)) req.Header.Set("Content-Type", "application/json") req.ContentLength = int64(len(rpcRequestBody)) @@ -131,10 +129,8 @@ func TestRpcGatewayFailover(t *testing.T) { assert.Equal(t, http.StatusOK, res.StatusCode) - bodyContent, _ := io.ReadAll(res.Body) - - t.Log("Response from RPC gateway:") - t.Logf("%s", bodyContent) + _, err = io.ReadAll(res.Body) + assert.Nil(t, err) err = gateway.Stop(context.TODO()) assert.Nil(t, err)