mirror of
https://github.com/yusing/godoxy.git
synced 2025-05-20 12:42:34 +02:00
add rule.on directives "cookie", "form", "postform"
This commit is contained in:
parent
5769abb626
commit
f5708fd539
3 changed files with 64 additions and 11 deletions
|
@ -7,7 +7,6 @@ import (
|
|||
"strings"
|
||||
|
||||
E "github.com/yusing/go-proxy/internal/error"
|
||||
"github.com/yusing/go-proxy/internal/logging"
|
||||
gphttp "github.com/yusing/go-proxy/internal/net/http"
|
||||
"github.com/yusing/go-proxy/internal/net/http/reverseproxy"
|
||||
"github.com/yusing/go-proxy/internal/net/types"
|
||||
|
@ -225,9 +224,6 @@ func buildCmd(executors []*CommandExecutor) (*CommandExecutor, error) {
|
|||
return &CommandExecutor{
|
||||
HandlerFunc: func(w http.ResponseWriter, r *http.Request) {
|
||||
for _, exec := range executors {
|
||||
logging.Debug().
|
||||
Str("directive", exec.directive).
|
||||
Msg("executing command")
|
||||
exec.HandlerFunc(w, r)
|
||||
}
|
||||
},
|
||||
|
|
|
@ -4,6 +4,7 @@ import "strings"
|
|||
|
||||
type Help struct {
|
||||
command string
|
||||
description string
|
||||
args map[string]string // args[arg] -> description
|
||||
}
|
||||
|
||||
|
@ -23,6 +24,11 @@ func (h *Help) String() string {
|
|||
sb.WriteString(arg)
|
||||
sb.WriteString("> ")
|
||||
}
|
||||
if h.description != "" {
|
||||
sb.WriteString("\n\t")
|
||||
sb.WriteString(h.description)
|
||||
sb.WriteRune('\n')
|
||||
}
|
||||
sb.WriteRune('\n')
|
||||
for arg, desc := range h.args {
|
||||
sb.WriteRune('\t')
|
||||
|
|
|
@ -20,6 +20,9 @@ type (
|
|||
const (
|
||||
OnHeader = "header"
|
||||
OnQuery = "query"
|
||||
OnCookie = "cookie"
|
||||
OnForm = "form"
|
||||
OnPostForm = "postform"
|
||||
OnMethod = "method"
|
||||
OnPath = "path"
|
||||
OnRemote = "remote"
|
||||
|
@ -56,6 +59,51 @@ var checkers = map[string]struct {
|
|||
return r.URL.Query().Get(args.(StrTuple).First) == args.(StrTuple).Second
|
||||
},
|
||||
},
|
||||
OnCookie: {
|
||||
help: Help{
|
||||
command: OnCookie,
|
||||
args: map[string]string{
|
||||
"key": "the cookie key",
|
||||
"value": "the cookie value",
|
||||
},
|
||||
},
|
||||
validate: toStrTuple,
|
||||
check: func(r *http.Request, args any) bool {
|
||||
cookies := r.CookiesNamed(args.(StrTuple).First)
|
||||
for _, cookie := range cookies {
|
||||
if cookie.Value == args.(StrTuple).Second {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
},
|
||||
},
|
||||
OnForm: {
|
||||
help: Help{
|
||||
command: OnForm,
|
||||
args: map[string]string{
|
||||
"key": "the form key",
|
||||
"value": "the form value",
|
||||
},
|
||||
},
|
||||
validate: toStrTuple,
|
||||
check: func(r *http.Request, args any) bool {
|
||||
return r.FormValue(args.(StrTuple).First) == args.(StrTuple).Second
|
||||
},
|
||||
},
|
||||
OnPostForm: {
|
||||
help: Help{
|
||||
command: OnPostForm,
|
||||
args: map[string]string{
|
||||
"key": "the form key",
|
||||
"value": "the form value",
|
||||
},
|
||||
},
|
||||
validate: toStrTuple,
|
||||
check: func(r *http.Request, args any) bool {
|
||||
return r.PostFormValue(args.(StrTuple).First) == args.(StrTuple).Second
|
||||
},
|
||||
},
|
||||
OnMethod: {
|
||||
help: Help{
|
||||
command: OnMethod,
|
||||
|
@ -71,6 +119,9 @@ var checkers = map[string]struct {
|
|||
OnPath: {
|
||||
help: Help{
|
||||
command: OnPath,
|
||||
description: `The path can be a glob pattern, e.g.:
|
||||
/path/to
|
||||
/path/to/*`,
|
||||
args: map[string]string{
|
||||
"path": "the request path, must start with /",
|
||||
},
|
||||
|
|
Loading…
Add table
Reference in a new issue