This repository has been archived by the owner on Feb 7, 2018. It is now read-only.
/
handler.go
148 lines (127 loc) · 4.22 KB
/
handler.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package compress
import (
"compress/gzip"
"net/http"
"strconv"
"strings"
"sync"
"github.com/volatile/core"
)
// gzippableMinSize is the minimal size (in bytes) a content needs to have to be gzipped.
//
// A TCP packet is normally 1500 bytes long.
// So if the response plus the TCP headers already fits into a single packet, there will be no gain from gzip.
const gzippableMinSize = 1400
// notGzippableTypes is a custom list of media types referring to a compressed content.
// Gzip will not be applied to any of these content types.
//
// For performance, only the most common officials (and future officials) are listed.
//
// All official media types: http://www.iana.org/assignments/media-types/media-types.xhtml
var notGzippableTypes = map[string]struct{}{
"application/font-woff": {},
"application/gzip": {},
"application/pdf": {},
"application/zip": {},
"audio/mp4": {},
"audio/mpeg": {},
"audio/webm": {},
"image/gif": {},
"image/jpeg": {},
"image/png": {},
"image/webp": {},
"video/h264": {},
"video/mp4": {},
"video/mpeg": {},
"video/ogg": {},
"video/vp8": {},
"video/webm": {},
}
var gzipPool = sync.Pool{New: func() interface{} { return gzip.NewWriter(nil) }}
// Use adds the handler to the default handlers stack.
// It compress all the compressible responses.
func Use() {
core.Use(func(c *core.Context) {
if !strings.Contains(c.Request.Header.Get("Accept-Encoding"), "gzip") || c.Request.Header.Get("Sec-WebSocket-Key") != "" {
c.Next()
return
}
cw := &compressWriter{
ResponseWriter: c.ResponseWriter,
gzipWriter: gzipPool.Get().(*gzip.Writer),
}
c.ResponseWriter = cw
defer gzipPool.Put(cw.gzipWriter)
defer cw.close()
defer c.Recover()
c.Next()
})
}
// compressWriter binds the downstream repsonse writing into gzipWriter if the first content is detected as gzippable.
type compressWriter struct {
http.ResponseWriter
gzipWriter *gzip.Writer
gzipDetect bool // gzipDetect tells if the gzippable detection has been done.
gzipUse bool // gzipUse tells if gzip is used for the response.
status int
}
// WriteHeader catches a downstream WriteHeader call and caches the status code.
// The header will be written later, at the first Write call, after the gzipping detection has been done.
func (cw *compressWriter) WriteHeader(status int) {
cw.status = status
}
// writePostponedHeader writes the response header when a cached status code exists.
func (cw *compressWriter) writePostponedHeader() {
if cw.status > 0 {
cw.ResponseWriter.WriteHeader(cw.status)
}
}
// Write sets the compressing headers and calls the gzip writer, but only if the Content-Type header defines a compressible content.
// Otherwise, it calls the original Write method.
func (cw *compressWriter) Write(b []byte) (int, error) {
if !cw.gzipDetect {
// Check content is not already encoded.
if cw.ResponseWriter.Header().Get("Content-Encoding") != "" {
goto NoGzipUse
}
// Check content has sufficient length.
if cl, _ := strconv.Atoi(cw.ResponseWriter.Header().Get("Content-Length")); cl <= 0 {
// If no Content-Length, take the length of this first chunk.
if len(b) < gzippableMinSize {
goto NoGzipUse
}
}
// Check content is of gzippable type.
if ct := cw.ResponseWriter.Header().Get("Content-Type"); ct == "" {
ct = http.DetectContentType(b)
cw.ResponseWriter.Header().Set("Content-Type", ct)
if i := strings.IndexByte(ct, ';'); i >= 0 {
ct = ct[:i]
}
ct = strings.ToLower(ct)
if _, ok := notGzippableTypes[ct]; ok {
goto NoGzipUse
}
}
cw.ResponseWriter.Header().Del("Content-Length") // Because the compressed content will have a new length.
cw.ResponseWriter.Header().Set("Content-Encoding", "gzip")
cw.gzipWriter.Reset(cw.ResponseWriter)
cw.gzipUse = true
NoGzipUse:
cw.writePostponedHeader()
cw.gzipDetect = true
}
if cw.gzipUse {
return cw.gzipWriter.Write(b)
}
return cw.ResponseWriter.Write(b)
}
// close closes the gzip writer if it has been used.
func (cw *compressWriter) close() {
if !cw.gzipDetect {
cw.writePostponedHeader()
}
if cw.gzipUse {
cw.gzipWriter.Close()
}
}