diff options
author | Toby Allen <[email protected]> | 2017-01-24 05:15:27 +0000 |
---|---|---|
committer | Matt Holt <[email protected]> | 2017-01-23 22:15:27 -0700 |
commit | bbee961415ea829e632257f8e88fbbd0c8008f8e (patch) | |
tree | c5fd470c79a7cd047d5ee06d75e20d22e6fbc87f | |
parent | 59e6ceb5182f8fd99d6ef06ce7df2151eda6b0f3 (diff) | |
download | caddy-bbee961415ea829e632257f8e88fbbd0c8008f8e.tar.gz caddy-bbee961415ea829e632257f8e88fbbd0c8008f8e.zip |
Introduce new Replacer fields {rewrite_path}, {rewrite_path_escaped}; issue #1185 (#1364)
* Fix #1185
* Return normal path if no rewrite has happened
* Revert change, not required
* Updated tests
-rw-r--r-- | caddyhttp/httpserver/replacer.go | 16 | ||||
-rw-r--r-- | caddyhttp/rewrite/rewrite_test.go | 5 | ||||
-rw-r--r-- | caddyhttp/rewrite/to_test.go | 1 |
3 files changed, 20 insertions, 2 deletions
diff --git a/caddyhttp/httpserver/replacer.go b/caddyhttp/httpserver/replacer.go index 0278eb510..a3dc258a5 100644 --- a/caddyhttp/httpserver/replacer.go +++ b/caddyhttp/httpserver/replacer.go @@ -223,8 +223,22 @@ func (r *replacer) getSubstitution(key string) string { } return host case "{path}": - return r.request.URL.Path + // if a rewrite has happened, the original URI should be used as the path + // rather than the rewritten URI + path := r.request.Header.Get("Caddy-Rewrite-Original-URI") + if path == "" { + path = r.request.URL.Path + } + return path case "{path_escaped}": + path := r.request.Header.Get("Caddy-Rewrite-Original-URI") + if path == "" { + path = r.request.URL.Path + } + return url.QueryEscape(path) + case "{rewrite_path}": + return r.request.URL.Path + case "{rewrite_path_escaped}": return url.QueryEscape(r.request.URL.Path) case "{query}": return r.request.URL.RawQuery diff --git a/caddyhttp/rewrite/rewrite_test.go b/caddyhttp/rewrite/rewrite_test.go index 7468a19e0..bb86cbc19 100644 --- a/caddyhttp/rewrite/rewrite_test.go +++ b/caddyhttp/rewrite/rewrite_test.go @@ -24,7 +24,8 @@ func TestRewrite(t *testing.T) { regexps := [][]string{ {"/reg/", ".*", "/to", ""}, {"/r/", "[a-z]+", "/toaz", "!.html|"}, - {"/url/", "a([a-z0-9]*)s([A-Z]{2})", "/to/{path}", ""}, + {"/path/", "[a-z0-9]", "/to/{path}", ""}, + {"/url/", "a([a-z0-9]*)s([A-Z]{2})", "/to/{rewrite_path}", ""}, {"/ab/", "ab", "/ab?{query}", ".txt|"}, {"/ab/", "ab", "/ab?type=html&{query}", ".html|"}, {"/abc/", "ab", "/abc/{file}", ".html|"}, @@ -71,6 +72,8 @@ func TestRewrite(t *testing.T) { {"/r/z", "/toaz"}, {"/r/z.html", "/r/z.html"}, {"/r/z.js", "/toaz"}, + {"/path/a1b2c", "/to/path/a1b2c"}, + {"/path/d3e4f", "/to/path/d3e4f"}, {"/url/asAB", "/to/url/asAB"}, {"/url/aBsAB", "/url/aBsAB"}, {"/url/a00sAB", "/to/url/a00sAB"}, diff --git a/caddyhttp/rewrite/to_test.go b/caddyhttp/rewrite/to_test.go index 5100a5f8b..ad41bc324 100644 --- a/caddyhttp/rewrite/to_test.go +++ b/caddyhttp/rewrite/to_test.go @@ -23,6 +23,7 @@ func TestTo(t *testing.T) { {"/?a=b", "/testdir /index.php?{query}", "/index.php?a=b"}, {"/?a=b", "/testdir/ /index.php?{query}", "/testdir/?a=b"}, {"/test?url=http://", " /p/{path}?{query}", "/p/test?url=http://"}, + {"/test?url=http://", " /p/{rewrite_path}?{query}", "/p/test?url=http://"}, {"/test/?url=http://", " /{uri}", "/test/?url=http://"}, } |