0
0
Fork 0
mirror of https://github.com/go-gitea/gitea synced 2024-11-28 09:12:57 +01:00

Refactor and simplify redirect to url (#3674)

This commit is contained in:
Lauris BH 2018-03-15 23:13:34 +02:00 committed by Kim "BKC" Carlbäcker
parent a2a49c93c7
commit 7b2b900e13
5 changed files with 29 additions and 24 deletions

View file

@ -9,6 +9,7 @@ import (
"html/template" "html/template"
"io" "io"
"net/http" "net/http"
"net/url"
"path" "path"
"strings" "strings"
"time" "time"
@ -75,6 +76,26 @@ func (ctx *Context) HasValue(name string) bool {
return ok 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. // HTML calls Context.HTML and converts template name to string.
func (ctx *Context) HTML(status int, name base.TplName) { func (ctx *Context) HTML(status int, name base.TplName) {
log.Debug("Template: %s", name) log.Debug("Template: %s", name)

View file

@ -307,11 +307,7 @@ func Action(ctx *context.Context) {
return return
} }
redirectTo := ctx.Query("redirect_to") ctx.RedirectToFirst(ctx.Query("redirect_to"), ctx.Repo.RepoLink)
if len(redirectTo) == 0 {
redirectTo = ctx.Repo.RepoLink
}
ctx.Redirect(redirectTo)
} }
// Download download an archive of a repository // Download download an archive of a repository

View file

@ -93,12 +93,8 @@ func checkAutoLogin(ctx *context.Context) bool {
} }
if isSucceed { if isSucceed {
if len(redirectTo) > 0 { ctx.SetCookie("redirect_to", "", -1, setting.AppSubURL)
ctx.SetCookie("redirect_to", "", -1, setting.AppSubURL) ctx.RedirectToFirst(redirectTo, setting.AppSubURL+string(setting.LandingPageURL))
ctx.Redirect(redirectTo)
} else {
ctx.Redirect(setting.AppSubURL + string(setting.LandingPageURL))
}
return true return true
} }
@ -350,7 +346,7 @@ func handleSignInFull(ctx *context.Context, u *models.User, remember bool, obeyR
if redirectTo, _ := url.QueryUnescape(ctx.GetCookie("redirect_to")); len(redirectTo) > 0 { if redirectTo, _ := url.QueryUnescape(ctx.GetCookie("redirect_to")); len(redirectTo) > 0 {
ctx.SetCookie("redirect_to", "", -1, setting.AppSubURL) ctx.SetCookie("redirect_to", "", -1, setting.AppSubURL)
if obeyRedirect { if obeyRedirect {
ctx.Redirect(redirectTo) ctx.RedirectToFirst(redirectTo)
} }
return return
} }
@ -439,7 +435,7 @@ func handleOAuth2SignIn(u *models.User, gothUser goth.User, ctx *context.Context
if redirectTo, _ := url.QueryUnescape(ctx.GetCookie("redirect_to")); len(redirectTo) > 0 { if redirectTo, _ := url.QueryUnescape(ctx.GetCookie("redirect_to")); len(redirectTo) > 0 {
ctx.SetCookie("redirect_to", "", -1, setting.AppSubURL) ctx.SetCookie("redirect_to", "", -1, setting.AppSubURL)
ctx.Redirect(redirectTo) ctx.RedirectToFirst(redirectTo)
return return
} }

View file

@ -50,12 +50,8 @@ func SignInOpenID(ctx *context.Context) {
} }
if isSucceed { if isSucceed {
if len(redirectTo) > 0 { ctx.SetCookie("redirect_to", "", -1, setting.AppSubURL)
ctx.SetCookie("redirect_to", "", -1, setting.AppSubURL) ctx.RedirectToFirst(redirectTo)
ctx.Redirect(redirectTo)
} else {
ctx.Redirect(setting.AppSubURL + "/")
}
return return
} }

View file

@ -271,9 +271,5 @@ func Action(ctx *context.Context) {
return return
} }
redirectTo := ctx.Query("redirect_to") ctx.RedirectToFirst(ctx.Query("redirect_to"), u.HomeLink())
if len(redirectTo) == 0 {
redirectTo = u.HomeLink()
}
ctx.Redirect(redirectTo)
} }