Add request_body middleware and some limits to HTTP servers

This commit is contained in:
Matthew Holt 2019-05-23 13:16:34 -06:00
parent c24a3e389f
commit 9e576c76e7
4 changed files with 40 additions and 1 deletions

View File

@ -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,
}

View File

@ -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)

View File

@ -1,4 +1,4 @@
package headers
package rewrite
import (
"net/http"

View File

@ -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"`