--- /dev/null
+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)
+}
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)
}
}
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)
)