diff options
author | Matthew Holt <mholt@users.noreply.github.com> | 2019-05-23 13:16:34 -0600 |
---|---|---|
committer | Matthew Holt <mholt@users.noreply.github.com> | 2019-05-23 13:16:34 -0600 |
commit | 9e576c76e74ff3d5b8d5a384fecf2a2ca191b402 (patch) | |
tree | 450517e2f358b3d1ed13ed017bfdc6016f30e0ca /modules/caddyhttp/requestbody | |
parent | c24a3e389fc63a2cf3d51e0afd743047514ff1d5 (diff) |
Add request_body middleware and some limits to HTTP servers
Diffstat (limited to 'modules/caddyhttp/requestbody')
-rw-r--r-- | modules/caddyhttp/requestbody/requestbody.go | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/modules/caddyhttp/requestbody/requestbody.go b/modules/caddyhttp/requestbody/requestbody.go new file mode 100644 index 0000000..8aba14e --- /dev/null +++ b/modules/caddyhttp/requestbody/requestbody.go @@ -0,0 +1,33 @@ +package requestbody + +import ( + "net/http" + + "bitbucket.org/lightcodelabs/caddy2" + "bitbucket.org/lightcodelabs/caddy2/modules/caddyhttp" +) + +func init() { + caddy2.RegisterModule(caddy2.Module{ + 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) |