headers

package
v2.7.0-beta.1 Latest Latest
Warning

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

Go to latest
Published: May 16, 2023 License: Apache-2.0 Imports: 8 Imported by: 25

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CaddyfileHeaderOp

func CaddyfileHeaderOp(ops *HeaderOps, field, value, replacement string) error

CaddyfileHeaderOp applies a new header operation according to field, value, and replacement. The field can be prefixed with "+" or "-" to specify adding or removing; otherwise, the value will be set (overriding any previous value). If replacement is non-empty, value will be treated as a regular expression which will be used to search and then replacement will be used to complete the substring replacement; in that case, any + or - prefix to field will be ignored.

Types

type Handler

type Handler struct {
	Request  *HeaderOps     `json:"request,omitempty"`
	Response *RespHeaderOps `json:"response,omitempty"`
}

Handler is a middleware which modifies request and response headers.

Changes to headers are applied immediately, except for the response headers when Deferred is true or when Required is set. In those cases, the changes are applied when the headers are written to the response. Note that deferred changes do not take effect if an error occurs later in the middleware chain.

Properties in this module accept placeholders.

Response header operations can be conditioned upon response status code and/or other header values.

func (Handler) CaddyModule

func (Handler) CaddyModule() caddy.ModuleInfo

CaddyModule returns the Caddy module information.

func (*Handler) Provision

func (h *Handler) Provision(ctx caddy.Context) error

Provision sets up h's configuration.

func (Handler) ServeHTTP

func (h Handler) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyhttp.Handler) error

func (Handler) Validate

func (h Handler) Validate() error

Validate ensures h's configuration is valid.

type HeaderOps

type HeaderOps struct {
	// Adds HTTP headers; does not replace any existing header fields.
	Add http.Header `json:"add,omitempty"`

	// Sets HTTP headers; replaces existing header fields.
	Set http.Header `json:"set,omitempty"`

	// Names of HTTP header fields to delete. Basic wildcards are supported:
	//
	// - Start with `*` for all field names with the given suffix;
	// - End with `*` for all field names with the given prefix;
	// - Start and end with `*` for all field names containing a substring.
	Delete []string `json:"delete,omitempty"`

	// Performs in-situ substring replacements of HTTP headers.
	// Keys are the field names on which to perform the associated replacements.
	// If the field name is `*`, the replacements are performed on all header fields.
	Replace map[string][]Replacement `json:"replace,omitempty"`
}

HeaderOps defines manipulations for HTTP headers.

func (HeaderOps) ApplyTo

func (ops HeaderOps) ApplyTo(hdr http.Header, repl *caddy.Replacer)

ApplyTo applies ops to hdr using repl.

func (HeaderOps) ApplyToRequest

func (ops HeaderOps) ApplyToRequest(r *http.Request)

ApplyToRequest applies ops to r, specially handling the Host header which the standard library does not include with the header map with all the others. This method mutates r.Host.

func (*HeaderOps) Provision added in v2.2.0

func (ops *HeaderOps) Provision(_ caddy.Context) error

Provision sets up the header operations.

type Replacement

type Replacement struct {
	// The substring to search for.
	Search string `json:"search,omitempty"`

	// The regular expression to search with.
	SearchRegexp string `json:"search_regexp,omitempty"`

	// The string with which to replace matches.
	Replace string `json:"replace,omitempty"`
	// contains filtered or unexported fields
}

Replacement describes a string replacement, either a simple and fast substring search or a slower but more powerful regex search.

type RespHeaderOps

type RespHeaderOps struct {
	*HeaderOps

	// If set, header operations will be deferred until
	// they are written out and only performed if the
	// response matches these criteria.
	Require *caddyhttp.ResponseMatcher `json:"require,omitempty"`

	// If true, header operations will be deferred until
	// they are written out. Superceded if Require is set.
	// Usually you will need to set this to true if any
	// fields are being deleted.
	Deferred bool `json:"deferred,omitempty"`
}

RespHeaderOps defines manipulations for response headers.

Jump to

Keyboard shortcuts

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