-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
sizelimit.go
40 lines (32 loc) · 909 Bytes
/
sizelimit.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
package rest
import (
"bytes"
"io"
"net/http"
)
// SizeLimit middleware checks if body size is above the limit and returns StatusRequestEntityTooLarge (413)
func SizeLimit(size int64) func(http.Handler) http.Handler {
return func(h http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
// check ContentLength
if r.ContentLength > size {
w.WriteHeader(http.StatusRequestEntityTooLarge)
return
}
// check size of the actual body
content, err := io.ReadAll(io.LimitReader(r.Body, size+1))
if err != nil {
w.WriteHeader(http.StatusServiceUnavailable)
return
}
_ = r.Body.Close() // the original body already consumed
if int64(len(content)) > size {
w.WriteHeader(http.StatusRequestEntityTooLarge)
return
}
r.Body = io.NopCloser(bytes.NewReader(content))
h.ServeHTTP(w, r)
}
return http.HandlerFunc(fn)
}
}