-
Notifications
You must be signed in to change notification settings - Fork 325
/
Copy pathbuffer.go
351 lines (300 loc) · 10.6 KB
/
buffer.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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/*
Package buffer provides http.Handler middleware that solves several problems when dealing with http requests:
Reads the entire request and response into buffer, optionally buffering it to disk for large requests.
Checks the limits for the requests and responses, rejecting in case if the limit was exceeded.
Changes request content-transfer-encoding from chunked and provides total size to the handlers.
Examples of a buffering middleware:
// sample HTTP handler.
handler := http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
w.Write([]byte("hello"))
})
// Buffer will read the body in buffer before passing the request to the handler
// calculate total size of the request and transform it from chunked encoding
// before passing to the server
buffer.New(handler)
// This version will buffer up to 2MB in memory and will serialize any extra
// to a temporary file, if the request size exceeds 10MB it will reject the request
buffer.New(handler,
buffer.MemRequestBodyBytes(2 * 1024 * 1024),
buffer.MaxRequestBodyBytes(10 * 1024 * 1024))
// Will do the same as above, but with responses
buffer.New(handler,
buffer.MemResponseBodyBytes(2 * 1024 * 1024),
buffer.MaxResponseBodyBytes(10 * 1024 * 1024))
// Buffer will replay the request if the handler returns error at least 3 times
// before returning the response
buffer.New(handler, buffer.Retry(`IsNetworkError() && Attempts() <= 2`))
*/
package buffer
import (
"bufio"
"fmt"
"io"
"net"
"net/http"
"reflect"
"github.com/mailgun/multibuf"
"github.com/vulcand/oxy/v2/utils"
)
const (
// DefaultMemBodyBytes Store up to 1MB in RAM.
DefaultMemBodyBytes = 1048576
// DefaultMaxBodyBytes No limit by default.
DefaultMaxBodyBytes = -1
// DefaultMaxRetryAttempts Maximum retry attempts.
DefaultMaxRetryAttempts = 10
)
var errHandler utils.ErrorHandler = &SizeErrHandler{}
// Buffer is responsible for buffering requests and responses
// It buffers large requests and responses to disk,.
type Buffer struct {
maxRequestBodyBytes int64
memRequestBodyBytes int64
maxResponseBodyBytes int64
memResponseBodyBytes int64
retryPredicate hpredicate
next http.Handler
errHandler utils.ErrorHandler
verbose bool
log utils.Logger
}
// New returns a new buffer middleware. New() function supports optional functional arguments.
func New(next http.Handler, setters ...Option) (*Buffer, error) {
strm := &Buffer{
next: next,
maxRequestBodyBytes: DefaultMaxBodyBytes,
memRequestBodyBytes: DefaultMemBodyBytes,
maxResponseBodyBytes: DefaultMaxBodyBytes,
memResponseBodyBytes: DefaultMemBodyBytes,
log: &utils.NoopLogger{},
}
for _, s := range setters {
if err := s(strm); err != nil {
return nil, err
}
}
if strm.errHandler == nil {
strm.errHandler = errHandler
}
return strm, nil
}
// Wrap sets the next handler to be called by buffer handler.
func (b *Buffer) Wrap(next http.Handler) error {
b.next = next
return nil
}
func (b *Buffer) ServeHTTP(w http.ResponseWriter, req *http.Request) {
if b.verbose {
dump := utils.DumpHTTPRequest(req)
b.log.Debug("vulcand/oxy/buffer: begin ServeHttp on request: %s", dump)
defer b.log.Debug("vulcand/oxy/buffer: completed ServeHttp on request: %s", dump)
}
if err := b.checkLimit(req); err != nil {
b.log.Error("vulcand/oxy/buffer: request body over limit, err: %v", err)
b.errHandler.ServeHTTP(w, req, err)
return
}
// Read the body while keeping limits in mind. This reader controls the maximum bytes
// to read into memory and disk. This reader returns an error if the total request size exceeds the
// predefined MaxSizeBytes. This can occur if we got chunked request, in this case ContentLength would be set to -1
// and the reader would be unbounded bufio in the http.Server
body, err := multibuf.New(req.Body, multibuf.MaxBytes(b.maxRequestBodyBytes), multibuf.MemBytes(b.memRequestBodyBytes))
if err != nil || body == nil {
if req.Context().Err() != nil {
b.log.Error("vulcand/oxy/buffer: error when reading request body, err: %v", req.Context().Err())
b.errHandler.ServeHTTP(w, req, req.Context().Err())
return
}
b.log.Error("vulcand/oxy/buffer: error when reading request body, err: %v", err)
b.errHandler.ServeHTTP(w, req, err)
return
}
// Set request body to buffered reader that can replay the read and execute Seek
// Note that we don't change the original request body as it's handled by the http server
// and we don't want to mess with standard library
defer func() {
if body != nil {
errClose := body.Close()
if errClose != nil {
b.log.Error("vulcand/oxy/buffer: failed to close body, err: %v", errClose)
}
}
}()
// We need to set ContentLength based on known request size. The incoming request may have been
// set without content length or using chunked TransferEncoding
totalSize, err := body.Size()
if err != nil {
b.log.Error("vulcand/oxy/buffer: failed to get request size, err: %v", err)
b.errHandler.ServeHTTP(w, req, err)
return
}
if totalSize == 0 {
body = nil
}
outReq := b.copyRequest(req, body, totalSize)
attempt := 1
for {
// We create a special writer that will limit the response size, buffer it to disk if necessary
writer, err := multibuf.NewWriterOnce(multibuf.MaxBytes(b.maxResponseBodyBytes), multibuf.MemBytes(b.memResponseBodyBytes))
if err != nil {
b.log.Error("vulcand/oxy/buffer: failed create response writer, err: %v", err)
b.errHandler.ServeHTTP(w, req, err)
return
}
// We are mimicking http.ResponseWriter to replace writer with our special writer
bw := &bufferWriter{
header: make(http.Header),
buffer: writer,
responseWriter: w,
log: b.log,
}
defer bw.Close()
b.next.ServeHTTP(bw, outReq)
if bw.hijacked {
b.log.Debug("vulcand/oxy/buffer: connection was hijacked downstream. Not taking any action in buffer.")
return
}
if bw.writeError != nil {
b.log.Error("vulcand/oxy/buffer: failed to copy response, err: %v", bw.writeError)
b.errHandler.ServeHTTP(w, req, bw.writeError)
return
}
var reader multibuf.MultiReader
if bw.expectBody(outReq) {
rdr, err := writer.Reader()
if err != nil {
b.log.Error("vulcand/oxy/buffer: failed to read response, err: %v", err)
b.errHandler.ServeHTTP(w, req, err)
return
}
defer rdr.Close()
reader = rdr
}
if (b.retryPredicate == nil || attempt > DefaultMaxRetryAttempts) ||
!b.retryPredicate(&context{r: req, attempt: attempt, responseCode: bw.code}) {
utils.CopyHeaders(w.Header(), bw.Header())
w.WriteHeader(bw.code)
if reader != nil {
_, _ = io.Copy(w, reader)
}
return
}
attempt++
if body != nil {
if _, err := body.Seek(0, 0); err != nil {
b.log.Error("vulcand/oxy/buffer: failed to rewind response body, err: %v", err)
b.errHandler.ServeHTTP(w, req, err)
return
}
}
outReq = b.copyRequest(req, body, totalSize)
b.log.Debug("vulcand/oxy/buffer: retry Request(%v %v) attempt %v", req.Method, req.URL, attempt)
}
}
func (b *Buffer) copyRequest(req *http.Request, body io.ReadCloser, bodySize int64) *http.Request {
o := *req
o.URL = utils.CopyURL(req.URL)
o.Header = make(http.Header)
utils.CopyHeaders(o.Header, req.Header)
o.ContentLength = bodySize
// remove TransferEncoding that could have been previously set because we have transformed the request from chunked encoding
o.TransferEncoding = []string{}
// http.Transport will close the request body on any error, we are controlling the close process ourselves, so we override the closer here
if body == nil {
o.Body = io.NopCloser(req.Body)
} else {
o.Body = io.NopCloser(body.(io.Reader))
}
return &o
}
func (b *Buffer) checkLimit(req *http.Request) error {
if b.maxRequestBodyBytes <= 0 {
return nil
}
if req.ContentLength > b.maxRequestBodyBytes {
return &multibuf.MaxSizeReachedError{MaxSize: b.maxRequestBodyBytes}
}
return nil
}
type bufferWriter struct {
header http.Header
code int
buffer multibuf.WriterOnce
responseWriter http.ResponseWriter
hijacked bool
writeError error
log utils.Logger
}
// RFC2616 #4.4.
func (b *bufferWriter) expectBody(r *http.Request) bool {
if r.Method == http.MethodHead {
return false
}
if (b.code >= 100 && b.code < 200) || b.code == 204 || b.code == 304 {
return false
}
// refer to https://github.com/vulcand/oxy/issues/113
// if b.header.Get("Content-Length") == "" && b.header.Get("Transfer-Encoding") == "" {
// return false
// }
if b.header.Get("Content-Length") == "0" {
return false
}
// Support for gRPC, gRPC Web.
if grpcStatus := b.header.Get("Grpc-Status"); grpcStatus != "" && grpcStatus != "0" {
return false
}
return true
}
func (b *bufferWriter) Close() error {
return b.buffer.Close()
}
func (b *bufferWriter) Header() http.Header {
return b.header
}
func (b *bufferWriter) Write(buf []byte) (int, error) {
length, err := b.buffer.Write(buf)
if err != nil {
// Since go1.11 (https://github.com/golang/go/commit/8f38f28222abccc505b9a1992deecfe3e2cb85de)
// if the writer returns an error, the reverse proxy panics
b.log.Error("write: %v", err)
length = len(buf)
b.writeError = err
}
return length, nil
}
// WriteHeader sets rw.Code.
func (b *bufferWriter) WriteHeader(code int) {
b.code = code
}
// CloseNotify CloseNotifier interface - this allows downstream connections to be terminated when the client terminates.
func (b *bufferWriter) CloseNotify() <-chan bool {
if cn, ok := b.responseWriter.(http.CloseNotifier); ok {
return cn.CloseNotify()
}
b.log.Warn("Upstream ResponseWriter of type %v does not implement http.CloseNotifier. Returning dummy channel.", reflect.TypeOf(b.responseWriter))
return make(<-chan bool)
}
// Hijack This allows connections to be hijacked for websockets for instance.
func (b *bufferWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) {
if hi, ok := b.responseWriter.(http.Hijacker); ok {
conn, rw, err := hi.Hijack()
if err == nil {
b.hijacked = true
}
return conn, rw, err
}
b.log.Warn("Upstream ResponseWriter of type %v does not implement http.Hijacker.", reflect.TypeOf(b.responseWriter))
return nil, nil, fmt.Errorf("the response writer wrapped in this proxy does not implement http.Hijacker. Its type is: %v", reflect.TypeOf(b.responseWriter))
}
// SizeErrHandler Size error handler.
type SizeErrHandler struct{}
func (e *SizeErrHandler) ServeHTTP(w http.ResponseWriter, req *http.Request, err error) {
//nolint:errorlint // must be changed
if _, ok := err.(*multibuf.MaxSizeReachedError); ok {
w.WriteHeader(http.StatusRequestEntityTooLarge)
_, _ = w.Write([]byte(http.StatusText(http.StatusRequestEntityTooLarge)))
return
}
utils.DefaultHandler.ServeHTTP(w, req, err)
}