-
Notifications
You must be signed in to change notification settings - Fork 1
/
httpgzip.go
65 lines (54 loc) · 1.59 KB
/
httpgzip.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// Package httpgzip provides an http.Handler wrapper that
// transparently compresses the response payload with gzip if the
// "Accept-Encoding: gzip" request header is provided. It sets the
// "Vary: Accept-Encoding" and "Content-Encoding: gzip" response
// headers.
package httpgzip
import (
"compress/gzip"
"net/http"
"strings"
)
type responseWriter struct {
http.ResponseWriter
gzipped *gzip.Writer
headerWritten bool
}
func (w *responseWriter) WriteHeader(status int) {
w.headerWritten = true
w.ResponseWriter.WriteHeader(status)
}
func (w *responseWriter) Write(data []byte) (int, error) {
// Try to detect the content type if one wasn't provided.
// If we don't do this, the underlying http.ResponseWriter
// does and sets it to application/gzip.
if !w.headerWritten && w.Header().Get("Content-Type") == "" {
w.Header().Set("Content-Type", http.DetectContentType(data))
}
return w.gzipped.Write(data)
}
func (w *responseWriter) Flush() {
if f, ok := w.ResponseWriter.(http.Flusher); ok {
w.gzipped.Flush()
f.Flush()
}
}
// Handler wraps the provided http.Handler with one that provides
// transparent gzip content encoding.
func Handler(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if !strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") {
h.ServeHTTP(w, r)
return
}
gw := gzip.NewWriter(w)
defer gw.Close()
grw := responseWriter{
ResponseWriter: w,
gzipped: gw,
}
w.Header().Set("Vary", "Accept-Encoding")
w.Header().Set("Content-Encoding", "gzip")
h.ServeHTTP(&grw, r)
})
}