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

https.go « handlers « internal - gitlab.com/gitlab-org/gitlab-pages.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 5598a8fc3236928a7b2ceceaa85d95f788b42fa5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package handlers

import (
	"net/http"

	"gitlab.com/gitlab-org/gitlab-pages/internal/request"
)

func HTTPSRedirectMiddleware(handler http.Handler, redirect bool) http.Handler {
	if !redirect {
		return handler
	}

	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		if !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)
}