diff options
author | Lauris BH <lauris@nix.lv> | 2018-03-15 23:13:34 +0200 |
---|---|---|
committer | Kim "BKC" Carlbäcker <kim.carlbacker@gmail.com> | 2018-03-15 22:13:34 +0100 |
commit | 7b2b900e13612f565051c64374b788b0c8a82751 (patch) | |
tree | d5d35b485ea898ea13fbf2eac9f879e311ef8ac8 /modules/context | |
parent | a2a49c93c78a81d1eaa6b0eaf84a0c3f4bcd2487 (diff) | |
download | gitea-7b2b900e13612f565051c64374b788b0c8a82751.tar.gz gitea-7b2b900e13612f565051c64374b788b0c8a82751.zip |
Refactor and simplify redirect to url (#3674)
Diffstat (limited to 'modules/context')
-rw-r--r-- | modules/context/context.go | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/modules/context/context.go b/modules/context/context.go index ea26184397..7f9193f821 100644 --- a/modules/context/context.go +++ b/modules/context/context.go @@ -9,6 +9,7 @@ import ( "html/template" "io" "net/http" + "net/url" "path" "strings" "time" @@ -75,6 +76,26 @@ func (ctx *Context) HasValue(name string) bool { return ok } +// RedirectToFirst redirects to first not empty URL +func (ctx *Context) RedirectToFirst(location ...string) { + for _, loc := range location { + if len(loc) == 0 { + continue + } + + u, err := url.Parse(loc) + if err != nil || (u.Scheme != "" && !strings.HasPrefix(strings.ToLower(loc), strings.ToLower(setting.AppURL))) { + continue + } + + ctx.Redirect(loc) + return + } + + ctx.Redirect(setting.AppSubURL + "/") + return +} + // HTML calls Context.HTML and converts template name to string. func (ctx *Context) HTML(status int, name base.TplName) { log.Debug("Template: %s", name) |