Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitlab-pages.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorfeistel <6742251-feistel@users.noreply.gitlab.com>2022-04-22 14:44:53 +0300
committerfeistel <6742251-feistel@users.noreply.gitlab.com>2022-04-22 14:44:53 +0300
commit83fd26e1ea6d443d7b6e3f99fec7f3f8fb942731 (patch)
treea9ba8162a1aeb10fccbb2213dd351429b95ced2b /internal
parent60864cb8ebd9b79abc57fd29d21241cdacb21efd (diff)
Move https middleware to internal/handlers
Diffstat (limited to 'internal')
-rw-r--r--internal/handlers/https.go27
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)
+}