-
Notifications
You must be signed in to change notification settings - Fork 2
/
handler.go
217 lines (195 loc) · 6.69 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
// Package compress provides a clever gzip compressing handler.
package compress
import (
"bufio"
"bytes"
"compress/gzip"
"net"
"net/http"
"strings"
"sync"
)
// 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 with the TCP headers already fits into a single packet, there will be no gain from gzip.
const gzippableMinSize = 1400
// notGzippableTypes is the list of media types having a compressed content by design.
// Gzip will not be applied to any of these content types.
//
// For best performance, only the most common officials (and future officials) are listed.
//
// 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": {},
"font/otf": {},
"font/ttf": {},
"font/woff": {},
"font/woff2": {},
"image/gif": {},
"image/jpeg": {},
"image/png": {},
"image/webp": {},
"video/h264": {},
"video/h265": {},
"video/mp4": {},
"video/mpeg": {},
"video/ogg": {},
"video/vp8": {},
"video/vp9": {},
"video/webm": {},
}
var gzipPool = sync.Pool{New: func() interface{} { return gzip.NewWriter(nil) }}
// A handler provides a clever gzip compressing handler.
type handler struct {
next http.Handler
}
// Handle returns a Handler wrapping another http.Handler.
func Handle(h http.Handler) http.Handler {
return &handler{h}
}
// HandleFunc returns a Handler wrapping an http.HandlerFunc.
func HandleFunc(f http.HandlerFunc) http.Handler {
return Handle(f)
}
func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Vary", "Accept-Encoding")
if !strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") || r.Header.Get("Sec-WebSocket-Key") != "" {
h.next.ServeHTTP(w, r)
return
}
cw := &compressWriter{
ResponseWriter: w,
gzipWriter: gzipPool.Get().(*gzip.Writer),
firstBytes: new(bytes.Buffer),
}
defer gzipPool.Put(cw.gzipWriter)
defer cw.close()
h.next.ServeHTTP(cw, r)
}
// compressWriter binds the downstream response writing into gzipWriter if the first content is detected as gzippable.
type compressWriter struct {
http.ResponseWriter
gzipWriter *gzip.Writer
firstBytes *bytes.Buffer
status int // status is the buffered response status in case WriteHeader has been called downstream.
gzipChecked bool // gzipChecked tells if the gzippable checking has been done.
gzipUsed bool // gzipUse tells if gzip is used for the response.
}
// WriteHeader catches a downstream WriteHeader call and buffers the status code.
// The header will be written later, at the first Write call, after the gzipping checking has been done.
func (cw *compressWriter) WriteHeader(status int) {
cw.status = status
}
// writeBufferedHeader writes the response header when a buffered status code exists.
func (cw *compressWriter) writeBufferedHeader() {
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.gzipChecked {
if cw.gzipUsed {
return cw.gzipWriter.Write(b)
}
return cw.ResponseWriter.Write(b)
}
if cw.ResponseWriter.Header().Get("Content-Encoding") != "" { // Content is already encoded.
cw.gzipCheckingDone()
return cw.ResponseWriter.Write(b)
}
if cw.firstBytes.Len()+len(b) < gzippableMinSize { // Still insufficient content length to determine gzippability: buffer these first bytes.
return cw.firstBytes.Write(b)
}
ct := cw.ResponseWriter.Header().Get("Content-Type")
if ct == "" {
ct = http.DetectContentType(append(cw.firstBytes.Bytes(), 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 {
cw.gzipCheckingDone()
return cw.ResponseWriter.Write(b)
}
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.gzipUsed = true
cw.gzipCheckingDone()
return cw.gzipWriter.Write(b)
}
// CloseNotify implements the http.CloseNotifier interface.
// No channel is returned if CloseNotify is not implemented by an upstream response writer.
func (cw *compressWriter) CloseNotify() <-chan bool {
n, ok := cw.ResponseWriter.(http.CloseNotifier)
if !ok {
return nil
}
return n.CloseNotify()
}
// Flush implements the http.Flusher interface.
// Nothing is done if Flush is not implemented by an upstream response writer.
func (cw *compressWriter) Flush() {
f, ok := cw.ResponseWriter.(http.Flusher)
if ok {
f.Flush()
}
}
// Hijack implements the http.Hijacker interface.
// Error http.ErrNotSupported is returned if Hijack is not implemented by an upstream response writer.
func (cw *compressWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) {
h, ok := cw.ResponseWriter.(http.Hijacker)
if !ok {
return nil, nil, http.ErrNotSupported
}
return h.Hijack()
}
// Push implements the http.Pusher interface.
// http.ErrNotSupported is returned if Push is not implemented by an upstream response writer or not supported by the client.
func (cw *compressWriter) Push(target string, opts *http.PushOptions) error {
p, ok := cw.ResponseWriter.(http.Pusher)
if !ok {
return http.ErrNotSupported
}
if opts == nil {
opts = new(http.PushOptions)
}
if opts.Header == nil {
opts.Header = make(http.Header)
}
if enc := opts.Header.Get("Accept-Encoding"); enc == "" {
opts.Header.Add("Accept-Encoding", "gzip")
}
return p.Push(target, opts)
}
// gzipCheckingDone writes the buffered data (status and firstBytes) and sets the gzipChecked flag to true.
func (cw *compressWriter) gzipCheckingDone() {
cw.writeBufferedHeader()
if cw.gzipUsed {
cw.firstBytes.WriteTo(cw.gzipWriter)
} else {
cw.firstBytes.WriteTo(cw.ResponseWriter)
}
cw.gzipChecked = true
}
// close writes the buffered data (status and firstBytes) if it has not been done yet and closes the gzip writer if it has been used.
func (cw *compressWriter) close() {
if !cw.gzipChecked {
cw.writeBufferedHeader()
cw.firstBytes.WriteTo(cw.ResponseWriter)
}
if cw.gzipUsed {
cw.gzipWriter.Close()
}
}