diff options
author | Matt Holt <[email protected]> | 2015-07-13 09:42:19 -0600 |
---|---|---|
committer | Matt Holt <[email protected]> | 2015-07-13 09:42:19 -0600 |
commit | fcf2622c260f81cb24e12bc4703f401a3a47a075 (patch) | |
tree | f2f50f40af79722e479685fcceb7c74d5d1c06e7 | |
parent | 1a82943db245db398c250c08871b25009f029cc1 (diff) | |
parent | d9ebc5398ac6b3583eb848590ac94046f01ea4cb (diff) | |
download | caddy-fcf2622c260f81cb24e12bc4703f401a3a47a075.tar.gz caddy-fcf2622c260f81cb24e12bc4703f401a3a47a075.zip |
Merge pull request #187 from evermax/master
redir: Preserve query string on catch-all redirect (fixes #180)
-rw-r--r-- | middleware/redirect/redirect.go | 19 | ||||
-rw-r--r-- | middleware/redirect/redirect_test.go | 37 |
2 files changed, 55 insertions, 1 deletions
diff --git a/middleware/redirect/redirect.go b/middleware/redirect/redirect.go index f9a1812b3..2c5de4103 100644 --- a/middleware/redirect/redirect.go +++ b/middleware/redirect/redirect.go @@ -6,6 +6,8 @@ import ( "fmt" "html" "net/http" + "net/url" + "path" "strings" "github.com/mholt/caddy/middleware" @@ -22,7 +24,22 @@ func (rd Redirect) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error for _, rule := range rd.Rules { if rule.From == "/" { // Catchall redirect preserves path (TODO: Standardize/formalize this behavior) - newPath := strings.TrimSuffix(rule.To, "/") + r.URL.Path + toURL, err := url.Parse(rule.To) + if err != nil { + return http.StatusInternalServerError, err + } + newPath := path.Join(toURL.Host, toURL.Path, r.URL.Path) + if strings.HasSuffix(r.URL.Path, "/") { + newPath = newPath + "/" + } + newPath = toURL.Scheme + "://" + newPath + parameters := toURL.Query() + for k, v := range r.URL.Query() { + parameters.Set(k, v[0]) + } + if len(parameters) > 0 { + newPath = newPath + "?" + parameters.Encode() + } if rule.Meta { fmt.Fprintf(w, metaRedir, html.EscapeString(newPath)) } else { diff --git a/middleware/redirect/redirect_test.go b/middleware/redirect/redirect_test.go index 662010f17..07c83116a 100644 --- a/middleware/redirect/redirect_test.go +++ b/middleware/redirect/redirect_test.go @@ -39,6 +39,43 @@ func TestMetaRedirect(t *testing.T) { } } +func TestParametersRedirect(t *testing.T) { + re := Redirect{ + Rules: []Rule{ + {From: "/", Meta: false, To: "http://example.com/"}, + }, + } + + req, err := http.NewRequest("GET", "/a?b=c", nil) + if err != nil { + t.Fatalf("Test: Could not create HTTP request: %v", err) + } + + rec := httptest.NewRecorder() + re.ServeHTTP(rec, req) + + if "http://example.com/a?b=c" != rec.Header().Get("Location") { + t.Fatalf("Test: expected location header %q but was %q", "http://example.com/a?b=c", rec.Header().Get("Location")) + } + + re = Redirect{ + Rules: []Rule{ + {From: "/", Meta: false, To: "http://example.com/a?b=c"}, + }, + } + + req, err = http.NewRequest("GET", "/d?e=f", nil) + if err != nil { + t.Fatalf("Test: Could not create HTTP request: %v", err) + } + + re.ServeHTTP(rec, req) + + if "http://example.com/a/d?b=c&e=f" != rec.Header().Get("Location") { + t.Fatalf("Test: expected location header %q but was %q", "http://example.com/a/d?b=c&e=f", rec.Header().Get("Location")) + } +} + func TestRedirect(t *testing.T) { for i, test := range []struct { from string |