Skip to content

Commit

Permalink
Cleanup CompressHandlerLevel
Browse files Browse the repository at this point in the history
  • Loading branch information
fharding1 committed Nov 18, 2019
1 parent 8a3748a commit 5c80e8c
Showing 1 changed file with 55 additions and 67 deletions.
122 changes: 55 additions & 67 deletions compress.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,77 +74,65 @@ func CompressHandlerLevel(h http.Handler, level int) http.Handler {
level = gzip.DefaultCompression
}

const (
gzipEncoding = "gzip"
flateEncoding = "deflate"
)

return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
L:
for _, enc := range strings.Split(r.Header.Get("Accept-Encoding"), ",") {
switch strings.TrimSpace(enc) {
case "gzip":
w.Header().Set("Content-Encoding", "gzip")
r.Header.Del("Accept-Encoding")
w.Header().Add("Vary", "Accept-Encoding")

gw, _ := gzip.NewWriterLevel(w, level)
defer gw.Close()

h, hok := w.(http.Hijacker)
if !hok { /* w is not Hijacker... oh well... */
h = nil
}

f, fok := w.(http.Flusher)
if !fok {
f = nil
}

cn, cnok := w.(http.CloseNotifier)
if !cnok {
cn = nil
}

w = &compressResponseWriter{
Writer: gw,
ResponseWriter: w,
Hijacker: h,
Flusher: f,
CloseNotifier: cn,
}

break L
case "deflate":
w.Header().Set("Content-Encoding", "deflate")
r.Header.Del("Accept-Encoding")
w.Header().Add("Vary", "Accept-Encoding")

fw, _ := flate.NewWriter(w, level)
defer fw.Close()

h, hok := w.(http.Hijacker)
if !hok { /* w is not Hijacker... oh well... */
h = nil
}

f, fok := w.(http.Flusher)
if !fok {
f = nil
}

cn, cnok := w.(http.CloseNotifier)
if !cnok {
cn = nil
}

w = &compressResponseWriter{
Writer: fw,
ResponseWriter: w,
Hijacker: h,
Flusher: f,
CloseNotifier: cn,
}

break L
// detect what encoding to use
var encoding string
for _, curEnc := range strings.Split(r.Header.Get("Accept-Encoding"), ",") {
curEnc = strings.TrimSpace(curEnc)
if curEnc == gzipEncoding || curEnc == flateEncoding {
encoding = curEnc
break
}
}

// if we weren't able to identify an encoding we're familiar with, pass on the
// request to the handler and return
if encoding == "" {
h.ServeHTTP(w, r)
return
}

// wrap the ResponseWriter with the writer for the chosen encoding
var encWriter io.WriteCloser
if encoding == gzipEncoding {
encWriter, _ = gzip.NewWriterLevel(w, level)
} else if encoding == flateEncoding {
encWriter, _ = flate.NewWriter(w, level)
}
defer encWriter.Close()

w.Header().Set("Content-Encoding", encoding)
r.Header.Del("Accept-Encoding")
w.Header().Add("Vary", "Accept-Encoding")

hijacker, ok := w.(http.Hijacker)
if !ok { /* w is not Hijacker... oh well... */
hijacker = nil
}

flusher, ok := w.(http.Flusher)
if !ok {
flusher = nil
}

closeNotifier, ok := w.(http.CloseNotifier)
if !ok {
closeNotifier = nil
}

w = &compressResponseWriter{
Writer: encWriter,
ResponseWriter: w,
Hijacker: hijacker,
Flusher: flusher,
CloseNotifier: closeNotifier,
}

h.ServeHTTP(w, r)
})
}

0 comments on commit 5c80e8c

Please sign in to comment.