diff options
author | Mike Fellows <Mike.Fellows@shaw.ca> | 2017-12-25 17:23:43 -0500 |
---|---|---|
committer | Kim "BKC" Carlbäcker <kim.carlbacker@gmail.com> | 2017-12-25 23:23:43 +0100 |
commit | fabf3f2fc29e143dabefd504cda78d3f47807d2c (patch) | |
tree | b808f7056694f556d203d9223fd2d72a5ba1860c /cmd | |
parent | f5155b99136b6e19ab494a97adfcee1810a3d5e7 (diff) | |
download | gitea-fabf3f2fc29e143dabefd504cda78d3f47807d2c.tar.gz gitea-fabf3f2fc29e143dabefd504cda78d3f47807d2c.zip |
Add an option to allow redirect of http port 80 to https. (#1928)
* Add an option to allow redirect of http port 80 to https.
This is an "opt in" option (default is to not redirect). It will only redirect
if protocol is https and the new REDIRECT_PORT_80 option is set to true.
The Port to redirect in previous commit was hardcoded to 80, now it can be
specified in the app.ini, defaulting to 80. The boolean option to turn
redirection on has been changed to REDIRECT_OTHER_PORT to be logically
consistent with the new port option.
Signed-off-by: Mike Fellows <mike.fellows@shaw.ca>
Diffstat (limited to 'cmd')
-rw-r--r-- | cmd/web.go | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/cmd/web.go b/cmd/web.go index 55546ea480..473688a8b8 100644 --- a/cmd/web.go +++ b/cmd/web.go @@ -51,6 +51,26 @@ and it takes care of all the other things for you`, }, } +func runHTTPRedirector() { + source := fmt.Sprintf("%s:%s", setting.HTTPAddr, setting.PortToRedirect) + dest := strings.TrimSuffix(setting.AppURL, "/") + log.Info("Redirecting: %s to %s", source, dest) + + handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + target := dest + r.URL.Path + if len(r.URL.RawQuery) > 0 { + target += "?" + r.URL.RawQuery + } + http.Redirect(w, r, target, http.StatusTemporaryRedirect) + }) + + var err = runHTTP(source, context2.ClearHandler(handler)) + + if err != nil { + log.Fatal(4, "Failed to start port redirection: %v", err) + } +} + func runWeb(ctx *cli.Context) error { if ctx.IsSet("config") { setting.CustomConf = ctx.String("config") @@ -124,6 +144,9 @@ func runWeb(ctx *cli.Context) error { case setting.HTTP: err = runHTTP(listenAddr, context2.ClearHandler(m)) case setting.HTTPS: + if setting.RedirectOtherPort { + go runHTTPRedirector() + } err = runHTTPS(listenAddr, setting.CertFile, setting.KeyFile, context2.ClearHandler(m)) case setting.FCGI: listener, err := net.Listen("tcp", listenAddr) |