diff options
author | feistel <6742251-feistel@users.noreply.gitlab.com> | 2022-04-22 14:44:53 +0300 |
---|---|---|
committer | feistel <6742251-feistel@users.noreply.gitlab.com> | 2022-04-22 14:44:53 +0300 |
commit | 83fd26e1ea6d443d7b6e3f99fec7f3f8fb942731 (patch) | |
tree | a9ba8162a1aeb10fccbb2213dd351429b95ced2b /internal/handlers/https.go | |
parent | 60864cb8ebd9b79abc57fd29d21241cdacb21efd (diff) |
Move https middleware to internal/handlers
Diffstat (limited to 'internal/handlers/https.go')
-rw-r--r-- | internal/handlers/https.go | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/internal/handlers/https.go b/internal/handlers/https.go new file mode 100644 index 00000000..84c7e159 --- /dev/null +++ b/internal/handlers/https.go @@ -0,0 +1,27 @@ +package handlers + +import ( + "net/http" + + "gitlab.com/gitlab-org/gitlab-pages/internal/request" +) + +func HTTPSRedirectMiddleware(handler http.Handler, redirect bool) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if redirect && !request.IsHTTPS(r) { + redirectToHTTPS(w, r, http.StatusTemporaryRedirect) + return + } + + handler.ServeHTTP(w, r) + }) +} + +func redirectToHTTPS(w http.ResponseWriter, r *http.Request, statusCode int) { + u := *r.URL + u.Scheme = request.SchemeHTTPS + u.Host = r.Host + u.User = nil + + http.Redirect(w, r, u.String(), statusCode) +} |