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()
|
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) {
|
||||||
&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 {
|
||||||
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) {
|
||||||
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) {
|
||||||
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 (
|
||||||
"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 {
|
||||||
// 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
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue