| 1 | // Copyright 2015 Matthew Holt and The Caddy Authors |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package maphandlerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/map`/ |
| 16 | |
| 17 | import ( |
| 18 | "stringsscip-go gomod github.com/golang/go/src go1.25.1 strings/" |
| 19 | |
| 20 | "github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfilescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/" |
| 21 | "github.com/caddyserver/caddy/v2/modules/caddyhttpscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/" |
| 22 | ) |
| 23 | |
| 24 | func initscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/map`/init().() { |
| 25 | httpcaddyfilescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/.RegisterHandlerDirectivescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/RegisterHandlerDirective().("map", parseCaddyfilescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/map`/parseCaddyfile().) |
| 26 | } |
| 27 | |
| 28 | // parseCaddyfile sets up the map handler from Caddyfile tokens. Syntax: |
| 29 | // |
| 30 | // map [<matcher>] <source> <destinations...> { |
| 31 | // [~]<input> <outputs...> |
| 32 | // default <defaults...> |
| 33 | // } |
| 34 | // |
| 35 | // If the input value is prefixed with a tilde (~), then the input will be parsed as a |
| 36 | // regular expression. |
| 37 | // |
| 38 | // The Caddyfile adapter treats outputs that are a literal hyphen (-) as a null/nil |
| 39 | // value. This is useful if you want to fall back to default for that particular output. |
| 40 | // |
| 41 | // The number of outputs for each mapping must not be more than the number of destinations. |
| 42 | // However, for convenience, there may be fewer outputs than destinations and any missing |
| 43 | // outputs will be filled in implicitly. |
| 44 | func parseCaddyfilescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/map`/parseCaddyfile().(h httpcaddyfilescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/.Helperscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/Helper#) (caddyhttpscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/.MiddlewareHandlerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/MiddlewareHandler#, error) { |
| 45 | h.Nextscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/caddyfile`/Dispenser#Next().() // consume directive name |
| 46 | |
| 47 | var handler Handlerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/map`/Handler# |
| 48 | |
| 49 | // source |
| 50 | if !h.NextArgscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/caddyfile`/Dispenser#NextArg().() { |
| 51 | return nil, h.ArgErrscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/caddyfile`/Dispenser#ArgErr().() |
| 52 | } |
| 53 | handler.Sourcescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/map`/Handler#Source. = h.Valscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/caddyfile`/Dispenser#Val().() |
| 54 | |
| 55 | // destinations |
| 56 | handler.Destinationsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/map`/Handler#Destinations. = h.RemainingArgsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/caddyfile`/Dispenser#RemainingArgs().() |
| 57 | if len(handler.Destinationsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/map`/Handler#Destinations.) == 0 { |
| 58 | return nil, h.Errscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/caddyfile`/Dispenser#Err().("missing destination argument(s)") |
| 59 | } |
| 60 | for _, dest := range handler.Destinationsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/map`/Handler#Destinations. { |
| 61 | if shorthand := httpcaddyfilescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/.WasReplacedPlaceholderShorthandscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/WasReplacedPlaceholderShorthand().(dest); shorthand != "" { |
| 62 | return nil, h.Errfscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/caddyfile`/Dispenser#Errf().("destination %s conflicts with a Caddyfile placeholder shorthand", shorthand) |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | // mappings |
| 67 | for h.NextBlockscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/caddyfile`/Dispenser#NextBlock().(0) { |
| 68 | // defaults are a special case |
| 69 | if h.Valscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/caddyfile`/Dispenser#Val().() == "default" { |
| 70 | if len(handler.Defaultsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/map`/Handler#Defaults.) > 0 { |
| 71 | return nil, h.Errscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/caddyfile`/Dispenser#Err().("defaults already defined") |
| 72 | } |
| 73 | handler.Defaultsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/map`/Handler#Defaults. = h.RemainingArgsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/caddyfile`/Dispenser#RemainingArgs().() |
| 74 | for len(handler.Defaultsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/map`/Handler#Defaults.) < len(handler.Destinationsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/map`/Handler#Destinations.) { |
| 75 | handler.Defaultsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/map`/Handler#Defaults. = append(handler.Defaultsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/map`/Handler#Defaults., "") |
| 76 | } |
| 77 | continue |
| 78 | } |
| 79 | |
| 80 | // every line maps an input value to one or more outputs |
| 81 | in := h.Valscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/caddyfile`/Dispenser#Val().() |
| 82 | var outs []any |
| 83 | for h.NextArgscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/caddyfile`/Dispenser#NextArg().() { |
| 84 | val := h.ScalarValscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/caddyfile`/Dispenser#ScalarVal().() |
| 85 | if val == "-" { |
| 86 | outs = append(outs, nil) |
| 87 | } else { |
| 88 | outs = append(outs, val) |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | // cannot have more outputs than destinations |
| 93 | if len(outs) > len(handler.Destinationsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/map`/Handler#Destinations.) { |
| 94 | return nil, h.Errscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/caddyfile`/Dispenser#Err().("too many outputs") |
| 95 | } |
| 96 | |
| 97 | // for convenience, can have fewer outputs than destinations, but the |
| 98 | // underlying handler won't accept that, so we fill in nil values |
| 99 | for len(outs) < len(handler.Destinationsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/map`/Handler#Destinations.) { |
| 100 | outs = append(outs, nil) |
| 101 | } |
| 102 | |
| 103 | // create the mapping |
| 104 | mapping := Mappingscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/map`/Mapping#{Outputsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/map`/Mapping#Outputs.: outs} |
| 105 | if stringsscip-go gomod github.com/golang/go/src go1.25.1 strings/.HasPrefixscip-go gomod github.com/golang/go/src go1.25.1 strings/HasPrefix().(in, "~") { |
| 106 | mapping.InputRegexpscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/map`/Mapping#InputRegexp. = in[1:] |
| 107 | } else { |
| 108 | mapping.Inputscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/map`/Mapping#Input. = in |
| 109 | } |
| 110 | |
| 111 | handler.Mappingsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/map`/Handler#Mappings. = append(handler.Mappingsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/map`/Handler#Mappings., mapping) |
| 112 | } |
| 113 | return handler, nil |
| 114 | } |