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

fix: run gunzip only otherwise pass through #139

Merged
merged 1 commit into from
Dec 14, 2023
Merged
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
14 changes: 14 additions & 0 deletions internal/proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"io"
"net/http"
"net/http/httputil"
"strconv"
"strings"
"time"

Expand Down Expand Up @@ -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{}

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