mirror of
https://github.com/caddyserver/caddy.git
synced 2024-11-29 12:16:16 +08:00
Add request placeholder support for querying request cookies. (#1392)
* Add request placeholder support for querying request cookies. This adds the ability to query the request cookies for placeholders using the syntax "@cookiename". For example, this would allow rewriting based on a cookie: rewrite { if @version is 'dev' to /dev/index.html } * Switch cookie special char from @ to : * Switch special char for cookies from : to ~
This commit is contained in:
parent
bdb61f4a1d
commit
dc3efc939c
|
@ -198,6 +198,13 @@ func (r *replacer) getSubstitution(key string) string {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// next check for cookies
|
||||||
|
if key[1] == '~' {
|
||||||
|
name := key[2 : len(key)-1]
|
||||||
|
if cookie, err := r.request.Cookie(name); err == nil {
|
||||||
|
return cookie.Value
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// search default replacements in the end
|
// search default replacements in the end
|
||||||
switch key {
|
switch key {
|
||||||
|
|
|
@ -47,6 +47,7 @@ func TestReplace(t *testing.T) {
|
||||||
repl := NewReplacer(request, recordRequest, "-")
|
repl := NewReplacer(request, recordRequest, "-")
|
||||||
// add some headers after creating replacer
|
// add some headers after creating replacer
|
||||||
request.Header.Set("CustomAdd", "caddy")
|
request.Header.Set("CustomAdd", "caddy")
|
||||||
|
request.Header.Set("Cookie", "foo=bar; taste=delicious")
|
||||||
|
|
||||||
hostname, err := os.Hostname()
|
hostname, err := os.Hostname()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -72,12 +73,17 @@ func TestReplace(t *testing.T) {
|
||||||
{"{when_iso}", "2006-01-02T15:04:12Z"},
|
{"{when_iso}", "2006-01-02T15:04:12Z"},
|
||||||
{"The Custom header is {>Custom}.", "The Custom header is foobarbaz."},
|
{"The Custom header is {>Custom}.", "The Custom header is foobarbaz."},
|
||||||
{"The CustomAdd header is {>CustomAdd}.", "The CustomAdd header is caddy."},
|
{"The CustomAdd header is {>CustomAdd}.", "The CustomAdd header is caddy."},
|
||||||
{"The request is {request}.", "The request is POST / HTTP/1.1\\r\\nHost: localhost\\r\\nCustom: foobarbaz\\r\\nCustomadd: caddy\\r\\nShorterval: 1\\r\\n\\r\\n."},
|
{"The request is {request}.", "The request is POST / HTTP/1.1\\r\\nHost: localhost\\r\\n" +
|
||||||
|
"Cookie: foo=bar; taste=delicious\\r\\nCustom: foobarbaz\\r\\nCustomadd: caddy\\r\\n" +
|
||||||
|
"Shorterval: 1\\r\\n\\r\\n."},
|
||||||
{"The cUsToM header is {>cUsToM}...", "The cUsToM header is foobarbaz..."},
|
{"The cUsToM header is {>cUsToM}...", "The cUsToM header is foobarbaz..."},
|
||||||
{"The Non-Existent header is {>Non-Existent}.", "The Non-Existent header is -."},
|
{"The Non-Existent header is {>Non-Existent}.", "The Non-Existent header is -."},
|
||||||
{"Bad {host placeholder...", "Bad {host placeholder..."},
|
{"Bad {host placeholder...", "Bad {host placeholder..."},
|
||||||
{"Bad {>Custom placeholder", "Bad {>Custom placeholder"},
|
{"Bad {>Custom placeholder", "Bad {>Custom placeholder"},
|
||||||
{"Bad {>Custom placeholder {>ShorterVal}", "Bad -"},
|
{"Bad {>Custom placeholder {>ShorterVal}", "Bad -"},
|
||||||
|
{"Bad {}", "Bad -"},
|
||||||
|
{"Cookies are {~taste}", "Cookies are delicious"},
|
||||||
|
{"Missing cookie is {~missing}", "Missing cookie is -"},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, c := range testCases {
|
for _, c := range testCases {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user