Commit 04514fb7 authored by Abiola Ibrahim's avatar Abiola Ibrahim

Fix for #659.

parent 4f5fe2de
...@@ -5,6 +5,7 @@ package rewrite ...@@ -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) ...@@ -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])
} }
} }
......
...@@ -34,6 +34,7 @@ func TestRewrite(t *testing.T) { ...@@ -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) { ...@@ -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) { ...@@ -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
} }
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