From 0e039a18684634a01aa3886a77c0a910e9678120 Mon Sep 17 00:00:00 2001 From: Abiola Ibrahim Date: Sun, 20 Sep 2015 08:49:55 +0100 Subject: [PATCH 1/2] Rewrite: Use middleware.Replacer. Bug fix for regexps starting with '/'. --- middleware/replacer.go | 10 ++++++++ middleware/rewrite/rewrite.go | 43 ++++++++--------------------------- 2 files changed, 20 insertions(+), 33 deletions(-) diff --git a/middleware/replacer.go b/middleware/replacer.go index df1f6e010..9d34253cf 100644 --- a/middleware/replacer.go +++ b/middleware/replacer.go @@ -3,6 +3,7 @@ package middleware import ( "net" "net/http" + "path" "strconv" "strings" "time" @@ -40,6 +41,7 @@ func NewReplacer(r *http.Request, rr *responseRecorder, emptyValue string) Repla "{host}": r.Host, "{path}": r.URL.Path, "{query}": r.URL.RawQuery, + "{frag}": r.URL.Fragment, "{fragment}": r.URL.Fragment, "{proto}": r.Proto, "{remote}": func() string { @@ -63,6 +65,14 @@ func NewReplacer(r *http.Request, rr *responseRecorder, emptyValue string) Repla "{when}": func() string { return time.Now().Format(timeFormat) }(), + "{file}": func() string { + _, file := path.Split(r.URL.Path) + return file + }(), + "{dir}": func() string { + dir, _ := path.Split(r.URL.Path) + return dir + }(), }, emptyValue: emptyValue, } diff --git a/middleware/rewrite/rewrite.go b/middleware/rewrite/rewrite.go index bf987b938..ed2e9bd9f 100644 --- a/middleware/rewrite/rewrite.go +++ b/middleware/rewrite/rewrite.go @@ -3,9 +3,8 @@ package rewrite import ( - "net/http" - "fmt" + "net/http" "net/url" "path" "path/filepath" @@ -96,15 +95,6 @@ func NewRegexpRule(base, pattern, to string, ext []string) (*RegexpRule, error) }, nil } -// regexpVars are variables that can be used for To (rewrite destination path). -var regexpVars = []string{ - "{path}", - "{query}", - "{file}", - "{dir}", - "{frag}", -} - // Rewrite rewrites the internal location of the current request. func (r *RegexpRule) Rewrite(req *http.Request) bool { rPath := req.URL.Path @@ -119,32 +109,19 @@ func (r *RegexpRule) Rewrite(req *http.Request) bool { return false } + // include trailing slash in regexp if present + start := len(r.Base) + if strings.HasSuffix(r.Base, "/") { + start -= 1 + } + // validate regexp - if !r.MatchString(rPath[len(r.Base):]) { + if !r.MatchString(rPath[start:]) { return false } - to := r.To - - // check variables - for _, v := range regexpVars { - if strings.Contains(r.To, v) { - switch v { - case "{path}": - to = strings.Replace(to, v, req.URL.Path[1:], -1) - case "{query}": - to = strings.Replace(to, v, req.URL.RawQuery, -1) - case "{frag}": - to = strings.Replace(to, v, req.URL.Fragment, -1) - case "{file}": - _, file := path.Split(req.URL.Path) - to = strings.Replace(to, v, file, -1) - case "{dir}": - dir, _ := path.Split(req.URL.Path) - to = path.Clean(strings.Replace(to, v, dir, -1)) - } - } - } + // replace variables + to := path.Clean(middleware.NewReplacer(req, nil, "").Replace(r.To)) // validate resulting path url, err := url.Parse(to) From 7f9fa5730b61064d26e723ff132a18ea79ce4f35 Mon Sep 17 00:00:00 2001 From: Abiola Ibrahim Date: Sun, 20 Sep 2015 18:13:53 +0100 Subject: [PATCH 2/2] Rewrite: Use only `fragment`, remove `frag`. --- middleware/replacer.go | 1 - middleware/rewrite/rewrite_test.go | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/middleware/replacer.go b/middleware/replacer.go index 9d34253cf..29c695b77 100644 --- a/middleware/replacer.go +++ b/middleware/replacer.go @@ -41,7 +41,6 @@ func NewReplacer(r *http.Request, rr *responseRecorder, emptyValue string) Repla "{host}": r.Host, "{path}": r.URL.Path, "{query}": r.URL.RawQuery, - "{frag}": r.URL.Fragment, "{fragment}": r.URL.Fragment, "{proto}": r.Proto, "{remote}": func() string { diff --git a/middleware/rewrite/rewrite_test.go b/middleware/rewrite/rewrite_test.go index d9056b431..b4845f106 100644 --- a/middleware/rewrite/rewrite_test.go +++ b/middleware/rewrite/rewrite_test.go @@ -28,7 +28,7 @@ func TestRewrite(t *testing.T) { []string{"/ab/", "ab", "/ab?type=html&{query}", ".html|"}, []string{"/abc/", "ab", "/abc/{file}", ".html|"}, []string{"/abcd/", "ab", "/a/{dir}/{file}", ".html|"}, - []string{"/abcde/", "ab", "/a#{frag}", ".html|"}, + []string{"/abcde/", "ab", "/a#{fragment}", ".html|"}, []string{"/ab/", `.*\.jpg`, "/ajpg", ""}, }