0
Fork 0
mirror of https://github.com/caddyserver/caddy.git synced 2024-12-23 22:27:38 -05:00

Rewrite: modified syntax.

This commit is contained in:
Abiola Ibrahim 2015-05-15 18:47:26 +01:00
parent b2afc30d12
commit ad7b453f03
2 changed files with 28 additions and 27 deletions

View file

@ -18,61 +18,61 @@ func Rewrite(c *Controller) (middleware.Middleware, error) {
} }
func rewriteParse(c *Controller) ([]rewrite.Rule, error) { func rewriteParse(c *Controller) ([]rewrite.Rule, error) {
var rewrites []rewrite.Rule var simpleRules []rewrite.Rule
var regexps []rewrite.Rule var regexpRules []rewrite.Rule
for c.Next() { for c.Next() {
var rule rewrite.Rule var rule rewrite.Rule
var err error var err error
var base = "/"
var pattern, to string
var ext []string
args := c.RemainingArgs() args := c.RemainingArgs()
switch len(args) { switch len(args) {
case 2: case 2:
if args[0] != "regexp" {
rule = rewrite.NewSimpleRule(args[0], args[1]) rule = rewrite.NewSimpleRule(args[0], args[1])
rewrites = append(rewrites, rule) simpleRules = append(simpleRules, rule)
continue case 1:
} base = args[0]
fallthrough
var base = args[1] case 0:
var pattern, to string
var ext []string
for c.NextBlock() { for c.NextBlock() {
switch c.Val() { switch c.Val() {
case "pattern": case "r", "regexp":
if !c.NextArg() { if !c.NextArg() {
return rewrites, c.ArgErr() return nil, c.ArgErr()
} }
pattern = c.Val() pattern = c.Val()
case "to": case "to":
if !c.NextArg() { if !c.NextArg() {
return rewrites, c.ArgErr() return nil, c.ArgErr()
} }
to = c.Val() to = c.Val()
case "ext": case "ext":
args1 := c.RemainingArgs() args1 := c.RemainingArgs()
if len(args1) == 0 { if len(args1) == 0 {
return rewrites, c.ArgErr() return nil, c.ArgErr()
} }
ext = args1 ext = args1
default: default:
return rewrites, c.ArgErr() return nil, c.ArgErr()
} }
} }
if pattern == "" || to == "" { if pattern == "" || to == "" {
return rewrites, c.ArgErr() return nil, c.ArgErr()
} }
if rule, err = rewrite.NewRegexpRule(base, pattern, to, ext); err != nil { if rule, err = rewrite.NewRegexpRule(base, pattern, to, ext); err != nil {
return rewrites, err return nil, err
} }
rewrites = append(regexps, rule) regexpRules = append(regexpRules, rule)
default: default:
return rewrites, c.ArgErr() return nil, c.ArgErr()
} }
} }
return append(rewrites, regexps...), nil // put simple rules in front to avoid regexp computation for them
return append(simpleRules, regexpRules...), nil
} }

View file

@ -79,9 +79,9 @@ func NewRegexpRule(base, pattern, to string, ext []string) (*RegexpRule, error)
}, nil }, nil
} }
var regexpVars [2]string = [2]string{ var regexpVars []string = []string{
"$path", "{path}",
"$query", "{query}",
} }
func (r *RegexpRule) Rewrite(req *http.Request) bool { func (r *RegexpRule) Rewrite(req *http.Request) bool {
@ -92,7 +92,7 @@ func (r *RegexpRule) Rewrite(req *http.Request) bool {
if !r.matchExt(rPath) { if !r.matchExt(rPath) {
return false return false
} }
if !r.MatchString(req.URL.Path) { if !r.MatchString(rPath[len(r.base):]) {
return false return false
} }
@ -127,6 +127,7 @@ func (r *RegexpRule) matchExt(rPath string) bool {
if ext == "" { if ext == "" {
ext = "/" ext = "/"
} }
mustUse := false mustUse := false
for _, v := range r.ext { for _, v := range r.ext {
use := true use := true
@ -143,9 +144,9 @@ func (r *RegexpRule) matchExt(rPath string) bool {
return use return use
} }
} }
if mustUse { if mustUse {
return false return false
} }
return true return true
} }