From 3c086fb2e67b885d7c3a6bd417b0e2d6fc66074f Mon Sep 17 00:00:00 2001 From: Abiola Ibrahim Date: Wed, 30 Dec 2015 21:47:37 +0100 Subject: [PATCH] Support for rewrite match group. --- middleware/rewrite/rewrite.go | 15 ++++++++++++--- middleware/rewrite/rewrite_test.go | 9 +++++++++ middleware/rewrite/to.go | 5 +++-- middleware/rewrite/to_test.go | 2 +- 4 files changed, 25 insertions(+), 6 deletions(-) diff --git a/middleware/rewrite/rewrite.go b/middleware/rewrite/rewrite.go index f173e754..60cc0b9d 100644 --- a/middleware/rewrite/rewrite.go +++ b/middleware/rewrite/rewrite.go @@ -54,7 +54,7 @@ func (s SimpleRule) Rewrite(fs http.FileSystem, r *http.Request) bool { r.Header.Set(headerFieldName, r.URL.RequestURI()) // attempt rewrite - return To(fs, r, s.To) + return To(fs, r, s.To, newReplacer(r)) } return false } @@ -111,6 +111,7 @@ func NewComplexRule(base, pattern, to string, ext []string, ifs []If) (*ComplexR // Rewrite rewrites the internal location of the current request. func (r *ComplexRule) Rewrite(fs http.FileSystem, req *http.Request) bool { rPath := req.URL.Path + replacer := newReplacer(req) // validate base if !middleware.Path(rPath).Matches(r.Base) { @@ -130,8 +131,16 @@ func (r *ComplexRule) Rewrite(fs http.FileSystem, req *http.Request) bool { // validate regexp if present if r.Regexp != nil { - if !r.MatchString(rPath[start:]) { + matches := r.FindStringSubmatch(rPath[start:]) + switch len(matches) { + case 0: + // no match return false + default: + // set regexp match variables {1}, {2} ... + for i := 1; i < len(matches); i++ { + replacer.Set(fmt.Sprint(i), matches[i]) + } } } @@ -143,7 +152,7 @@ func (r *ComplexRule) Rewrite(fs http.FileSystem, req *http.Request) bool { } // attempt rewrite - return To(fs, req, r.To) + return To(fs, req, r.To, replacer) } // matchExt matches rPath against registered file extensions. diff --git a/middleware/rewrite/rewrite_test.go b/middleware/rewrite/rewrite_test.go index a538b79d..5b891606 100644 --- a/middleware/rewrite/rewrite_test.go +++ b/middleware/rewrite/rewrite_test.go @@ -31,6 +31,9 @@ func TestRewrite(t *testing.T) { {"/abcd/", "ab", "/a/{dir}/{file}", ".html|"}, {"/abcde/", "ab", "/a#{fragment}", ".html|"}, {"/ab/", `.*\.jpg`, "/ajpg", ""}, + {"/reggrp", `/ad/([0-9]+)([a-z]*)`, "/a{1}/{2}", ""}, + {"/reg2grp", `(.*)`, "/{1}", ""}, + {"/reg3grp", `(.*)/(.*)/(.*)`, "/{1}{2}{3}", ""}, } for _, regexpRule := range regexps { @@ -81,6 +84,12 @@ func TestRewrite(t *testing.T) { {"/abcde/abcde.html", "/a"}, {"/abcde/abcde.html#1234", "/a#1234"}, {"/ab/ab.jpg", "/ajpg"}, + {"/reggrp/ad/12", "/a12"}, + {"/reggrp/ad/124a", "/a124/a"}, + {"/reggrp/ad/124abc", "/a124/abc"}, + {"/reg2grp/ad/124abc", "/ad/124abc"}, + {"/reg3grp/ad/aa/66", "/adaa66"}, + {"/reg3grp/ad612/n1n/ab", "/ad612n1nab"}, } for i, test := range tests { diff --git a/middleware/rewrite/to.go b/middleware/rewrite/to.go index d6c7f521..8a577c5e 100644 --- a/middleware/rewrite/to.go +++ b/middleware/rewrite/to.go @@ -6,14 +6,15 @@ import ( "net/url" "path" "strings" + + "github.com/mholt/caddy/middleware" ) // To attempts rewrite. It attempts to rewrite to first valid path // or the last path if none of the paths are valid. // Returns true if rewrite is successful and false otherwise. -func To(fs http.FileSystem, r *http.Request, to string) bool { +func To(fs http.FileSystem, r *http.Request, to string, replacer middleware.Replacer) bool { tos := strings.Fields(to) - replacer := newReplacer(r) // try each rewrite paths t := "" diff --git a/middleware/rewrite/to_test.go b/middleware/rewrite/to_test.go index 2d8b535a..6133c0b6 100644 --- a/middleware/rewrite/to_test.go +++ b/middleware/rewrite/to_test.go @@ -36,7 +36,7 @@ func TestTo(t *testing.T) { if err != nil { t.Error(err) } - To(fs, r, test.to) + To(fs, r, test.to, newReplacer(r)) if uri(r.URL) != test.expected { t.Errorf("Test %v: expected %v found %v", i, test.expected, uri(r.URL)) }