MaxBody returns a middleware that limits the maximum body of requests.
(maxBytes int64)
| 23 | |
| 24 | // MaxBody returns a middleware that limits the maximum body of requests. |
| 25 | func MaxBody(maxBytes int64) MiddlewareFunc { |
| 26 | return func(next http.Handler) http.Handler { |
| 27 | if maxBytes == 0 { |
| 28 | return next |
| 29 | } |
| 30 | return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 31 | r.Body = &wrappedMaxBytesReader{ |
| 32 | ReadCloser: http.MaxBytesReader(w, r.Body, maxBytes), |
| 33 | maxBytes: maxBytes, |
| 34 | } |
| 35 | next.ServeHTTP(w, r) |
| 36 | }) |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | // NOTE: Ideally this would translate to a HTTP 413 error code, |
| 41 | // but we can't represent those at the moment. |