@marcel91200 / caddy Mirror · read-only / commits / ed503118
Fork 0

Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS

Mirrored from https://github.com/Slazee/caddy.git

Synced Nov 13, 09:15 UTC

caddyhttp: add placeholder {http.request.orig_uri.path.*} (#5161)

XYenon committed Oct 25, 2022 at 01:57 UTC ed503118dd8bd2dda2e780bdf7a0ada74338a462
1 file changed +30 -8
modules/caddyhttp/replacer.go
+30 -8
@@ -266,6 +266,27 @@ func addHTTPVarsToReplacer(repl *caddy.Replacer, req *http.Request, w http.Respo
266 return pathParts[idx], true
267 }
268
269 + // orig uri path parts
270 + if strings.HasPrefix(key, reqOrigURIPathReplPrefix) {
271 + idxStr := key[len(reqOrigURIPathReplPrefix):]
272 + idx, err := strconv.Atoi(idxStr)
273 + if err != nil {
274 + return "", false
275 + }
276 + or, _ := req.Context().Value(OriginalRequestCtxKey).(http.Request)
277 + pathParts := strings.Split(or.URL.Path, "/")
278 + if len(pathParts) > 0 && pathParts[0] == "" {
279 + pathParts = pathParts[1:]
280 + }
281 + if idx < 0 {
282 + return "", false
283 + }
284 + if idx >= len(pathParts) {
285 + return "", true
286 + }
287 + return pathParts[idx], true
288 + }
289 +
290 // middleware variables
291 if strings.HasPrefix(key, varsReplPrefix) {
292 varName := key[len(varsReplPrefix):]
@@ -471,12 +492,13 @@ func (rid *requestID) String() string {
492 }
493
494 const (
474 - reqCookieReplPrefix = "http.request.cookie."
475 - reqHeaderReplPrefix = "http.request.header."
476 - reqHostLabelsReplPrefix = "http.request.host.labels."
477 - reqTLSReplPrefix = "http.request.tls."
478 - reqURIPathReplPrefix = "http.request.uri.path."
479 - reqURIQueryReplPrefix = "http.request.uri.query."
480 - respHeaderReplPrefix = "http.response.header."
481 - varsReplPrefix = "http.vars."
495 + reqCookieReplPrefix = "http.request.cookie."
496 + reqHeaderReplPrefix = "http.request.header."
497 + reqHostLabelsReplPrefix = "http.request.host.labels."
498 + reqTLSReplPrefix = "http.request.tls."
499 + reqURIPathReplPrefix = "http.request.uri.path."
500 + reqURIQueryReplPrefix = "http.request.uri.query."
501 + respHeaderReplPrefix = "http.response.header."
502 + varsReplPrefix = "http.vars."
503 + reqOrigURIPathReplPrefix = "http.request.orig_uri.path."
504 )