Skip to content

Commit

Permalink
Add: rename directive to set_body
Browse files Browse the repository at this point in the history
  • Loading branch information
AdrienPensart committed Oct 5, 2024
1 parent 49f89a2 commit 9aeba18
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
10 changes: 5 additions & 5 deletions modules/caddyhttp/requestbody/caddyfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
}

Expand Down
8 changes: 4 additions & 4 deletions modules/caddyhttp/requestbody/requestbody.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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)
Expand Down

0 comments on commit 9aeba18

Please sign in to comment.