Go API Documentation

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

No package summary is available.

Package

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

Vars

Types

Handler

Handler implements a middleware that maps inputs to outputs. Specifically, it compares a source value against the map inputs, and for one that matches, it applies the output values to each destination. Destinations become placeholder names.

Mapped placeholders are not evaluated until they are used, so even for very large mappings, this handler is quite efficient.

Field name Field type Comment
Source

string

Source is the placeholder from which to get the input value.

Destinations

[]string

Destinations are the names of placeholders in which to store the outputs. Destination values should be wrapped in braces, for example, {my_placeholder}.

Mappings

[]Mapping

Mappings from source values (inputs) to destination values (outputs). The first matching, non-nil mapping will be applied.

Defaults

[]string

If no mappings match or if the mapped output is null/nil, the associated default output will be applied (optional).

Mapping

Mapping describes a mapping from input to outputs.

Field name Field type Comment
Input

string

The input value to match. Must be distinct from other mappings. Mutually exclusive to input_regexp.

InputRegexp

string

The input regular expression to match. Mutually exclusive to input.

Outputs

[]any

Upon a match with the input, each output is positionally correlated with each destination of the parent handler. An output that is null (nil) will be treated as if it was not mapped at all.

re

*regexp.Regexp

No comment on field.

Functions

func (*Handler) Provision

Provision sets up h.

Uses: fmt.Errorf, regexp.Compile, strings.Count, strings.HasPrefix, strings.Trim.

func (*Handler) Validate

Validate ensures that h is configured properly.

Uses: fmt.Errorf.

func (Handler) CaddyModule

CaddyModule returns the Caddy module information.

func (Handler) ServeHTTP

Uses: slices.Index.

Private functions

func init

References: httpcaddyfile.RegisterHandlerDirective.

func parseCaddyfile

parseCaddyfile sets up the map handler from Caddyfile tokens. Syntax:

map [<matcher>] <source> <destinations...> {
    [~]<input> <outputs...>
    default    <defaults...>
}

If the input value is prefixed with a tilde (~), then the input will be parsed as a regular expression.

The Caddyfile adapter treats outputs that are a literal hyphen (-) as a null/nil value. This is useful if you want to fall back to default for that particular output.

The number of outputs for each mapping must not be more than the number of destinations. However, for convenience, there may be fewer outputs than destinations and any missing outputs will be filled in implicitly.

References: httpcaddyfile.WasReplacedPlaceholderShorthand, strings.HasPrefix.


Tests

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

Test functions

TestHandler

References: caddyhttp.HandlerFunc, caddyhttp.NewTestReplacer, context.WithValue, http.MethodGet, http.NewRequest, http.Request, http.ResponseWriter, httptest.NewRecorder, reflect.DeepEqual.