From e486c9c6e7717f79b1a5d9e1701a721ddc3f361e Mon Sep 17 00:00:00 2001 From: Pedro Nasser Date: Wed, 15 Jun 2016 19:55:02 -0300 Subject: [PATCH 1/2] fix rewrite bug with url query + test case --- caddyhttp/rewrite/to.go | 13 ++++++++++--- caddyhttp/rewrite/to_test.go | 1 + 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/caddyhttp/rewrite/to.go b/caddyhttp/rewrite/to.go index 2cfe1de4..87c0b26b 100644 --- a/caddyhttp/rewrite/to.go +++ b/caddyhttp/rewrite/to.go @@ -18,8 +18,15 @@ func To(fs http.FileSystem, r *http.Request, to string, replacer httpserver.Repl // try each rewrite paths t := "" + query := "" for _, v := range tos { - t = path.Clean(replacer.Replace(v)) + t = replacer.Replace(v) + tparts := strings.Split(t, "?") + t = path.Clean(tparts[0]) + + if len(tparts) > 1 { + query = tparts[1] + } // add trailing slash for directories, if present if strings.HasSuffix(v, "/") && !strings.HasSuffix(t, "/") { @@ -47,9 +54,9 @@ func To(fs http.FileSystem, r *http.Request, to string, replacer httpserver.Repl // perform rewrite r.URL.Path = u.Path - if u.RawQuery != "" { + if query != "" { // overwrite query string if present - r.URL.RawQuery = u.RawQuery + r.URL.RawQuery = query } if u.Fragment != "" { // overwrite fragment if present diff --git a/caddyhttp/rewrite/to_test.go b/caddyhttp/rewrite/to_test.go index 6133c0b6..75b7156d 100644 --- a/caddyhttp/rewrite/to_test.go +++ b/caddyhttp/rewrite/to_test.go @@ -22,6 +22,7 @@ func TestTo(t *testing.T) { {"/?a=b", "/testfile /index.php?{query}", "/testfile?a=b"}, {"/?a=b", "/testdir /index.php?{query}", "/index.php?a=b"}, {"/?a=b", "/testdir/ /index.php?{query}", "/testdir/?a=b"}, + {"/test?url=http://caddyserver.com", " /p/{path}?{query}", "/p/test?url=http://caddyserver.com"}, } uri := func(r *url.URL) string { From 54355d8fb3bdb3cfa40167572da5d542c933c5a3 Mon Sep 17 00:00:00 2001 From: Pedro Nasser Date: Thu, 16 Jun 2016 10:03:31 -0300 Subject: [PATCH 2/2] replace strings.Split for SplitN --- caddyhttp/rewrite/to.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/caddyhttp/rewrite/to.go b/caddyhttp/rewrite/to.go index 87c0b26b..c49d4da1 100644 --- a/caddyhttp/rewrite/to.go +++ b/caddyhttp/rewrite/to.go @@ -21,7 +21,7 @@ func To(fs http.FileSystem, r *http.Request, to string, replacer httpserver.Repl query := "" for _, v := range tos { t = replacer.Replace(v) - tparts := strings.Split(t, "?") + tparts := strings.SplitN(t, "?", 2) t = path.Clean(tparts[0]) if len(tparts) > 1 {