-
Notifications
You must be signed in to change notification settings - Fork 0
/
http_gzip.go
290 lines (251 loc) · 6.7 KB
/
http_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
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
package xmw
import (
"compress/gzip"
"io"
"regexp"
"sync"
"github.com/askasoft/pango/str"
"github.com/askasoft/pango/xin"
)
// http://nginx.org/en/docs/http/ngx_http_gzip_module.html
// These constants are copied from the gzip package.
const (
GzipBestCompression = gzip.BestCompression
GzipBestSpeed = gzip.BestSpeed
GzipDefaultCompression = gzip.DefaultCompression
GzipNoCompression = gzip.NoCompression
)
// HTTPGziper Compresses responses using the “gzip” method
type HTTPGziper struct {
// protoMajor Sets the minimum HTTP Major version of a request required to compress a response.
// Default: 1
protoMajor int
// protoMinor Sets the minimum HTTP Minor version of a request required to compress a response.
// Default: 1
protoMinor int
// Proxied Enables or disables gzipping of responses for proxied requests depending on the request and response.
// The fact that the request is proxied is determined by the presence of the “Via” request header field.
// Default: any
proxied ProxiedFlag
// Vary Enables or disables inserting the “Vary: Accept-Encoding” response header field.
// Default: true
vary bool
// the minimum length of a response that will be gzipped.
// Default: 1024
minLength int
// CompressLevel Sets a gzip compression level of a response.
// Default: DefaultCompression
compressLevel int
// mimeTypes Enables gzipping of responses for the specified MIME types.
mimeTypes *stringSet
// ignorePathPrefixs Ignored URL Path Prefixs
ignorePathPrefixs prefixs
// ignoreRegexps Ignored URL Path Regexp
ignorePathRegexps regexps
// disabled Disable gzip
// Default: false
disabled bool
// pool gzip writer pool
pool *sync.Pool
}
// DefaultHTTPGziper create a default zipper
// = NewHTTPGziper(DefaultCompression, 1024)
func DefaultHTTPGziper() *HTTPGziper {
return NewHTTPGziper(GzipDefaultCompression, 1024)
}
// NewHTTPGziper create a zipper
// proxied: ProxiedAny
// vary: true
// minLength: 1024
func NewHTTPGziper(compressLevel, minLength int) *HTTPGziper {
z := &HTTPGziper{
protoMajor: 1,
protoMinor: 1,
proxied: ProxiedAny,
vary: true,
compressLevel: compressLevel,
minLength: minLength,
}
z.pool = &sync.Pool{
New: func() any {
gw := &gzipWriter{}
w, err := gzip.NewWriterLevel(io.Discard, z.compressLevel)
if err != nil {
panic(err)
}
gw.gzw = w
return gw
},
}
z.SetMimeTypes(
"text/html",
"text/plain",
"text/xml",
"text/css",
"text/javascript",
"text/json",
"text/comma-separated-values",
"text/tab-separated-values",
"application/xml",
"application/xhtml+xml",
"application/rss+xml",
"application/atom_xml",
"application/json",
"application/javascript",
"application/x-javascript",
)
return z
}
// SetHTTPVersion Sets the minimum HTTP Proto version of a request required to compress a response.
func (z *HTTPGziper) SetHTTPVersion(major, minor int) {
z.protoMajor = major
z.protoMinor = minor
}
// SetProxied Enables or disables gzipping of responses for proxied requests depending on the request and response.
// The fact that the request is proxied is determined by the presence of the “Via” request header field.
// The directive accepts multiple parameters:
// off
//
// disables compression for all proxied requests, ignoring other parameters;
//
// any (Default)
//
// enables compression for all proxied requests.
//
// auth
//
// enables compression if a request header includes the “Authorization” field;
//
// expired
//
// enables compression if a response header includes the “Expires” field with a value that disables caching;
//
// no-cache
//
// enables compression if a response header includes the “Cache-Control” field with the “no-cache” parameter;
//
// no-store
//
// enables compression if a response header includes the “Cache-Control” field with the “no-store” parameter;
//
// private
//
// enables compression if a response header includes the “Cache-Control” field with the “private” parameter;
//
// no_last_modified
//
// enables compression if a response header does not include the “Last-Modified” field;
//
// no_etag
//
// enables compression if a response header does not include the “ETag” field;
func (z *HTTPGziper) SetProxied(ps ...string) {
z.proxied = toProxiedFlag(ps...)
}
// Vary Enables or disables inserting the “Vary: Accept-Encoding” response header field.
// Default: true
func (z *HTTPGziper) Vary(vary bool) {
z.vary = vary
}
// SetMimeTypes Enables gzipping of responses for the specified MIME types.
// The special value "*" matches any MIME type.
// Default:
//
// text/html
// text/plain
// text/xml
// text/css
// text/javascript
// text/json
// text/comma-separated-values
// text/tab-separated-values
// application/xml
// application/xhtml+xml
// application/rss+xml
// application/atom_xml
// application/json
// application/javascript
// application/x-javascript
func (z *HTTPGziper) SetMimeTypes(mts ...string) {
if len(mts) == 0 {
z.mimeTypes = nil
return
}
hs := newStringSet(mts...)
if hs.Contains("*") {
hs = nil
}
z.mimeTypes = hs
}
// IgnorePathPrefix ignore URL path prefix
func (z *HTTPGziper) IgnorePathPrefix(ps ...string) {
z.ignorePathPrefixs = ps
}
// IgnorePathRegexp ignore URL path regexp
func (z *HTTPGziper) IgnorePathRegexp(ps ...string) {
rs := make([]*regexp.Regexp, len(ps))
for i, p := range ps {
rs[i] = regexp.MustCompile(p)
}
z.ignorePathRegexps = rs
}
// Disable disable the gzip compress or not
func (z *HTTPGziper) Disable(disabled bool) {
z.disabled = disabled
}
// Handler returns the xin.HandlerFunc
func (z *HTTPGziper) Handler() xin.HandlerFunc {
return z.Handle
}
// Handle process xin request
func (z *HTTPGziper) Handle(c *xin.Context) {
if !z.shouldCompress(c) {
c.Next()
return
}
gw := z.pool.Get().(*gzipWriter)
gw.hgz = z
gw.ctx = c
gw.ResponseWriter = c.Writer
c.Writer = gw
defer func() {
gw.Close()
z.pool.Put(gw)
}()
c.Next()
}
func (z *HTTPGziper) shouldCompress(c *xin.Context) bool {
req := c.Request
if z.disabled {
return false
}
if !req.ProtoAtLeast(z.protoMajor, z.protoMinor) {
return false
}
if !str.ContainsFold(req.Header.Get("Accept-Encoding"), "gzip") {
return false
}
if str.ContainsFold(req.Header.Get("Connection"), "Upgrade") {
return false
}
if str.ContainsFold(req.Header.Get("Content-Type"), "text/event-stream") {
return false
}
if req.Header.Get("Via") != "" {
if z.proxied == ProxiedOff {
return false
}
if z.proxied&ProxiedAuth == ProxiedAuth {
if req.Header.Get("Authorization") == "" {
return false
}
}
}
if z.ignorePathPrefixs.Contains(req.URL.Path) {
return false
}
if z.ignorePathRegexps.Contains(req.URL.Path) {
return false
}
return true
}