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

Merge pull request #684 from abiosoft/master

Fix for #659.
This commit is contained in:
Abiola Ibrahim 2016-03-18 07:08:24 +01:00
commit 6b801b111b
2 changed files with 16 additions and 1 deletions

View file

@ -5,6 +5,7 @@ package rewrite
import ( import (
"fmt" "fmt"
"net/http" "net/http"
"net/url"
"path" "path"
"path/filepath" "path/filepath"
"regexp" "regexp"
@ -165,7 +166,17 @@ func (r *ComplexRule) Rewrite(fs http.FileSystem, req *http.Request) (re Result)
return return
default: default:
// set regexp match variables {1}, {2} ... // set regexp match variables {1}, {2} ...
// url escaped values of ? and #.
q, f := url.QueryEscape("?"), url.QueryEscape("#")
for i := 1; i < len(matches); i++ { for i := 1; i < len(matches); i++ {
// Special case of unescaped # and ? by stdlib regexp.
// Reverse the unescape.
if strings.ContainsAny(matches[i], "?#") {
matches[i] = strings.NewReplacer("?", q, "#", f).Replace(matches[i])
}
replacer.Set(fmt.Sprint(i), matches[i]) replacer.Set(fmt.Sprint(i), matches[i])
} }
} }

View file

@ -34,6 +34,7 @@ func TestRewrite(t *testing.T) {
{"/reggrp", `/ad/([0-9]+)([a-z]*)`, "/a{1}/{2}", ""}, {"/reggrp", `/ad/([0-9]+)([a-z]*)`, "/a{1}/{2}", ""},
{"/reg2grp", `(.*)`, "/{1}", ""}, {"/reg2grp", `(.*)`, "/{1}", ""},
{"/reg3grp", `(.*)/(.*)/(.*)`, "/{1}{2}{3}", ""}, {"/reg3grp", `(.*)/(.*)/(.*)`, "/{1}{2}{3}", ""},
{"/hashtest", "(.*)", "/{1}", ""},
} }
for _, regexpRule := range regexps { for _, regexpRule := range regexps {
@ -90,6 +91,9 @@ func TestRewrite(t *testing.T) {
{"/reg2grp/ad/124abc", "/ad/124abc"}, {"/reg2grp/ad/124abc", "/ad/124abc"},
{"/reg3grp/ad/aa/66", "/adaa66"}, {"/reg3grp/ad/aa/66", "/adaa66"},
{"/reg3grp/ad612/n1n/ab", "/ad612n1nab"}, {"/reg3grp/ad612/n1n/ab", "/ad612n1nab"},
{"/hashtest/a%20%23%20test", "/a%20%23%20test"},
{"/hashtest/a%20%3F%20test", "/a%20%3F%20test"},
{"/hashtest/a%20%3F%23test", "/a%20%3F%23test"},
} }
for i, test := range tests { for i, test := range tests {
@ -154,6 +158,6 @@ func TestRewrite(t *testing.T) {
} }
func urlPrinter(w http.ResponseWriter, r *http.Request) (int, error) { func urlPrinter(w http.ResponseWriter, r *http.Request) (int, error) {
fmt.Fprintf(w, r.URL.String()) fmt.Fprint(w, r.URL.String())
return 0, nil return 0, nil
} }