mirror of
https://github.com/yusing/godoxy.git
synced 2026-04-23 01:08:47 +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:
@@ -3,19 +3,21 @@ package rules
|
||||
import "net/http"
|
||||
|
||||
type (
|
||||
handlerFunc func(w http.ResponseWriter, r *http.Request) error
|
||||
|
||||
CommandHandler interface {
|
||||
// CommandHandler can read and modify the values
|
||||
// then handle the request
|
||||
// finally proceed to next command (or return) base on situation
|
||||
Handle(cached Cache, w http.ResponseWriter, r *http.Request) (proceed bool)
|
||||
Handle(w http.ResponseWriter, r *http.Request) error
|
||||
IsResponseHandler() bool
|
||||
}
|
||||
// NonTerminatingCommand will run then proceed to next command or reverse proxy.
|
||||
NonTerminatingCommand http.HandlerFunc
|
||||
NonTerminatingCommand handlerFunc
|
||||
// TerminatingCommand will run then return immediately.
|
||||
TerminatingCommand http.HandlerFunc
|
||||
// DynamicCommand will return base on the request
|
||||
// and can read or modify the values.
|
||||
DynamicCommand func(cached Cache, w http.ResponseWriter, r *http.Request) (proceed bool)
|
||||
TerminatingCommand handlerFunc
|
||||
// OnResponseCommand will run then return based on the response.
|
||||
OnResponseCommand handlerFunc
|
||||
// BypassCommand will skip all the following commands
|
||||
// and directly return to reverse proxy.
|
||||
BypassCommand struct{}
|
||||
@@ -23,29 +25,55 @@ type (
|
||||
Commands []CommandHandler
|
||||
)
|
||||
|
||||
func (c NonTerminatingCommand) Handle(cached Cache, w http.ResponseWriter, r *http.Request) (proceed bool) {
|
||||
c(w, r)
|
||||
return true
|
||||
func (c NonTerminatingCommand) Handle(w http.ResponseWriter, r *http.Request) error {
|
||||
return c(w, r)
|
||||
}
|
||||
|
||||
func (c TerminatingCommand) Handle(cached Cache, w http.ResponseWriter, r *http.Request) (proceed bool) {
|
||||
c(w, r)
|
||||
func (c NonTerminatingCommand) IsResponseHandler() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func (c DynamicCommand) Handle(cached Cache, w http.ResponseWriter, r *http.Request) (proceed bool) {
|
||||
return c(cached, w, r)
|
||||
func (c TerminatingCommand) Handle(w http.ResponseWriter, r *http.Request) error {
|
||||
if err := c(w, r); err != nil {
|
||||
return err
|
||||
}
|
||||
return errTerminated
|
||||
}
|
||||
|
||||
func (c BypassCommand) Handle(cached Cache, w http.ResponseWriter, r *http.Request) (proceed bool) {
|
||||
func (c TerminatingCommand) IsResponseHandler() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func (c OnResponseCommand) Handle(w http.ResponseWriter, r *http.Request) error {
|
||||
return c(w, r)
|
||||
}
|
||||
|
||||
func (c OnResponseCommand) IsResponseHandler() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
func (c Commands) Handle(cached Cache, w http.ResponseWriter, r *http.Request) (proceed bool) {
|
||||
func (c BypassCommand) Handle(w http.ResponseWriter, r *http.Request) error {
|
||||
return errTerminated
|
||||
}
|
||||
|
||||
func (c BypassCommand) IsResponseHandler() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func (c Commands) Handle(w http.ResponseWriter, r *http.Request) error {
|
||||
for _, cmd := range c {
|
||||
if !cmd.Handle(cached, w, r) {
|
||||
return false
|
||||
if err := cmd.Handle(w, r); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return true
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c Commands) IsResponseHandler() bool {
|
||||
for _, cmd := range c {
|
||||
if cmd.IsResponseHandler() {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user