mirror of
https://github.com/caddyserver/caddy.git
synced 2024-12-26 21:53:48 +03:00
Add: request_body replace directive
This commit is contained in:
parent
57ae9c3107
commit
03175f0c5c
2 changed files with 19 additions and 0 deletions
|
@ -68,6 +68,12 @@ func parseCaddyfile(h httpcaddyfile.Helper) (caddyhttp.MiddlewareHandler, error)
|
||||||
}
|
}
|
||||||
rb.WriteTimeout = timeout
|
rb.WriteTimeout = timeout
|
||||||
|
|
||||||
|
case "set":
|
||||||
|
var setStr string
|
||||||
|
if !h.AllArgs(&setStr) {
|
||||||
|
return nil, h.ArgErr()
|
||||||
|
}
|
||||||
|
rb.Set = setStr
|
||||||
default:
|
default:
|
||||||
return nil, h.Errf("unrecognized request_body subdirective '%s'", h.Val())
|
return nil, h.Errf("unrecognized request_body subdirective '%s'", h.Val())
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,6 +18,7 @@ import (
|
||||||
"errors"
|
"errors"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"go.uber.org/zap"
|
"go.uber.org/zap"
|
||||||
|
@ -43,6 +44,8 @@ type RequestBody struct {
|
||||||
// EXPERIMENTAL. Subject to change/removal.
|
// EXPERIMENTAL. Subject to change/removal.
|
||||||
WriteTimeout time.Duration `json:"write_timeout,omitempty"`
|
WriteTimeout time.Duration `json:"write_timeout,omitempty"`
|
||||||
|
|
||||||
|
Set string `json:"set,omitempty"`
|
||||||
|
|
||||||
logger *zap.Logger
|
logger *zap.Logger
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -60,6 +63,16 @@ func (rb *RequestBody) Provision(ctx caddy.Context) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (rb RequestBody) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyhttp.Handler) error {
|
func (rb RequestBody) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyhttp.Handler) error {
|
||||||
|
if rb.Set != "" {
|
||||||
|
err := r.Body.Close()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
repl := r.Context().Value(caddy.ReplacerCtxKey).(*caddy.Replacer)
|
||||||
|
replacedBody := repl.ReplaceAll(rb.Set, "")
|
||||||
|
r.Body = io.NopCloser(strings.NewReader(replacedBody))
|
||||||
|
r.ContentLength = int64(len(rb.Set))
|
||||||
|
}
|
||||||
if r.Body == nil {
|
if r.Body == nil {
|
||||||
return next.ServeHTTP(w, r)
|
return next.ServeHTTP(w, r)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue