Commit 3c086fb2 authored by Abiola Ibrahim's avatar Abiola Ibrahim

Support for rewrite match group.

parent 55aa492d
...@@ -54,7 +54,7 @@ func (s SimpleRule) Rewrite(fs http.FileSystem, r *http.Request) bool { ...@@ -54,7 +54,7 @@ func (s SimpleRule) Rewrite(fs http.FileSystem, r *http.Request) bool {
r.Header.Set(headerFieldName, r.URL.RequestURI()) r.Header.Set(headerFieldName, r.URL.RequestURI())
// attempt rewrite // attempt rewrite
return To(fs, r, s.To) return To(fs, r, s.To, newReplacer(r))
} }
return false return false
} }
...@@ -111,6 +111,7 @@ func NewComplexRule(base, pattern, to string, ext []string, ifs []If) (*ComplexR ...@@ -111,6 +111,7 @@ func NewComplexRule(base, pattern, to string, ext []string, ifs []If) (*ComplexR
// Rewrite rewrites the internal location of the current request. // Rewrite rewrites the internal location of the current request.
func (r *ComplexRule) Rewrite(fs http.FileSystem, req *http.Request) bool { func (r *ComplexRule) Rewrite(fs http.FileSystem, req *http.Request) bool {
rPath := req.URL.Path rPath := req.URL.Path
replacer := newReplacer(req)
// validate base // validate base
if !middleware.Path(rPath).Matches(r.Base) { if !middleware.Path(rPath).Matches(r.Base) {
...@@ -130,8 +131,16 @@ func (r *ComplexRule) Rewrite(fs http.FileSystem, req *http.Request) bool { ...@@ -130,8 +131,16 @@ func (r *ComplexRule) Rewrite(fs http.FileSystem, req *http.Request) bool {
// validate regexp if present // validate regexp if present
if r.Regexp != nil { if r.Regexp != nil {
if !r.MatchString(rPath[start:]) { matches := r.FindStringSubmatch(rPath[start:])
switch len(matches) {
case 0:
// no match
return false 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 { ...@@ -143,7 +152,7 @@ func (r *ComplexRule) Rewrite(fs http.FileSystem, req *http.Request) bool {
} }
// attempt rewrite // attempt rewrite
return To(fs, req, r.To) return To(fs, req, r.To, replacer)
} }
// matchExt matches rPath against registered file extensions. // matchExt matches rPath against registered file extensions.
......
...@@ -31,6 +31,9 @@ func TestRewrite(t *testing.T) { ...@@ -31,6 +31,9 @@ func TestRewrite(t *testing.T) {
{"/abcd/", "ab", "/a/{dir}/{file}", ".html|"}, {"/abcd/", "ab", "/a/{dir}/{file}", ".html|"},
{"/abcde/", "ab", "/a#{fragment}", ".html|"}, {"/abcde/", "ab", "/a#{fragment}", ".html|"},
{"/ab/", `.*\.jpg`, "/ajpg", ""}, {"/ab/", `.*\.jpg`, "/ajpg", ""},
{"/reggrp", `/ad/([0-9]+)([a-z]*)`, "/a{1}/{2}", ""},
{"/reg2grp", `(.*)`, "/{1}", ""},
{"/reg3grp", `(.*)/(.*)/(.*)`, "/{1}{2}{3}", ""},
} }
for _, regexpRule := range regexps { for _, regexpRule := range regexps {
...@@ -81,6 +84,12 @@ func TestRewrite(t *testing.T) { ...@@ -81,6 +84,12 @@ func TestRewrite(t *testing.T) {
{"/abcde/abcde.html", "/a"}, {"/abcde/abcde.html", "/a"},
{"/abcde/abcde.html#1234", "/a#1234"}, {"/abcde/abcde.html#1234", "/a#1234"},
{"/ab/ab.jpg", "/ajpg"}, {"/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 { for i, test := range tests {
......
...@@ -6,14 +6,15 @@ import ( ...@@ -6,14 +6,15 @@ import (
"net/url" "net/url"
"path" "path"
"strings" "strings"
"github.com/mholt/caddy/middleware"
) )
// To attempts rewrite. It attempts to rewrite to first valid path // To attempts rewrite. It attempts to rewrite to first valid path
// or the last path if none of the paths are valid. // or the last path if none of the paths are valid.
// Returns true if rewrite is successful and false otherwise. // 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) tos := strings.Fields(to)
replacer := newReplacer(r)
// try each rewrite paths // try each rewrite paths
t := "" t := ""
......
...@@ -36,7 +36,7 @@ func TestTo(t *testing.T) { ...@@ -36,7 +36,7 @@ func TestTo(t *testing.T) {
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} }
To(fs, r, test.to) To(fs, r, test.to, newReplacer(r))
if uri(r.URL) != test.expected { if uri(r.URL) != test.expected {
t.Errorf("Test %v: expected %v found %v", i, test.expected, uri(r.URL)) t.Errorf("Test %v: expected %v found %v", i, test.expected, uri(r.URL))
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment