-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompress.go
75 lines (57 loc) · 1.6 KB
/
compress.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
66
67
68
69
70
71
72
73
74
75
package compress
import (
"compress/flate"
"compress/gzip"
"io"
"net/http"
"github.com/go-http-utils/compressible"
"github.com/go-http-utils/headers"
"github.com/go-http-utils/negotiator"
)
// Version is this package's version.
const Version = "0.1.0"
type compressWriter struct {
rw http.ResponseWriter
w io.WriteCloser
}
func (cw compressWriter) Header() http.Header {
return cw.rw.Header()
}
func (cw compressWriter) WriteHeader(status int) {
cw.rw.Header().Del(headers.ContentLength)
cw.rw.WriteHeader(status)
}
func (cw compressWriter) Write(b []byte) (int, error) {
cw.rw.Header().Del(headers.ContentLength)
return cw.w.Write(b)
}
// Handler wraps the http.Handler h with compress support.
func Handler(h http.Handler) http.Handler {
return http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
var w io.WriteCloser
if req.Method != http.MethodHead &&
res.Header().Get(headers.ContentEncoding) == "" &&
compressible.Test(req.Header.Get(headers.ContentType)) {
n := negotiator.New(req)
encoding, matched := n.Encoding([]string{"gzip", "deflate"})
if !matched {
res.WriteHeader(http.StatusNotAcceptable)
res.Write([]byte("supported encodings: gzip, deflate"))
return
}
switch encoding {
case "gzip":
w = gzip.NewWriter(res)
case "deflate":
w, _ = flate.NewWriter(res, flate.DefaultCompression)
}
cw := compressWriter{rw: res, w: w}
cw.Header().Set(headers.ContentEncoding, encoding)
cw.Header().Set(headers.Vary, headers.AcceptEncoding)
defer cw.w.Close()
h.ServeHTTP(cw, req)
return
}
h.ServeHTTP(res, req)
})
}