rewrite

package
v1.0.126 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 18, 2026 License: MIT Imports: 3 Imported by: 0

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

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) Add

func (rw *Rewriter) Add(rule Rule) Rule

Add appends a rule and returns it with the assigned ID.

func (*Rewriter) ApplyRequest

func (rw *Rewriter) ApplyRequest(host string, h http.Header)

ApplyRequest mutates h in-place for every matching rule.

func (*Rewriter) ApplyResponse

func (rw *Rewriter) ApplyResponse(host string, resp *http.Response)

ApplyResponse mutates resp.Header in-place for every matching rule.

func (*Rewriter) List

func (rw *Rewriter) List() []Rule

List returns a snapshot of the current rules.

func (*Rewriter) RemoveByID

func (rw *Rewriter) RemoveByID(id int) bool

RemoveByID deletes the rule with the given ID. Returns false if not found.

func (*Rewriter) SetRules

func (rw *Rewriter) SetRules(rules []Rule)

SetRules replaces the full rule set (used during startup from config).

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"

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL