mirror of
https://github.com/yusing/godoxy.git
synced 2026-03-18 15:23:51 +01:00
- 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
53 lines
1.0 KiB
Go
53 lines
1.0 KiB
Go
package rules
|
|
|
|
import (
|
|
"io"
|
|
"net/http"
|
|
"strings"
|
|
"unsafe"
|
|
)
|
|
|
|
type templateString struct {
|
|
string
|
|
isTemplate bool
|
|
}
|
|
|
|
type keyValueTemplate struct {
|
|
key string
|
|
tmpl templateString
|
|
}
|
|
|
|
func (tmpl *keyValueTemplate) Unpack() (string, templateString) {
|
|
return tmpl.key, tmpl.tmpl
|
|
}
|
|
|
|
func (tmpl *templateString) ExpandVars(w http.ResponseWriter, req *http.Request, dstW io.Writer) error {
|
|
if !tmpl.isTemplate {
|
|
_, err := dstW.Write(strtobNoCopy(tmpl.string))
|
|
return err
|
|
}
|
|
|
|
return ExpandVars(GetInitResponseModifier(w), req, tmpl.string, dstW)
|
|
}
|
|
|
|
func (tmpl *templateString) ExpandVarsToString(w http.ResponseWriter, req *http.Request) (string, error) {
|
|
if !tmpl.isTemplate {
|
|
return tmpl.string, nil
|
|
}
|
|
|
|
var buf strings.Builder
|
|
err := ExpandVars(GetInitResponseModifier(w), req, tmpl.string, &buf)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return buf.String(), nil
|
|
}
|
|
|
|
func (tmpl *templateString) Len() int {
|
|
return len(tmpl.string)
|
|
}
|
|
|
|
func strtobNoCopy(s string) []byte {
|
|
return unsafe.Slice(unsafe.StringData(s), len(s))
|
|
}
|