Debian Codegraph @

caddyconfig/httpcaddyfile/directives.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 httpcaddyfilescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/
16
17 import (
18 "encoding/jsonscip-go gomod github.com/golang/go/src go1.25.1 `encoding/json`/"
19 "mapsscip-go gomod github.com/golang/go/src go1.25.1 maps/"
20 "netscip-go gomod github.com/golang/go/src go1.25.1 net/"
21 "slicesscip-go gomod github.com/golang/go/src go1.25.1 slices/"
22 "sortscip-go gomod github.com/golang/go/src go1.25.1 sort/"
23 "strconvscip-go gomod github.com/golang/go/src go1.25.1 strconv/"
24 "stringsscip-go gomod github.com/golang/go/src go1.25.1 strings/"
25
26 "github.com/caddyserver/caddy/v2scip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/"
27 "github.com/caddyserver/caddy/v2/caddyconfigscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig`/"
28 "github.com/caddyserver/caddy/v2/caddyconfig/caddyfilescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/caddyfile`/"
29 "github.com/caddyserver/caddy/v2/modules/caddyhttpscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/"
30 )
31
32 // defaultDirectiveOrder specifies the default order
33 // to apply directives in HTTP routes. This must only
34 // consist of directives that are included in Caddy's
35 // standard distribution.
36 //
37 // e.g. The 'root' directive goes near the start in
38 // case rewrites or redirects depend on existence of
39 // files, i.e. the file matcher, which must know the
40 // root first.
41 //
42 // e.g. The 'header' directive goes before 'redir' so
43 // that headers can be manipulated before doing redirects.
44 //
45 // e.g. The 'respond' directive is near the end because it
46 // writes a response and terminates the middleware chain.
47 var defaultDirectiveOrderscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/defaultDirectiveOrder. = []string{
48 "tracing",
49
50 // set variables that may be used by other directives
51 "map",
52 "vars",
53 "fs",
54 "root",
55 "log_append",
56 "skip_log", // TODO: deprecated, renamed to log_skip
57 "log_skip",
58 "log_name",
59
60 "header",
61 "copy_response_headers", // only in reverse_proxy's handle_response
62 "request_body",
63
64 "redir",
65
66 // incoming request manipulation
67 "method",
68 "rewrite",
69 "uri",
70 "try_files",
71
72 // middleware handlers; some wrap responses
73 "basicauth", // TODO: deprecated, renamed to basic_auth
74 "basic_auth",
75 "forward_auth",
76 "request_header",
77 "encode",
78 "push",
79 "intercept",
80 "templates",
81
82 // special routing & dispatching directives
83 "invoke",
84 "handle",
85 "handle_path",
86 "route",
87
88 // handlers that typically respond to requests
89 "abort",
90 "error",
91 "copy_response", // only in reverse_proxy's handle_response
92 "respond",
93 "metrics",
94 "reverse_proxy",
95 "php_fastcgi",
96 "file_server",
97 "acme_server",
98 }
99
100 // directiveOrder specifies the order to apply directives
101 // in HTTP routes, after being modified by either the
102 // plugins or by the user via the "order" global option.
103 var directiveOrderscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/directiveOrder. = defaultDirectiveOrderscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/defaultDirectiveOrder.
104
105 // RegisterDirective registers a unique directive dir with an
106 // associated unmarshaling (setup) function. When directive dir
107 // is encountered in a Caddyfile, setupFunc will be called to
108 // unmarshal its tokens.
109 func RegisterDirectivescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/RegisterDirective().(dir string, setupFunc UnmarshalFuncscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/UnmarshalFunc#) {
110 if _, ok := registeredDirectivesscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/registeredDirectives.[dir]; ok {
111 panic("directive " + dir + " already registered")
112 }
113 registeredDirectivesscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/registeredDirectives.[dir] = setupFunc
114 }
115
116 // RegisterHandlerDirective is like RegisterDirective, but for
117 // directives which specifically output only an HTTP handler.
118 // Directives registered with this function will always have
119 // an optional matcher token as the first argument.
120 func RegisterHandlerDirectivescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/RegisterHandlerDirective().(dir string, setupFunc UnmarshalHandlerFuncscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/UnmarshalHandlerFunc#) {
121 RegisterDirectivescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/RegisterDirective().(dir, func(h Helperscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/Helper#) ([]ConfigValuescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/ConfigValue#, error) {
122 if !h.Nextscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/caddyfile`/Dispenser#Next().() {
123 return nil, h.ArgErrscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/caddyfile`/Dispenser#ArgErr().()
124 }
125
126 matcherSet, err := h.ExtractMatcherSetscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/Helper#ExtractMatcherSet().()
127 if err != nil {
128 return nil, err
129 }
130
131 val, err := setupFunc(h)
132 if err != nil {
133 return nil, err
134 }
135
136 return h.NewRoutescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/Helper#NewRoute().(matcherSet, val), nil
137 })
138 }
139
140 // RegisterDirectiveOrder registers the default order for a
141 // directive from a plugin.
142 //
143 // This is useful when a plugin has a well-understood place
144 // it should run in the middleware pipeline, and it allows
145 // users to avoid having to define the order themselves.
146 //
147 // The directive dir may be placed in the position relative
148 // to ('before' or 'after') a directive included in Caddy's
149 // standard distribution. It cannot be relative to another
150 // plugin's directive.
151 //
152 // EXPERIMENTAL: This API may change or be removed.
153 func RegisterDirectiveOrderscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/RegisterDirectiveOrder().(dir string, position Positionalscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/Positional#, standardDir string) {
154 // check if directive was already ordered
155 if slicesscip-go gomod github.com/golang/go/src go1.25.1 slices/.Containsscip-go gomod github.com/golang/go/src go1.25.1 slices/Contains().(directiveOrderscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/directiveOrder., dir) {
156 panic("directive '" + dir + "' already ordered")
157 }
158
159 if position != Beforescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/Before. && position != Afterscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/After. {
160 panic("the 2nd argument must be either 'before' or 'after', got '" + position + "'")
161 }
162
163 // check if directive exists in standard distribution, since
164 // we can't allow plugins to depend on one another; we can't
165 // guarantee the order that plugins are loaded in.
166 foundStandardDir := slicesscip-go gomod github.com/golang/go/src go1.25.1 slices/.Containsscip-go gomod github.com/golang/go/src go1.25.1 slices/Contains().(defaultDirectiveOrderscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/defaultDirectiveOrder., standardDir)
167 if !foundStandardDir {
168 panic("the 3rd argument '" + standardDir + "' must be a directive that exists in the standard distribution of Caddy")
169 }
170
171 // insert directive into proper position
172 newOrder := directiveOrderscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/directiveOrder.
173 for i, d := range newOrder {
174 if d != standardDir {
175 continue
176 }
177 switch position {
178 case Beforescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/Before.:
179 newOrder = append(newOrder[:i], append([]string{dir}, newOrder[i:]...)...)
180 case Afterscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/After.:
181 newOrder = append(newOrder[:i+1], append([]string{dir}, newOrder[i+1:]...)...)
182 case Firstscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/First., Lastscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/Last.:
183 }
184 break
185 }
186 directiveOrderscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/directiveOrder. = newOrder
187 }
188
189 // RegisterGlobalOption registers a unique global option opt with
190 // an associated unmarshaling (setup) function. When the global
191 // option opt is encountered in a Caddyfile, setupFunc will be
192 // called to unmarshal its tokens.
193 func RegisterGlobalOptionscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/RegisterGlobalOption().(opt string, setupFunc UnmarshalGlobalFuncscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/UnmarshalGlobalFunc#) {
194 if _, ok := registeredGlobalOptionsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/registeredGlobalOptions.[opt]; ok {
195 panic("global option " + opt + " already registered")
196 }
197 registeredGlobalOptionsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/registeredGlobalOptions.[opt] = setupFunc
198 }
199
200 // Helper is a type which helps setup a value from
201 // Caddyfile tokens.
202 type Helperscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/Helper# struct {
203 *caddyfilescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/caddyfile`/.Dispenserscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/Helper#Dispenser.
204 // State stores intermediate variables during caddyfile adaptation.
205 Statescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/Helper#State. map[string]any
206 // BlockState stores intermediate variables scoped to the current block.
207 // It propagates down, but unlike state not back up from child to parent.
208 BlockStatescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/Helper#BlockState. map[string]any
209 optionsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/Helper#options. map[string]any
210 warningsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/Helper#warnings. *[]caddyconfigscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig`/.Warningscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig`/Warning#
211 matcherDefsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/Helper#matcherDefs. map[string]caddyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/.ModuleMapscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/ModuleMap#
212 parentBlockscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/Helper#parentBlock. caddyfilescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/caddyfile`/.ServerBlockscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/caddyfile`/ServerBlock#
213 groupCounterscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/Helper#groupCounter. counterscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/counter#
214 }
215
216 // Option gets the option keyed by name.
217 func (h Helperscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/Helper#) Optionscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/Helper#Option().(name string) any {
218 return h.optionsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/Helper#options.[name]
219 }
220
221 // Caddyfiles returns the list of config files from
222 // which tokens in the current server block were loaded.
223 func (h Helperscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/Helper#) Caddyfilesscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/Helper#Caddyfiles().() []string {
224 // first obtain set of names of files involved
225 // in this server block, without duplicates
226 files := make(map[string]struct{})
227 for _, segment := range h.parentBlockscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/Helper#parentBlock..Segmentsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/caddyfile`/ServerBlock#Segments. {
228 for _, token := range segment {
229 files[token.Filescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/caddyfile`/Token#File.] = struct{}{}
230 }
231 }
232 // then convert the set into a slice
233 filesSlice := make([]string, 0, len(files))
234 for file := range files {
235 filesSlice = append(filesSlice, file)
236 }
237 sortscip-go gomod github.com/golang/go/src go1.25.1 sort/.Stringsscip-go gomod github.com/golang/go/src go1.25.1 sort/Strings().(filesSlice)
238 return filesSlice
239 }
240
241 // JSON converts val into JSON. Any errors are added to warnings.
242 func (h Helperscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/Helper#) JSONscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/Helper#JSON().(val any) jsonscip-go gomod github.com/golang/go/src go1.25.1 `encoding/json`/.RawMessagescip-go gomod github.com/golang/go/src go1.25.1 `encoding/json`/RawMessage# {
243 return caddyconfigscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig`/.JSONscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig`/JSON().(val, h.warningsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/Helper#warnings.)
244 }
245
246 // MatcherToken assumes the next argument token is (possibly) a matcher,
247 // and if so, returns the matcher set along with a true value. If the next
248 // token is not a matcher, nil and false is returned. Note that a true
249 // value may be returned with a nil matcher set if it is a catch-all.
250 func (h Helperscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/Helper#) MatcherTokenscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/Helper#MatcherToken().() (caddyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/.ModuleMapscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/ModuleMap#, bool, error) {
251 if !h.NextArgscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/caddyfile`/Dispenser#NextArg().() {
252 return nil, false, nil
253 }
254 return matcherSetFromMatcherTokenscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/matcherSetFromMatcherToken().(h.Dispenserscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/Helper#Dispenser..Tokenscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/caddyfile`/Dispenser#Token().(), h.matcherDefsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/Helper#matcherDefs., h.warningsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/Helper#warnings.)
255 }
256
257 // ExtractMatcherSet is like MatcherToken, except this is a higher-level
258 // method that returns the matcher set described by the matcher token,
259 // or nil if there is none, and deletes the matcher token from the
260 // dispenser and resets it as if this look-ahead never happened. Useful
261 // when wrapping a route (one or more handlers) in a user-defined matcher.
262 func (h Helperscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/Helper#) ExtractMatcherSetscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/Helper#ExtractMatcherSet().() (caddyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/.ModuleMapscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/ModuleMap#, error) {
263 matcherSet, hasMatcher, err := h.MatcherTokenscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/Helper#MatcherToken().()
264 if err != nil {
265 return nil, err
266 }
267 if hasMatcher {
268 // strip matcher token; we don't need to
269 // use the return value here because a
270 // new dispenser should have been made
271 // solely for this directive's tokens,
272 // with no other uses of same slice
273 h.Dispenserscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/Helper#Dispenser..Deletescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/caddyfile`/Dispenser#Delete().()
274 }
275 h.Dispenserscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/Helper#Dispenser..Resetscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/caddyfile`/Dispenser#Reset().() // pretend this lookahead never happened
276 return matcherSet, nil
277 }
278
279 // NewRoute returns config values relevant to creating a new HTTP route.
280 func (h Helperscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/Helper#) NewRoutescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/Helper#NewRoute().(matcherSet caddyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/.ModuleMapscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/ModuleMap#,
281 handler 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#,
282 ) []ConfigValuescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/ConfigValue# {
283 mod, err := caddyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/.GetModulescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/GetModule().(caddyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/.GetModuleIDscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/GetModuleID().(handler))
284 if err != nil {
285 *h.warningsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/Helper#warnings. = append(*h.warningsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/Helper#warnings., caddyconfigscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig`/.Warningscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig`/Warning#{
286 Filescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig`/Warning#File.: h.Filescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/caddyfile`/Dispenser#File().(),
287 Linescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig`/Warning#Line.: h.Linescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/caddyfile`/Dispenser#Line().(),
288 Messagescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig`/Warning#Message.: err.Error(),
289 })
290 return nil
291 }
292 var matcherSetsRaw []caddyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/.ModuleMapscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/ModuleMap#
293 if matcherSet != nil {
294 matcherSetsRaw = append(matcherSetsRaw, matcherSet)
295 }
296 return []ConfigValuescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/ConfigValue#{
297 {
298 Classscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/ConfigValue#Class.: "route",
299 Valuescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/ConfigValue#Value.: caddyhttpscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/.Routescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Route#{
300 MatcherSetsRawscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Route#MatcherSetsRaw.: matcherSetsRaw,
301 HandlersRawscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Route#HandlersRaw.: []jsonscip-go gomod github.com/golang/go/src go1.25.1 `encoding/json`/.RawMessagescip-go gomod github.com/golang/go/src go1.25.1 `encoding/json`/RawMessage#{caddyconfigscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig`/.JSONModuleObjectscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig`/JSONModuleObject().(handler, "handler", mod.IDscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/ModuleInfo#ID..Namescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/ModuleID#Name().(), h.warningsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/Helper#warnings.)},
302 },
303 },
304 }
305 }
306
307 // GroupRoutes adds the routes (caddyhttp.Route type) in vals to the
308 // same group, if there is more than one route in vals.
309 func (h Helperscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/Helper#) GroupRoutesscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/Helper#GroupRoutes().(vals []ConfigValuescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/ConfigValue#) {
310 // ensure there's at least two routes; group of one is pointless
311 var count int
312 for _, v := range vals {
313 if _, ok := v.Valuescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/ConfigValue#Value..(caddyhttpscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/.Routescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Route#); ok {
314 count++
315 if count > 1 {
316 break
317 }
318 }
319 }
320 if count < 2 {
321 return
322 }
323
324 // now that we know the group will have some effect, do it
325 groupName := h.groupCounterscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/Helper#groupCounter..nextGroupscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/counter#nextGroup().()
326 for i := range vals {
327 if route, ok := vals[i].Valuescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/ConfigValue#Value..(caddyhttpscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/.Routescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Route#); ok {
328 route.Groupscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Route#Group. = groupName
329 vals[i].Valuescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/ConfigValue#Value. = route
330 }
331 }
332 }
333
334 // WithDispenser returns a new instance based on d. All others Helper
335 // fields are copied, so typically maps are shared with this new instance.
336 func (h Helperscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/Helper#) WithDispenserscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/Helper#WithDispenser().(d *caddyfilescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/caddyfile`/.Dispenserscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/caddyfile`/Dispenser#) Helperscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/Helper# {
337 h.Dispenserscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/Helper#Dispenser. = d
338 return h
339 }
340
341 // ParseSegmentAsSubroute parses the segment such that its subdirectives
342 // are themselves treated as directives, from which a subroute is built
343 // and returned.
344 func ParseSegmentAsSubroutescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/ParseSegmentAsSubroute().(h 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) {
345 allResults, err := parseSegmentAsConfigscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/parseSegmentAsConfig().(h)
346 if err != nil {
347 return nil, err
348 }
349
350 return buildSubroutescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/buildSubroute().(allResults, h.groupCounterscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/Helper#groupCounter., true)
351 }
352
353 // parseSegmentAsConfig parses the segment such that its subdirectives
354 // are themselves treated as directives, including named matcher definitions,
355 // and the raw Config structs are returned.
356 func parseSegmentAsConfigscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/parseSegmentAsConfig().(h Helperscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/Helper#) ([]ConfigValuescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/ConfigValue#, error) {
357 var allResults []ConfigValuescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/ConfigValue#
358
359 for h.Nextscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/caddyfile`/Dispenser#Next().() {
360 // don't allow non-matcher args on the first line
361 if h.NextArgscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/caddyfile`/Dispenser#NextArg().() {
362 return nil, h.ArgErrscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/caddyfile`/Dispenser#ArgErr().()
363 }
364
365 // slice the linear list of tokens into top-level segments
366 var segments []caddyfilescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/caddyfile`/.Segmentscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/caddyfile`/Segment#
367 for nesting := h.Nestingscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/caddyfile`/Dispenser#Nesting().(); h.NextBlockscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/caddyfile`/Dispenser#NextBlock().(nesting); {
368 segments = append(segments, h.NextSegmentscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/caddyfile`/Dispenser#NextSegment().())
369 }
370
371 // copy existing matcher definitions so we can augment
372 // new ones that are defined only in this scope
373 matcherDefs := make(map[string]caddyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/.ModuleMapscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/ModuleMap#, len(h.matcherDefsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/Helper#matcherDefs.))
374 mapsscip-go gomod github.com/golang/go/src go1.25.1 maps/.Copyscip-go gomod github.com/golang/go/src go1.25.1 maps/Copy().(matcherDefs, h.matcherDefsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/Helper#matcherDefs.)
375
376 // find and extract any embedded matcher definitions in this scope
377 for i := 0; i < len(segments); i++ {
378 seg := segments[i]
379 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().(seg.Directivescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/caddyfile`/Segment#Directive().(), matcherPrefixscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/matcherPrefix.) {
380 // parse, then add the matcher to matcherDefs
381 err := parseMatcherDefinitionsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/parseMatcherDefinitions().(caddyfilescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/caddyfile`/.NewDispenserscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/caddyfile`/NewDispenser().(seg), matcherDefs)
382 if err != nil {
383 return nil, err
384 }
385 // remove the matcher segment (consumed), then step back the loop
386 segments = append(segments[:i], segments[i+1:]...)
387 i--
388 }
389 }
390
391 // clone BlockState once for the entire block so sibling directives
392 // can share state, but changes don't leak to the parent scope
393 subBlockState := make(map[string]any, len(h.BlockStatescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/Helper#BlockState.))
394 mapsscip-go gomod github.com/golang/go/src go1.25.1 maps/.Copyscip-go gomod github.com/golang/go/src go1.25.1 maps/Copy().(subBlockState, h.BlockStatescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/Helper#BlockState.)
395
396 // with matchers ready to go, evaluate each directive's segment
397 for _, seg := range segments {
398 dir := seg.Directivescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/caddyfile`/Segment#Directive().()
399 dirFunc, ok := registeredDirectivesscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/registeredDirectives.[dir]
400 if !ok {
401 return nil, h.Errfscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/caddyfile`/Dispenser#Errf().("unrecognized directive: %s - are you sure your Caddyfile structure (nesting and braces) is correct?", dir)
402 }
403
404 subHelper := h
405 subHelper.Dispenserscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/Helper#Dispenser. = caddyfilescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/caddyfile`/.NewDispenserscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/caddyfile`/NewDispenser().(seg)
406 subHelper.matcherDefsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/Helper#matcherDefs. = matcherDefs
407 subHelper.BlockStatescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/Helper#BlockState. = subBlockState
408
409 results, err := dirFunc(subHelper)
410 if err != nil {
411 return nil, h.Errfscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/caddyfile`/Dispenser#Errf().("parsing caddyfile tokens for '%s': %v", dir, err)
412 }
413
414 dir = normalizeDirectiveNamescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/normalizeDirectiveName().(dir)
415
416 for _, result := range results {
417 result.directivescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/ConfigValue#directive. = dir
418 allResults = append(allResults, result)
419 }
420 }
421 }
422
423 return allResults, nil
424 }
425
426 // ConfigValue represents a value to be added to the final
427 // configuration, or a value to be consulted when building
428 // the final configuration.
429 type ConfigValuescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/ConfigValue# struct {
430 // The kind of value this is. As the config is
431 // being built, the adapter will look in the
432 // "pile" for values belonging to a certain
433 // class when it is setting up a certain part
434 // of the config. The associated value will be
435 // type-asserted and placed accordingly.
436 Classscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/ConfigValue#Class. string
437
438 // The value to be used when building the config.
439 // Generally its type is associated with the
440 // name of the Class.
441 Valuescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/ConfigValue#Value. any
442
443 directivescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/ConfigValue#directive. string
444 }
445
446 func sortRoutesscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/sortRoutes().(routes []ConfigValuescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/ConfigValue#) {
447 dirPositions := make(map[string]int)
448 for i, dir := range directiveOrderscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/directiveOrder. {
449 dirPositions[dir] = i
450 }
451
452 sortscip-go gomod github.com/golang/go/src go1.25.1 sort/.SliceStablescip-go gomod github.com/golang/go/src go1.25.1 sort/SliceStable().(routes, func(i, j int) bool {
453 // if the directives are different, just use the established directive order
454 iDir, jDir := routes[i].directivescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/ConfigValue#directive., routes[j].directivescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/ConfigValue#directive.
455 if iDir != jDir {
456 return dirPositions[iDir] < dirPositions[jDir]
457 }
458
459 // directives are the same; sub-sort by path matcher length if there's
460 // only one matcher set and one path (this is a very common case and
461 // usually -- but not always -- helpful/expected, oh well; user can
462 // always take manual control of order using handler or route blocks)
463 iRoute, ok := routes[i].Valuescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/ConfigValue#Value..(caddyhttpscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/.Routescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Route#)
464 if !ok {
465 return false
466 }
467 jRoute, ok := routes[j].Valuescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/ConfigValue#Value..(caddyhttpscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/.Routescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Route#)
468 if !ok {
469 return false
470 }
471
472 // decode the path matchers if there is just one matcher set
473 var iPM, jPM caddyhttpscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/.MatchPathscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/MatchPath#
474 if len(iRoute.MatcherSetsRawscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Route#MatcherSetsRaw.) == 1 {
475 _ = jsonscip-go gomod github.com/golang/go/src go1.25.1 `encoding/json`/.Unmarshalscip-go gomod github.com/golang/go/src go1.25.1 `encoding/json`/Unmarshal().(iRoute.MatcherSetsRawscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Route#MatcherSetsRaw.[0]["path"], &iPM)
476 }
477 if len(jRoute.MatcherSetsRawscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Route#MatcherSetsRaw.) == 1 {
478 _ = jsonscip-go gomod github.com/golang/go/src go1.25.1 `encoding/json`/.Unmarshalscip-go gomod github.com/golang/go/src go1.25.1 `encoding/json`/Unmarshal().(jRoute.MatcherSetsRawscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Route#MatcherSetsRaw.[0]["path"], &jPM)
479 }
480
481 // if there is only one path in the path matcher, sort by longer path
482 // (more specific) first; missing path matchers or multi-matchers are
483 // treated as zero-length paths
484 var iPathLen, jPathLen int
485 if len(iPM) == 1 {
486 iPathLen = len(iPM[0])
487 }
488 if len(jPM) == 1 {
489 jPathLen = len(jPM[0])
490 }
491
492 sortByPath := func() bool {
493 // we can only confidently compare path lengths if both
494 // directives have a single path to match (issue #5037)
495 if iPathLen > 0 && jPathLen > 0 {
496 // trim the trailing wildcard if there is one
497 iPathTrimmed := stringsscip-go gomod github.com/golang/go/src go1.25.1 strings/.TrimSuffixscip-go gomod github.com/golang/go/src go1.25.1 strings/TrimSuffix().(iPM[0], "*")
498 jPathTrimmed := stringsscip-go gomod github.com/golang/go/src go1.25.1 strings/.TrimSuffixscip-go gomod github.com/golang/go/src go1.25.1 strings/TrimSuffix().(jPM[0], "*")
499
500 // if both paths are the same except for a trailing wildcard,
501 // sort by the shorter path first (which is more specific)
502 if iPathTrimmed == jPathTrimmed {
503 return iPathLen < jPathLen
504 }
505
506 // we use the trimmed length to compare the paths
507 // https://github.com/caddyserver/caddy/issues/7012#issuecomment-2870142195
508 // credit to https://github.com/Hellio404
509 // for sorts with many items, mixing matchers w/ and w/o wildcards will confuse the sort and result in incorrect orders
510 iPathLen = len(iPathTrimmed)
511 jPathLen = len(jPathTrimmed)
512
513 // if both paths have the same length, sort lexically
514 // https://github.com/caddyserver/caddy/pull/7015#issuecomment-2871993588
515 if iPathLen == jPathLen {
516 return iPathTrimmed < jPathTrimmed
517 }
518
519 // sort most-specific (longest) path first
520 return iPathLen > jPathLen
521 }
522
523 // if both directives don't have a single path to compare,
524 // sort whichever one has a matcher first; if both have
525 // a matcher, sort equally (stable sort preserves order)
526 return len(iRoute.MatcherSetsRawscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Route#MatcherSetsRaw.) > 0 && len(jRoute.MatcherSetsRawscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Route#MatcherSetsRaw.) == 0
527 }()
528
529 // some directives involve setting values which can overwrite
530 // each other, so it makes most sense to reverse the order so
531 // that the least-specific matcher is first, allowing the last
532 // matching one to win
533 if iDir == "vars" {
534 return !sortByPath
535 }
536
537 // everything else is most-specific matcher first
538 return sortByPath
539 })
540 }
541
542 // serverBlock pairs a Caddyfile server block with
543 // a "pile" of config values, keyed by class name,
544 // as well as its parsed keys for convenience.
545 type serverBlockscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/serverBlock# struct {
546 blockscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/serverBlock#block. caddyfilescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/caddyfile`/.ServerBlockscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/caddyfile`/ServerBlock#
547 pilescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/serverBlock#pile. map[string][]ConfigValuescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/ConfigValue# // config values obtained from directives
548 parsedKeysscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/serverBlock#parsedKeys. []Addressscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/Address#
549 }
550
551 // hostsFromKeys returns a list of all the non-empty hostnames found in
552 // the keys of the server block sb. If logger mode is false, a key with
553 // an empty hostname portion will return an empty slice, since that
554 // server block is interpreted to effectively match all hosts. An empty
555 // string is never added to the slice.
556 //
557 // If loggerMode is true, then the non-standard ports of keys will be
558 // joined to the hostnames. This is to effectively match the Host
559 // header of requests that come in for that key.
560 //
561 // The resulting slice is not sorted but will never have duplicates.
562 func (sb serverBlockscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/serverBlock#) hostsFromKeysscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/serverBlock#hostsFromKeys().(loggerMode bool) []string {
563 // ensure each entry in our list is unique
564 hostMap := make(map[string]struct{})
565 for _, addr := range sb.parsedKeysscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/serverBlock#parsedKeys. {
566 if addr.Hostscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/Address#Host. == "" {
567 if !loggerMode {
568 // server block contains a key like ":443", i.e. the host portion
569 // is empty / catch-all, which means to match all hosts
570 return []string{}
571 }
572 // never append an empty string
573 continue
574 }
575 if loggerMode &&
576 addr.Portscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/Address#Port. != "" &&
577 addr.Portscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/Address#Port. != strconvscip-go gomod github.com/golang/go/src go1.25.1 strconv/.Itoascip-go gomod github.com/golang/go/src go1.25.1 strconv/Itoa().(caddyhttpscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/.DefaultHTTPPortscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/DefaultHTTPPort.) &&
578 addr.Portscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/Address#Port. != strconvscip-go gomod github.com/golang/go/src go1.25.1 strconv/.Itoascip-go gomod github.com/golang/go/src go1.25.1 strconv/Itoa().(caddyhttpscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/.DefaultHTTPSPortscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/DefaultHTTPSPort.) {
579 hostMap[netscip-go gomod github.com/golang/go/src go1.25.1 net/.JoinHostPortscip-go gomod github.com/golang/go/src go1.25.1 net/JoinHostPort().(addr.Hostscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/Address#Host., addr.Portscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/Address#Port.)] = struct{}{}
580 } else {
581 hostMap[addr.Hostscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/Address#Host.] = struct{}{}
582 }
583 }
584
585 // convert map to slice
586 sblockHosts := make([]string, 0, len(hostMap))
587 for host := range hostMap {
588 sblockHosts = append(sblockHosts, host)
589 }
590
591 return sblockHosts
592 }
593
594 func (sb serverBlockscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/serverBlock#) hostsFromKeysNotHTTPscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/serverBlock#hostsFromKeysNotHTTP().(httpPort string) []string {
595 // ensure each entry in our list is unique
596 hostMap := make(map[string]struct{})
597 for _, addr := range sb.parsedKeysscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/serverBlock#parsedKeys. {
598 if addr.Hostscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/Address#Host. == "" {
599 continue
600 }
601 if addr.Schemescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/Address#Scheme. != "http" && addr.Portscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/Address#Port. != httpPort {
602 hostMap[addr.Hostscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/Address#Host.] = struct{}{}
603 }
604 }
605
606 // convert map to slice
607 sblockHosts := make([]string, 0, len(hostMap))
608 for host := range hostMap {
609 sblockHosts = append(sblockHosts, host)
610 }
611
612 return sblockHosts
613 }
614
615 // hasHostCatchAllKey returns true if sb has a key that
616 // omits a host portion, i.e. it "catches all" hosts.
617 func (sb serverBlockscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/serverBlock#) hasHostCatchAllKeyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/serverBlock#hasHostCatchAllKey().() bool {
618 return slicesscip-go gomod github.com/golang/go/src go1.25.1 slices/.ContainsFuncscip-go gomod github.com/golang/go/src go1.25.1 slices/ContainsFunc().(sb.parsedKeysscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/serverBlock#parsedKeys., func(addr Addressscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/Address#) bool {
619 return addr.Hostscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/Address#Host. == ""
620 })
621 }
622
623 // isAllHTTP returns true if all sb keys explicitly specify
624 // the http:// scheme
625 func (sb serverBlockscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/serverBlock#) isAllHTTPscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/serverBlock#isAllHTTP().() bool {
626 return !slicesscip-go gomod github.com/golang/go/src go1.25.1 slices/.ContainsFuncscip-go gomod github.com/golang/go/src go1.25.1 slices/ContainsFunc().(sb.parsedKeysscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/serverBlock#parsedKeys., func(addr Addressscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/Address#) bool {
627 return addr.Schemescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/Address#Scheme. != "http"
628 })
629 }
630
631 // Positional are the supported modes for ordering directives.
632 type Positionalscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/Positional# string
633
634 const (
635 Beforescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/Before. Positionalscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/Positional# = "before"
636 Afterscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/After. Positionalscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/Positional# = "after"
637 Firstscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/First. Positionalscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/Positional# = "first"
638 Lastscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/Last. Positionalscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/Positional# = "last"
639 )
640
641 type (
642 // UnmarshalFunc is a function which can unmarshal Caddyfile
643 // tokens into zero or more config values using a Helper type.
644 // These are passed in a call to RegisterDirective.
645 UnmarshalFuncscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/UnmarshalFunc# func(h Helperscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/Helper#) ([]ConfigValuescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/ConfigValue#, error)
646
647 // UnmarshalHandlerFunc is like UnmarshalFunc, except the
648 // output of the unmarshaling is an HTTP handler. This
649 // function does not need to deal with HTTP request matching
650 // which is abstracted away. Since writing HTTP handlers
651 // with Caddyfile support is very common, this is a more
652 // convenient way to add a handler to the chain since a lot
653 // of the details common to HTTP handlers are taken care of
654 // for you. These are passed to a call to
655 // RegisterHandlerDirective.
656 UnmarshalHandlerFuncscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/UnmarshalHandlerFunc# func(h 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)
657
658 // UnmarshalGlobalFunc is a function which can unmarshal Caddyfile
659 // tokens from a global option. It is passed the tokens to parse and
660 // existing value from the previous instance of this global option
661 // (if any). It returns the value to associate with this global option.
662 UnmarshalGlobalFuncscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/UnmarshalGlobalFunc# func(d *caddyfilescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/caddyfile`/.Dispenserscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/caddyfile`/Dispenser#, existingVal any) (any, error)
663 )
664
665 var registeredDirectivesscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/registeredDirectives. = make(map[string]UnmarshalFuncscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/UnmarshalFunc#)
666
667 var registeredGlobalOptionsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/registeredGlobalOptions. = make(map[string]UnmarshalGlobalFuncscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/UnmarshalGlobalFunc#)