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