-
Notifications
You must be signed in to change notification settings - Fork 0
/
response.go
216 lines (181 loc) · 4.98 KB
/
response.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
package httpzip
import (
"compress/gzip"
"compress/zlib"
"io"
"net/http"
"strings"
)
type encMethod string
const (
// Encoding methods implemented by this library.
// Names should match expected http header values.
encGzip = encMethod("gzip")
encDeflate = encMethod("deflate")
// Size of buffer to store initial uncompressed data.
// Should be at least 512 to comply with detectContentType requirment.
initBufferSize = 512
)
// NewResponseHandler returns handler which transparently compresses response
// written by passed handler h. The compression algorithm is being chosen
// accordingly to the value of Accept-Encoding header: both gzip and deflate
// are supported, with gzip taking precedence if both are present.
//
// If WriteHeader is called on the handler compression is disabled. This is to
// avoid sending empty archives or compressing small error responses.
//
// The returned handler preserves http.CloseNotifier, http.Hijacker and http.Flusher
// implementation of h, if any.
func NewResponseHandler(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var enc encMethod
// Encode response
accept := r.Header.Get("Accept-Encoding")
if strings.Contains(accept, string(encGzip)) {
enc = encGzip
} else if strings.Contains(accept, string(encDeflate)) {
enc = encDeflate
}
if enc != "" {
rw := newResponseWriter(w, enc)
defer rw.Close()
w = rw
}
// Pass to the wrapped handler
w.Header().Add("Vary", "Accept-Encoding")
h.ServeHTTP(w, r)
})
}
// responseWriter is a ResponseWriter wrapper that will be provided to user
type responseWriter struct {
http.ResponseWriter // original response writer
method encMethod
buf []byte
cw compressor
err error
// Interfaces form http package implemented by standard ResponseWriter.
// May be nil if wrapped ResponseWriter doesn't implement them.
http.CloseNotifier
http.Hijacker
}
func newResponseWriter(rw http.ResponseWriter, method encMethod) *responseWriter {
r := &responseWriter{
ResponseWriter: rw,
method: method,
buf: make([]byte, 0, initBufferSize),
cw: nil,
err: nil,
}
if v, ok := rw.(http.CloseNotifier); ok {
r.CloseNotifier = v
}
if v, ok := rw.(http.Hijacker); ok {
r.Hijacker = v
}
return r
}
func (w *responseWriter) Write(p []byte) (nn int, err error) {
if w.err != nil {
return 0, w.err
}
if w.buf != nil {
n := copy(w.buf[len(w.buf):cap(w.buf)], p)
w.buf = w.buf[:len(w.buf)+n]
p = p[n:]
if len(w.buf) == cap(w.buf) {
w.err = w.initCompressor(true)
if w.err != nil {
return 0, w.err
}
}
nn = n
}
if len(p) > 0 && w.err == nil {
var n int
n, err = w.cw.Write(p)
nn += n
}
return
}
// WriteHeader is called before any Write and usually in case of an error.
// As we don't know how much data is going to be written (if any) we skip
// compression. If we enabled compression here we would always compress small
// error responses and send compression header even for empty responses.
func (w *responseWriter) WriteHeader(c int) {
w.initCompressor(false)
w.ResponseWriter.WriteHeader(c)
}
func (w *responseWriter) Flush() {
if w.err != nil {
return
}
// If there is anything in the buffer, pass to compressor
if len(w.buf) > 0 {
w.err = w.initCompressor(true)
}
if w.cw != nil {
if err := w.cw.Flush(); err != nil {
w.err = err
}
}
if f, _ := w.ResponseWriter.(http.Flusher); f != nil {
f.Flush()
}
}
func (w *responseWriter) Close() {
w.initCompressor(false)
w.cw.Close()
}
func (w *responseWriter) detectContentType() {
if w.buf != nil && w.Header().Get("Content-Type") == "" {
w.Header().Set("Content-Type", http.DetectContentType(w.buf))
}
}
// create compressor, feed it with buffer;
// if ok is set to false we don't want to use compression.
// It's a no-op if compressor is already initialized.
func (w *responseWriter) initCompressor(ok bool) (err error) {
if w.cw != nil {
return nil
}
if ok {
w.detectContentType()
switch w.method {
case encGzip:
w.cw = gzip.NewWriter(w.ResponseWriter)
case encDeflate:
w.cw = zlib.NewWriter(w.ResponseWriter)
default:
panic(w.method)
}
// Set Content-Encoding and delete Content-Length as it gets invalidated
w.Header().Add("Content-Encoding", string(w.method))
w.Header().Del("Content-Length")
} else {
w.cw = noneCompressor{w.ResponseWriter}
}
// Don't write empty buffer as it would write a gzip header,
// flushing the HTTP header onto the wire.
if len(w.buf) > 0 {
_, err = w.cw.Write(w.buf)
}
w.buf = nil
return err
}
// compressor is a common interface for compressors. It's similar to
// writeFlusher, but flush returns error, which is ignored by this library.
type compressor interface {
io.WriteCloser
Flush() error
}
// noneCompressor implements compressor interface without
// performing any compression
type noneCompressor struct {
io.Writer
}
func (noneCompressor) Close() error {
return nil
}
func (noneCompressor) Flush() error {
return nil
}