Debian Codegraph @

modules/caddyhttp/rewrite/rewrite.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 rewritescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/rewrite`/
16
17 import (
18 "fmtscip-go gomod github.com/golang/go/src go1.25.1 fmt/"
19 "net/httpscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/"
20 "net/urlscip-go gomod github.com/golang/go/src go1.25.1 `net/url`/"
21 "regexpscip-go gomod github.com/golang/go/src go1.25.1 regexp/"
22 "strconvscip-go gomod github.com/golang/go/src go1.25.1 strconv/"
23 "stringsscip-go gomod github.com/golang/go/src go1.25.1 strings/"
24
25 "go.uber.org/zapscip-go gomod go.uber.org/zap v1.28.0 `go.uber.org/zap`/"
26
27 "github.com/caddyserver/caddy/v2scip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/"
28 "github.com/caddyserver/caddy/v2/modules/caddyhttpscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/"
29 )
30
31 func initscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/rewrite`/init().() {
32 caddyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/.RegisterModulescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/RegisterModule().(Rewritescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/rewrite`/Rewrite#{})
33 }
34
35 // Rewrite is a middleware which can rewrite/mutate HTTP requests.
36 //
37 // The Method and URI properties are "setters" (the request URI
38 // will be overwritten with the given values). Other properties are
39 // "modifiers" (they modify existing values in a differentiable
40 // way). It is atypical to combine the use of setters and
41 // modifiers in a single rewrite.
42 //
43 // To ensure consistent behavior, prefix and suffix stripping is
44 // performed in the URL-decoded (unescaped, normalized) space by
45 // default except for the specific bytes where an escape sequence
46 // is used in the prefix or suffix pattern.
47 //
48 // For all modifiers, paths are cleaned before being modified so that
49 // multiple, consecutive slashes are collapsed into a single slash,
50 // and dot elements are resolved and removed. In the special case
51 // of a prefix, suffix, or substring containing "//" (repeated slashes),
52 // slashes will not be merged while cleaning the path so that
53 // the rewrite can be interpreted literally.
54 type Rewritescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/rewrite`/Rewrite# struct {
55 // Changes the request's HTTP verb.
56 Methodscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/rewrite`/Rewrite#Method. string `json:"method,omitempty"`
57
58 // Changes the request's URI, which consists of path and query string.
59 // Only components of the URI that are specified will be changed.
60 // For example, a value of "/foo.html" or "foo.html" will only change
61 // the path and will preserve any existing query string. Similarly, a
62 // value of "?a=b" will only change the query string and will not affect
63 // the path. Both can also be changed: "/foo?a=b" - this sets both the
64 // path and query string at the same time.
65 //
66 // You can also use placeholders. For example, to preserve the existing
67 // query string, you might use: "?{http.request.uri.query}&a=b". Any
68 // key-value pairs you add to the query string will not overwrite
69 // existing values (individual pairs are append-only).
70 //
71 // To clear the query string, explicitly set an empty one: "?"
72 URIscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/rewrite`/Rewrite#URI. string `json:"uri,omitempty"`
73
74 // Strips the given prefix from the beginning of the URI path.
75 // The prefix should be written in normalized (unescaped) form,
76 // but if an escaping (`%xx`) is used, the path will be required
77 // to have that same escape at that position in order to match.
78 StripPathPrefixscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/rewrite`/Rewrite#StripPathPrefix. string `json:"strip_path_prefix,omitempty"`
79
80 // Strips the given suffix from the end of the URI path.
81 // The suffix should be written in normalized (unescaped) form,
82 // but if an escaping (`%xx`) is used, the path will be required
83 // to have that same escape at that position in order to match.
84 StripPathSuffixscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/rewrite`/Rewrite#StripPathSuffix. string `json:"strip_path_suffix,omitempty"`
85
86 // Performs substring replacements on the URI.
87 URISubstringscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/rewrite`/Rewrite#URISubstring. []substrReplacerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/rewrite`/substrReplacer# `json:"uri_substring,omitempty"`
88
89 // Performs regular expression replacements on the URI path.
90 PathRegexpscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/rewrite`/Rewrite#PathRegexp. []*regexReplacerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/rewrite`/regexReplacer# `json:"path_regexp,omitempty"`
91
92 // Mutates the query string of the URI.
93 Queryscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/rewrite`/Rewrite#Query. *queryOpsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/rewrite`/queryOps# `json:"query,omitempty"`
94
95 loggerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/rewrite`/Rewrite#logger. *zapscip-go gomod go.uber.org/zap v1.28.0 `go.uber.org/zap`/.Loggerscip-go gomod go.uber.org/zap v1.28.0 `go.uber.org/zap`/Logger#
96 }
97
98 // CaddyModule returns the Caddy module information.
99 func (Rewritescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/rewrite`/Rewrite#) CaddyModulescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/rewrite`/Rewrite#CaddyModule().() caddyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/.ModuleInfoscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/ModuleInfo# {
100 return caddyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/.ModuleInfoscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/ModuleInfo#{
101 IDscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/ModuleInfo#ID.: "http.handlers.rewrite",
102 Newscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/ModuleInfo#New.: func() caddyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/.Modulescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/Module# { return new(Rewritescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/rewrite`/Rewrite#) },
103 }
104 }
105
106 // Provision sets up rewr.
107 func (rewr *Rewritescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/rewrite`/Rewrite#) Provisionscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/rewrite`/Rewrite#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 {
108 rewr.loggerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/rewrite`/Rewrite#logger. = ctx.Loggerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/Context#Logger().()
109
110 for i, rep := range rewr.PathRegexpscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/rewrite`/Rewrite#PathRegexp. {
111 if rep.Findscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/rewrite`/regexReplacer#Find. == "" {
112 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().("path_regexp find cannot be empty")
113 }
114 re, err := regexpscip-go gomod github.com/golang/go/src go1.25.1 regexp/.Compilescip-go gomod github.com/golang/go/src go1.25.1 regexp/Compile().(rep.Findscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/rewrite`/regexReplacer#Find.)
115 if err != nil {
116 return fmtscip-go gomod github.com/golang/go/src go1.25.1 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.1 fmt/Errorf().("compiling regular expression %d: %v", i, err)
117 }
118 rep.rescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/rewrite`/regexReplacer#re. = re
119 }
120 if rewr.Queryscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/rewrite`/Rewrite#Query. != nil {
121 for _, replacementOp := range rewr.Queryscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/rewrite`/Rewrite#Query..Replacescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/rewrite`/queryOps#Replace. {
122 err := replacementOp.Provisionscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/rewrite`/queryOpsReplacement#Provision().(ctx)
123 if err != nil {
124 return fmtscip-go gomod github.com/golang/go/src go1.25.1 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.1 fmt/Errorf().("compiling regular expression %s in query rewrite replace operation: %v", replacementOp.SearchRegexpscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/rewrite`/queryOpsReplacement#SearchRegexp., err)
125 }
126 }
127 }
128
129 return nil
130 }
131
132 func (rewr Rewritescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/rewrite`/Rewrite#) ServeHTTPscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/rewrite`/Rewrite#ServeHTTP().(w httpscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/.ResponseWriterscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/ResponseWriter#, r *httpscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/.Requestscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Request#, next caddyhttpscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/.Handlerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Handler#) error {
133 repl := r.Contextscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Request#Context().().Valuescip-go gomod github.com/golang/go/src go1.25.1 context/Context#Value().(caddyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/.ReplacerCtxKeyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/ReplacerCtxKey.).(*caddyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/.Replacerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/Replacer#)
134 const message = "rewrote request"
135
136 c := rewr.loggerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/rewrite`/Rewrite#logger..Checkscip-go gomod go.uber.org/zap v1.28.0 `go.uber.org/zap`/Logger#Check().(zapscip-go gomod go.uber.org/zap v1.28.0 `go.uber.org/zap`/.DebugLevelscip-go gomod go.uber.org/zap v1.28.0 `go.uber.org/zap`/DebugLevel., message)
137 if c == nil {
138 rewr.Rewritescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/rewrite`/Rewrite#Rewrite().(r, repl)
139 return next.ServeHTTPscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Handler#ServeHTTP.(w, r)
140 }
141
142 changed := rewr.Rewritescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/rewrite`/Rewrite#Rewrite().(r, repl)
143
144 if changed {
145 c.Writescip-go gomod go.uber.org/zap v1.28.0 `go.uber.org/zap/zapcore`/CheckedEntry#Write().(
146 zapscip-go gomod go.uber.org/zap v1.28.0 `go.uber.org/zap`/.Objectscip-go gomod go.uber.org/zap v1.28.0 `go.uber.org/zap`/Object().("request", caddyhttpscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/.LoggableHTTPRequestscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/LoggableHTTPRequest#{Requestscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/LoggableHTTPRequest#Request.: r}),
147 zapscip-go gomod go.uber.org/zap v1.28.0 `go.uber.org/zap`/.Stringscip-go gomod go.uber.org/zap v1.28.0 `go.uber.org/zap`/String().("method", r.Methodscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Request#Method.),
148 zapscip-go gomod go.uber.org/zap v1.28.0 `go.uber.org/zap`/.Stringscip-go gomod go.uber.org/zap v1.28.0 `go.uber.org/zap`/String().("uri", r.RequestURIscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Request#RequestURI.),
149 )
150 }
151
152 return next.ServeHTTPscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Handler#ServeHTTP.(w, r)
153 }
154
155 // rewrite performs the rewrites on r using repl, which should
156 // have been obtained from r, but is passed in for efficiency.
157 // It returns true if any changes were made to r.
158 func (rewr Rewritescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/rewrite`/Rewrite#) Rewritescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/rewrite`/Rewrite#Rewrite().(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#, repl *caddyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/.Replacerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/Replacer#) bool {
159 oldMethod := r.Methodscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Request#Method.
160 oldURI := r.RequestURIscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Request#RequestURI.
161
162 // method
163 if rewr.Methodscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/rewrite`/Rewrite#Method. != "" {
164 r.Methodscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Request#Method. = stringsscip-go gomod github.com/golang/go/src go1.25.1 strings/.ToUpperscip-go gomod github.com/golang/go/src go1.25.1 strings/ToUpper().(repl.ReplaceAllscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/Replacer#ReplaceAll().(rewr.Methodscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/rewrite`/Rewrite#Method., ""))
165 }
166
167 // uri (path, query string and... fragment, because why not)
168 if uri := rewr.URIscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/rewrite`/Rewrite#URI.; uri != "" {
169 // find the bounds of each part of the URI that exist
170 pathStart, qsStart, fragStart := -1, -1, -1
171 pathEnd, qsEnd := -1, -1
172 loop:
173 for i, ch := range uri {
174 switch {
175 case ch == '?' && qsStart < 0:
176 pathEnd, qsStart = i, i+1
177 case ch == '#' && fragStart < 0: // everything after fragment is fragment (very clear in RFC 3986 section 4.2)
178 if qsStart < 0 {
179 pathEnd = i
180 } else {
181 qsEnd = i
182 }
183 fragStart = i + 1
184 break loop
185 case pathStart < 0 && qsStart < 0:
186 pathStart = i
187 }
188 }
189 if pathStart >= 0 && pathEnd < 0 {
190 pathEnd = len(uri)
191 }
192 if qsStart >= 0 && qsEnd < 0 {
193 qsEnd = len(uri)
194 }
195
196 // isolate the three main components of the URI
197 var path, query, frag string
198 if pathStart > -1 {
199 path = uri[pathStart:pathEnd]
200 }
201 if qsStart > -1 {
202 query = uri[qsStart:qsEnd]
203 }
204 if fragStart > -1 {
205 frag = uri[fragStart:]
206 }
207
208 // build components which are specified, and store them
209 // in a temporary variable so that they all read the
210 // same version of the URI
211 var newPath, newQuery, newFrag string
212
213 if path != "" {
214 path = escapePathPlaceholdersscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/rewrite`/escapePathPlaceholders().(path, r, repl)
215 newPath = repl.ReplaceAllscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/Replacer#ReplaceAll().(path, "")
216 }
217
218 // before continuing, we need to check if a query string
219 // snuck into the path component during replacements
220 if before, after, found := stringsscip-go gomod github.com/golang/go/src go1.25.1 strings/.Cutscip-go gomod github.com/golang/go/src go1.25.1 strings/Cut().(newPath, "?"); found {
221 // recompute; new path contains a query string
222 var injectedQuery string
223 newPath, injectedQuery = before, after
224 // don't overwrite explicitly-configured query string
225 if query == "" {
226 // the injected query came from the first-pass placeholder
227 // expansion above, which means any '{' or '}' bytes in it
228 // must have come from replacement values (e.g. a request
229 // header), not from operator-written placeholder syntax.
230 // escape them so buildQueryString does not re-expand them,
231 // which would allow attacker input like {env.SECRET} to be
232 // evaluated (see GHSA-j8px-rmrx-76h9).
233 injectedQuery = stringsscip-go gomod github.com/golang/go/src go1.25.1 strings/.ReplaceAllscip-go gomod github.com/golang/go/src go1.25.1 strings/ReplaceAll().(injectedQuery, "{", "%7B")
234 injectedQuery = stringsscip-go gomod github.com/golang/go/src go1.25.1 strings/.ReplaceAllscip-go gomod github.com/golang/go/src go1.25.1 strings/ReplaceAll().(injectedQuery, "}", "%7D")
235 query = injectedQuery
236 }
237 }
238
239 if query != "" {
240 newQuery = buildQueryStringscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/rewrite`/buildQueryString().(query, repl)
241 }
242 if frag != "" {
243 newFrag = repl.ReplaceAllscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/Replacer#ReplaceAll().(frag, "")
244 }
245
246 // update the URI with the new components
247 // only after building them
248 if pathStart >= 0 {
249 if path, err := urlscip-go gomod github.com/golang/go/src go1.25.1 `net/url`/.PathUnescapescip-go gomod github.com/golang/go/src go1.25.1 `net/url`/PathUnescape().(newPath); err != nil {
250 r.URLscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Request#URL..Pathscip-go gomod github.com/golang/go/src go1.25.1 `net/url`/URL#Path. = newPath
251 } else {
252 r.URLscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Request#URL..Pathscip-go gomod github.com/golang/go/src go1.25.1 `net/url`/URL#Path. = path
253 }
254 r.URLscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Request#URL..RawPathscip-go gomod github.com/golang/go/src go1.25.1 `net/url`/URL#RawPath. = "" // force recomputing when EscapedPath() is called
255 }
256 if qsStart >= 0 {
257 r.URLscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Request#URL..RawQueryscip-go gomod github.com/golang/go/src go1.25.1 `net/url`/URL#RawQuery. = newQuery
258 }
259 if fragStart >= 0 {
260 r.URLscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Request#URL..Fragmentscip-go gomod github.com/golang/go/src go1.25.1 `net/url`/URL#Fragment. = newFrag
261 }
262 }
263
264 // strip path prefix or suffix
265 if rewr.StripPathPrefixscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/rewrite`/Rewrite#StripPathPrefix. != "" {
266 prefix := repl.ReplaceAllscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/Replacer#ReplaceAll().(rewr.StripPathPrefixscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/rewrite`/Rewrite#StripPathPrefix., "")
267 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().(prefix, "/") {
268 prefix = "/" + prefix
269 }
270 mergeSlashes := !stringsscip-go gomod github.com/golang/go/src go1.25.1 strings/.Containsscip-go gomod github.com/golang/go/src go1.25.1 strings/Contains().(prefix, "//")
271 changePathscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/rewrite`/changePath().(r, func(escapedPath string) string {
272 escapedPath = caddyhttpscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/.CleanPathscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/CleanPath().(escapedPath, mergeSlashes)
273 return trimPathPrefixscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/rewrite`/trimPathPrefix().(escapedPath, prefix)
274 })
275 }
276 if rewr.StripPathSuffixscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/rewrite`/Rewrite#StripPathSuffix. != "" {
277 suffix := repl.ReplaceAllscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/Replacer#ReplaceAll().(rewr.StripPathSuffixscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/rewrite`/Rewrite#StripPathSuffix., "")
278 mergeSlashes := !stringsscip-go gomod github.com/golang/go/src go1.25.1 strings/.Containsscip-go gomod github.com/golang/go/src go1.25.1 strings/Contains().(suffix, "//")
279 changePathscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/rewrite`/changePath().(r, func(escapedPath string) string {
280 escapedPath = caddyhttpscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/.CleanPathscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/CleanPath().(escapedPath, mergeSlashes)
281 return reversescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/rewrite`/reverse().(trimPathPrefixscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/rewrite`/trimPathPrefix().(reversescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/rewrite`/reverse().(escapedPath), reversescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/rewrite`/reverse().(suffix)))
282 })
283 }
284
285 // substring replacements in URI
286 for _, rep := range rewr.URISubstringscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/rewrite`/Rewrite#URISubstring. {
287 rep.doscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/rewrite`/substrReplacer#do().(r, repl)
288 }
289
290 // regular expression replacements on the path
291 for _, rep := range rewr.PathRegexpscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/rewrite`/Rewrite#PathRegexp. {
292 rep.doscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/rewrite`/regexReplacer#do().(r, repl)
293 }
294
295 // apply query operations
296 if rewr.Queryscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/rewrite`/Rewrite#Query. != nil {
297 rewr.Queryscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/rewrite`/Rewrite#Query..doscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/rewrite`/queryOps#do().(r, repl)
298 }
299
300 // update the encoded copy of the URI
301 r.RequestURIscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Request#RequestURI. = r.URLscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Request#URL..RequestURIscip-go gomod github.com/golang/go/src go1.25.1 `net/url`/URL#RequestURI().()
302
303 // return true if anything changed
304 return r.Methodscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Request#Method. != oldMethod || r.RequestURIscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Request#RequestURI. != oldURI
305 }
306
307 func escapePathPlaceholdersscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/rewrite`/escapePathPlaceholders().(path string, 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#, repl *caddyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/.Replacerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/Replacer#) string {
308 // Replace path-valued placeholders in escaped form before the URI is parsed,
309 // otherwise literal '?' and '%' bytes from the path can be interpreted as URI
310 // delimiters or percent-escape sequences during the rewrite.
311 pathPlaceholder := "{http.request.uri.path}"
312 if stringsscip-go gomod github.com/golang/go/src go1.25.1 strings/.Containsscip-go gomod github.com/golang/go/src go1.25.1 strings/Contains().(path, pathPlaceholder) {
313 path = stringsscip-go gomod github.com/golang/go/src go1.25.1 strings/.ReplaceAllscip-go gomod github.com/golang/go/src go1.25.1 strings/ReplaceAll().(path, pathPlaceholder, r.URLscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Request#URL..EscapedPathscip-go gomod github.com/golang/go/src go1.25.1 `net/url`/URL#EscapedPath().())
314 }
315
316 fileMatchRelativePlaceholder := "{http.matchers.file.relative}"
317 if stringsscip-go gomod github.com/golang/go/src go1.25.1 strings/.Containsscip-go gomod github.com/golang/go/src go1.25.1 strings/Contains().(path, fileMatchRelativePlaceholder) {
318 if val, ok := repl.Getscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/Replacer#Get().("http.matchers.file.relative"); ok {
319 if relativePath, ok := val.(string); ok {
320 path = stringsscip-go gomod github.com/golang/go/src go1.25.1 strings/.ReplaceAllscip-go gomod github.com/golang/go/src go1.25.1 strings/ReplaceAll().(path, fileMatchRelativePlaceholder, escapePathPreservingSlashesscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/rewrite`/escapePathPreservingSlashes().(relativePath))
321 }
322 }
323 }
324
325 return path
326 }
327
328 func escapePathPreservingSlashesscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/rewrite`/escapePathPreservingSlashes().(path string) string {
329 return stringsscip-go gomod github.com/golang/go/src go1.25.1 strings/.ReplaceAllscip-go gomod github.com/golang/go/src go1.25.1 strings/ReplaceAll().(urlscip-go gomod github.com/golang/go/src go1.25.1 `net/url`/.PathEscapescip-go gomod github.com/golang/go/src go1.25.1 `net/url`/PathEscape().(path), "%2F", "/")
330 }
331
332 // buildQueryString takes an input query string and
333 // performs replacements on each component, returning
334 // the resulting query string. This function appends
335 // duplicate keys rather than replaces.
336 func buildQueryStringscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/rewrite`/buildQueryString().(qs string, repl *caddyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/.Replacerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/Replacer#) string {
337 var sb 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#
338
339 // first component must be key, which is the same
340 // as if we just wrote a value in previous iteration
341 wroteVal := true
342
343 for len(qs) > 0 {
344 // determine the end of this component, which will be at
345 // the next equal sign or ampersand, whichever comes first
346 nextEq, nextAmp := stringsscip-go gomod github.com/golang/go/src go1.25.1 strings/.Indexscip-go gomod github.com/golang/go/src go1.25.1 strings/Index().(qs, "="), stringsscip-go gomod github.com/golang/go/src go1.25.1 strings/.Indexscip-go gomod github.com/golang/go/src go1.25.1 strings/Index().(qs, "&")
347 ampIsNext := nextAmp >= 0 && (nextAmp < nextEq || nextEq < 0)
348 end := len(qs) // assume no delimiter remains...
349 if ampIsNext {
350 end = nextAmp // ...unless ampersand is first...
351 } else if nextEq >= 0 && (nextEq < nextAmp || nextAmp < 0) {
352 end = nextEq // ...or unless equal is first.
353 }
354
355 // consume the component and write the result
356 comp := qs[:end]
357 comp, _ = repl.ReplaceFuncscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/Replacer#ReplaceFunc().(comp, func(name string, val any) (any, error) {
358 if name == "http.request.uri.query" && wroteVal {
359 return val, nil // already escaped
360 }
361 var valStr string
362 switch v := val.(type) {
363 case string:
364 valStr = v
365 case fmtscip-go gomod github.com/golang/go/src go1.25.1 fmt/.Stringerscip-go gomod github.com/golang/go/src go1.25.1 fmt/Stringer#:
366 valStr = v.Stringscip-go gomod github.com/golang/go/src go1.25.1 fmt/Stringer#String().()
367 case int:
368 valStr = 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().(v)
369 default:
370 valStr = 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().("%+v", v)
371 }
372 return urlscip-go gomod github.com/golang/go/src go1.25.1 `net/url`/.QueryEscapescip-go gomod github.com/golang/go/src go1.25.1 `net/url`/QueryEscape().(valStr), nil
373 })
374 if end < len(qs) {
375 end++ // consume delimiter
376 }
377 qs = qs[end:]
378
379 // if previous iteration wrote a value,
380 // that means we are writing a key
381 if wroteVal {
382 if sb.Lenscip-go gomod github.com/golang/go/src go1.25.1 strings/Builder#Len().() > 0 && len(comp) > 0 {
383 sb.WriteRunescip-go gomod github.com/golang/go/src go1.25.1 strings/Builder#WriteRune().('&')
384 }
385 } else {
386 sb.WriteRunescip-go gomod github.com/golang/go/src go1.25.1 strings/Builder#WriteRune().('=')
387 }
388 sb.WriteStringscip-go gomod github.com/golang/go/src go1.25.1 strings/Builder#WriteString().(comp)
389
390 // remember for the next iteration that we just wrote a value,
391 // which means the next iteration MUST write a key
392 wroteVal = ampIsNext
393 }
394
395 return sb.Stringscip-go gomod github.com/golang/go/src go1.25.1 strings/Builder#String().()
396 }
397
398 // trimPathPrefix is like strings.TrimPrefix, but customized for advanced URI
399 // path prefix matching. The string prefix will be trimmed from the beginning
400 // of escapedPath if escapedPath starts with prefix. Rather than a naive 1:1
401 // comparison of each byte to determine if escapedPath starts with prefix,
402 // both strings are iterated in lock-step, and if prefix has a '%' encoding
403 // at a particular position, escapedPath must also have the same encoding
404 // representation for that character. In other words, if the prefix string
405 // uses the escaped form for a character, escapedPath must literally use the
406 // same escape at that position. Otherwise, all character comparisons are
407 // performed in normalized/unescaped space.
408 func trimPathPrefixscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/rewrite`/trimPathPrefix().(escapedPath, prefix string) string {
409 var iPath, iPrefix int
410 for iPath < len(escapedPath) && iPrefix < len(prefix) {
411 prefixCh := prefix[iPrefix]
412 ch := string(escapedPath[iPath])
413
414 if ch == "%" && prefixCh != '%' && len(escapedPath) >= iPath+3 {
415 var err error
416 ch, err = urlscip-go gomod github.com/golang/go/src go1.25.1 `net/url`/.PathUnescapescip-go gomod github.com/golang/go/src go1.25.1 `net/url`/PathUnescape().(escapedPath[iPath : iPath+3])
417 if err != nil {
418 // should be impossible unless EscapedPath() is returning invalid values!
419 return escapedPath
420 }
421 iPath += 2
422 }
423
424 // prefix comparisons are case-insensitive to consistency with
425 // path matcher, which is case-insensitive for good reasons
426 if !stringsscip-go gomod github.com/golang/go/src go1.25.1 strings/.EqualFoldscip-go gomod github.com/golang/go/src go1.25.1 strings/EqualFold().(ch, string(prefixCh)) {
427 return escapedPath
428 }
429
430 iPath++
431 iPrefix++
432 }
433
434 // if we iterated through the entire prefix, we found it, so trim it
435 if iPath >= len(prefix) {
436 return escapedPath[iPath:]
437 }
438
439 // otherwise we did not find the prefix
440 return escapedPath
441 }
442
443 func reversescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/rewrite`/reverse().(s string) string {
444 r := []rune(s)
445 for i, j := 0, len(r)-1; i < len(r)/2; i, j = i+1, j-1 {
446 r[i], r[j] = r[j], r[i]
447 }
448 return string(r)
449 }
450
451 // substrReplacer describes either a simple and fast substring replacement.
452 type substrReplacerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/rewrite`/substrReplacer# struct {
453 // A substring to find. Supports placeholders.
454 Findscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/rewrite`/substrReplacer#Find. string `json:"find,omitempty"`
455
456 // The substring to replace with. Supports placeholders.
457 Replacescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/rewrite`/substrReplacer#Replace. string `json:"replace,omitempty"`
458
459 // Maximum number of replacements per string.
460 // Set to <= 0 for no limit (default).
461 Limitscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/rewrite`/substrReplacer#Limit. int `json:"limit,omitempty"`
462 }
463
464 // do performs the substring replacement on r.
465 func (rep substrReplacerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/rewrite`/substrReplacer#) doscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/rewrite`/substrReplacer#do().(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#, repl *caddyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/.Replacerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/Replacer#) {
466 if rep.Findscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/rewrite`/substrReplacer#Find. == "" {
467 return
468 }
469
470 lim := rep.Limitscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/rewrite`/substrReplacer#Limit.
471 if lim == 0 {
472 lim = -1
473 }
474
475 find := repl.ReplaceAllscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/Replacer#ReplaceAll().(rep.Findscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/rewrite`/substrReplacer#Find., "")
476 replace := repl.ReplaceAllscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/Replacer#ReplaceAll().(rep.Replacescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/rewrite`/substrReplacer#Replace., "")
477
478 mergeSlashes := !stringsscip-go gomod github.com/golang/go/src go1.25.1 strings/.Containsscip-go gomod github.com/golang/go/src go1.25.1 strings/Contains().(rep.Findscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/rewrite`/substrReplacer#Find., "//")
479
480 changePathscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/rewrite`/changePath().(r, func(pathOrRawPath string) string {
481 return stringsscip-go gomod github.com/golang/go/src go1.25.1 strings/.Replacescip-go gomod github.com/golang/go/src go1.25.1 strings/Replace().(caddyhttpscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/.CleanPathscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/CleanPath().(pathOrRawPath, mergeSlashes), find, replace, lim)
482 })
483
484 r.URLscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Request#URL..RawQueryscip-go gomod github.com/golang/go/src go1.25.1 `net/url`/URL#RawQuery. = stringsscip-go gomod github.com/golang/go/src go1.25.1 strings/.Replacescip-go gomod github.com/golang/go/src go1.25.1 strings/Replace().(r.URLscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Request#URL..RawQueryscip-go gomod github.com/golang/go/src go1.25.1 `net/url`/URL#RawQuery., find, replace, lim)
485 }
486
487 // regexReplacer describes a replacement using a regular expression.
488 type regexReplacerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/rewrite`/regexReplacer# struct {
489 // The regular expression to find.
490 Findscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/rewrite`/regexReplacer#Find. string `json:"find,omitempty"`
491
492 // The substring to replace with. Supports placeholders and
493 // regular expression capture groups.
494 Replacescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/rewrite`/regexReplacer#Replace. string `json:"replace,omitempty"`
495
496 rescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/rewrite`/regexReplacer#re. *regexpscip-go gomod github.com/golang/go/src go1.25.1 regexp/.Regexpscip-go gomod github.com/golang/go/src go1.25.1 regexp/Regexp#
497 }
498
499 func (rep regexReplacerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/rewrite`/regexReplacer#) doscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/rewrite`/regexReplacer#do().(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#, repl *caddyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/.Replacerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/Replacer#) {
500 if rep.Findscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/rewrite`/regexReplacer#Find. == "" || rep.rescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/rewrite`/regexReplacer#re. == nil {
501 return
502 }
503 replace := repl.ReplaceAllscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/Replacer#ReplaceAll().(rep.Replacescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/rewrite`/regexReplacer#Replace., "")
504 changePathscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/rewrite`/changePath().(r, func(pathOrRawPath string) string {
505 return rep.rescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/rewrite`/regexReplacer#re..ReplaceAllStringscip-go gomod github.com/golang/go/src go1.25.1 regexp/Regexp#ReplaceAllString().(pathOrRawPath, replace)
506 })
507 }
508
509 func changePathscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/rewrite`/changePath().(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#, newVal func(pathOrRawPath string) string) {
510 req.URLscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Request#URL..RawPathscip-go gomod github.com/golang/go/src go1.25.1 `net/url`/URL#RawPath. = newVal(req.URLscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Request#URL..EscapedPathscip-go gomod github.com/golang/go/src go1.25.1 `net/url`/URL#EscapedPath().())
511 if p, err := urlscip-go gomod github.com/golang/go/src go1.25.1 `net/url`/.PathUnescapescip-go gomod github.com/golang/go/src go1.25.1 `net/url`/PathUnescape().(req.URLscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Request#URL..RawPathscip-go gomod github.com/golang/go/src go1.25.1 `net/url`/URL#RawPath.); err == nil && p != "" {
512 req.URLscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Request#URL..Pathscip-go gomod github.com/golang/go/src go1.25.1 `net/url`/URL#Path. = p
513 } else {
514 req.URLscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Request#URL..Pathscip-go gomod github.com/golang/go/src go1.25.1 `net/url`/URL#Path. = newVal(req.URLscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Request#URL..Pathscip-go gomod github.com/golang/go/src go1.25.1 `net/url`/URL#Path.)
515 }
516 // RawPath is only set if it's different from the normalized Path (std lib)
517 if req.URLscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Request#URL..RawPathscip-go gomod github.com/golang/go/src go1.25.1 `net/url`/URL#RawPath. == req.URLscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Request#URL..Pathscip-go gomod github.com/golang/go/src go1.25.1 `net/url`/URL#Path. {
518 req.URLscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Request#URL..RawPathscip-go gomod github.com/golang/go/src go1.25.1 `net/url`/URL#RawPath. = ""
519 }
520 }
521
522 // queryOps describes the operations to perform on query keys: add, set, rename and delete.
523 type queryOpsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/rewrite`/queryOps# struct {
524 // Renames a query key from Key to Val, without affecting the value.
525 Renamescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/rewrite`/queryOps#Rename. []queryOpsArgumentsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/rewrite`/queryOpsArguments# `json:"rename,omitempty"`
526
527 // Sets query parameters; overwrites a query key with the given value.
528 Setscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/rewrite`/queryOps#Set. []queryOpsArgumentsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/rewrite`/queryOpsArguments# `json:"set,omitempty"`
529
530 // Adds query parameters; does not overwrite an existing query field,
531 // and only appends an additional value for that key if any already exist.
532 Addscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/rewrite`/queryOps#Add. []queryOpsArgumentsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/rewrite`/queryOpsArguments# `json:"add,omitempty"`
533
534 // Replaces query parameters.
535 Replacescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/rewrite`/queryOps#Replace. []*queryOpsReplacementscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/rewrite`/queryOpsReplacement# `json:"replace,omitempty"`
536
537 // Deletes a given query key by name.
538 Deletescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/rewrite`/queryOps#Delete. []string `json:"delete,omitempty"`
539 }
540
541 // Provision compiles the query replace operation regex.
542 func (replacement *queryOpsReplacementscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/rewrite`/queryOpsReplacement#) Provisionscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/rewrite`/queryOpsReplacement#Provision().(_ caddyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/.Contextscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/Context#) error {
543 if replacement.SearchRegexpscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/rewrite`/queryOpsReplacement#SearchRegexp. != "" {
544 re, err := regexpscip-go gomod github.com/golang/go/src go1.25.1 regexp/.Compilescip-go gomod github.com/golang/go/src go1.25.1 regexp/Compile().(replacement.SearchRegexpscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/rewrite`/queryOpsReplacement#SearchRegexp.)
545 if err != nil {
546 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().("replacement for query field '%s': %v", replacement.Keyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/rewrite`/queryOpsReplacement#Key., err)
547 }
548 replacement.rescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/rewrite`/queryOpsReplacement#re. = re
549 }
550 return nil
551 }
552
553 func (q *queryOpsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/rewrite`/queryOps#) doscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/rewrite`/queryOps#do().(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#, repl *caddyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/.Replacerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/Replacer#) {
554 query := r.URLscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Request#URL..Queryscip-go gomod github.com/golang/go/src go1.25.1 `net/url`/URL#Query().()
555 for _, renameParam := range q.Renamescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/rewrite`/queryOps#Rename. {
556 key := repl.ReplaceAllscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/Replacer#ReplaceAll().(renameParam.Keyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/rewrite`/queryOpsArguments#Key., "")
557 val := repl.ReplaceAllscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/Replacer#ReplaceAll().(renameParam.Valscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/rewrite`/queryOpsArguments#Val., "")
558 if key == "" || val == "" {
559 continue
560 }
561 if key == val {
562 continue
563 }
564 originalValues, ok := query[key]
565 if !ok {
566 continue
567 }
568 query[val] = originalValues
569 delete(query, key)
570 }
571
572 for _, setParam := range q.Setscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/rewrite`/queryOps#Set. {
573 key := repl.ReplaceAllscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/Replacer#ReplaceAll().(setParam.Keyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/rewrite`/queryOpsArguments#Key., "")
574 if key == "" {
575 continue
576 }
577 val := repl.ReplaceAllscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/Replacer#ReplaceAll().(setParam.Valscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/rewrite`/queryOpsArguments#Val., "")
578 query[key] = []string{val}
579 }
580
581 for _, addParam := range q.Addscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/rewrite`/queryOps#Add. {
582 key := repl.ReplaceAllscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/Replacer#ReplaceAll().(addParam.Keyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/rewrite`/queryOpsArguments#Key., "")
583 if key == "" {
584 continue
585 }
586 val := repl.ReplaceAllscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/Replacer#ReplaceAll().(addParam.Valscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/rewrite`/queryOpsArguments#Val., "")
587 query[key] = append(query[key], val)
588 }
589
590 for _, replaceParam := range q.Replacescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/rewrite`/queryOps#Replace. {
591 key := repl.ReplaceAllscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/Replacer#ReplaceAll().(replaceParam.Keyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/rewrite`/queryOpsReplacement#Key., "")
592 search := repl.ReplaceKnownscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/Replacer#ReplaceKnown().(replaceParam.Searchscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/rewrite`/queryOpsReplacement#Search., "")
593 replace := repl.ReplaceKnownscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/Replacer#ReplaceKnown().(replaceParam.Replacescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/rewrite`/queryOpsReplacement#Replace., "")
594
595 // replace all query keys...
596 if key == "*" {
597 for fieldName, vals := range query {
598 for i := range vals {
599 if replaceParam.rescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/rewrite`/queryOpsReplacement#re. != nil {
600 query[fieldName][i] = replaceParam.rescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/rewrite`/queryOpsReplacement#re..ReplaceAllStringscip-go gomod github.com/golang/go/src go1.25.1 regexp/Regexp#ReplaceAllString().(query[fieldName][i], replace)
601 } else {
602 query[fieldName][i] = stringsscip-go gomod github.com/golang/go/src go1.25.1 strings/.ReplaceAllscip-go gomod github.com/golang/go/src go1.25.1 strings/ReplaceAll().(query[fieldName][i], search, replace)
603 }
604 }
605 }
606 continue
607 }
608
609 for fieldName, vals := range query {
610 for i := range vals {
611 if replaceParam.rescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/rewrite`/queryOpsReplacement#re. != nil {
612 query[fieldName][i] = replaceParam.rescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/rewrite`/queryOpsReplacement#re..ReplaceAllStringscip-go gomod github.com/golang/go/src go1.25.1 regexp/Regexp#ReplaceAllString().(query[fieldName][i], replace)
613 } else {
614 query[fieldName][i] = stringsscip-go gomod github.com/golang/go/src go1.25.1 strings/.ReplaceAllscip-go gomod github.com/golang/go/src go1.25.1 strings/ReplaceAll().(query[fieldName][i], search, replace)
615 }
616 }
617 }
618 }
619
620 for _, deleteParam := range q.Deletescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/rewrite`/queryOps#Delete. {
621 param := repl.ReplaceAllscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/Replacer#ReplaceAll().(deleteParam, "")
622 if param == "" {
623 continue
624 }
625 delete(query, param)
626 }
627
628 r.URLscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Request#URL..RawQueryscip-go gomod github.com/golang/go/src go1.25.1 `net/url`/URL#RawQuery. = query.Encodescip-go gomod github.com/golang/go/src go1.25.1 `net/url`/Values#Encode().()
629 }
630
631 type queryOpsArgumentsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/rewrite`/queryOpsArguments# struct {
632 // A key in the query string. Note that query string keys may appear multiple times.
633 Keyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/rewrite`/queryOpsArguments#Key. string `json:"key,omitempty"`
634
635 // The value for the given operation; for add and set, this is
636 // simply the value of the query, and for rename this is the
637 // query key to rename to.
638 Valscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/rewrite`/queryOpsArguments#Val. string `json:"val,omitempty"`
639 }
640
641 type queryOpsReplacementscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/rewrite`/queryOpsReplacement# struct {
642 // The key to replace in the query string.
643 Keyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/rewrite`/queryOpsReplacement#Key. string `json:"key,omitempty"`
644
645 // The substring to search for.
646 Searchscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/rewrite`/queryOpsReplacement#Search. string `json:"search,omitempty"`
647
648 // The regular expression to search with.
649 SearchRegexpscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/rewrite`/queryOpsReplacement#SearchRegexp. string `json:"search_regexp,omitempty"`
650
651 // The string with which to replace matches.
652 Replacescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/rewrite`/queryOpsReplacement#Replace. string `json:"replace,omitempty"`
653
654 rescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/rewrite`/queryOpsReplacement#re. *regexpscip-go gomod github.com/golang/go/src go1.25.1 regexp/.Regexpscip-go gomod github.com/golang/go/src go1.25.1 regexp/Regexp#
655 }
656
657 // Interface guard
658 var _ 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# = (*Rewritescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/rewrite`/Rewrite#)(nil)