-
Notifications
You must be signed in to change notification settings - Fork 0
/
request.go
38 lines (32 loc) · 916 Bytes
/
request.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
package oakmux
import "net/http"
// r.Body = http.MaxBytesReader(w, r.Body, MAX_API_PARAMS_SIZE)
// https://golang.hotexamples.com/examples/net.http/-/MaxBytesReader/golang-maxbytesreader-function-examples.html
type RequestReadLimiter struct {
readLimit int64
next Handler
}
func NewRequestReadLimiter(next Handler, readLimit int64) *RequestReadLimiter {
if next == nil {
panic("cannot use a <nil> HTTP handler")
}
if readLimit == 0 {
panic("cannot use a 0 read limit")
}
return &RequestReadLimiter{
readLimit: readLimit,
next: next,
}
}
func NewRequestReadLimiterMiddleware(readLimit int64) Middleware {
return func(next Handler) Handler {
return NewRequestReadLimiter(next, readLimit)
}
}
func (l *RequestReadLimiter) ServeHyperText(
w http.ResponseWriter,
r *http.Request,
) error {
r.Body = http.MaxBytesReader(w, r.Body, l.readLimit)
return l.next.ServeHyperText(w, r)
}