caddyhttp: add replacer placeholders for escaped values (#7181)

This commit is contained in:
Bang Lee 2025-08-25 23:07:51 +08:00 committed by GitHub
parent 551f793700
commit 5e2953670e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 46 additions and 1 deletions

View File

@ -64,10 +64,13 @@ func placeholderShorthands() []string {
"{orig_?query}", "{http.request.orig_uri.prefixed_query}",
"{method}", "{http.request.method}",
"{uri}", "{http.request.uri}",
"{%uri}", "{http.request.uri_escaped}",
"{path}", "{http.request.uri.path}",
"{%path}", "{http.request.uri.path_escaped}",
"{dir}", "{http.request.uri.path.dir}",
"{file}", "{http.request.uri.path.file}",
"{query}", "{http.request.uri.query}",
"{%query}", "{http.request.uri.query_escaped}",
"{?query}", "{http.request.uri.prefixed_query}",
"{remote}", "{http.request.remote}",
"{remote_host}", "{http.request.remote.host}",

View File

@ -172,8 +172,12 @@ func addHTTPVarsToReplacer(repl *caddy.Replacer, req *http.Request, w http.Respo
// current URI, including any internal rewrites
case "http.request.uri":
return req.URL.RequestURI(), true
case "http.request.uri_escaped":
return url.QueryEscape(req.URL.RequestURI()), true
case "http.request.uri.path":
return req.URL.Path, true
case "http.request.uri.path_escaped":
return url.QueryEscape(req.URL.Path), true
case "http.request.uri.path.file":
_, file := path.Split(req.URL.Path)
return file, true
@ -186,6 +190,8 @@ func addHTTPVarsToReplacer(repl *caddy.Replacer, req *http.Request, w http.Respo
return path.Ext(req.URL.Path), true
case "http.request.uri.query":
return req.URL.RawQuery, true
case "http.request.uri.query_escaped":
return url.QueryEscape(req.URL.RawQuery), true
case "http.request.uri.prefixed_query":
if req.URL.RawQuery == "" {
return "", true

View File

@ -28,7 +28,7 @@ import (
)
func TestHTTPVarReplacement(t *testing.T) {
req, _ := http.NewRequest(http.MethodGet, "/foo/bar.tar.gz", nil)
req, _ := http.NewRequest(http.MethodGet, "/foo/bar.tar.gz?a=1&b=2", nil)
repl := caddy.NewReplacer()
localAddr, _ := net.ResolveTCPAddr("tcp", "192.168.159.1:80")
ctx := context.WithValue(req.Context(), caddy.ReplacerCtxKey, repl)
@ -142,6 +142,22 @@ eqp31wM9il1n+guTNyxJd+FzVAH+hCZE5K+tCgVDdVFUlDEHHbS/wqb2PSIoouLV
get: "http.request.host.labels.2",
expect: "",
},
{
get: "http.request.uri",
expect: "/foo/bar.tar.gz?a=1&b=2",
},
{
get: "http.request.uri_escaped",
expect: "%2Ffoo%2Fbar.tar.gz%3Fa%3D1%26b%3D2",
},
{
get: "http.request.uri.path",
expect: "/foo/bar.tar.gz",
},
{
get: "http.request.uri.path_escaped",
expect: "%2Ffoo%2Fbar.tar.gz",
},
{
get: "http.request.uri.path.file",
expect: "bar.tar.gz",
@ -155,6 +171,26 @@ eqp31wM9il1n+guTNyxJd+FzVAH+hCZE5K+tCgVDdVFUlDEHHbS/wqb2PSIoouLV
get: "http.request.uri.path.file.ext",
expect: ".gz",
},
{
get: "http.request.uri.query",
expect: "a=1&b=2",
},
{
get: "http.request.uri.query_escaped",
expect: "a%3D1%26b%3D2",
},
{
get: "http.request.uri.query.a",
expect: "1",
},
{
get: "http.request.uri.query.b",
expect: "2",
},
{
get: "http.request.uri.prefixed_query",
expect: "?a=1&b=2",
},
{
get: "http.request.tls.cipher_suite",
expect: "TLS_AES_256_GCM_SHA384",