implement redirecting to https if configured and https server is running
This commit is contained in:
parent
f0536b6347
commit
1515c353f8
20
helpers.go
20
helpers.go
|
@ -9,6 +9,7 @@ import (
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
@ -138,12 +139,31 @@ func preInstallHandler(handler http.Handler) http.Handler {
|
||||||
}
|
}
|
||||||
|
|
||||||
// postInstall lets the handler run only if firstRun is false, and redirects to /install.html otherwise
|
// postInstall lets the handler run only if firstRun is false, and redirects to /install.html otherwise
|
||||||
|
// it also enforces HTTPS if it is enabled and configured
|
||||||
func postInstall(handler func(http.ResponseWriter, *http.Request)) func(http.ResponseWriter, *http.Request) {
|
func postInstall(handler func(http.ResponseWriter, *http.Request)) func(http.ResponseWriter, *http.Request) {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
if config.firstRun && !strings.HasPrefix(r.URL.Path, "/install.") {
|
if config.firstRun && !strings.HasPrefix(r.URL.Path, "/install.") {
|
||||||
http.Redirect(w, r, "/install.html", http.StatusSeeOther) // should not be cacheable
|
http.Redirect(w, r, "/install.html", http.StatusSeeOther) // should not be cacheable
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
// enforce https?
|
||||||
|
if config.TLS.ForceHTTPS && r.TLS == nil && config.TLS.Enabled && config.TLS.PortHTTPS != 0 && httpsServer.server != nil {
|
||||||
|
// yes, and we want host from host:port
|
||||||
|
host, _, err := net.SplitHostPort(r.Host)
|
||||||
|
if err != nil {
|
||||||
|
// no port in host
|
||||||
|
host = r.Host
|
||||||
|
}
|
||||||
|
// construct new URL to redirect to
|
||||||
|
newURL := url.URL{
|
||||||
|
Scheme: "https",
|
||||||
|
Host: net.JoinHostPort(host, strconv.Itoa(config.TLS.PortHTTPS)),
|
||||||
|
Path: r.URL.Path,
|
||||||
|
RawQuery: r.URL.RawQuery,
|
||||||
|
}
|
||||||
|
http.Redirect(w, r, newURL.String(), http.StatusTemporaryRedirect)
|
||||||
|
return
|
||||||
|
}
|
||||||
handler(w, r)
|
handler(w, r)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue