cmd/routed: configure the http listener to redirect if the method allows redirecting
This commit is contained in:
parent
0319e6dd9f
commit
eacd9781f7
|
@ -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,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue