From 7b39945af4167beb5b97abf6eb0cd23eaf12c615 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 --- Dockerfile | 2 +- internal/middleware/gzip.go | 43 --------------------- internal/middleware/gzip_test.go | 65 -------------------------------- internal/proxy/proxy.go | 8 ++-- 4 files changed, 6 insertions(+), 112 deletions(-) delete mode 100644 internal/middleware/gzip.go delete mode 100644 internal/middleware/gzip_test.go 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) {