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

Support for rewrite match group.

This commit is contained in:
Abiola Ibrahim 2015-12-30 21:47:37 +01:00
parent 55aa492dc1
commit 3c086fb2e6
4 changed files with 25 additions and 6 deletions

View file

@ -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.

View file

@ -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 {

View file

@ -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 := ""

View file

@ -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))
}