From 80dd142861070413cdf15eb5064257bec5133dd8 Mon Sep 17 00:00:00 2001 From: yusing Date: Sun, 12 Oct 2025 09:38:06 +0800 Subject: [PATCH] refactor(rules): rename Static and Returning commands into Terminating and NonTerminating commands --- internal/route/rules/command.go | 14 +++++++------- internal/route/rules/do.go | 14 +++++++------- internal/route/rules/fields.go | 6 +++--- 3 files changed, 17 insertions(+), 17 deletions(-) diff --git a/internal/route/rules/command.go b/internal/route/rules/command.go index 3136066f..2b58fdbc 100644 --- a/internal/route/rules/command.go +++ b/internal/route/rules/command.go @@ -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 } diff --git a/internal/route/rules/do.go b/internal/route/rules/do.go index 381997ad..034460b8 100644 --- a/internal/route/rules/do.go +++ b/internal/route/rules/do.go @@ -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") diff --git a/internal/route/rules/fields.go b/internal/route/rules/fields.go index 91fdf72b..3abcff6f 100644 --- a/internal/route/rules/fields.go +++ b/internal/route/rules/fields.go @@ -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) }), }