Skip to content

Commit

Permalink
Add code to handle response gzipped content
Browse files Browse the repository at this point in the history
  • Loading branch information
vkuznet committed Jun 28, 2024
1 parent a06a782 commit 5062c5a
Showing 1 changed file with 32 additions and 1 deletion.
33 changes: 32 additions & 1 deletion redirect.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"bytes"
"compress/gzip"
"encoding/base64"
"encoding/json"
"fmt"
Expand Down Expand Up @@ -128,6 +129,18 @@ func reverseProxy(targetURL string, w http.ResponseWriter, r *http.Request) {
resp.Header.Set("Response-Proto", resp.Proto)
resp.Header.Set("Response-Time", time.Since(start).String())
resp.Header.Set("Response-Time-Seconds", fmt.Sprintf("%v", time.Since(start).Seconds()))

// If the response is gzipped, decompress it
if strings.Contains(resp.Header.Get("Content-Encoding"), "gzip") {
gzReader, err := gzip.NewReader(resp.Body)
if err != nil {
return err
}
defer gzReader.Close()
resp.Body = io.NopCloser(gzReader)
resp.Header.Del("Content-Encoding")
resp.Header.Del("Content-Length")
}
return nil
}
proxy.ErrorHandler = func(rw http.ResponseWriter, r *http.Request, err error) {
Expand All @@ -144,7 +157,25 @@ func reverseProxy(targetURL string, w http.ResponseWriter, r *http.Request) {
}

// ServeHttp is non blocking and uses a go routine under the hood
proxy.ServeHTTP(w, r)
// proxy.ServeHTTP(w, r)
proxy.ServeHTTP(newGzipResponseWriter(w), r)
}

// gzipResponseWriter wraps http.ResponseWriter to decompress gzip-encoded responses
type gzipResponseWriter struct {
io.Writer
http.ResponseWriter
}

func (w *gzipResponseWriter) Write(b []byte) (int, error) {
return w.Writer.Write(b)
}

func newGzipResponseWriter(w http.ResponseWriter) *gzipResponseWriter {
return &gzipResponseWriter{
Writer: w,
ResponseWriter: w,
}
}

// helper function to get random service url
Expand Down

0 comments on commit 5062c5a

Please sign in to comment.