Debian Codegraph @

modules/caddyhttp/map/map.go

raw symbols SCIP
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 "fmtscip-go gomod github.com/golang/go/src go1.25.1 fmt/"
19 "net/httpscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/"
20 "regexpscip-go gomod github.com/golang/go/src go1.25.1 regexp/"
21 "slicesscip-go gomod github.com/golang/go/src go1.25.1 slices/"
22 "stringsscip-go gomod github.com/golang/go/src go1.25.1 strings/"
23
24 "github.com/caddyserver/caddy/v2scip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/"
25 "github.com/caddyserver/caddy/v2/modules/caddyhttpscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/"
26 )
27
28 func initscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/map`/init().() {
29 caddyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/.RegisterModulescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/RegisterModule().(Handlerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/map`/Handler#{})
30 }
31
32 // Handler implements a middleware that maps inputs to outputs. Specifically, it
33 // compares a source value against the map inputs, and for one that matches, it
34 // applies the output values to each destination. Destinations become placeholder
35 // names.
36 //
37 // Mapped placeholders are not evaluated until they are used, so even for very
38 // large mappings, this handler is quite efficient.
39 type Handlerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/map`/Handler# struct {
40 // Source is the placeholder from which to get the input value.
41 Sourcescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/map`/Handler#Source. string `json:"source,omitempty"`
42
43 // Destinations are the names of placeholders in which to store the outputs.
44 // Destination values should be wrapped in braces, for example, {my_placeholder}.
45 Destinationsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/map`/Handler#Destinations. []string `json:"destinations,omitempty"`
46
47 // Mappings from source values (inputs) to destination values (outputs).
48 // The first matching, non-nil mapping will be applied.
49 Mappingsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/map`/Handler#Mappings. []Mappingscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/map`/Mapping# `json:"mappings,omitempty"`
50
51 // If no mappings match or if the mapped output is null/nil, the associated
52 // default output will be applied (optional).
53 Defaultsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/map`/Handler#Defaults. []string `json:"defaults,omitempty"`
54 }
55
56 // CaddyModule returns the Caddy module information.
57 func (Handlerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/map`/Handler#) CaddyModulescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/map`/Handler#CaddyModule().() caddyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/.ModuleInfoscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/ModuleInfo# {
58 return caddyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/.ModuleInfoscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/ModuleInfo#{
59 IDscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/ModuleInfo#ID.: "http.handlers.map",
60 Newscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/ModuleInfo#New.: func() caddyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/.Modulescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/Module# { return new(Handlerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/map`/Handler#) },
61 }
62 }
63
64 // Provision sets up h.
65 func (h *Handlerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/map`/Handler#) Provisionscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/map`/Handler#Provision().(_ caddyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/.Contextscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/Context#) error {
66 for j, dest := range h.Destinationsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/map`/Handler#Destinations. {
67 if stringsscip-go gomod github.com/golang/go/src go1.25.1 strings/.Countscip-go gomod github.com/golang/go/src go1.25.1 strings/Count().(dest, "{") != 1 || !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().(dest, "{") {
68 return fmtscip-go gomod github.com/golang/go/src go1.25.1 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.1 fmt/Errorf().("destination must be a placeholder and only a placeholder")
69 }
70 h.Destinationsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/map`/Handler#Destinations.[j] = stringsscip-go gomod github.com/golang/go/src go1.25.1 strings/.Trimscip-go gomod github.com/golang/go/src go1.25.1 strings/Trim().(dest, "{}")
71 }
72
73 for i, m := range h.Mappingsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/map`/Handler#Mappings. {
74 if m.InputRegexpscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/map`/Mapping#InputRegexp. == "" {
75 continue
76 }
77 var err error
78 h.Mappingsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/map`/Handler#Mappings.[i].rescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/map`/Mapping#re., err = regexpscip-go gomod github.com/golang/go/src go1.25.1 regexp/.Compilescip-go gomod github.com/golang/go/src go1.25.1 regexp/Compile().(m.InputRegexpscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/map`/Mapping#InputRegexp.)
79 if err != nil {
80 return fmtscip-go gomod github.com/golang/go/src go1.25.1 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.1 fmt/Errorf().("compiling regexp for mapping %d: %v", i, err)
81 }
82 }
83
84 // TODO: improve efficiency even further by using an actual map type
85 // for the non-regexp mappings, OR sort them and do a binary search
86
87 return nil
88 }
89
90 // Validate ensures that h is configured properly.
91 func (h *Handlerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/map`/Handler#) Validatescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/map`/Handler#Validate().() error {
92 nDest, nDef := len(h.Destinationsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/map`/Handler#Destinations.), len(h.Defaultsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/map`/Handler#Defaults.)
93 if nDef > 0 && nDef != nDest {
94 return fmtscip-go gomod github.com/golang/go/src go1.25.1 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.1 fmt/Errorf().("%d destinations != %d defaults", nDest, nDef)
95 }
96
97 seen := make(map[string]int)
98 for i, m := range h.Mappingsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/map`/Handler#Mappings. {
99 // prevent confusing/ambiguous mappings
100 if m.Inputscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/map`/Mapping#Input. != "" && m.InputRegexpscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/map`/Mapping#InputRegexp. != "" {
101 return fmtscip-go gomod github.com/golang/go/src go1.25.1 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.1 fmt/Errorf().("mapping %d has both input and input_regexp fields specified, which is confusing", i)
102 }
103
104 // prevent duplicate mappings
105 input := m.Inputscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/map`/Mapping#Input.
106 if m.InputRegexpscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/map`/Mapping#InputRegexp. != "" {
107 input = m.InputRegexpscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/map`/Mapping#InputRegexp.
108 }
109 if prev, ok := seen[input]; ok {
110 return fmtscip-go gomod github.com/golang/go/src go1.25.1 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.1 fmt/Errorf().("mapping %d has a duplicate input '%s' previously used with mapping %d", i, input, prev)
111 }
112 seen[input] = i
113
114 // ensure mappings have 1:1 output-to-destination correspondence
115 nOut := len(m.Outputsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/map`/Mapping#Outputs.)
116 if nOut != nDest {
117 return fmtscip-go gomod github.com/golang/go/src go1.25.1 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.1 fmt/Errorf().("mapping %d has %d outputs but there are %d destinations defined", i, nOut, nDest)
118 }
119 }
120
121 return nil
122 }
123
124 func (h Handlerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/map`/Handler#) ServeHTTPscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/map`/Handler#ServeHTTP().(w httpscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/.ResponseWriterscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/ResponseWriter#, r *httpscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/.Requestscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Request#, next caddyhttpscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/.Handlerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Handler#) error {
125 repl := r.Contextscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Request#Context().().Valuescip-go gomod github.com/golang/go/src go1.25.1 context/Context#Value().(caddyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/.ReplacerCtxKeyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/ReplacerCtxKey.).(*caddyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/.Replacerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/Replacer#)
126
127 // defer work until a variable is actually evaluated by using replacer's Map callback
128 repl.Mapscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/Replacer#Map().(func(key string) (any, bool) {
129 // return early if the variable is not even a configured destination
130 destIdx := slicesscip-go gomod github.com/golang/go/src go1.25.1 slices/.Indexscip-go gomod github.com/golang/go/src go1.25.1 slices/Index().(h.Destinationsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/map`/Handler#Destinations., key)
131 if destIdx < 0 {
132 return nil, false
133 }
134
135 input := repl.ReplaceAllscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/Replacer#ReplaceAll().(h.Sourcescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/map`/Handler#Source., "")
136
137 // find the first mapping matching the input and return
138 // the requested destination/output value
139 for _, m := range h.Mappingsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/map`/Handler#Mappings. {
140 output := m.Outputsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/map`/Mapping#Outputs.[destIdx]
141 if output == nil {
142 continue
143 }
144 outputStr := caddyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/.ToStringscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/ToString().(output)
145
146 // evaluate regular expression if configured
147 if m.rescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/map`/Mapping#re. != nil {
148 var result []byte
149 matches := m.rescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/map`/Mapping#re..FindStringSubmatchIndexscip-go gomod github.com/golang/go/src go1.25.1 regexp/Regexp#FindStringSubmatchIndex().(input)
150 if matches == nil {
151 continue
152 }
153 result = m.rescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/map`/Mapping#re..ExpandStringscip-go gomod github.com/golang/go/src go1.25.1 regexp/Regexp#ExpandString().(result, outputStr, input, matches)
154 return string(result), true
155 }
156
157 // otherwise simple string comparison
158 if input == m.Inputscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/map`/Mapping#Input. {
159 return repl.ReplaceAllscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/Replacer#ReplaceAll().(outputStr, ""), true
160 }
161 }
162
163 // fall back to default if no match or if matched nil value
164 if len(h.Defaultsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/map`/Handler#Defaults.) > destIdx {
165 return repl.ReplaceAllscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/Replacer#ReplaceAll().(h.Defaultsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/map`/Handler#Defaults.[destIdx], ""), true
166 }
167
168 return nil, true
169 })
170
171 return next.ServeHTTPscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Handler#ServeHTTP.(w, r)
172 }
173
174 // Mapping describes a mapping from input to outputs.
175 type Mappingscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/map`/Mapping# struct {
176 // The input value to match. Must be distinct from other mappings.
177 // Mutually exclusive to input_regexp.
178 Inputscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/map`/Mapping#Input. string `json:"input,omitempty"`
179
180 // The input regular expression to match. Mutually exclusive to input.
181 InputRegexpscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/map`/Mapping#InputRegexp. string `json:"input_regexp,omitempty"`
182
183 // Upon a match with the input, each output is positionally correlated
184 // with each destination of the parent handler. An output that is null
185 // (nil) will be treated as if it was not mapped at all.
186 Outputsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/map`/Mapping#Outputs. []any `json:"outputs,omitempty"`
187
188 rescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/map`/Mapping#re. *regexpscip-go gomod github.com/golang/go/src go1.25.1 regexp/.Regexpscip-go gomod github.com/golang/go/src go1.25.1 regexp/Regexp#
189 }
190
191 // Interface guards
192 var (
193 _ caddyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/.Provisionerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/Provisioner# = (*Handlerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/map`/Handler#)(nil)
194 _ caddyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/.Validatorscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/Validator# = (*Handlerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/map`/Handler#)(nil)
195 _ 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# = (*Handlerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/map`/Handler#)(nil)
196 )