Go API Documentation

github.com/caddyserver/caddy/v2/modules/caddyhttp/headers

No package summary is available.

Package

Files: 2. Third party imports: 0. Imports from organisation: 0. Tests: 0. Benchmarks: 0.

Vars

Types

Handler

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.

Field name Field type Comment
Request

*HeaderOps

No comment on field.
Response

*RespHeaderOps

No comment on field.

HeaderOps

HeaderOps defines manipulations for HTTP headers.

Field name Field type Comment
Add

http.Header

Adds HTTP headers; does not replace any existing header fields.

Set

http.Header

Sets HTTP headers; replaces existing header fields.

Delete

[]string

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.
Replace

map[string][]Replacement

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.

Replacement

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

Field name Field type Comment
Search

string

The substring to search for.

SearchRegexp

string

The regular expression to search with.

Replace

string

The string with which to replace matches.

re

*regexp.Regexp

No comment on field.

RespHeaderOps

RespHeaderOps defines manipulations for response headers.

Field name Field type Comment

*HeaderOps

No comment on field.
Require

*caddyhttp.ResponseMatcher

If set, header operations will be deferred until they are written out and only performed if the response matches these criteria.

Deferred

bool

If true, header operations will be deferred until they are written out. Superseded if Require is set. Usually you will need to set this to true if any fields are being deleted.

responseWriterWrapper

responseWriterWrapper defers response header operations until WriteHeader is called.

Field name Field type Comment

*caddyhttp.ResponseWriterWrapper

No comment on field.
replacer

*caddy.Replacer

No comment on field.
require

*caddyhttp.ResponseMatcher

No comment on field.
headerOps

*HeaderOps

No comment on field.
wroteHeader

bool

No comment on field.

Functions

func CaddyfileHeaderOp

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-nil, 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.

func (*Handler) Provision

Provision sets up h's configuration.

func (*HeaderOps) Provision

Provision sets up the header operations.

Uses: fmt.Errorf, regexp.Compile.

func (*responseWriterWrapper) Write

Uses: http.StatusOK.

func (*responseWriterWrapper) WriteHeader

func (Handler) CaddyModule

CaddyModule returns the Caddy module information.

func (Handler) ServeHTTP

Uses: caddyhttp.ResponseWriterWrapper.

func (Handler) Validate

Validate ensures h's configuration is valid.

func (HeaderOps) ApplyTo

ApplyTo applies ops to hdr using repl.

Uses: http.CanonicalHeaderKey, strings.Contains, strings.HasPrefix, strings.HasSuffix, strings.Join, strings.ReplaceAll, strings.ToLower.

func (HeaderOps) ApplyToRequest

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.

Private functions

func applyHeaderOp

References: caddyhttp.ResponseMatcher, fmt.Errorf, http.Header, strings.HasPrefix, strings.TrimLeft, strings.TrimPrefix.

func init

References: httpcaddyfile.RegisterDirective.

func parseCaddyfile

parseCaddyfile sets up the handler for response headers from Caddyfile tokens. Syntax:

header [<matcher>] [[+|-|?|>]<field> [<value|regexp>] [<replacement>]] {
	[+]<field> [<value|regexp> [<replacement>]]
	?<field> <default_value>
	-<field>
	><field>
	[defer]
}

Either a block can be opened or a single header field can be configured in the first line, but not both in the same directive. Header operations are deferred to write-time if any headers are being deleted or if the 'defer' subdirective is used. + appends a header value, - deletes a field, ? conditionally sets a value only if the header field is not already set, and > sets a field with defer enabled.

References: caddyhttp.ParseNamedResponseMatcher, caddyhttp.ResponseMatcher, httpcaddyfile.ConfigValue, reflect.DeepEqual, strings.HasPrefix, strings.TrimSuffix.

func parseReqHdrCaddyfile

parseReqHdrCaddyfile sets up the handler for request headers from Caddyfile tokens. Syntax:

request_header [<matcher>] [[+|-]<field> [<value|regexp>] [<replacement>]]

References: httpcaddyfile.ConfigValue, strings.TrimSuffix.

func validate

References: fmt.Errorf.


Tests

Files: 1. Third party imports: 0. Imports from organisation: 0. Tests: 1. Benchmarks: 0.

Types

nextHandler

This type doesn't have documentation.

Field name Field type Comment
type

func(http.ResponseWriter, *http.Request) error

No comment on field.

Test functions

TestHandler

References: caddyhttp.ResponseMatcher, context.WithValue, fmt.Errorf, http.Header, http.Request, http.ResponseWriter, httptest.NewRecorder, reflect.DeepEqual.