-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcompress.go
185 lines (160 loc) · 4.86 KB
/
compress.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
// This file is copied from Huma v1 middleware, slightly modified to use v2
// for content negotiation.
package main
import (
"bytes"
"compress/gzip"
"io"
"net/http"
"path"
"strconv"
"sync"
"github.com/andybalholm/brotli"
"github.com/danielgtaylor/huma/v2/negotiation"
)
const gzipEncoding = "gzip"
const brotliEncoding = "br"
var supportedEncodings []string = []string{brotliEncoding, gzipEncoding}
var compressDenyList []string = []string{".gif", ".png", ".jpg", ".jpeg", ".zip", ".gz", ".bz2"}
type contentEncodingWriter struct {
http.ResponseWriter
status int
encoding string
buf *bytes.Buffer
writer io.Writer
minSize int
gzPool *sync.Pool
brPool *sync.Pool
wroteHeader bool
}
func (w *contentEncodingWriter) Write(data []byte) (int, error) {
if w.writer != nil {
// We are writing compressed data.
return w.writer.Write(data)
}
if w.Header().Get("Content-Encoding") != "" {
// Content encoding was already set, so we should ignore this!
return w.ResponseWriter.Write(data)
}
// Buffer the data until we can decide whether to compress it or not.
w.buf.Write(data)
cl, _ := strconv.Atoi(w.Header().Get("Content-Length"))
if cl >= w.minSize || w.buf.Len() >= w.minSize {
// We reached our minimum compression size. Set the writer, write the buffer
// and make sure to set the correct headers.
switch w.encoding {
case gzipEncoding:
gz := w.gzPool.Get().(*gzip.Writer)
gz.Reset(w.ResponseWriter)
w.writer = gz
case brotliEncoding:
br := w.brPool.Get().(*brotli.Writer)
br.Reset(w.ResponseWriter)
w.writer = br
}
w.Header().Set("Content-Encoding", w.encoding)
w.Header().Add("Vary", "Accept-Encoding")
w.ResponseWriter.WriteHeader(w.status)
w.wroteHeader = true
bufData := w.buf.Bytes()
w.buf.Reset()
return w.writer.Write(bufData)
}
// Not sure yet whether this should be compressed.
return len(data), nil
}
func (w *contentEncodingWriter) WriteHeader(code int) {
w.Header().Del("Content-Length")
w.status = code
}
func (w *contentEncodingWriter) finish() {
if !w.wroteHeader {
w.ResponseWriter.WriteHeader(w.status)
}
if w.buf.Len() > 0 {
w.ResponseWriter.Write(w.buf.Bytes())
}
}
func (w *contentEncodingWriter) Close() {
if w.writer != nil {
if wc, ok := w.writer.(io.WriteCloser); ok {
wc.Close()
}
// Return the writer to the pool so it can be reused.
switch w.encoding {
case gzipEncoding:
w.gzPool.Put(w.writer)
case brotliEncoding:
w.brPool.Put(w.writer)
}
}
}
// ContentEncoding uses content negotiation with the client to pick
// an appropriate encoding (compression) method and transparently encodes
// the response. Supports GZip and Brotli.
func ContentEncoding(next http.Handler) http.Handler {
// Use pools to reduce allocations. We use a byte buffer to temporarily store
// some of each response in order to determine whether compression should
// be applied. The others are just re-using the GZip and Brotli compressors.
bufPool := sync.Pool{
New: func() interface{} {
return new(bytes.Buffer)
},
}
gzPool := sync.Pool{
New: func() interface{} {
return gzip.NewWriter(io.Discard)
},
}
brPool := sync.Pool{
New: func() interface{} {
return brotli.NewWriter(io.Discard)
},
}
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if ext := path.Ext(r.URL.Path); ext != "" {
for _, deny := range compressDenyList {
if ext == deny {
// This is a file type we should not try to compress.
next.ServeHTTP(w, r)
return
}
}
}
if ac := r.Header.Get("Accept-Encoding"); ac != "" {
best := negotiation.SelectQValueFast(ac, supportedEncodings)
if best != "" {
buf := bufPool.Get().(*bytes.Buffer)
buf.Reset()
defer bufPool.Put(buf)
cew := &contentEncodingWriter{
ResponseWriter: w,
status: http.StatusOK,
encoding: best,
buf: buf,
gzPool: &gzPool,
brPool: &brPool,
// minSize of the body at which compression is enabled. Internet MTU
// size is 1500 bytes, so anything smaller will still require sending
// at least that size (including headers). Google's research at
// http://dev.chromium.org/spdy/spdy-whitepaper suggests headers
// are at least 200 bytes and average 700-800 bytes. If we assume
// an average 30% compression ratio and 500 bytes of headers, then
// (1400 * 0.7) + 500 = 1480 bytes, just about the minimum MTU.
minSize: 1400,
}
// Make sure to clean up and return the buffer and encoder to their
// respective pools for re-use when done.
defer cew.Close()
w = cew
}
}
next.ServeHTTP(w, r)
if cew, ok := w.(*contentEncodingWriter); ok {
// If a content-encoding was negotiated, and we didn't panic in the
// handler, we want to finish by flushing any buffered writes as needed
// before returning.
cew.finish()
}
})
}