Skip to content

Commit c0e0fb7

Browse files
lafrikslunny
authored andcommitted
Refactor and simplify redirect to url (#3674) (#3676)
1 parent 0c61212 commit c0e0fb7

File tree

5 files changed

+29
-24
lines changed

5 files changed

+29
-24
lines changed

modules/context/context.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"html/template"
1010
"io"
1111
"net/http"
12+
"net/url"
1213
"path"
1314
"strings"
1415
"time"
@@ -75,6 +76,26 @@ func (ctx *Context) HasValue(name string) bool {
7576
return ok
7677
}
7778

79+
// RedirectToFirst redirects to first not empty URL
80+
func (ctx *Context) RedirectToFirst(location ...string) {
81+
for _, loc := range location {
82+
if len(loc) == 0 {
83+
continue
84+
}
85+
86+
u, err := url.Parse(loc)
87+
if err != nil || (u.Scheme != "" && !strings.HasPrefix(strings.ToLower(loc), strings.ToLower(setting.AppURL))) {
88+
continue
89+
}
90+
91+
ctx.Redirect(loc)
92+
return
93+
}
94+
95+
ctx.Redirect(setting.AppSubURL + "/")
96+
return
97+
}
98+
7899
// HTML calls Context.HTML and converts template name to string.
79100
func (ctx *Context) HTML(status int, name base.TplName) {
80101
log.Debug("Template: %s", name)

routers/repo/repo.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -307,11 +307,7 @@ func Action(ctx *context.Context) {
307307
return
308308
}
309309

310-
redirectTo := ctx.Query("redirect_to")
311-
if len(redirectTo) == 0 {
312-
redirectTo = ctx.Repo.RepoLink
313-
}
314-
ctx.Redirect(redirectTo)
310+
ctx.RedirectToFirst(ctx.Query("redirect_to"), ctx.Repo.RepoLink)
315311
}
316312

317313
// Download download an archive of a repository

routers/user/auth.go

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -93,12 +93,8 @@ func checkAutoLogin(ctx *context.Context) bool {
9393
}
9494

9595
if isSucceed {
96-
if len(redirectTo) > 0 {
97-
ctx.SetCookie("redirect_to", "", -1, setting.AppSubURL)
98-
ctx.Redirect(redirectTo)
99-
} else {
100-
ctx.Redirect(setting.AppSubURL + string(setting.LandingPageURL))
101-
}
96+
ctx.SetCookie("redirect_to", "", -1, setting.AppSubURL)
97+
ctx.RedirectToFirst(redirectTo, setting.AppSubURL+string(setting.LandingPageURL))
10298
return true
10399
}
104100

@@ -350,7 +346,7 @@ func handleSignInFull(ctx *context.Context, u *models.User, remember bool, obeyR
350346
if redirectTo, _ := url.QueryUnescape(ctx.GetCookie("redirect_to")); len(redirectTo) > 0 {
351347
ctx.SetCookie("redirect_to", "", -1, setting.AppSubURL)
352348
if obeyRedirect {
353-
ctx.Redirect(redirectTo)
349+
ctx.RedirectToFirst(redirectTo)
354350
}
355351
return
356352
}
@@ -439,7 +435,7 @@ func handleOAuth2SignIn(u *models.User, gothUser goth.User, ctx *context.Context
439435

440436
if redirectTo, _ := url.QueryUnescape(ctx.GetCookie("redirect_to")); len(redirectTo) > 0 {
441437
ctx.SetCookie("redirect_to", "", -1, setting.AppSubURL)
442-
ctx.Redirect(redirectTo)
438+
ctx.RedirectToFirst(redirectTo)
443439
return
444440
}
445441

routers/user/auth_openid.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,8 @@ func SignInOpenID(ctx *context.Context) {
4949
}
5050

5151
if isSucceed {
52-
if len(redirectTo) > 0 {
53-
ctx.SetCookie("redirect_to", "", -1, setting.AppSubURL)
54-
ctx.Redirect(redirectTo)
55-
} else {
56-
ctx.Redirect(setting.AppSubURL + "/")
57-
}
52+
ctx.SetCookie("redirect_to", "", -1, setting.AppSubURL)
53+
ctx.RedirectToFirst(redirectTo)
5854
return
5955
}
6056

routers/user/profile.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -271,9 +271,5 @@ func Action(ctx *context.Context) {
271271
return
272272
}
273273

274-
redirectTo := ctx.Query("redirect_to")
275-
if len(redirectTo) == 0 {
276-
redirectTo = u.HomeLink()
277-
}
278-
ctx.Redirect(redirectTo)
274+
ctx.RedirectToFirst(ctx.Query("redirect_to"), u.HomeLink())
279275
}

0 commit comments

Comments
 (0)