diff options
| author | Matthew Holt <mholt@users.noreply.github.com> | 2022-03-11 12:34:55 -0700 | 
|---|---|---|
| committer | Matthew Holt <mholt@users.noreply.github.com> | 2022-03-11 12:34:55 -0700 | 
| commit | 3d616e8c6d65e5617f5a918d72fb1514c9c7144e (patch) | |
| tree | c9d4625edc176dae528faf7cfebdd107089c4397 /modules/caddyhttp/requestbody | |
| parent | b82e22b459b9f81278810dc32916ca270efa888c (diff) | |
requestbody: Return HTTP 413 (fix #4558)
Diffstat (limited to 'modules/caddyhttp/requestbody')
| -rw-r--r-- | modules/caddyhttp/requestbody/requestbody.go | 18 | 
1 files changed, 17 insertions, 1 deletions
diff --git a/modules/caddyhttp/requestbody/requestbody.go b/modules/caddyhttp/requestbody/requestbody.go index 76cd274..dfc0fd9 100644 --- a/modules/caddyhttp/requestbody/requestbody.go +++ b/modules/caddyhttp/requestbody/requestbody.go @@ -15,6 +15,7 @@  package requestbody  import ( +	"io"  	"net/http"  	"github.com/caddyserver/caddy/v2" @@ -28,6 +29,7 @@ func init() {  // RequestBody is a middleware for manipulating the request body.  type RequestBody struct {  	// The maximum number of bytes to allow reading from the body by a later handler. +	// If more bytes are read, an error with HTTP status 413 is returned.  	MaxSize int64 `json:"max_size,omitempty"`  } @@ -44,10 +46,24 @@ func (rb RequestBody) ServeHTTP(w http.ResponseWriter, r *http.Request, next cad  		return next.ServeHTTP(w, r)  	}  	if rb.MaxSize > 0 { -		r.Body = http.MaxBytesReader(w, r.Body, rb.MaxSize) +		r.Body = errorWrapper{http.MaxBytesReader(w, r.Body, rb.MaxSize)}  	}  	return next.ServeHTTP(w, r)  } +// errorWrapper wraps errors that are returned from Read() +// so that they can be associated with a proper status code. +type errorWrapper struct { +	io.ReadCloser +} + +func (ew errorWrapper) Read(p []byte) (n int, err error) { +	n, err = ew.ReadCloser.Read(p) +	if err != nil && err.Error() == "http: request body too large" { +		err = caddyhttp.Error(http.StatusRequestEntityTooLarge, err) +	} +	return +} +  // Interface guard  var _ caddyhttp.MiddlewareHandler = (*RequestBody)(nil)  | 
