support $resp_header(name) substitution

This commit is contained in:
yusing 2024-12-03 11:09:30 +08:00
parent 52d5e2f36d
commit cebc0c5405

View file

@ -15,6 +15,7 @@ type varReplaceFunc func(string) string
var (
reArg = regexp.MustCompile(`\$arg\([\w-_]+\)`)
reHeader = regexp.MustCompile(`\$header\([\w-]+\)`)
reRespHeader = regexp.MustCompile(`\$resp_header\([\w-]+\)`)
reStatic = regexp.MustCompile(`\$[\w_]+`)
)
@ -91,6 +92,13 @@ func varReplacer(req *Request, resp *Response) varReplaceFunc {
return req.Header.Get(header)
})
if resp != nil {
s = reRespHeader.ReplaceAllStringFunc(s, func(match string) string {
header := http.CanonicalHeaderKey(match[14 : len(match)-1])
return resp.Header.Get(header)
})
}
// Replace static variables
return reStatic.ReplaceAllStringFunc(s, func(match string) string {
if fn, ok := pairs[match]; ok {