mirror of
https://github.com/caddyserver/caddy.git
synced 2025-08-30 23:02:33 -04:00
caddyhttp: add replacer placeholders for escaped values (#7181)
This commit is contained in:
parent
551f793700
commit
5e2953670e
@ -64,10 +64,13 @@ func placeholderShorthands() []string {
|
|||||||
"{orig_?query}", "{http.request.orig_uri.prefixed_query}",
|
"{orig_?query}", "{http.request.orig_uri.prefixed_query}",
|
||||||
"{method}", "{http.request.method}",
|
"{method}", "{http.request.method}",
|
||||||
"{uri}", "{http.request.uri}",
|
"{uri}", "{http.request.uri}",
|
||||||
|
"{%uri}", "{http.request.uri_escaped}",
|
||||||
"{path}", "{http.request.uri.path}",
|
"{path}", "{http.request.uri.path}",
|
||||||
|
"{%path}", "{http.request.uri.path_escaped}",
|
||||||
"{dir}", "{http.request.uri.path.dir}",
|
"{dir}", "{http.request.uri.path.dir}",
|
||||||
"{file}", "{http.request.uri.path.file}",
|
"{file}", "{http.request.uri.path.file}",
|
||||||
"{query}", "{http.request.uri.query}",
|
"{query}", "{http.request.uri.query}",
|
||||||
|
"{%query}", "{http.request.uri.query_escaped}",
|
||||||
"{?query}", "{http.request.uri.prefixed_query}",
|
"{?query}", "{http.request.uri.prefixed_query}",
|
||||||
"{remote}", "{http.request.remote}",
|
"{remote}", "{http.request.remote}",
|
||||||
"{remote_host}", "{http.request.remote.host}",
|
"{remote_host}", "{http.request.remote.host}",
|
||||||
|
@ -172,8 +172,12 @@ func addHTTPVarsToReplacer(repl *caddy.Replacer, req *http.Request, w http.Respo
|
|||||||
// current URI, including any internal rewrites
|
// current URI, including any internal rewrites
|
||||||
case "http.request.uri":
|
case "http.request.uri":
|
||||||
return req.URL.RequestURI(), true
|
return req.URL.RequestURI(), true
|
||||||
|
case "http.request.uri_escaped":
|
||||||
|
return url.QueryEscape(req.URL.RequestURI()), true
|
||||||
case "http.request.uri.path":
|
case "http.request.uri.path":
|
||||||
return req.URL.Path, true
|
return req.URL.Path, true
|
||||||
|
case "http.request.uri.path_escaped":
|
||||||
|
return url.QueryEscape(req.URL.Path), true
|
||||||
case "http.request.uri.path.file":
|
case "http.request.uri.path.file":
|
||||||
_, file := path.Split(req.URL.Path)
|
_, file := path.Split(req.URL.Path)
|
||||||
return file, true
|
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
|
return path.Ext(req.URL.Path), true
|
||||||
case "http.request.uri.query":
|
case "http.request.uri.query":
|
||||||
return req.URL.RawQuery, true
|
return req.URL.RawQuery, true
|
||||||
|
case "http.request.uri.query_escaped":
|
||||||
|
return url.QueryEscape(req.URL.RawQuery), true
|
||||||
case "http.request.uri.prefixed_query":
|
case "http.request.uri.prefixed_query":
|
||||||
if req.URL.RawQuery == "" {
|
if req.URL.RawQuery == "" {
|
||||||
return "", true
|
return "", true
|
||||||
|
@ -28,7 +28,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func TestHTTPVarReplacement(t *testing.T) {
|
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()
|
repl := caddy.NewReplacer()
|
||||||
localAddr, _ := net.ResolveTCPAddr("tcp", "192.168.159.1:80")
|
localAddr, _ := net.ResolveTCPAddr("tcp", "192.168.159.1:80")
|
||||||
ctx := context.WithValue(req.Context(), caddy.ReplacerCtxKey, repl)
|
ctx := context.WithValue(req.Context(), caddy.ReplacerCtxKey, repl)
|
||||||
@ -142,6 +142,22 @@ eqp31wM9il1n+guTNyxJd+FzVAH+hCZE5K+tCgVDdVFUlDEHHbS/wqb2PSIoouLV
|
|||||||
get: "http.request.host.labels.2",
|
get: "http.request.host.labels.2",
|
||||||
expect: "",
|
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",
|
get: "http.request.uri.path.file",
|
||||||
expect: "bar.tar.gz",
|
expect: "bar.tar.gz",
|
||||||
@ -155,6 +171,26 @@ eqp31wM9il1n+guTNyxJd+FzVAH+hCZE5K+tCgVDdVFUlDEHHbS/wqb2PSIoouLV
|
|||||||
get: "http.request.uri.path.file.ext",
|
get: "http.request.uri.path.file.ext",
|
||||||
expect: ".gz",
|
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",
|
get: "http.request.tls.cipher_suite",
|
||||||
expect: "TLS_AES_256_GCM_SHA384",
|
expect: "TLS_AES_256_GCM_SHA384",
|
||||||
|
Loading…
x
Reference in New Issue
Block a user