Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion golink.go
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,13 @@ func deleteLinkStats(link *Link) {
// requests. It redirects all requests to the HTTPs version of the same URL.
func redirectHandler(hostname string) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, (&url.URL{Scheme: "https", Host: hostname, Path: r.URL.Path}).String(), http.StatusFound)
u := &url.URL{
Scheme: "https",
Host: hostname,
Path: r.URL.Path,
RawQuery: r.URL.RawQuery,
}
http.Redirect(w, r, u.String(), http.StatusFound)
})
}

Expand Down
13 changes: 13 additions & 0 deletions golink_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -637,3 +637,16 @@ func TestNoHSTSShortDomain(t *testing.T) {
})
}
}

func TestHTTPSRedirectHandlerWithQuery(t *testing.T) {
h := redirectHandler("foobar.com")
r := httptest.NewRequest("GET", "http://example.com/?query=bar", nil)
w := httptest.NewRecorder()
h.ServeHTTP(w, r)
if w.Code != http.StatusFound {
t.Errorf("got %d; want %d", w.Code, http.StatusFound)
}
if w.Header().Get("Location") != "https://foobar.com/?query=bar" {
t.Errorf("got %q; want %q", w.Header().Get("Location"), "https://foobar.com/?query=bar")
}
}
Loading