-
Notifications
You must be signed in to change notification settings - Fork 56
/
hsts.go
34 lines (27 loc) · 806 Bytes
/
hsts.go
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
32
33
34
package serverHandler
import (
"mjpclab.dev/ghfs/src/util"
"net/http"
)
func (h *aliasHandler) tryHsts(w http.ResponseWriter, r *http.Request) (needRedirect bool) {
if r.TLS != nil {
w.Header().Set("Strict-Transport-Security", "max-age="+h.hstsMaxAge)
return
}
location := "https://" + r.Host + r.RequestURI
http.Redirect(w, r, location, getRedirectCode(r))
return true
}
func (h *aliasHandler) tryToHttps(w http.ResponseWriter, r *http.Request) (needRedirect bool) {
if r.TLS != nil {
return
}
hostname, _ := util.ExtractHostnamePort(r.Host)
var targetPort string
if len(h.toHttpsPort) > 0 && h.toHttpsPort != ":443" {
targetPort = h.toHttpsPort
}
location := "https://" + hostname + targetPort + r.RequestURI
http.Redirect(w, r, location, getRedirectCode(r))
return true
}