From ad7b453f036347ca17d89427d2d42b5a5648c96a Mon Sep 17 00:00:00 2001 From: Abiola Ibrahim Date: Fri, 15 May 2015 18:47:26 +0100 Subject: [PATCH] Rewrite: modified syntax. --- config/setup/rewrite.go | 44 +++++++++++++++++------------------ middleware/rewrite/rewrite.go | 11 +++++---- 2 files changed, 28 insertions(+), 27 deletions(-) diff --git a/config/setup/rewrite.go b/config/setup/rewrite.go index 4829f55c..897f597e 100644 --- a/config/setup/rewrite.go +++ b/config/setup/rewrite.go @@ -18,61 +18,61 @@ func Rewrite(c *Controller) (middleware.Middleware, error) { } func rewriteParse(c *Controller) ([]rewrite.Rule, error) { - var rewrites []rewrite.Rule - var regexps []rewrite.Rule + var simpleRules []rewrite.Rule + var regexpRules []rewrite.Rule for c.Next() { var rule rewrite.Rule var err error + var base = "/" + var pattern, to string + var ext []string args := c.RemainingArgs() switch len(args) { case 2: - if args[0] != "regexp" { - rule = rewrite.NewSimpleRule(args[0], args[1]) - rewrites = append(rewrites, rule) - continue - } - - var base = args[1] - var pattern, to string - var ext []string - + rule = rewrite.NewSimpleRule(args[0], args[1]) + simpleRules = append(simpleRules, rule) + case 1: + base = args[0] + fallthrough + case 0: for c.NextBlock() { switch c.Val() { - case "pattern": + case "r", "regexp": if !c.NextArg() { - return rewrites, c.ArgErr() + return nil, c.ArgErr() } pattern = c.Val() case "to": if !c.NextArg() { - return rewrites, c.ArgErr() + return nil, c.ArgErr() } to = c.Val() case "ext": args1 := c.RemainingArgs() if len(args1) == 0 { - return rewrites, c.ArgErr() + return nil, c.ArgErr() } ext = args1 default: - return rewrites, c.ArgErr() + return nil, c.ArgErr() } } if pattern == "" || to == "" { - return rewrites, c.ArgErr() + return nil, c.ArgErr() } 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: - 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 } diff --git a/middleware/rewrite/rewrite.go b/middleware/rewrite/rewrite.go index 0567290c..cd51622e 100644 --- a/middleware/rewrite/rewrite.go +++ b/middleware/rewrite/rewrite.go @@ -79,9 +79,9 @@ func NewRegexpRule(base, pattern, to string, ext []string) (*RegexpRule, error) }, nil } -var regexpVars [2]string = [2]string{ - "$path", - "$query", +var regexpVars []string = []string{ + "{path}", + "{query}", } func (r *RegexpRule) Rewrite(req *http.Request) bool { @@ -92,7 +92,7 @@ func (r *RegexpRule) Rewrite(req *http.Request) bool { if !r.matchExt(rPath) { return false } - if !r.MatchString(req.URL.Path) { + if !r.MatchString(rPath[len(r.base):]) { return false } @@ -127,6 +127,7 @@ func (r *RegexpRule) matchExt(rPath string) bool { if ext == "" { ext = "/" } + mustUse := false for _, v := range r.ext { use := true @@ -143,9 +144,9 @@ func (r *RegexpRule) matchExt(rPath string) bool { return use } } + if mustUse { return false } - return true }