-
Notifications
You must be signed in to change notification settings - Fork 56
/
hsts.go
42 lines (32 loc) · 902 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
35
36
37
38
39
40
41
42
package serverHandler
import (
"mjpclab.dev/ghfs/src/util"
"net/http"
)
func (h *aliasHandler) hsts(w http.ResponseWriter, r *http.Request) (needRedirect bool) {
_, port := util.ExtractHostnamePort(r.Host)
if len(port) > 0 {
return
}
header := w.Header()
header.Set("Strict-Transport-Security", "max-age=31536000")
if r.TLS != nil {
return
}
location := "https://" + r.Host + r.RequestURI
http.Redirect(w, r, location, http.StatusMovedPermanently)
return true
}
func (h *aliasHandler) https(w http.ResponseWriter, r *http.Request) (needRedirect bool) {
if r.TLS != nil {
return
}
hostname, _ := util.ExtractHostnamePort(r.Host)
var targetPort string
if len(h.httpsPort) > 0 && h.httpsPort != ":443" {
targetPort = h.httpsPort
}
location := "https://" + hostname + targetPort + r.RequestURI
http.Redirect(w, r, location, http.StatusMovedPermanently)
return true
}