refactor(rules): rename Static and Returning commands into Terminating and NonTerminating commands

This commit is contained in:
yusing
2025-10-12 09:38:06 +08:00
parent 92aa61e732
commit 80dd142861
3 changed files with 17 additions and 17 deletions

View File

@@ -9,12 +9,12 @@ type (
// finally proceed to next command (or return) base on situation
Handle(cached Cache, w http.ResponseWriter, r *http.Request) (proceed bool)
}
// StaticCommand will run then proceed to next command or reverse proxy.
StaticCommand http.HandlerFunc
// ReturningCommand will run then return immediately.
ReturningCommand http.HandlerFunc
// NonTerminatingCommand will run then proceed to next command or reverse proxy.
NonTerminatingCommand http.HandlerFunc
// TerminatingCommand will run then return immediately.
TerminatingCommand http.HandlerFunc
// DynamicCommand will return base on the request
// and can raed or modify the values.
// and can read or modify the values.
DynamicCommand func(cached Cache, w http.ResponseWriter, r *http.Request) (proceed bool)
// BypassCommand will skip all the following commands
// and directly return to reverse proxy.
@@ -23,12 +23,12 @@ type (
Commands []CommandHandler
)
func (c StaticCommand) Handle(cached Cache, w http.ResponseWriter, r *http.Request) (proceed bool) {
func (c NonTerminatingCommand) Handle(cached Cache, w http.ResponseWriter, r *http.Request) (proceed bool) {
c(w, r)
return true
}
func (c ReturningCommand) Handle(cached Cache, w http.ResponseWriter, r *http.Request) (proceed bool) {
func (c TerminatingCommand) Handle(cached Cache, w http.ResponseWriter, r *http.Request) (proceed bool) {
c(w, r)
return false
}

View File

@@ -67,7 +67,7 @@ var commands = map[string]struct {
},
build: func(args any) CommandHandler {
orig, repl := args.(*StrTuple).Unpack()
return StaticCommand(func(w http.ResponseWriter, r *http.Request) {
return NonTerminatingCommand(func(w http.ResponseWriter, r *http.Request) {
path := r.URL.Path
if len(path) > 0 && path[0] != '/' {
path = "/" + path
@@ -92,7 +92,7 @@ var commands = map[string]struct {
validate: validateFSPath,
build: func(args any) CommandHandler {
root := args.(string)
return ReturningCommand(func(w http.ResponseWriter, r *http.Request) {
return TerminatingCommand(func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, path.Join(root, path.Clean(r.URL.Path)))
})
},
@@ -107,7 +107,7 @@ var commands = map[string]struct {
validate: validateURL,
build: func(args any) CommandHandler {
target := args.(*nettypes.URL).String()
return ReturningCommand(func(w http.ResponseWriter, r *http.Request) {
return TerminatingCommand(func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, target, http.StatusTemporaryRedirect)
})
},
@@ -136,7 +136,7 @@ var commands = map[string]struct {
},
build: func(args any) CommandHandler {
code, text := args.(*Tuple[int, string]).Unpack()
return ReturningCommand(func(w http.ResponseWriter, r *http.Request) {
return TerminatingCommand(func(w http.ResponseWriter, r *http.Request) {
http.Error(w, text, code)
})
},
@@ -156,7 +156,7 @@ var commands = map[string]struct {
},
build: func(args any) CommandHandler {
realm := args.(string)
return ReturningCommand(func(w http.ResponseWriter, r *http.Request) {
return TerminatingCommand(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("WWW-Authenticate", `Basic realm="`+realm+`"`)
http.Error(w, "Unauthorized", http.StatusUnauthorized)
})
@@ -176,7 +176,7 @@ var commands = map[string]struct {
target.Scheme = "http"
}
rp := reverseproxy.NewReverseProxy("", &target.URL, gphttp.NewTransport())
return ReturningCommand(rp.ServeHTTP)
return TerminatingCommand(rp.ServeHTTP)
},
},
CommandSet: {
@@ -280,7 +280,7 @@ func (cmd *Command) Parse(v string) error {
func buildCmd(executors []CommandHandler) (CommandHandler, error) {
for i, exec := range executors {
switch exec.(type) {
case ReturningCommand, BypassCommand:
case TerminatingCommand, BypassCommand:
if i != len(executors)-1 {
return nil, ErrInvalidCommandSequence.
Withf("a returning / bypass command must be the last command")

View File

@@ -41,14 +41,14 @@ var modFields = map[string]struct {
builder: func(args any) *FieldHandler {
k, v := args.(*StrTuple).Unpack()
return &FieldHandler{
set: StaticCommand(func(w http.ResponseWriter, r *http.Request) {
set: NonTerminatingCommand(func(w http.ResponseWriter, r *http.Request) {
w.Header()[k] = []string{v}
}),
add: StaticCommand(func(w http.ResponseWriter, r *http.Request) {
add: NonTerminatingCommand(func(w http.ResponseWriter, r *http.Request) {
h := w.Header()
h[k] = append(h[k], v)
}),
remove: StaticCommand(func(w http.ResponseWriter, r *http.Request) {
remove: NonTerminatingCommand(func(w http.ResponseWriter, r *http.Request) {
delete(w.Header(), k)
}),
}