Skip to content

Commit

Permalink
Refactor and simplify redirect to url (#3674) (#3676)
Browse files Browse the repository at this point in the history
  • Loading branch information
lafriks authored and lunny committed Mar 16, 2018
1 parent 0c61212 commit c0e0fb7
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 24 deletions.
21 changes: 21 additions & 0 deletions modules/context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"html/template"
"io"
"net/http"
"net/url"
"path"
"strings"
"time"
Expand Down Expand Up @@ -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)
Expand Down
6 changes: 1 addition & 5 deletions routers/repo/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,11 +307,7 @@ func Action(ctx *context.Context) {
return
}

redirectTo := ctx.Query("redirect_to")
if len(redirectTo) == 0 {
redirectTo = ctx.Repo.RepoLink
}
ctx.Redirect(redirectTo)
ctx.RedirectToFirst(ctx.Query("redirect_to"), ctx.Repo.RepoLink)
}

// Download download an archive of a repository
Expand Down
12 changes: 4 additions & 8 deletions routers/user/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,8 @@ func checkAutoLogin(ctx *context.Context) bool {
}

if isSucceed {
if len(redirectTo) > 0 {
ctx.SetCookie("redirect_to", "", -1, setting.AppSubURL)
ctx.Redirect(redirectTo)
} else {
ctx.Redirect(setting.AppSubURL + string(setting.LandingPageURL))
}
ctx.SetCookie("redirect_to", "", -1, setting.AppSubURL)
ctx.RedirectToFirst(redirectTo, setting.AppSubURL+string(setting.LandingPageURL))
return true
}

Expand Down Expand Up @@ -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 {
ctx.SetCookie("redirect_to", "", -1, setting.AppSubURL)
if obeyRedirect {
ctx.Redirect(redirectTo)
ctx.RedirectToFirst(redirectTo)
}
return
}
Expand Down Expand Up @@ -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 {
ctx.SetCookie("redirect_to", "", -1, setting.AppSubURL)
ctx.Redirect(redirectTo)
ctx.RedirectToFirst(redirectTo)
return
}

Expand Down
8 changes: 2 additions & 6 deletions routers/user/auth_openid.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,8 @@ func SignInOpenID(ctx *context.Context) {
}

if isSucceed {
if len(redirectTo) > 0 {
ctx.SetCookie("redirect_to", "", -1, setting.AppSubURL)
ctx.Redirect(redirectTo)
} else {
ctx.Redirect(setting.AppSubURL + "/")
}
ctx.SetCookie("redirect_to", "", -1, setting.AppSubURL)
ctx.RedirectToFirst(redirectTo)
return
}

Expand Down
6 changes: 1 addition & 5 deletions routers/user/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,9 +271,5 @@ func Action(ctx *context.Context) {
return
}

redirectTo := ctx.Query("redirect_to")
if len(redirectTo) == 0 {
redirectTo = u.HomeLink()
}
ctx.Redirect(redirectTo)
ctx.RedirectToFirst(ctx.Query("redirect_to"), u.HomeLink())
}

0 comments on commit c0e0fb7

Please sign in to comment.