-
Notifications
You must be signed in to change notification settings - Fork 32
/
gzip.go
45 lines (38 loc) · 975 Bytes
/
gzip.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
package main
import (
"compress/gzip"
"io"
"net/http"
"strings"
)
// GZip solution derived from
// https://www.lemoda.net/go/gzip-handler/index.html and
// https://stackoverflow.com/a/50898293/197160
type gzipResponseWriter struct {
io.Writer
http.ResponseWriter
}
// Use the Writer part of gzipResponseWriter to write the output.
func (w gzipResponseWriter) Write(b []byte) (int, error) {
return w.Writer.Write(b)
}
func inAnyStr(s string, container []string) bool {
for i := 0; i < len(container); i++ {
if strings.Contains(container[i], s) {
return true
}
}
return false
}
func gzipHandler(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if inAnyStr("gzip", r.Header["Accept-Encoding"]) {
w.Header().Set("Content-Encoding", "gzip")
gz := gzip.NewWriter(w)
defer gz.Close()
h.ServeHTTP(gzipResponseWriter{Writer: gz, ResponseWriter: w}, r)
return
}
h.ServeHTTP(w, r)
})
}