mirror of
https://github.com/yusing/godoxy.git
synced 2026-03-20 08:35:11 +01:00
* 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>`
44 lines
1003 B
Go
44 lines
1003 B
Go
package rules
|
|
|
|
import (
|
|
"bytes"
|
|
"io"
|
|
"net/http"
|
|
)
|
|
|
|
type templateOrStr interface {
|
|
Execute(w io.Writer, data any) error
|
|
}
|
|
|
|
type strTemplate string
|
|
|
|
func (t strTemplate) Execute(w io.Writer, _ any) error {
|
|
n, err := w.Write([]byte(t))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if n != len(t) {
|
|
return io.ErrShortWrite
|
|
}
|
|
return nil
|
|
}
|
|
|
|
type keyValueTemplate = Tuple[string, templateOrStr]
|
|
|
|
func executeRequestTemplateString(tmpl templateOrStr, r *http.Request) (string, error) {
|
|
var buf bytes.Buffer
|
|
err := tmpl.Execute(&buf, reqResponseTemplateData{Request: r})
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return buf.String(), nil
|
|
}
|
|
|
|
func executeRequestTemplateTo(tmpl templateOrStr, o io.Writer, r *http.Request) error {
|
|
return tmpl.Execute(o, reqResponseTemplateData{Request: r})
|
|
}
|
|
|
|
func executeReqRespTemplateTo(tmpl templateOrStr, o io.Writer, w http.ResponseWriter, r *http.Request) error {
|
|
return tmpl.Execute(o, reqResponseTemplateData{Request: r, Response: GetInitResponseModifier(w).Response()})
|
|
}
|