cmd/routed: configure the http listener to redirect if the method allows redirecting

This commit is contained in:
Cadey Ratio 2017-10-01 11:29:31 -07:00
parent 0319e6dd9f
commit eacd9781f7
No known key found for this signature in database
GPG Key ID: D607EE27C2E7F89A
1 changed files with 10 additions and 3 deletions

View File

@ -54,14 +54,21 @@ func main() {
defer l.Close() defer l.Close()
hs := &http.Server{ hs := &http.Server{
Handler: middleware.Trace(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case http.MethodPatch, http.MethodPut, http.MethodPost:
http.Error(w, "use https", http.StatusNotAcceptable)
ln.Log(r.Context(), ln.Action("cannot redirect (wrong method)"), ln.F{"remote": r.RemoteAddr, "host": r.Host, "path": r.URL.Path})
return
}
r.URL.Host = r.Host r.URL.Host = r.Host
r.URL.Scheme = "https" r.URL.Scheme = "https"
ln.Log(r.Context(), ln.Action("redirecting insecure HTTP to HTTPS")) ln.Log(r.Context(), ln.Action("redirecting insecure HTTP to HTTPS"), ln.F{"remote": r.RemoteAddr, "host": r.Host, "path": r.URL.Path})
http.Redirect(w, r, r.URL.String(), http.StatusPermanentRedirect) http.Redirect(w, r, r.URL.String(), http.StatusPermanentRedirect)
})), }),
Addr: scfg.WebAddr, Addr: scfg.WebAddr,
} }