Debian Codegraph @

modules/caddyhttp/routes.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 caddyhttpscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/
16
17 import (
18 "encoding/jsonscip-go gomod github.com/golang/go/src go1.25.1 `encoding/json`/"
19 "fmtscip-go gomod github.com/golang/go/src go1.25.1 fmt/"
20 "net/httpscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/"
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 )
26
27 // Route consists of a set of rules for matching HTTP requests,
28 // a list of handlers to execute, and optional flow control
29 // parameters which customize the handling of HTTP requests
30 // in a highly flexible and performant manner.
31 type Routescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Route# struct {
32 // Group is an optional name for a group to which this
33 // route belongs. Grouping a route makes it mutually
34 // exclusive with others in its group; if a route belongs
35 // to a group, only the first matching route in that group
36 // will be executed.
37 Groupscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Route#Group. string `json:"group,omitempty"`
38
39 // The matcher sets which will be used to qualify this
40 // route for a request (essentially the "if" statement
41 // of this route). Each matcher set is OR'ed, but matchers
42 // within a set are AND'ed together.
43 MatcherSetsRawscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Route#MatcherSetsRaw. RawMatcherSetsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/RawMatcherSets# `json:"match,omitempty" caddy:"namespace=http.matchers"`
44
45 // The list of handlers for this route. Upon matching a request, they are chained
46 // together in a middleware fashion: requests flow from the first handler to the last
47 // (top of the list to the bottom), with the possibility that any handler could stop
48 // the chain and/or return an error. Responses flow back through the chain (bottom of
49 // the list to the top) as they are written out to the client.
50 //
51 // Not all handlers call the next handler in the chain. For example, the reverse_proxy
52 // handler always sends a request upstream or returns an error. Thus, configuring
53 // handlers after reverse_proxy in the same route is illogical, since they would never
54 // be executed. You will want to put handlers which originate the response at the very
55 // end of your route(s). The documentation for a module should state whether it invokes
56 // the next handler, but sometimes it is common sense.
57 //
58 // Some handlers manipulate the response. Remember that requests flow down the list, and
59 // responses flow up the list.
60 //
61 // For example, if you wanted to use both `templates` and `encode` handlers, you would
62 // need to put `templates` after `encode` in your route, because responses flow up.
63 // Thus, `templates` will be able to parse and execute the plain-text response as a
64 // template, and then return it up to the `encode` handler which will then compress it
65 // into a binary format.
66 //
67 // If `templates` came before `encode`, then `encode` would write a compressed,
68 // binary-encoded response to `templates` which would not be able to parse the response
69 // properly.
70 //
71 // The correct order, then, is this:
72 //
73 // [
74 // {"handler": "encode"},
75 // {"handler": "templates"},
76 // {"handler": "file_server"}
77 // ]
78 //
79 // The request flows ⬇️ DOWN (`encode` -> `templates` -> `file_server`).
80 //
81 // 1. First, `encode` will choose how to `encode` the response and wrap the response.
82 // 2. Then, `templates` will wrap the response with a buffer.
83 // 3. Finally, `file_server` will originate the content from a file.
84 //
85 // The response flows ⬆️ UP (`file_server` -> `templates` -> `encode`):
86 //
87 // 1. First, `file_server` will write the file to the response.
88 // 2. That write will be buffered and then executed by `templates`.
89 // 3. Lastly, the write from `templates` will flow into `encode` which will compress the stream.
90 //
91 // If you think of routes in this way, it will be easy and even fun to solve the puzzle of writing correct routes.
92 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# `json:"handle,omitempty" caddy:"namespace=http.handlers inline_key=handler"`
93
94 // If true, no more routes will be executed after this one.
95 Terminalscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Route#Terminal. bool `json:"terminal,omitempty"`
96
97 // decoded values
98 MatcherSetsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Route#MatcherSets. MatcherSetsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/MatcherSets# `json:"-"`
99 Handlersscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Route#Handlers. []MiddlewareHandlerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/MiddlewareHandler# `json:"-"`
100
101 middlewarescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Route#middleware. []Middlewarescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Middleware#
102 metricsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Route#metrics. *Metricsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Metrics#
103 metricsCtxscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Route#metricsCtx. 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#
104 handlerNamescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Route#handlerName. string
105 }
106
107 // Empty returns true if the route has all zero/default values.
108 func (r Routescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Route#) Emptyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Route#Empty().() bool {
109 return len(r.MatcherSetsRawscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Route#MatcherSetsRaw.) == 0 &&
110 len(r.MatcherSetsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Route#MatcherSets.) == 0 &&
111 len(r.HandlersRawscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Route#HandlersRaw.) == 0 &&
112 len(r.Handlersscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Route#Handlers.) == 0 &&
113 !r.Terminalscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Route#Terminal. &&
114 r.Groupscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Route#Group. == ""
115 }
116
117 func (r Routescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Route#) Stringscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Route#String().() string {
118 var handlersRaw stringsscip-go gomod github.com/golang/go/src go1.25.1 strings/.Builderscip-go gomod github.com/golang/go/src go1.25.1 strings/Builder#
119 handlersRaw.WriteBytescip-go gomod github.com/golang/go/src go1.25.1 strings/Builder#WriteByte().('[')
120 for _, hr := range r.HandlersRawscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Route#HandlersRaw. {
121 handlersRaw.WriteBytescip-go gomod github.com/golang/go/src go1.25.1 strings/Builder#WriteByte().(' ')
122 handlersRaw.WriteStringscip-go gomod github.com/golang/go/src go1.25.1 strings/Builder#WriteString().(string(hr))
123 }
124 handlersRaw.WriteBytescip-go gomod github.com/golang/go/src go1.25.1 strings/Builder#WriteByte().(']')
125
126 return fmtscip-go gomod github.com/golang/go/src go1.25.1 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.25.1 fmt/Sprintf().(`{Group:"%s" MatcherSetsRaw:%s HandlersRaw:%s Terminal:%t}`,
127 r.Groupscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Route#Group., r.MatcherSetsRawscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Route#MatcherSetsRaw., handlersRaw.Stringscip-go gomod github.com/golang/go/src go1.25.1 strings/Builder#String().(), r.Terminalscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Route#Terminal.)
128 }
129
130 // Provision sets up both the matchers and handlers in the route.
131 func (r *Routescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Route#) Provisionscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Route#Provision().(ctx 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#, metrics *Metricsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Metrics#) error {
132 err := r.ProvisionMatchersscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Route#ProvisionMatchers().(ctx)
133 if err != nil {
134 return err
135 }
136 return r.ProvisionHandlersscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Route#ProvisionHandlers().(ctx, metrics)
137 }
138
139 // ProvisionMatchers sets up all the matchers by loading the
140 // matcher modules. Only call this method directly if you need
141 // to set up matchers and handlers separately without having
142 // to provision a second time; otherwise use Provision instead.
143 func (r *Routescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Route#) ProvisionMatchersscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Route#ProvisionMatchers().(ctx 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 {
144 // matchers
145 matchersIface, err := ctx.LoadModulescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/Context#LoadModule().(r, "MatcherSetsRaw")
146 if err != nil {
147 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().("loading matcher modules: %v", err)
148 }
149 err = r.MatcherSetsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Route#MatcherSets..FromInterfacescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/MatcherSets#FromInterface().(matchersIface)
150 if err != nil {
151 return err
152 }
153 return nil
154 }
155
156 // ProvisionHandlers sets up all the handlers by loading the
157 // handler modules. Only call this method directly if you need
158 // to set up matchers and handlers separately without having
159 // to provision a second time; otherwise use Provision instead.
160 func (r *Routescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Route#) ProvisionHandlersscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Route#ProvisionHandlers().(ctx 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#, metrics *Metricsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Metrics#) error {
161 handlersIface, err := ctx.LoadModulescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/Context#LoadModule().(r, "HandlersRaw")
162 if err != nil {
163 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().("loading handler modules: %v", err)
164 }
165 for _, handler := range handlersIface.([]any) {
166 r.Handlersscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Route#Handlers. = append(r.Handlersscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Route#Handlers., handler.(MiddlewareHandlerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/MiddlewareHandler#))
167 }
168
169 // Store metrics info for route-level instrumentation (applied once
170 // per route in wrapRoute, instead of per-handler which was redundant).
171 r.metricsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Route#metrics. = metrics
172 r.metricsCtxscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Route#metricsCtx. = ctx
173 if len(r.Handlersscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Route#Handlers.) > 0 {
174 r.handlerNamescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Route#handlerName. = caddyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/.GetModuleNamescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/GetModuleName().(r.Handlersscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Route#Handlers.[0])
175 }
176
177 // Make ProvisionHandlers idempotent by clearing the middleware field
178 r.middlewarescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Route#middleware. = []Middlewarescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Middleware#{}
179
180 // pre-compile the middleware handler chain
181 for _, midhandler := range r.Handlersscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Route#Handlers. {
182 r.middlewarescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Route#middleware. = append(r.middlewarescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Route#middleware., wrapMiddlewarescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/wrapMiddleware().(ctx, midhandler))
183 }
184 return nil
185 }
186
187 // Compile prepares a middleware chain from the route list.
188 // This should only be done once during the request, just
189 // before the middleware chain is executed.
190 func (r Routescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Route#) Compilescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Route#Compile().(next Handlerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Handler#) Handlerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Handler# {
191 return wrapRoutescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/wrapRoute().(r)(next)
192 }
193
194 // RouteList is a list of server routes that can
195 // create a middleware chain.
196 type RouteListscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/RouteList# []Routescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Route#
197
198 // Provision sets up both the matchers and handlers in the routes.
199 func (routes RouteListscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/RouteList#) Provisionscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/RouteList#Provision().(ctx 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 {
200 err := routes.ProvisionMatchersscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/RouteList#ProvisionMatchers().(ctx)
201 if err != nil {
202 return err
203 }
204 return routes.ProvisionHandlersscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/RouteList#ProvisionHandlers().(ctx, nil)
205 }
206
207 // ProvisionMatchers sets up all the matchers by loading the
208 // matcher modules. Only call this method directly if you need
209 // to set up matchers and handlers separately without having
210 // to provision a second time; otherwise use Provision instead.
211 func (routes RouteListscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/RouteList#) ProvisionMatchersscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/RouteList#ProvisionMatchers().(ctx 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 {
212 for i := range routes {
213 err := routes[i].ProvisionMatchersscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Route#ProvisionMatchers().(ctx)
214 if err != nil {
215 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().("route %d: %v", i, err)
216 }
217 }
218 return nil
219 }
220
221 // ProvisionHandlers sets up all the handlers by loading the
222 // handler modules. Only call this method directly if you need
223 // to set up matchers and handlers separately without having
224 // to provision a second time; otherwise use Provision instead.
225 func (routes RouteListscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/RouteList#) ProvisionHandlersscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/RouteList#ProvisionHandlers().(ctx 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#, metrics *Metricsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Metrics#) error {
226 for i := range routes {
227 err := routes[i].ProvisionHandlersscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Route#ProvisionHandlers().(ctx, metrics)
228 if err != nil {
229 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().("route %d: %v", i, err)
230 }
231 }
232 return nil
233 }
234
235 // Compile prepares a middleware chain from the route list.
236 // This should only be done either once during provisioning
237 // for top-level routes, or on each request just before the
238 // middleware chain is executed for subroutes.
239 func (routes RouteListscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/RouteList#) Compilescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/RouteList#Compile().(next Handlerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Handler#) Handlerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Handler# {
240 mid := make([]Middlewarescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Middleware#, 0, len(routes))
241 for _, route := range routes {
242 mid = append(mid, wrapRoutescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/wrapRoute().(route))
243 }
244 stack := next
245 for _, middleware := range slicesscip-go gomod github.com/golang/go/src go1.25.1 slices/.Backwardscip-go gomod github.com/golang/go/src go1.25.1 slices/Backward().(mid) {
246 stack = middleware(stack)
247 }
248 return stack
249 }
250
251 // wrapRoute wraps route with a middleware and handler so that it can
252 // be chained in and defer evaluation of its matchers to request-time.
253 // Like wrapMiddleware, it is vital that this wrapping takes place in
254 // its own stack frame so as to not overwrite the reference to the
255 // intended route by looping and changing the reference each time.
256 func wrapRoutescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/wrapRoute().(route Routescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Route#) Middlewarescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Middleware# {
257 return func(next Handlerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Handler#) Handlerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Handler# {
258 return HandlerFuncscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/HandlerFunc#(func(rw 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#, req *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#) error {
259 // TODO: Update this comment, it seems we've moved the copy into the handler?
260 // copy the next handler (it's an interface, so it's just
261 // a very lightweight copy of a pointer); this is important
262 // because this is a closure to the func below, which
263 // re-assigns the value as it compiles the middleware stack;
264 // if we don't make this copy, we'd affect the underlying
265 // pointer for all future request (yikes); we could
266 // alternatively solve this by moving the func below out of
267 // this closure and into a standalone package-level func,
268 // but I just thought this made more sense
269 nextCopy := next
270
271 // route must match at least one of the matcher sets
272 matches, err := route.MatcherSetsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Route#MatcherSets..AnyMatchWithErrorscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/MatcherSets#AnyMatchWithError().(req)
273 if err != nil {
274 // allow matchers the opportunity to short circuit
275 // the request and trigger the error handling chain
276 return err
277 }
278 if !matches {
279 // call the next handler, and skip this one,
280 // since the matcher didn't match
281 return nextCopy.ServeHTTPscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Handler#ServeHTTP.(rw, req)
282 }
283
284 // if route is part of a group, ensure only the
285 // first matching route in the group is applied
286 if route.Groupscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Route#Group. != "" {
287 groups := req.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().(routeGroupCtxKeyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/routeGroupCtxKey.).(map[string]struct{})
288
289 if _, ok := groups[route.Groupscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Route#Group.]; ok {
290 // this group has already been
291 // satisfied by a matching route
292 return nextCopy.ServeHTTPscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Handler#ServeHTTP.(rw, req)
293 }
294
295 // this matching route satisfies the group
296 groups[route.Groupscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Route#Group.] = struct{}{}
297 }
298
299 // make terminal routes terminate
300 if route.Terminalscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Route#Terminal. {
301 if _, ok := req.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().(ErrorCtxKeyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/ErrorCtxKey.).(error); ok {
302 nextCopy = errorEmptyHandlerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/errorEmptyHandler.
303 } else {
304 nextCopy = emptyHandlerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/emptyHandler.
305 }
306 }
307
308 // compile this route's handler stack
309 for _, middleware := range slicesscip-go gomod github.com/golang/go/src go1.25.1 slices/.Backwardscip-go gomod github.com/golang/go/src go1.25.1 slices/Backward().(route.middlewarescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Route#middleware.) {
310 nextCopy = middleware(nextCopy)
311 }
312
313 // Apply metrics instrumentation once for the entire route,
314 // rather than wrapping each individual handler. This avoids
315 // redundant metrics collection that caused significant CPU
316 // overhead (see issue #4644).
317 if route.metricsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Route#metrics. != nil {
318 nextCopy = newMetricsInstrumentedRoutescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/newMetricsInstrumentedRoute().(
319 route.metricsCtxscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Route#metricsCtx., route.handlerNamescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Route#handlerName., nextCopy, route.metricsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Route#metrics.,
320 )
321 }
322
323 return nextCopy.ServeHTTPscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Handler#ServeHTTP.(rw, req)
324 })
325 }
326 }
327
328 // wrapMiddleware wraps mh such that it can be correctly
329 // appended to a list of middleware in preparation for
330 // compiling into a handler chain.
331 func wrapMiddlewarescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/wrapMiddleware().(ctx 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#, mh MiddlewareHandlerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/MiddlewareHandler#) Middlewarescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Middleware# {
332 return func(next Handlerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Handler#) Handlerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Handler# {
333 return HandlerFuncscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/HandlerFunc#(func(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#) error {
334 // EXPERIMENTAL: Trace each module that gets invoked
335 if server, ok := 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().(ServerCtxKeyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/ServerCtxKey.).(*Serverscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Server#); ok && server != nil {
336 server.logTracescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Server#logTrace().(mh)
337 }
338 return mh.ServeHTTPscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/MiddlewareHandler#ServeHTTP.(w, r, next)
339 })
340 }
341 }
342
343 // MatcherSet is a set of matchers which
344 // must all match in order for the request
345 // to be matched successfully.
346 type MatcherSetscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/MatcherSet# []any
347
348 // Match returns true if the request matches all
349 // matchers in mset or if there are no matchers.
350 func (mset MatcherSetscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/MatcherSet#) Matchscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/MatcherSet#Match().(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#) bool {
351 for _, m := range mset {
352 if me, ok := m.(RequestMatcherWithErrorscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/RequestMatcherWithError#); ok {
353 match, _ := me.MatchWithErrorscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/RequestMatcherWithError#MatchWithError.(r)
354 if !match {
355 return false
356 }
357 continue
358 }
359 if me, ok := m.(RequestMatcherscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/RequestMatcher#); ok {
360 if !me.Matchscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/RequestMatcher#Match.(r) {
361 return false
362 }
363 continue
364 }
365 return false
366 }
367 return true
368 }
369
370 // MatchWithError returns true if r matches m.
371 func (mset MatcherSetscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/MatcherSet#) MatchWithErrorscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/MatcherSet#MatchWithError().(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#) (bool, error) {
372 for _, m := range mset {
373 if me, ok := m.(RequestMatcherWithErrorscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/RequestMatcherWithError#); ok {
374 match, err := me.MatchWithErrorscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/RequestMatcherWithError#MatchWithError.(r)
375 if err != nil || !match {
376 return match, err
377 }
378 continue
379 }
380 if me, ok := m.(RequestMatcherscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/RequestMatcher#); ok {
381 if !me.Matchscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/RequestMatcher#Match.(r) {
382 // for backwards compatibility
383 err, ok := GetVarscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/GetVar().(r.Contextscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Request#Context().(), MatcherErrorVarKeyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/MatcherErrorVarKey.).(error)
384 if ok {
385 // clear out the error from context since we've consumed it
386 SetVarscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/SetVar().(r.Contextscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Request#Context().(), MatcherErrorVarKeyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/MatcherErrorVarKey., nil)
387 return false, err
388 }
389 return false, nil
390 }
391 continue
392 }
393 return false, 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().("matcher is not a RequestMatcher or RequestMatcherWithError: %#v", m)
394 }
395 return true, nil
396 }
397
398 // RawMatcherSets is a group of matcher sets
399 // in their raw, JSON form.
400 type RawMatcherSetsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/RawMatcherSets# []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#
401
402 // MatcherSets is a group of matcher sets capable
403 // of checking whether a request matches any of
404 // the sets.
405 type MatcherSetsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/MatcherSets# []MatcherSetscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/MatcherSet#
406
407 // AnyMatch returns true if req matches any of the
408 // matcher sets in ms or if there are no matchers,
409 // in which case the request always matches.
410 //
411 // Deprecated: Use AnyMatchWithError instead.
412 func (ms MatcherSetsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/MatcherSets#) AnyMatchscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/MatcherSets#AnyMatch().(req *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#) bool {
413 for _, m := range ms {
414 match, err := m.MatchWithErrorscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/MatcherSet#MatchWithError().(req)
415 if err != nil {
416 SetVarscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/SetVar().(req.Contextscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Request#Context().(), MatcherErrorVarKeyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/MatcherErrorVarKey., err)
417 return false
418 }
419 if match {
420 return match
421 }
422 }
423 return len(ms) == 0
424 }
425
426 // AnyMatchWithError returns true if req matches any of the
427 // matcher sets in ms or if there are no matchers, in which
428 // case the request always matches. If any matcher returns
429 // an error, we cut short and return the error.
430 func (ms MatcherSetsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/MatcherSets#) AnyMatchWithErrorscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/MatcherSets#AnyMatchWithError().(req *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#) (bool, error) {
431 for _, m := range ms {
432 match, err := m.MatchWithErrorscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/MatcherSet#MatchWithError().(req)
433 if err != nil || match {
434 return match, err
435 }
436 }
437 return len(ms) == 0, nil
438 }
439
440 // FromInterface fills ms from an 'any' value obtained from LoadModule.
441 func (ms *MatcherSetsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/MatcherSets#) FromInterfacescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/MatcherSets#FromInterface().(matcherSets any) error {
442 for _, matcherSetIfaces := range matcherSets.([]map[string]any) {
443 var matcherSet MatcherSetscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/MatcherSet#
444 for _, matcher := range matcherSetIfaces {
445 if m, ok := matcher.(RequestMatcherWithErrorscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/RequestMatcherWithError#); ok {
446 matcherSet = append(matcherSet, m)
447 continue
448 }
449 if m, ok := matcher.(RequestMatcherscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/RequestMatcher#); ok {
450 matcherSet = append(matcherSet, m)
451 continue
452 }
453 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().("decoded module is not a RequestMatcher or RequestMatcherWithError: %#v", matcher)
454 }
455 *ms = append(*ms, matcherSet)
456 }
457 return nil
458 }
459
460 // TODO: Is this used?
461 func (ms MatcherSetsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/MatcherSets#) Stringscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/MatcherSets#String().() string {
462 var result stringsscip-go gomod github.com/golang/go/src go1.25.1 strings/.Builderscip-go gomod github.com/golang/go/src go1.25.1 strings/Builder#
463 result.WriteBytescip-go gomod github.com/golang/go/src go1.25.1 strings/Builder#WriteByte().('[')
464 for _, matcherSet := range ms {
465 for _, matcher := range matcherSet {
466 fmtscip-go gomod github.com/golang/go/src go1.25.1 fmt/.Fprintfscip-go gomod github.com/golang/go/src go1.25.1 fmt/Fprintf().(&result, " %#v", matcher)
467 }
468 }
469 result.WriteBytescip-go gomod github.com/golang/go/src go1.25.1 strings/Builder#WriteByte().(']')
470 return result.Stringscip-go gomod github.com/golang/go/src go1.25.1 strings/Builder#String().()
471 }
472
473 var routeGroupCtxKeyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/routeGroupCtxKey. = caddyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/.CtxKeyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/CtxKey#("route_group")