Skip to content

Commit ea729d7

Browse files
committed
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. Signed-off-by: Mike Fellows <[email protected]>
1 parent ccd3577 commit ea729d7

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed

cmd/web.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,26 @@ and it takes care of all the other things for you`,
4848
},
4949
}
5050

51+
func runHTTPRedirector() {
52+
source := setting.HTTPAddr + ":80"
53+
dest := strings.TrimSuffix(setting.AppURL, "/")
54+
log.Info("Redirecting: %s to %s", source, dest)
55+
56+
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
57+
target := dest + r.URL.Path
58+
if len(r.URL.RawQuery) > 0 {
59+
target += "?" + r.URL.RawQuery
60+
}
61+
http.Redirect(w, r, target, http.StatusTemporaryRedirect)
62+
})
63+
64+
var err = runHTTP(source, context2.ClearHandler(handler))
65+
66+
if err != nil {
67+
log.Fatal(4, "Failed to start port 80 redirector: %v", err)
68+
}
69+
}
70+
5171
func runWeb(ctx *cli.Context) error {
5272
if ctx.IsSet("config") {
5373
setting.CustomConf = ctx.String("config")
@@ -91,6 +111,9 @@ func runWeb(ctx *cli.Context) error {
91111
case setting.HTTP:
92112
err = runHTTP(listenAddr, context2.ClearHandler(m))
93113
case setting.HTTPS:
114+
if setting.RedirectPort80 {
115+
go runHTTPRedirector()
116+
}
94117
err = runHTTPS(listenAddr, setting.CertFile, setting.KeyFile, context2.ClearHandler(m))
95118
case setting.FCGI:
96119
listener, err := net.Listen("tcp", listenAddr)

conf/app.ini

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,9 @@ ROOT_URL = %(PROTOCOL)s://%(DOMAIN)s:%(HTTP_PORT)s/
103103
; Listen address. Either a IPv4/IPv6 address or the path to a unix socket.
104104
HTTP_ADDR = 0.0.0.0
105105
HTTP_PORT = 3000
106+
; If true, and PROTOCOL is set to https, http requests on port 80 will be
107+
; redirected to ROOT_URL. Default is false.
108+
REDIRECT_PORT_80 = false
106109
; Permission for unix socket
107110
UNIX_SOCKET_PERMISSION = 666
108111
; Local (DMZ) URL for Gitea workers (such as SSH update) accessing web service.

modules/setting/setting.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ var (
7777
HTTPAddr string
7878
HTTPPort string
7979
LocalURL string
80+
RedirectPort80 bool
8081
OfflineMode bool
8182
DisableRouterLog bool
8283
CertFile string
@@ -689,6 +690,7 @@ func NewContext() {
689690
defaultLocalURL += ":" + HTTPPort + "/"
690691
}
691692
LocalURL = sec.Key("LOCAL_ROOT_URL").MustString(defaultLocalURL)
693+
RedirectPort80 = sec.Key("REDIRECT_PORT_80").MustBool(false)
692694
OfflineMode = sec.Key("OFFLINE_MODE").MustBool()
693695
DisableRouterLog = sec.Key("DISABLE_ROUTER_LOG").MustBool()
694696
StaticRootPath = sec.Key("STATIC_ROOT_PATH").MustString(workDir)

0 commit comments

Comments
 (0)