feat: implement dynamic request variable replacement in replacer

Replace the no-op reverseProxyReplacer.Replace with strings.NewReplacer
supporting {method}, {host}, {path}, {query}, {scheme}, {uri}, {proto}
This commit is contained in:
wjqserver 2026-04-21 17:36:38 +08:00
parent 5d9bb3187d
commit fa925582d7
2 changed files with 129 additions and 3 deletions

View file

@ -264,11 +264,32 @@ func (ops *HeaderOps) Provision() error {
}
type reverseProxyReplacer struct {
req *http.Request
req *http.Request
repl *strings.Replacer
}
func newReverseProxyReplacer(req *http.Request) *reverseProxyReplacer {
return &reverseProxyReplacer{req: req}
r := &reverseProxyReplacer{req: req}
if req != nil {
uri := req.RequestURI
if uri == "" {
uri = req.URL.RequestURI()
}
scheme := "http"
if req.TLS != nil {
scheme = "https"
}
r.repl = strings.NewReplacer(
"{method}", req.Method,
"{host}", req.Host,
"{path}", req.URL.Path,
"{query}", req.URL.RawQuery,
"{scheme}", scheme,
"{uri}", uri,
"{proto}", req.Proto,
)
}
return r
}
func newReverseProxyReplacerFromHeader(hdr http.Header) *reverseProxyReplacer {
@ -279,7 +300,10 @@ func (r *reverseProxyReplacer) Replace(s string) string {
if r == nil || s == "" {
return s
}
return s
if r.repl == nil {
return s
}
return r.repl.Replace(s)
}
type reverseProxyHandler struct {