MCPcopy
hub / github.com/caddyserver/caddy / escapePathPlaceholders

Function escapePathPlaceholders

modules/caddyhttp/rewrite/rewrite.go:307–326  ·  view source on GitHub ↗
(path string, r *http.Request, repl *caddy.Replacer)

Source from the content-addressed store, hash-verified

305}
306
307func escapePathPlaceholders(path string, r *http.Request, repl *caddy.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 strings.Contains(path, pathPlaceholder) {
313 path = strings.ReplaceAll(path, pathPlaceholder, r.URL.EscapedPath())
314 }
315
316 fileMatchRelativePlaceholder := "{http.matchers.file.relative}"
317 if strings.Contains(path, fileMatchRelativePlaceholder) {
318 if val, ok := repl.Get("http.matchers.file.relative"); ok {
319 if relativePath, ok := val.(string); ok {
320 path = strings.ReplaceAll(path, fileMatchRelativePlaceholder, escapePathPreservingSlashes(relativePath))
321 }
322 }
323 }
324
325 return path
326}
327
328func escapePathPreservingSlashes(path string) string {
329 return strings.ReplaceAll(url.PathEscape(path), "%2F", "/")

Callers 1

RewriteMethod · 0.85

Calls 3

ReplaceAllMethod · 0.80
GetMethod · 0.65

Tested by

no test coverage detected