mirror of
https://github.com/caddyserver/caddy.git
synced 2025-02-16 23:22:45 +08:00
Add: rename directive to set_body
This commit is contained in:
parent
49f89a2f6f
commit
9aeba18adf
|
@ -68,14 +68,14 @@ func parseCaddyfile(h httpcaddyfile.Helper) (caddyhttp.MiddlewareHandler, error)
|
|||
}
|
||||
rb.WriteTimeout = timeout
|
||||
|
||||
case "replace":
|
||||
var replaceStr string
|
||||
if !h.AllArgs(&replaceStr) {
|
||||
case "set":
|
||||
var setStr string
|
||||
if !h.AllArgs(&setStr) {
|
||||
return nil, h.ArgErr()
|
||||
}
|
||||
rb.Replace = replaceStr
|
||||
rb.Set = setStr
|
||||
default:
|
||||
return nil, h.Errf("unrecognized request_body subdirective '%s'", h.Val())
|
||||
return nil, h.Errf("unrecognized set_body subdirective '%s'", h.Val())
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -42,7 +42,7 @@ type RequestBody struct {
|
|||
// EXPERIMENTAL. Subject to change/removal.
|
||||
WriteTimeout time.Duration `json:"write_timeout,omitempty"`
|
||||
|
||||
Replace string `json:"replace,omitempty"`
|
||||
Set string `json:"set,omitempty"`
|
||||
|
||||
logger *zap.Logger
|
||||
}
|
||||
|
@ -61,15 +61,15 @@ 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 != "" {
|
||||
if rb.Set != "" {
|
||||
err := r.Body.Close()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
repl := r.Context().Value(caddy.ReplacerCtxKey).(*caddy.Replacer)
|
||||
replacedBody := repl.ReplaceAll(rb.Replace, "")
|
||||
replacedBody := repl.ReplaceAll(rb.Set, "")
|
||||
r.Body = io.NopCloser(strings.NewReader(replacedBody))
|
||||
r.ContentLength = int64(len(rb.Replace))
|
||||
r.ContentLength = int64(len(rb.Set))
|
||||
}
|
||||
if r.Body == nil {
|
||||
return next.ServeHTTP(w, r)
|
||||
|
|
Loading…
Reference in New Issue
Block a user