mirror of
https://github.com/yusing/godoxy.git
synced 2026-04-25 01:38:30 +02:00
feat(rules): add post-request rules system with response manipulation (#160)
* Add comprehensive post-request rules support for response phase * Enable response body, status, and header manipulation via set commands * Refactor command handlers to support both request and response phases * Implement response modifier system for post-request template execution * Support response-based rule matching with status and header checks * Add comprehensive benchmarks for matcher performance * Refactor authentication and proxying commands for unified error handling * Support negated conditions with ! * Enhance error handling, error formatting and validation * Routes: add `rule_file` field with rule preset support * Environment variable substitution: now supports variables without `GODOXY_` prefix * new conditions: * `on resp_header <key> [<value>]` * `on status <status>` * new commands: * `require_auth` * `set resp_header <key> <template>` * `set resp_body <template>` * `set status <code>` * `log <level> <path> <template>` * `notify <level> <provider> <title_template> <body_template>`
This commit is contained in:
120
internal/route/rules/matcher.go
Normal file
120
internal/route/rules/matcher.go
Normal file
@@ -0,0 +1,120 @@
|
||||
package rules
|
||||
|
||||
import (
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"github.com/gobwas/glob"
|
||||
gperr "github.com/yusing/goutils/errs"
|
||||
)
|
||||
|
||||
type (
|
||||
Matcher func(string) bool
|
||||
MatcherType string
|
||||
)
|
||||
|
||||
const (
|
||||
MatcherTypeString MatcherType = "string"
|
||||
MatcherTypeGlob MatcherType = "glob"
|
||||
MatcherTypeRegex MatcherType = "regex"
|
||||
)
|
||||
|
||||
func unquoteExpr(s string) (string, gperr.Error) {
|
||||
if s == "" {
|
||||
return "", nil
|
||||
}
|
||||
switch s[0] {
|
||||
case '"', '\'', '`':
|
||||
if s[0] != s[len(s)-1] {
|
||||
return "", ErrUnterminatedQuotes
|
||||
}
|
||||
return s[1 : len(s)-1], nil
|
||||
default:
|
||||
return s, nil
|
||||
}
|
||||
}
|
||||
|
||||
func ExtractExpr(s string) (matcherType MatcherType, expr string, err gperr.Error) {
|
||||
idx := strings.IndexByte(s, '(')
|
||||
if idx == -1 {
|
||||
return MatcherTypeString, s, nil
|
||||
}
|
||||
idxEnd := strings.LastIndexByte(s, ')')
|
||||
if idxEnd == -1 {
|
||||
return "", "", ErrUnterminatedBrackets
|
||||
}
|
||||
|
||||
expr, err = unquoteExpr(s[idx+1 : idxEnd])
|
||||
if err != nil {
|
||||
return "", "", err
|
||||
}
|
||||
matcherType = MatcherType(strings.ToLower(s[:idx]))
|
||||
|
||||
switch matcherType {
|
||||
case MatcherTypeGlob, MatcherTypeRegex, MatcherTypeString:
|
||||
return
|
||||
default:
|
||||
return "", "", ErrInvalidArguments.Withf("invalid matcher type: %s", matcherType)
|
||||
}
|
||||
}
|
||||
|
||||
func ParseMatcher(expr string) (Matcher, gperr.Error) {
|
||||
negate := false
|
||||
if strings.HasPrefix(expr, "!") {
|
||||
negate = true
|
||||
expr = expr[1:]
|
||||
}
|
||||
|
||||
t, expr, err := ExtractExpr(expr)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
switch t {
|
||||
case MatcherTypeString:
|
||||
return StringMatcher(expr, negate)
|
||||
case MatcherTypeGlob:
|
||||
return GlobMatcher(expr, negate)
|
||||
case MatcherTypeRegex:
|
||||
return RegexMatcher(expr, negate)
|
||||
}
|
||||
// won't reach here
|
||||
return nil, ErrInvalidArguments.Withf("invalid matcher type: %s", t)
|
||||
}
|
||||
|
||||
func StringMatcher(s string, negate bool) (Matcher, gperr.Error) {
|
||||
if negate {
|
||||
return func(s2 string) bool {
|
||||
return s != s2
|
||||
}, nil
|
||||
}
|
||||
return func(s2 string) bool {
|
||||
return s == s2
|
||||
}, nil
|
||||
}
|
||||
|
||||
func GlobMatcher(expr string, negate bool) (Matcher, gperr.Error) {
|
||||
g, err := glob.Compile(expr)
|
||||
if err != nil {
|
||||
return nil, ErrInvalidArguments.With(err)
|
||||
}
|
||||
if negate {
|
||||
return func(s string) bool {
|
||||
return !g.Match(s)
|
||||
}, nil
|
||||
}
|
||||
return g.Match, nil
|
||||
}
|
||||
|
||||
func RegexMatcher(expr string, negate bool) (Matcher, gperr.Error) {
|
||||
re, err := regexp.Compile(expr)
|
||||
if err != nil {
|
||||
return nil, ErrInvalidArguments.With(err)
|
||||
}
|
||||
if negate {
|
||||
return func(s string) bool {
|
||||
return !re.MatchString(s)
|
||||
}, nil
|
||||
}
|
||||
return re.MatchString, nil
|
||||
}
|
||||
Reference in New Issue
Block a user