diff --git a/modules/caddyhttp/requestbody/caddyfile.go b/modules/caddyhttp/requestbody/caddyfile.go index 8378ad7f471..7e4ee129eb3 100644 --- a/modules/caddyhttp/requestbody/caddyfile.go +++ b/modules/caddyhttp/requestbody/caddyfile.go @@ -68,6 +68,12 @@ func parseCaddyfile(h httpcaddyfile.Helper) (caddyhttp.MiddlewareHandler, error) } rb.WriteTimeout = timeout + case "replace": + var replaceStr string + if !h.AllArgs(&replaceStr) { + return nil, h.ArgErr() + } + rb.Replace = replaceStr default: return nil, h.Errf("unrecognized request_body subdirective '%s'", h.Val()) } diff --git a/modules/caddyhttp/requestbody/requestbody.go b/modules/caddyhttp/requestbody/requestbody.go index d00455a8c97..79ea8c394bc 100644 --- a/modules/caddyhttp/requestbody/requestbody.go +++ b/modules/caddyhttp/requestbody/requestbody.go @@ -17,6 +17,7 @@ package requestbody import ( "io" "net/http" + "strings" "time" "go.uber.org/zap" @@ -41,6 +42,8 @@ type RequestBody struct { // EXPERIMENTAL. Subject to change/removal. WriteTimeout time.Duration `json:"write_timeout,omitempty"` + Replace string `json:"replace,omitempty"` + logger *zap.Logger } @@ -58,6 +61,14 @@ func (rb *RequestBody) Provision(ctx caddy.Context) error { } func (rb RequestBody) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyhttp.Handler) error { + if rb.Replace != "" { + err := r.Body.Close() + if err != nil { + return err + } + r.Body = io.NopCloser(strings.NewReader(rb.Replace)) + r.ContentLength = int64(len(rb.Replace)) + } if r.Body == nil { return next.ServeHTTP(w, r) }