Skip to content
This repository was archived by the owner on Jan 16, 2021. It is now read-only.

Commit c291f4d

Browse files
committed
enforce HTTPS
Redirect HTTP links to HTTPS and set HSTS correctly. This is specific to the godoc.org set up (with nginx passing a X-Scheme header back) and without fixing up api.godoc.org. Fixes #304.
1 parent cb58823 commit c291f4d

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

gddo-server/https.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package main
2+
3+
import "net/http"
4+
5+
type httpsEnforcerHandler struct {
6+
h http.Handler
7+
}
8+
9+
func (h httpsEnforcerHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
10+
if r.Host == "godoc.org" {
11+
w.Header().Add("Strict-Transport-Security", "max-age=631138519; includeSubdomains; preload")
12+
if r.Header.Get("X-Scheme") != "https" {
13+
r.URL.Scheme = "https"
14+
http.Redirect(w, r, r.URL.String(), http.StatusFound)
15+
return
16+
}
17+
}
18+
h.h.ServeHTTP(w, r)
19+
}

gddo-server/main.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -898,7 +898,11 @@ func main() {
898898

899899
cacheBusters.Handler = mux
900900

901-
if err := http.ListenAndServe(*httpAddr, hostMux{{"api.", apiMux}, {"", mux}}); err != nil {
901+
allMux := httpsEnforcerHandler{
902+
hostMux{{"api.", apiMux}, {"", mux}},
903+
}
904+
905+
if err := http.ListenAndServe(*httpAddr, allMux); err != nil {
902906
log.Fatal(err)
903907
}
904908
}

0 commit comments

Comments
 (0)