Commit 4d5bc9fa authored by Abiola Ibrahim's avatar Abiola Ibrahim

Backward compatibility ensured.

parent 98d8c0f8
......@@ -78,8 +78,8 @@ func rewriteParse(c *Controller) ([]rewrite.Rule, error) {
return nil, c.ArgErr()
}
}
// ensure pattern and to are specified
if pattern == "" || to == "" {
// ensure to is specified
if to == "" {
return nil, c.ArgErr()
}
if rule, err = rewrite.NewComplexRule(base, pattern, to, ext, ifs); err != nil {
......
......@@ -119,11 +119,6 @@ func TestRewriteParse(t *testing.T) {
&rewrite.ComplexRule{Base: "/path", To: "/dest", Regexp: regexp.MustCompile("rr")},
&rewrite.ComplexRule{Base: "/", To: "/to", Regexp: regexp.MustCompile("[a-z]+")},
}},
{`rewrite {
to /to
}`, true, []rewrite.Rule{
&rewrite.ComplexRule{},
}},
{`rewrite {
r .*
}`, true, []rewrite.Rule{
......
......@@ -128,10 +128,19 @@ func (r *ComplexRule) Rewrite(fs http.FileSystem, req *http.Request) bool {
start--
}
// validate regexp
// validate regexp if present
if r.Regexp != nil {
if !r.MatchString(rPath[start:]) {
return false
}
}
// validate rewrite conditions
for _, i := range r.Ifs {
if !i.True(req) {
return false
}
}
// attempt rewrite
return To(fs, req, r.To)
......
......@@ -18,6 +18,7 @@ func TestRewrite(t *testing.T) {
NewSimpleRule("/a", "/b"),
NewSimpleRule("/b", "/b{uri}"),
},
FileSys: http.Dir("."),
}
regexpRules := [][]string{
......@@ -37,7 +38,7 @@ func TestRewrite(t *testing.T) {
if s := strings.Split(regexpRule[3], "|"); len(s) > 1 {
ext = s[:len(s)-1]
}
rule, err := NewComplexRule(regexpRule[0], regexpRule[1], regexpRule[2], ext)
rule, err := NewComplexRule(regexpRule[0], regexpRule[1], regexpRule[2], ext, nil)
if err != nil {
t.Fatal(err)
}
......
......@@ -4,6 +4,7 @@ import (
"log"
"net/http"
"net/url"
"path"
"strings"
)
......@@ -17,7 +18,7 @@ func To(fs http.FileSystem, r *http.Request, to string) bool {
// try each rewrite paths
t := ""
for _, v := range tos {
t = replacer.Replace(v)
t = path.Clean(replacer.Replace(v))
if isValidFile(fs, t) {
break
}
......
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