/
writerwrapper.go
192 lines (161 loc) · 4.21 KB
/
writerwrapper.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
package gzip
import (
"compress/gzip"
"net/http"
"strconv"
"strings"
)
// writerWrapper wraps the originalHandler
// to test whether to gzip and gzip the body if applicable.
type writerWrapper struct {
// header filter are applied by its sequence
Filters []ResponseHeaderFilter
// min content length to enable compress
MinContentLength int64
OriginWriter http.ResponseWriter
// use initGzipWriter() to init gzipWriter when in need
GetGzipWriter func() *gzip.Writer
// must close gzip writer and put it back to pool
PutGzipWriter func(*gzip.Writer)
// internal below
// compress or not
// default to true
shouldCompress bool
gzipWriter *gzip.Writer
// is header already flushed?
headerFlushed bool
didFirstWrite bool
statusCode int
}
func newWriterWrapper(filters []ResponseHeaderFilter, minContentLength int64, originWriter http.ResponseWriter, getGzipWriter func() *gzip.Writer, putGzipWriter func(*gzip.Writer)) *writerWrapper {
return &writerWrapper{
shouldCompress: true,
Filters: filters,
MinContentLength: minContentLength,
OriginWriter: originWriter,
GetGzipWriter: getGzipWriter,
PutGzipWriter: putGzipWriter,
}
}
// interface guard
var _ http.ResponseWriter = (*writerWrapper)(nil)
var _ http.Flusher = (*writerWrapper)(nil)
func (w *writerWrapper) headerWritten() bool {
return w.statusCode != 0
}
func (w *writerWrapper) initGzipWriter() {
w.gzipWriter = w.GetGzipWriter()
w.gzipWriter.Reset(w.OriginWriter)
}
// Header implements http.ResponseWriter
func (w *writerWrapper) Header() http.Header {
return w.OriginWriter.Header()
}
// Write implements http.ResponseWriter
func (w *writerWrapper) Write(data []byte) (int, error) {
if !w.headerWritten() {
w.WriteHeader(http.StatusOK)
}
header := w.Header()
for _, filter := range w.Filters {
w.shouldCompress = filter.ShouldCompress(header)
if !w.shouldCompress {
break
}
}
// use origin handler directly
if !w.shouldCompress {
w.flushHeader()
w.didFirstWrite = true
return w.OriginWriter.Write(data)
}
if !w.didFirstWrite {
w.didFirstWrite = true
w.shouldCompress = w.enoughContentLength(data)
w.flushHeader()
if w.shouldCompress {
w.initGzipWriter()
}
}
if w.shouldCompress {
return w.gzipWriter.Write(data)
}
return w.OriginWriter.Write(data)
}
func (w *writerWrapper) enoughContentLength(data []byte) bool {
var (
header = w.Header()
_, haveCl = header["Content-Length"]
contentLength int64
)
if haveCl {
contentLength, _ = strconv.ParseInt(header.Get("Content-Length"), 10, 64)
} else {
contentLength = int64(len(data))
}
if contentLength == 0 || contentLength < w.MinContentLength {
return false
}
return true
}
// WriteHeader implements http.ResponseWriter
//
// WriteHeader does not really calls originalHandler's WriteHeader,
// and the calling will actually be handler by flushHeader().
func (w *writerWrapper) WriteHeader(statusCode int) {
if w.headerWritten() {
return
}
w.statusCode = statusCode
if !w.shouldCompress {
return
}
if statusCode == http.StatusNoContent ||
statusCode == http.StatusNotModified {
w.shouldCompress = false
return
}
}
// flushHeader must always be called and called after
// WriteHeader() is called and
// w.shouldCompress is decided.
func (w *writerWrapper) flushHeader() {
if w.headerFlushed {
return
}
// if neither WriteHeader() or Write() are called,
// do nothing
if !w.headerWritten() {
return
}
if w.shouldCompress {
header := w.Header()
header.Del("Content-Length")
header.Set("Content-Encoding", "gzip")
header.Add("Vary", "Accept-Encoding")
originalEtag := w.Header().Get("ETag")
if originalEtag != "" && !strings.HasPrefix(originalEtag, "W/") {
w.Header().Set("ETag", "W/"+originalEtag)
}
}
w.OriginWriter.WriteHeader(w.statusCode)
w.headerFlushed = true
}
// CleanUp flushes header and closed gzip writer
//
// Write() and WriteHeader() should not be called
// after CleanUp()
func (w *writerWrapper) CleanUp() {
w.flushHeader()
if w.gzipWriter != nil {
w.PutGzipWriter(w.gzipWriter)
w.gzipWriter = nil
}
}
// Flush implements http.Flusher
func (w *writerWrapper) Flush() {
w.CleanUp()
if flusher, ok := w.OriginWriter.(http.Flusher); ok {
flusher.Flush()
}
}