]> go.fuhry.dev Git - runtime.git/commitdiff
[http] add "mutate" action
authorDan Fuhry <dan@fuhry.com>
Wed, 28 Oct 2026 03:48:41 +0000 (23:48 -0400)
committerDan Fuhry <dan@fuhry.com>
Wed, 28 Oct 2026 03:48:41 +0000 (23:48 -0400)
http/BUILD.bazel
http/route_action_mutate.go [new file with mode: 0644]
http/server.go

index 31a36196d845e8df65cdfacb8bb22e4f0de7a72e..16063342b14eab5cc1bd6b5ad91cdbc35564e427 100644 (file)
@@ -10,6 +10,7 @@ go_multi_library(
         "proxy_protocol.go",
         "range.go",
         "route_action_healthcheck.go",
+        "route_action_mutate.go",
         "route_action_proxy.go",
         "route_action_redirect.go",
         "route_action_s3.go",
diff --git a/http/route_action_mutate.go b/http/route_action_mutate.go
new file mode 100644 (file)
index 0000000..c844025
--- /dev/null
@@ -0,0 +1,111 @@
+package http
+
+import (
+       "net/http"
+       "strings"
+
+       "gopkg.in/yaml.v3"
+)
+
+type HeaderMutation struct {
+       Direction string `yaml:"direction"`
+       Header    string `yaml:"header,omitempty"`
+       Action    string `yaml:"action"`
+       Value     string `yaml:"value,omitempty"`
+       From      string `yaml:"from,omitempty"`
+       To        string `yaml:"to,omitempty"`
+}
+
+type mutateAction struct {
+       Mutations []HeaderMutation `yaml:"header_mutations"`
+}
+
+func (a *mutateAction) Handle(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
+       // Apply request mutations
+       for _, m := range a.Mutations {
+               if m.Direction == "request" || m.Direction == "both" {
+                       a.applyMutation(r.Header, m)
+               }
+       }
+
+       // Wrap response writer to capture changes from next handler
+       wrapper := newResponseWriterWrapper(w)
+
+       // Copy existing headers so we don't lose them if next handler doesn't overwrite
+       for k, v := range w.Header() {
+               wrapper.headers[k] = v
+       }
+
+       next(wrapper, r)
+
+       // Apply response mutations after next handler has run
+       for _, m := range a.Mutations {
+               if m.Direction == "response" || m.Direction == "both" {
+                       a.applyMutation(wrapper.headers, m)
+               }
+       }
+}
+
+func (a *mutateAction) applyMutation(headers http.Header, m HeaderMutation) {
+       switch strings.ToLower(m.Action) {
+       case "append":
+               headers.Add(m.Header, m.Value)
+       case "set":
+               headers.Set(m.Header, m.Value)
+       case "remove":
+               headers.Del(m.Header)
+       case "default":
+               if headers.Get(m.Header) == "" {
+                       headers.Set(m.Header, m.Value)
+               }
+       case "rename":
+               if m.From != "" && headers.Get(m.From) != "" {
+                       headers.Set(m.To, headers.Get(m.From))
+                       headers.Del(m.From)
+               }
+       }
+}
+
+type responseWriterWrapper struct {
+       http.ResponseWriter
+       headers http.Header
+}
+
+func newResponseWriterWrapper(w http.ResponseWriter) *responseWriterWrapper {
+       return &responseWriterWrapper{
+               ResponseWriter: w,
+               headers:        make(http.Header),
+       }
+}
+
+func (w *responseWriterWrapper) Header() http.Header {
+       return w.headers
+}
+
+func (w *responseWriterWrapper) WriteHeader(code int) {
+       w.ResponseWriter.WriteHeader(code)
+}
+
+func (w *responseWriterWrapper) Write(b []byte) (int, error) {
+       return w.ResponseWriter.Write(b)
+}
+
+func mutateActionFromRouteYaml(node *yaml.Node) (RouteAction, error) {
+       var rawNode struct {
+               HeaderMutations []HeaderMutation `yaml:"header_mutations,omitempty"`
+       }
+
+       if err := node.Decode(&rawNode); err != nil {
+               return nil, err
+       }
+
+       if len(rawNode.HeaderMutations) > 0 {
+               return &mutateAction{Mutations: rawNode.HeaderMutations}, nil
+       }
+
+       return nil, nil
+}
+
+func init() {
+       AddRouteParseFunc(mutateActionFromRouteYaml)
+}
index 2c4d08161e5ae2736e7208de6ea59dc7ae9dc465..55c9a253ab84bb4d028539024f9f8e5bb7e09ade 100644 (file)
@@ -25,7 +25,17 @@ import (
 
 type serverCtxVar int
 
+// Interface RouteAction describes a handler instance for a specific HTTP route.
+//
+// Route actions may function as either middleware (mutating or processing the request in some way
+// before passing it on) or endpoints (serving a direct response).
+//
+// Each route has exactly one action. Each RouteAction may have its own configuration and state.
 type RouteAction interface {
+       // Handle handles a single request.
+       //
+       // Handle must either send a direct HTTP response, or call `next(w, r)` to chain to the next
+       // routing rule.
        Handle(w http.ResponseWriter, r *http.Request, next http.HandlerFunc)
 }
 
@@ -103,7 +113,16 @@ type authznEntry struct {
 }
 
 type (
-       initHook       func(context.Context, *yaml.Node) (context.Context, error)
+       initHook func(context.Context, *yaml.Node) (context.Context, error)
+
+       // routeParseFunc describes a function that decides whether a particular RouteAction type
+       // applies to a route.
+       //
+       // A routeParseFunc returns:
+       //   - `nil, nil` if the route doesn't match the RouteAction type.
+       //   - `RouteAction, nil` if the route contains valid configuration for the RouteAction type.
+       //   - `nil, error` if the route attempts to configure the corresponding RouteAction type, but
+       //     the supplied configuration is invalid.
        routeParseFunc func(*yaml.Node) (RouteAction, error)
 )