diff options
author | feistel <6742251-feistel@users.noreply.gitlab.com> | 2022-04-20 18:57:30 +0300 |
---|---|---|
committer | feistel <6742251-feistel@users.noreply.gitlab.com> | 2022-04-20 18:57:30 +0300 |
commit | 60864cb8ebd9b79abc57fd29d21241cdacb21efd (patch) | |
tree | ee4a753189c5b54a9c5e90e06f1558089a5ba10d | |
parent | dabfd71101d4598f31ab474f6a7bfc75bfa0a045 (diff) |
Add early https redirect
-rw-r--r-- | app.go | 20 |
1 files changed, 14 insertions, 6 deletions
@@ -117,12 +117,6 @@ func (a *theApp) checkAuthAndServeNotFound(domain *domain.Domain, w http.Respons } func (a *theApp) tryAuxiliaryHandlers(w http.ResponseWriter, r *http.Request, https bool, host string, domain *domain.Domain) bool { - // Add auto redirect - if !https && a.config.General.RedirectHTTP { - a.redirectToHTTPS(w, r, http.StatusTemporaryRedirect) - return true - } - if a.Handlers.HandleArtifactRequest(host, w, r) { return true } @@ -188,6 +182,17 @@ func (a *theApp) auxiliaryMiddleware(handler http.Handler) http.Handler { }) } +func (a *theApp) httpsRedirectMiddleware(handler http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if a.config.General.RedirectHTTP && !request.IsHTTPS(r) { + a.redirectToHTTPS(w, r, http.StatusTemporaryRedirect) + return + } + + handler.ServeHTTP(w, r) + }) +} + // serveFileOrNotFoundHandler will serve static content or // return a 404 Not Found response func (a *theApp) serveFileOrNotFoundHandler() http.Handler { @@ -255,6 +260,9 @@ func (a *theApp) buildHandlerPipeline() (http.Handler, error) { // Custom response headers handler = customheaders.NewMiddleware(handler, a.CustomHeaders) + // Add auto redirect + handler = a.httpsRedirectMiddleware(handler) + // Correlation ID injection middleware var correlationOpts []correlation.InboundHandlerOption if a.config.General.PropagateCorrelationID { |