From a07814f567f2325b0253f7b9a301346b8a4f2fe6 Mon Sep 17 00:00:00 2001 From: Saswat Padhi Date: Sun, 27 Nov 2022 05:02:53 +0000 Subject: [PATCH 1/2] Normalize `AppURL` according to RFC 3986 Scheme-based normalization (RFC 3986, section 6.2.3) was already implemented, but only for `defaultAppURL`. This commit implements the same for `AppURL`. Signed-off-by: Saswat Padhi --- modules/setting/setting.go | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/modules/setting/setting.go b/modules/setting/setting.go index df7310b09b467..34416aea63a0d 100644 --- a/modules/setting/setting.go +++ b/modules/setting/setting.go @@ -749,19 +749,23 @@ func loadFromConf(allowEmpty bool, extraConfig string) { PerWriteTimeout = sec.Key("PER_WRITE_TIMEOUT").MustDuration(PerWriteTimeout) PerWritePerKbTimeout = sec.Key("PER_WRITE_PER_KB_TIMEOUT").MustDuration(PerWritePerKbTimeout) - defaultAppURL := string(Protocol) + "://" + Domain - if (Protocol == HTTP && HTTPPort != "80") || (Protocol == HTTPS && HTTPPort != "443") { - defaultAppURL += ":" + HTTPPort - } - AppURL = sec.Key("ROOT_URL").MustString(defaultAppURL + "/") - // This should be TrimRight to ensure that there is only a single '/' at the end of AppURL. - AppURL = strings.TrimRight(AppURL, "/") + "/" + defaultAppURL := string(Protocol) + "://" + Domain + ":" + HTTPPort + AppURL = sec.Key("ROOT_URL").MustString(defaultAppURL) - // Check if has app suburl. + // Check validity of AppURL appURL, err := url.Parse(AppURL) if err != nil { log.Fatal("Invalid ROOT_URL '%s': %s", AppURL, err) + } else { + // Remove default ports from AppURL. + // (scheme-based URL normalization, RFC 3986 section 6.2.3) + if (appURL.Scheme == string(HTTP) && appURL.Port() == "80") || (appURL.Scheme == string(HTTPS) && appURL.Port() == "443") { + appURL.Host = appURL.Hostname() + } + // This should be TrimRight to ensure that there is only a single '/' at the end of AppURL. + AppURL = strings.TrimRight(appURL.String(), "/") + "/" } + // Suburl should start with '/' and end without '/', such as '/{subpath}'. // This value is empty if site does not have sub-url. AppSubURL = strings.TrimSuffix(appURL.Path, "/") From 4f5ebbf98c52d019ad9749513583db4d07910565 Mon Sep 17 00:00:00 2001 From: Saswat Padhi Date: Tue, 29 Nov 2022 02:59:26 +0000 Subject: [PATCH 2/2] [minor] remove unnecessary `else` case Signed-off-by: Saswat Padhi --- modules/setting/setting.go | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/modules/setting/setting.go b/modules/setting/setting.go index 5eef924f56164..f0d7f029271c9 100644 --- a/modules/setting/setting.go +++ b/modules/setting/setting.go @@ -755,15 +755,14 @@ func loadFromConf(allowEmpty bool, extraConfig string) { appURL, err := url.Parse(AppURL) if err != nil { log.Fatal("Invalid ROOT_URL '%s': %s", AppURL, err) - } else { - // Remove default ports from AppURL. - // (scheme-based URL normalization, RFC 3986 section 6.2.3) - if (appURL.Scheme == string(HTTP) && appURL.Port() == "80") || (appURL.Scheme == string(HTTPS) && appURL.Port() == "443") { - appURL.Host = appURL.Hostname() - } - // This should be TrimRight to ensure that there is only a single '/' at the end of AppURL. - AppURL = strings.TrimRight(appURL.String(), "/") + "/" } + // Remove default ports from AppURL. + // (scheme-based URL normalization, RFC 3986 section 6.2.3) + if (appURL.Scheme == string(HTTP) && appURL.Port() == "80") || (appURL.Scheme == string(HTTPS) && appURL.Port() == "443") { + appURL.Host = appURL.Hostname() + } + // This should be TrimRight to ensure that there is only a single '/' at the end of AppURL. + AppURL = strings.TrimRight(appURL.String(), "/") + "/" // Suburl should start with '/' and end without '/', such as '/{subpath}'. // This value is empty if site does not have sub-url.