mirror of
https://github.com/yusing/godoxy.git
synced 2026-04-10 18:56:55 +02:00
feat(rules): replace go templates with custom variable expansion
- Replace template syntax ({{ .Request.Method }}) with $-prefixed variables ($req_method)
- Implement custom variable parser with static ($req_method, $status_code) and dynamic ($header(), $arg(), $form()) variables
- Replace templateOrStr interface with templateString struct and ExpandVars methods
- Add parser improvements for reliable quote handling
- Add new error types: ErrUnterminatedParenthesis, ErrUnexpectedVar, ErrExpectOneOrTwoArgs
- Update all tests and help text to use new variable syntax
- Add comprehensive unit and benchmark tests for variable expansion
This commit is contained in:
81
internal/route/rules/vars_dynamic.go
Normal file
81
internal/route/rules/vars_dynamic.go
Normal file
@@ -0,0 +1,81 @@
|
||||
package rules
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
var (
|
||||
VarHeader = "header"
|
||||
VarResponseHeader = "resp_header"
|
||||
VarQuery = "arg"
|
||||
VarForm = "form"
|
||||
VarPostForm = "postform"
|
||||
)
|
||||
|
||||
type dynamicVarGetter func(args []string, w *ResponseModifier, req *http.Request) (string, error)
|
||||
|
||||
var dynamicVarSubsMap = map[string]dynamicVarGetter{
|
||||
VarHeader: func(args []string, w *ResponseModifier, req *http.Request) (string, error) {
|
||||
key, index, err := getKeyAndIndex(args)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return getValueByKeyAtIndex(req.Header, key, index)
|
||||
},
|
||||
VarResponseHeader: func(args []string, w *ResponseModifier, req *http.Request) (string, error) {
|
||||
key, index, err := getKeyAndIndex(args)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return getValueByKeyAtIndex(w.Header(), key, index)
|
||||
},
|
||||
VarQuery: func(args []string, w *ResponseModifier, req *http.Request) (string, error) {
|
||||
key, index, err := getKeyAndIndex(args)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return getValueByKeyAtIndex(GetSharedData(w).GetQueries(req), key, index)
|
||||
},
|
||||
VarForm: func(args []string, w *ResponseModifier, req *http.Request) (string, error) {
|
||||
key, index, err := getKeyAndIndex(args)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return getValueByKeyAtIndex(req.Form, key, index)
|
||||
},
|
||||
VarPostForm: func(args []string, w *ResponseModifier, req *http.Request) (string, error) {
|
||||
key, index, err := getKeyAndIndex(args)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return getValueByKeyAtIndex(req.PostForm, key, index)
|
||||
},
|
||||
}
|
||||
|
||||
func getValueByKeyAtIndex[Values http.Header | url.Values](values Values, key string, index int) (string, error) {
|
||||
// NOTE: do not use Header.Get or http.CanonicalHeaderKey here, respect to user input
|
||||
if values, ok := values[key]; ok && index < len(values) {
|
||||
return values[index], nil
|
||||
}
|
||||
// ignore unknown header or index out of range
|
||||
return "", nil
|
||||
}
|
||||
|
||||
func getKeyAndIndex(args []string) (key string, index int, err error) {
|
||||
switch len(args) {
|
||||
case 0:
|
||||
return "", 0, ErrExpectNoArg
|
||||
case 1:
|
||||
return args[0], 0, nil
|
||||
case 2:
|
||||
index, err = strconv.Atoi(args[1])
|
||||
if err != nil {
|
||||
return "", 0, ErrInvalidArguments.Withf("invalid index %q", args[1])
|
||||
}
|
||||
return args[0], index, nil
|
||||
default:
|
||||
return "", 0, ErrExpectOneOrTwoArgs
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user