From 09a0eca98bdde2e2d396ebc01e85a442b1812174 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 --- internal/proxy/proxy.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/internal/proxy/proxy.go b/internal/proxy/proxy.go index f8c5709..44051b8 100644 --- a/internal/proxy/proxy.go +++ b/internal/proxy/proxy.go @@ -5,6 +5,7 @@ import ( "io" "net/http" "net/http/httputil" + "strconv" "strings" "time" @@ -102,6 +103,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{} @@ -123,14 +134,17 @@ func (h *Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) { if h.HasNodeProviderFailed(pw.statusCode) { h.metricResponseTime.WithLabelValues(target.Config.Name, r.Method).Observe(time.Since(start).Seconds()) + h.metricResponseStatus.WithLabelValues(target.Config.Name, strconv.Itoa(pw.statusCode)).Inc() h.metricRequestErrors.WithLabelValues(target.Config.Name, "rerouted").Inc() continue } + h.copyHeaders(w, pw) w.WriteHeader(pw.statusCode) w.Write(pw.body.Bytes()) // nolint:errcheck + h.metricResponseStatus.WithLabelValues(target.Config.Name, strconv.Itoa(pw.statusCode)).Inc() h.metricResponseTime.WithLabelValues(target.Config.Name, r.Method).Observe(time.Since(start).Seconds()) return