From 4d5bc9fa6c2589a086f7033658c265a66ae7705c Mon Sep 17 00:00:00 2001 From: Abiola Ibrahim Date: Wed, 23 Dec 2015 09:02:52 +0100 Subject: [PATCH] Backward compatibility ensured. --- caddy/setup/rewrite.go | 4 ++-- caddy/setup/rewrite_test.go | 5 ----- middleware/rewrite/rewrite.go | 15 ++++++++++++--- middleware/rewrite/rewrite_test.go | 3 ++- middleware/rewrite/to.go | 3 ++- 5 files changed, 18 insertions(+), 12 deletions(-) diff --git a/caddy/setup/rewrite.go b/caddy/setup/rewrite.go index c70a2f94..e642d071 100644 --- a/caddy/setup/rewrite.go +++ b/caddy/setup/rewrite.go @@ -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 { diff --git a/caddy/setup/rewrite_test.go b/caddy/setup/rewrite_test.go index ddef7cd4..13c1372f 100644 --- a/caddy/setup/rewrite_test.go +++ b/caddy/setup/rewrite_test.go @@ -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{ diff --git a/middleware/rewrite/rewrite.go b/middleware/rewrite/rewrite.go index cc60e82c..f173e754 100644 --- a/middleware/rewrite/rewrite.go +++ b/middleware/rewrite/rewrite.go @@ -128,9 +128,18 @@ func (r *ComplexRule) Rewrite(fs http.FileSystem, req *http.Request) bool { start-- } - // validate regexp - if !r.MatchString(rPath[start:]) { - return false + // 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 diff --git a/middleware/rewrite/rewrite_test.go b/middleware/rewrite/rewrite_test.go index ca4cc512..6230f0d9 100644 --- a/middleware/rewrite/rewrite_test.go +++ b/middleware/rewrite/rewrite_test.go @@ -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) } diff --git a/middleware/rewrite/to.go b/middleware/rewrite/to.go index 0ee8a159..294b8f04 100644 --- a/middleware/rewrite/to.go +++ b/middleware/rewrite/to.go @@ -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 }