Skip to content

Commit 6826e70

Browse files
authored
golink: preserve query during HTTP -> HTTPS redirects (#164)
Preserve query parameters when redirecting from HTTP to HTTPS where previously they were omitted. Fixes #163 Signed-off-by: Patrick O'Doherty <[email protected]>
1 parent 9b756d5 commit 6826e70

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

golink.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,13 @@ func deleteLinkStats(link *Link) {
384384
// requests. It redirects all requests to the HTTPs version of the same URL.
385385
func redirectHandler(hostname string) http.Handler {
386386
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
387-
http.Redirect(w, r, (&url.URL{Scheme: "https", Host: hostname, Path: r.URL.Path}).String(), http.StatusFound)
387+
u := &url.URL{
388+
Scheme: "https",
389+
Host: hostname,
390+
Path: r.URL.Path,
391+
RawQuery: r.URL.RawQuery,
392+
}
393+
http.Redirect(w, r, u.String(), http.StatusFound)
388394
})
389395
}
390396

golink_test.go

+13
Original file line numberDiff line numberDiff line change
@@ -637,3 +637,16 @@ func TestNoHSTSShortDomain(t *testing.T) {
637637
})
638638
}
639639
}
640+
641+
func TestHTTPSRedirectHandlerWithQuery(t *testing.T) {
642+
h := redirectHandler("foobar.com")
643+
r := httptest.NewRequest("GET", "http://example.com/?query=bar", nil)
644+
w := httptest.NewRecorder()
645+
h.ServeHTTP(w, r)
646+
if w.Code != http.StatusFound {
647+
t.Errorf("got %d; want %d", w.Code, http.StatusFound)
648+
}
649+
if w.Header().Get("Location") != "https://foobar.com/?query=bar" {
650+
t.Errorf("got %q; want %q", w.Header().Get("Location"), "https://foobar.com/?query=bar")
651+
}
652+
}

0 commit comments

Comments
 (0)