From 1365f6519bad28177d39052f5eb1a0644ffcd7b1 Mon Sep 17 00:00:00 2001 From: Dan Fuhry Date: Tue, 27 Oct 2026 23:48:41 -0400 Subject: [PATCH] [http] add "mutate" action --- http/BUILD.bazel | 1 + http/route_action_mutate.go | 111 ++++++++++++++++++++++++++++++++++++ http/server.go | 21 ++++++- 3 files changed, 132 insertions(+), 1 deletion(-) create mode 100644 http/route_action_mutate.go diff --git a/http/BUILD.bazel b/http/BUILD.bazel index 31a3619..1606334 100644 --- a/http/BUILD.bazel +++ b/http/BUILD.bazel @@ -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 index 0000000..c844025 --- /dev/null +++ b/http/route_action_mutate.go @@ -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) +} diff --git a/http/server.go b/http/server.go index 2c4d081..55c9a25 100644 --- a/http/server.go +++ b/http/server.go @@ -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) ) -- 2.52.0