Skip to content

Commit

Permalink
Use chi gzip middleware (#190)
Browse files Browse the repository at this point in the history
  • Loading branch information
p53 authored Jul 15, 2022
1 parent 30e6724 commit b92c703
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 74 deletions.
67 changes: 0 additions & 67 deletions middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,11 @@ limitations under the License.
package main

import (
"compress/gzip"
"context"
"fmt"
"io"
"io/ioutil"
"net/http"
"regexp"
"strings"
"sync"
"time"

uuid "github.com/gofrs/uuid"
Expand All @@ -44,69 +40,6 @@ const (
normalizeFlags purell.NormalizationFlags = purell.FlagRemoveDotSegments | purell.FlagRemoveDuplicateSlashes
)

var gzPool = sync.Pool{
New: func() interface{} {
w := gzip.NewWriter(ioutil.Discard)
return w
},
}

type gzipResponseWriter struct {
io.Writer
http.ResponseWriter
bytes int
code int
tee io.Writer
}

func (w *gzipResponseWriter) WriteHeader(status int) {
w.Header().Del("Content-Length")
w.ResponseWriter.WriteHeader(status)
w.code = status
}

func (w *gzipResponseWriter) Write(b []byte) (int, error) {
n, err := w.Writer.Write(b)
w.bytes += n
return n, err
}

func (w *gzipResponseWriter) BytesWritten() int {
return w.bytes
}

func (w *gzipResponseWriter) Status() int {
return w.code
}

func (w *gzipResponseWriter) Tee(writ io.Writer) {
w.tee = writ
}

func (w *gzipResponseWriter) Unwrap() http.ResponseWriter {
return w.ResponseWriter
}

// gzipMiddleware is responsible for compressing a response
func gzipMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(wrt http.ResponseWriter, req *http.Request) {
if !strings.Contains(req.Header.Get("Accept-Encoding"), "gzip") {
next.ServeHTTP(wrt, req)
return
}

wrt.Header().Set("Content-Encoding", "gzip")

gz := gzPool.Get().(*gzip.Writer)
defer gzPool.Put(gz)

gz.Reset(wrt)
defer gz.Close()

next.ServeHTTP(&gzipResponseWriter{ResponseWriter: wrt, Writer: gz}, req)
})
}

// entrypointMiddleware is custom filtering for incoming requests
func entrypointMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(wrt http.ResponseWriter, req *http.Request) {
Expand Down
4 changes: 1 addition & 3 deletions middleware_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1719,9 +1719,7 @@ func TestGzipCompression(t *testing.T) {
Headers: map[string]string{
"Accept-Encoding": "gzip, deflate, br",
},
ExpectedHeaders: map[string]string{
"Content-Encoding": "gzip",
},
ExpectedNoProxyHeaders: []string{"Content-Encoding"},
},
},
},
Expand Down
8 changes: 4 additions & 4 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,13 +226,13 @@ func (r *oauthProxy) useDefaultStack(engine chi.Router) {
engine.Use(r.requestIDMiddleware(r.config.RequestIDHeader))
}

// @step: enable the entrypoint middleware
engine.Use(entrypointMiddleware)

if r.config.EnableCompression {
engine.Use(gzipMiddleware)
engine.Use(middleware.Compress(5))
}

// @step: enable the entrypoint middleware
engine.Use(entrypointMiddleware)

if r.config.EnableLogging {
engine.Use(r.loggingMiddleware)
}
Expand Down

0 comments on commit b92c703

Please sign in to comment.