Documentation
¶
Overview ¶
Package rewrite owns per-host HTTP header rewrite rules: the rule DTO, the ordered active rule set, and the request/response header mutators. It is a self-contained leaf (stdlib only, no Culvert coupling) extracted from the flat package main per ADR-0002.
Index ¶
- type Rewriter
- func (rw *Rewriter) Add(rule Rule) Rule
- func (rw *Rewriter) ApplyRequest(host string, h http.Header)
- func (rw *Rewriter) ApplyResponse(host string, resp *http.Response)
- func (rw *Rewriter) List() []Rule
- func (rw *Rewriter) RemoveByID(id int) bool
- func (rw *Rewriter) SetRules(rules []Rule)
- func (rw *Rewriter) Snapshot() func()
- type Rule
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Rewriter ¶
type Rewriter struct {
// contains filtered or unexported fields
}
Rewriter holds the ordered list of active rewrite rules and applies them.
func NewRewriter ¶
func NewRewriter() *Rewriter
NewRewriter returns a Rewriter with ID assignment starting at 1.
func (*Rewriter) ApplyRequest ¶
ApplyRequest mutates h in-place for every matching rule.
func (*Rewriter) ApplyResponse ¶
ApplyResponse mutates resp.Header in-place for every matching rule.
func (*Rewriter) RemoveByID ¶
RemoveByID deletes the rule with the given ID. Returns false if not found.
func (*Rewriter) Snapshot ¶
func (rw *Rewriter) Snapshot() func()
Snapshot captures the current rules and ID counter and returns a closure that restores them, under the mutex on both ends. Production code never calls this; it exists so package main's startup-slice test isolation helper can save and restore the package-global rewriter without reaching across the package boundary into the unexported fields (ADR-0002 extraction).
type Rule ¶
type Rule struct {
// ID is assigned automatically when the rule is added at runtime.
ID int `json:"id"`
// Host is an exact hostname or wildcard pattern (*.example.com).
// Empty string matches every request.
Host string `yaml:"host" json:"host"`
// Request header operations — applied before forwarding to upstream.
ReqSet map[string]string `yaml:"req_set" json:"req_set,omitempty"` // set / overwrite
ReqAdd map[string]string `yaml:"req_add" json:"req_add,omitempty"` // append
ReqRemove []string `yaml:"req_remove" json:"req_remove,omitempty"` // delete
// Response header operations — applied before returning to client.
RespSet map[string]string `yaml:"resp_set" json:"resp_set,omitempty"`
RespAdd map[string]string `yaml:"resp_add" json:"resp_add,omitempty"`
RespRemove []string `yaml:"resp_remove" json:"resp_remove,omitempty"`
}
Rule defines header mutations applied to requests and/or responses whose destination host matches the given pattern.
Example (config.yaml):
rewrite:
- host: "*.internal.corp"
req_set:
X-Forwarded-By: "Culvert"
resp_remove:
- Server
- host: "" # empty = match all hosts
resp_set:
Strict-Transport-Security: "max-age=31536000"