-
Notifications
You must be signed in to change notification settings - Fork 0
/
gzip.go
68 lines (57 loc) · 1.23 KB
/
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package tiddlywikid
import (
"compress/gzip"
"flag"
"net/http"
"strings"
"sync"
)
var (
gzipLv = flag.Int("gz", 2, "gzip disable = 0, DefaultCompression = -1, BestSpeed = 1, BestCompression = 9")
gzWiterPool sync.Pool
)
type GzipResponseWriter struct {
http.ResponseWriter
gzip *gzip.Writer
}
func (w *GzipResponseWriter) Write(p []byte) (int, error) {
if w.gzip == nil {
return w.ResponseWriter.Write(p)
}
return w.gzip.Write(p)
}
func (w *GzipResponseWriter) Close() error {
if w.gzip != nil {
err := w.gzip.Close()
gzWiterPool.Put(w.gzip)
return err
}
return nil
}
func CanAcceptsGzip(r *http.Request) bool {
s := strings.ToLower(r.Header.Get("Accept-Encoding"))
for _, ss := range strings.Split(s, ",") {
if strings.HasPrefix(ss, "gzip") {
return true
}
}
return false
}
func TryGzipResponse(w http.ResponseWriter, r *http.Request) *GzipResponseWriter {
if !CanAcceptsGzip(r) || *gzipLv == 0 {
return nil
}
gw, ok := gzWiterPool.Get().(*gzip.Writer)
if ok {
gw.Reset(w)
} else {
var err error
gw, err = gzip.NewWriterLevel(w, *gzipLv)
if err != nil {
gw = gzip.NewWriter(w)
}
}
w.Header().Set("Content-Encoding", "gzip")
w.Header().Del("Content-Length")
return &GzipResponseWriter{w, gw}
}