Skip to content

Commit d318310

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 7b28154 commit d318310

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
@@ -51,6 +51,26 @@ and it takes care of all the other things for you`,
5151
},
5252
}
5353

54+
func runHTTPRedirector() {
55+
source := setting.HTTPAddr + ":80"
56+
dest := strings.TrimSuffix(setting.AppURL, "/")
57+
log.Info("Redirecting: %s to %s", source, dest)
58+
59+
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
60+
target := dest + r.URL.Path
61+
if len(r.URL.RawQuery) > 0 {
62+
target += "?" + r.URL.RawQuery
63+
}
64+
http.Redirect(w, r, target, http.StatusTemporaryRedirect)
65+
})
66+
67+
var err = runHTTP(source, context2.ClearHandler(handler))
68+
69+
if err != nil {
70+
log.Fatal(4, "Failed to start port 80 redirector: %v", err)
71+
}
72+
}
73+
5474
func runWeb(ctx *cli.Context) error {
5575
if ctx.IsSet("config") {
5676
setting.CustomConf = ctx.String("config")
@@ -124,6 +144,9 @@ func runWeb(ctx *cli.Context) error {
124144
case setting.HTTP:
125145
err = runHTTP(listenAddr, context2.ClearHandler(m))
126146
case setting.HTTPS:
147+
if setting.RedirectPort80 {
148+
go runHTTPRedirector()
149+
}
127150
err = runHTTPS(listenAddr, setting.CertFile, setting.KeyFile, context2.ClearHandler(m))
128151
case setting.FCGI:
129152
listener, err := net.Listen("tcp", listenAddr)

custom/conf/app.ini.sample

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,9 @@ ROOT_URL = %(PROTOCOL)s://%(DOMAIN)s:%(HTTP_PORT)s/
107107
; Listen address. Either a IPv4/IPv6 address or the path to a unix socket.
108108
HTTP_ADDR = 0.0.0.0
109109
HTTP_PORT = 3000
110+
; If true, and PROTOCOL is set to https, http requests on port 80 will be
111+
; redirected to ROOT_URL. Default is false.
112+
REDIRECT_PORT_80 = false
110113
; Permission for unix socket
111114
UNIX_SOCKET_PERMISSION = 666
112115
; 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
@@ -89,6 +89,7 @@ var (
8989
HTTPAddr string
9090
HTTPPort string
9191
LocalURL string
92+
RedirectPort80 bool
9293
OfflineMode bool
9394
DisableRouterLog bool
9495
CertFile string
@@ -732,6 +733,7 @@ func NewContext() {
732733
defaultLocalURL += ":" + HTTPPort + "/"
733734
}
734735
LocalURL = sec.Key("LOCAL_ROOT_URL").MustString(defaultLocalURL)
736+
RedirectPort80 = sec.Key("REDIRECT_PORT_80").MustBool(false)
735737
OfflineMode = sec.Key("OFFLINE_MODE").MustBool()
736738
DisableRouterLog = sec.Key("DISABLE_ROUTER_LOG").MustBool()
737739
StaticRootPath = sec.Key("STATIC_ROOT_PATH").MustString(AppWorkPath)

0 commit comments

Comments
 (0)