Commit 7ae9e3a2 authored by Abiola Ibrahim's avatar Abiola Ibrahim

Rewrite: Added new variables file, dir, fragment.

parent 74d162f3
......@@ -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 {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment