mirror of
https://github.com/caddyserver/caddy.git
synced 2024-12-23 22:27:38 -05:00
Backward compatibility ensured.
This commit is contained in:
parent
98d8c0f81b
commit
4d5bc9fa6c
5 changed files with 18 additions and 12 deletions
|
@ -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,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
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue