mirror of
https://github.com/caddyserver/caddy.git
synced 2024-12-23 22:27:38 -05:00
Rewrite: Added new variables file, dir, fragment.
This commit is contained in:
parent
74d162f377
commit
7ae9e3a262
2 changed files with 18 additions and 1 deletions
|
@ -99,6 +99,9 @@ func NewRegexpRule(base, pattern, to string, ext []string) (*RegexpRule, error)
|
|||
var regexpVars []string = []string{
|
||||
"{path}",
|
||||
"{query}",
|
||||
"{file}",
|
||||
"{dir}",
|
||||
"{frag}",
|
||||
}
|
||||
|
||||
// Rewrite rewrites the internal location of the current request.
|
||||
|
@ -130,6 +133,14 @@ func (r *RegexpRule) Rewrite(req *http.Request) bool {
|
|||
to = strings.Replace(to, v, req.URL.Path[1:], -1)
|
||||
case "{query}":
|
||||
to = strings.Replace(to, v, req.URL.RawQuery, -1)
|
||||
case "{frag}":
|
||||
to = strings.Replace(to, v, req.URL.Fragment, -1)
|
||||
case "{file}":
|
||||
_, file := path.Split(req.URL.Path)
|
||||
to = strings.Replace(to, v, file, -1)
|
||||
case "{dir}":
|
||||
dir, _ := path.Split(req.URL.Path)
|
||||
to = path.Clean(strings.Replace(to, v, dir, -1))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -25,6 +25,9 @@ func TestRewrite(t *testing.T) {
|
|||
[]string{"/url/", "a([a-z0-9]*)s([A-Z]{2})", "/to/{path}", ""},
|
||||
[]string{"/ab/", "ab", "/ab?{query}", ".txt|"},
|
||||
[]string{"/ab/", "ab", "/ab?type=html&{query}", ".html|"},
|
||||
[]string{"/abc/", "ab", "/abc/{file}", ".html|"},
|
||||
[]string{"/abcd/", "ab", "/a/{dir}/{file}", ".html|"},
|
||||
[]string{"/abcde/", "ab", "/a#{frag}", ".html|"},
|
||||
}
|
||||
|
||||
for _, regexpRule := range regexpRules {
|
||||
|
@ -69,7 +72,10 @@ func TestRewrite(t *testing.T) {
|
|||
{"/ab/ab.txt", "/ab"},
|
||||
{"/ab/ab.txt?name=name", "/ab?name=name"},
|
||||
{"/ab/ab.html?name=name", "/ab?type=html&name=name"},
|
||||
{"/ab/ab.html", "/ab?type=html&"},
|
||||
{"/abc/ab.html", "/abc/ab.html"},
|
||||
{"/abcd/abcd.html", "/a/abcd/abcd.html"},
|
||||
{"/abcde/abcde.html", "/a"},
|
||||
{"/abcde/abcde.html#1234", "/a#1234"},
|
||||
}
|
||||
|
||||
for i, test := range tests {
|
||||
|
|
Loading…
Reference in a new issue