2019-05-23 14:16:34 -05:00
|
|
|
package requestbody
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
2019-06-14 12:58:28 -05:00
|
|
|
"github.com/caddyserver/caddy"
|
|
|
|
"github.com/caddyserver/caddy/modules/caddyhttp"
|
2019-05-23 14:16:34 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2019-06-14 12:58:28 -05:00
|
|
|
caddy.RegisterModule(caddy.Module{
|
2019-05-23 14:16:34 -05:00
|
|
|
Name: "http.middleware.request_body",
|
|
|
|
New: func() interface{} { return new(RequestBody) },
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// RequestBody is a middleware for manipulating the request body.
|
|
|
|
type RequestBody struct {
|
|
|
|
MaxSize int64 `json:"max_size,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rb RequestBody) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyhttp.Handler) error {
|
|
|
|
if r.Body == nil {
|
|
|
|
return next.ServeHTTP(w, r)
|
|
|
|
}
|
|
|
|
if rb.MaxSize > 0 {
|
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, rb.MaxSize)
|
|
|
|
}
|
|
|
|
return next.ServeHTTP(w, r)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Interface guard
|
|
|
|
var _ caddyhttp.MiddlewareHandler = (*RequestBody)(nil)
|