Skip to content
This repository has been archived by the owner on Jul 22, 2024. It is now read-only.

WIP: fix: run gunzip only otherwise pass through #138

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/golang.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -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 \
Expand Down
43 changes: 0 additions & 43 deletions internal/middleware/gzip.go

This file was deleted.

65 changes: 0 additions & 65 deletions internal/middleware/gzip_test.go

This file was deleted.

19 changes: 16 additions & 3 deletions internal/proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -100,6 +102,16 @@ func (h *Proxy) HasNodeProviderFailed(statusCode int) bool {
return statusCode >= http.StatusInternalServerError || statusCode == http.StatusTooManyRequests
}

func (h *Proxy) copyHeaders(dst http.ResponseWriter, src http.ResponseWriter) {
for k, v := range src.Header() {
if len(v) == 0 {
continue
}

dst.Header().Set(k, v[0])
}
}

func (h *Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) {
body := &bytes.Buffer{}

Expand All @@ -113,10 +125,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) {
Expand All @@ -125,6 +137,7 @@ func (h *Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) {

continue
}
h.copyHeaders(w, pw)

w.WriteHeader(pw.statusCode)
w.Write(pw.body.Bytes()) // nolint:errcheck
Expand Down
10 changes: 3 additions & 7 deletions internal/rpcgateway/rpcgateway_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand All @@ -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)
Expand Down
Loading