diff options
author | Lauris BH <lauris@nix.lv> | 2018-03-16 13:59:47 +0200 |
---|---|---|
committer | Lunny Xiao <xiaolunwen@gmail.com> | 2018-03-16 19:59:47 +0800 |
commit | c0e0fb7d39048e20e9814f23716dd8a43ec649e1 (patch) | |
tree | 9fd77c5681e2525d72258a3ea992fc11503c7804 /modules | |
parent | 0c612124f9f012ace5098eceb95a354f6395494c (diff) | |
download | gitea-c0e0fb7d39048e20e9814f23716dd8a43ec649e1.tar.gz gitea-c0e0fb7d39048e20e9814f23716dd8a43ec649e1.zip |
Refactor and simplify redirect to url (#3674) (#3676)
Diffstat (limited to 'modules')
-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 311186e644..8961694843 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) |