Skip to content

Commit 1535d0f

Browse files
authored
safeweb: move http.Serve for HTTP redirects into lib (tailscale#11592)
Refactor the interaction between caller/library when establishing the HTTP to HTTPS redirects by moving the call to http.Serve into safeweb. This makes linting for other uses of http.Serve easier without having to account for false positives created by the old interface. Updates tailscale/corp#8027 Signed-off-by: Patrick O'Doherty <patrick@tailscale.com>
1 parent f384742 commit 1535d0f

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

safeweb/http.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -242,10 +242,12 @@ func (s *Server) serveBrowser(w http.ResponseWriter, r *http.Request) {
242242
s.csrfProtect(s.BrowserMux).ServeHTTP(w, r)
243243
}
244244

245-
// RedirectHTTP returns a handler that redirects all incoming HTTP requests to
246-
// the provided fully qualified domain name (FQDN).
247-
func (s *Server) RedirectHTTP(fqdn string) http.Handler {
248-
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
245+
// ServeRedirectHTTP serves a single HTTP handler on the provided listener that
246+
// redirects all incoming HTTP requests to the HTTPS address of the provided
247+
// fully qualified domain name (FQDN). Callers are responsible for closing the
248+
// listener.
249+
func (s *Server) ServeRedirectHTTP(ln net.Listener, fqdn string) error {
250+
return http.Serve(ln, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
249251
new := url.URL{
250252
Scheme: "https",
251253
Host: fqdn,
@@ -254,7 +256,7 @@ func (s *Server) RedirectHTTP(fqdn string) http.Handler {
254256
}
255257

256258
http.Redirect(w, r, new.String(), http.StatusMovedPermanently)
257-
})
259+
}))
258260
}
259261

260262
// Serve starts the server and listens on the provided listener. It will block

0 commit comments

Comments
 (0)