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 | |
parent | c24a3e389fc63a2cf3d51e0afd743047514ff1d5 (diff) |
Add request_body middleware and some limits to HTTP servers
-rw-r--r-- | modules/caddyhttp/caddyhttp.go | 3 | ||||
-rw-r--r-- | modules/caddyhttp/requestbody/requestbody.go | 33 | ||||
-rw-r--r-- | modules/caddyhttp/rewrite/rewrite.go | 2 | ||||
-rw-r--r-- | modules/caddyhttp/server.go | 3 |
4 files changed, 40 insertions, 1 deletions
diff --git a/modules/caddyhttp/caddyhttp.go b/modules/caddyhttp/caddyhttp.go index 0fde218..305a918 100644 --- a/modules/caddyhttp/caddyhttp.go +++ b/modules/caddyhttp/caddyhttp.go @@ -110,6 +110,9 @@ func (app *App) Start() error { s := &http.Server{ ReadTimeout: time.Duration(srv.ReadTimeout), ReadHeaderTimeout: time.Duration(srv.ReadHeaderTimeout), + WriteTimeout: time.Duration(srv.WriteTimeout), + IdleTimeout: time.Duration(srv.IdleTimeout), + MaxHeaderBytes: srv.MaxHeaderBytes, Handler: srv, } 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) diff --git a/modules/caddyhttp/rewrite/rewrite.go b/modules/caddyhttp/rewrite/rewrite.go index b898b1f..fc91d28 100644 --- a/modules/caddyhttp/rewrite/rewrite.go +++ b/modules/caddyhttp/rewrite/rewrite.go @@ -1,4 +1,4 @@ -package headers +package rewrite import ( "net/http" diff --git a/modules/caddyhttp/server.go b/modules/caddyhttp/server.go index 8a16cea..7b14ffa 100644 --- a/modules/caddyhttp/server.go +++ b/modules/caddyhttp/server.go @@ -17,6 +17,9 @@ type Server struct { Listen []string `json:"listen,omitempty"` ReadTimeout caddy2.Duration `json:"read_timeout,omitempty"` ReadHeaderTimeout caddy2.Duration `json:"read_header_timeout,omitempty"` + WriteTimeout caddy2.Duration `json:"write_timeout,omitempty"` + IdleTimeout caddy2.Duration `json:"idle_timeout,omitempty"` + MaxHeaderBytes int `json:"max_header_bytes,omitempty"` Routes RouteList `json:"routes,omitempty"` Errors *httpErrorConfig `json:"errors,omitempty"` TLSConnPolicies caddytls.ConnectionPolicies `json:"tls_connection_policies,omitempty"` |