1
1
mirror of https://github.com/go-gitea/gitea synced 2025-07-22 18:28:37 +00:00

Rename ctx.Form() to ctx.FormString() and move code into own file (#16571)

Followup from #16562 prepare for #16567

* Rename ctx.Form() to ctx.FormString()
* Reimplement FormX func to need less code and cpu cycles
* Move code into own file
This commit is contained in:
6543
2021-08-11 02:31:13 +02:00
committed by GitHub
parent 2eeae4edb6
commit c4d70a0325
64 changed files with 236 additions and 449 deletions

View File

@@ -111,7 +111,7 @@ func checkAutoLogin(ctx *context.Context) bool {
return true
}
redirectTo := ctx.Form("redirect_to")
redirectTo := ctx.FormString("redirect_to")
if len(redirectTo) > 0 {
middleware.SetRedirectToCookie(ctx.Resp, redirectTo)
} else {
@@ -1333,7 +1333,7 @@ func handleUserCreated(ctx *context.Context, u *models.User, gothUser *goth.User
// Activate render activate user page
func Activate(ctx *context.Context) {
code := ctx.Form("code")
code := ctx.FormString("code")
if len(code) == 0 {
ctx.Data["IsActivatePage"] = true
@@ -1381,7 +1381,7 @@ func Activate(ctx *context.Context) {
// ActivatePost handles account activation with password check
func ActivatePost(ctx *context.Context) {
code := ctx.Form("code")
code := ctx.FormString("code")
if len(code) == 0 {
ctx.Redirect(setting.AppSubURL + "/user/activate")
return
@@ -1397,7 +1397,7 @@ func ActivatePost(ctx *context.Context) {
// if account is local account, verify password
if user.LoginSource == 0 {
password := ctx.Form("password")
password := ctx.FormString("password")
if len(password) == 0 {
ctx.Data["Code"] = code
ctx.Data["NeedsPassword"] = true
@@ -1454,8 +1454,8 @@ func handleAccountActivation(ctx *context.Context, user *models.User) {
// ActivateEmail render the activate email page
func ActivateEmail(ctx *context.Context) {
code := ctx.Form("code")
emailStr := ctx.Form("email")
code := ctx.FormString("code")
emailStr := ctx.FormString("email")
// Verify code.
if email := models.VerifyActiveEmailCode(code, emailStr); email != nil {
@@ -1491,7 +1491,7 @@ func ForgotPasswd(ctx *context.Context) {
return
}
email := ctx.Form("email")
email := ctx.FormString("email")
ctx.Data["Email"] = email
ctx.Data["IsResetRequest"] = true
@@ -1508,7 +1508,7 @@ func ForgotPasswdPost(ctx *context.Context) {
}
ctx.Data["IsResetRequest"] = true
email := ctx.Form("email")
email := ctx.FormString("email")
ctx.Data["Email"] = email
u, err := models.GetUserByEmail(email)
@@ -1548,7 +1548,7 @@ func ForgotPasswdPost(ctx *context.Context) {
}
func commonResetPassword(ctx *context.Context) (*models.User, *models.TwoFactor) {
code := ctx.Form("code")
code := ctx.FormString("code")
ctx.Data["Title"] = ctx.Tr("auth.reset_password")
ctx.Data["Code"] = code
@@ -1617,7 +1617,7 @@ func ResetPasswdPost(ctx *context.Context) {
}
// Validate password length.
passwd := ctx.Form("password")
passwd := ctx.FormString("password")
if len(passwd) < setting.MinPasswordLength {
ctx.Data["IsResetForm"] = true
ctx.Data["Err_Password"] = true
@@ -1644,7 +1644,7 @@ func ResetPasswdPost(ctx *context.Context) {
regenerateScratchToken := false
if twofa != nil {
if ctx.FormBool("scratch_code") {
if !twofa.VerifyScratchToken(ctx.Form("token")) {
if !twofa.VerifyScratchToken(ctx.FormString("token")) {
ctx.Data["IsResetForm"] = true
ctx.Data["Err_Token"] = true
ctx.RenderWithErr(ctx.Tr("auth.twofa_scratch_token_incorrect"), tplResetPassword, nil)
@@ -1652,7 +1652,7 @@ func ResetPasswdPost(ctx *context.Context) {
}
regenerateScratchToken = true
} else {
passcode := ctx.Form("passcode")
passcode := ctx.FormString("passcode")
ok, err := twofa.ValidateTOTP(passcode)
if err != nil {
ctx.Error(http.StatusInternalServerError, "ValidateTOTP", err.Error())
@@ -1689,7 +1689,7 @@ func ResetPasswdPost(ctx *context.Context) {
log.Trace("User password reset: %s", u.Name)
ctx.Data["IsResetFailed"] = true
remember := len(ctx.Form("remember")) != 0
remember := len(ctx.FormString("remember")) != 0
if regenerateScratchToken {
// Invalidate the scratch token.

View File

@@ -34,7 +34,7 @@ const (
func SignInOpenID(ctx *context.Context) {
ctx.Data["Title"] = ctx.Tr("sign_in")
if ctx.Form("openid.return_to") != "" {
if ctx.FormString("openid.return_to") != "" {
signInOpenIDVerify(ctx)
return
}
@@ -46,7 +46,7 @@ func SignInOpenID(ctx *context.Context) {
return
}
redirectTo := ctx.Form("redirect_to")
redirectTo := ctx.FormString("redirect_to")
if len(redirectTo) > 0 {
middleware.SetRedirectToCookie(ctx.Resp, redirectTo)
} else {

View File

@@ -157,7 +157,7 @@ func Dashboard(ctx *context.Context) {
IncludePrivate: true,
OnlyPerformedBy: false,
IncludeDeleted: false,
Date: ctx.Form("date"),
Date: ctx.FormString("date"),
})
if ctx.Written() {
@@ -200,11 +200,11 @@ func Milestones(ctx *context.Context) {
repoCond = userRepoCond
repoIDs []int64
reposQuery = ctx.Form("repos")
isShowClosed = ctx.Form("state") == "closed"
sortType = ctx.Form("sort")
reposQuery = ctx.FormString("repos")
isShowClosed = ctx.FormString("state") == "closed"
sortType = ctx.FormString("sort")
page = ctx.FormInt("page")
keyword = strings.Trim(ctx.Form("q"), " ")
keyword = strings.Trim(ctx.FormString("q"), " ")
)
if page <= 1 {
@@ -380,7 +380,7 @@ func buildIssueOverview(ctx *context.Context, unitType models.UnitType) {
var (
viewType string
sortType = ctx.Form("sort")
sortType = ctx.FormString("sort")
filterMode = models.FilterModeAll
)
@@ -390,14 +390,14 @@ func buildIssueOverview(ctx *context.Context, unitType models.UnitType) {
// - Remember pre-determined viewType string for later. Will be posted to ctx.Data.
// Organization does not have view type and filter mode.
// User:
// - Use ctx.Form("type") to determine filterMode.
// - Use ctx.FormString("type") to determine filterMode.
// The type is set when clicking for example "assigned to me" on the overview page.
// - Remember either this or a fallback. Will be posted to ctx.Data.
// --------------------------------------------------------------------------------
// TODO: distinguish during routing
viewType = ctx.Form("type")
viewType = ctx.FormString("type")
switch viewType {
case "assigned":
filterMode = models.FilterModeAssign
@@ -456,7 +456,7 @@ func buildIssueOverview(ctx *context.Context, unitType models.UnitType) {
}
// keyword holds the search term entered into the search field.
keyword := strings.Trim(ctx.Form("q"), " ")
keyword := strings.Trim(ctx.FormString("q"), " ")
ctx.Data["Keyword"] = keyword
// Execute keyword search for issues.
@@ -477,7 +477,7 @@ func buildIssueOverview(ctx *context.Context, unitType models.UnitType) {
}
// Educated guess: Do or don't show closed issues.
isShowClosed := ctx.Form("state") == "closed"
isShowClosed := ctx.FormString("state") == "closed"
opts.IsClosed = util.OptionalBoolOf(isShowClosed)
// Filter repos and count issues in them. Count will be used later.
@@ -502,7 +502,7 @@ func buildIssueOverview(ctx *context.Context, unitType models.UnitType) {
// Get IDs for labels (a filter option for issues/pulls).
// Required for IssuesOptions.
var labelIDs []int64
selectedLabels := ctx.Form("labels")
selectedLabels := ctx.FormString("labels")
if len(selectedLabels) > 0 && selectedLabels != "0" {
labelIDs, err = base.StringsToInt64s(strings.Split(selectedLabels, ","))
if err != nil {
@@ -512,9 +512,9 @@ func buildIssueOverview(ctx *context.Context, unitType models.UnitType) {
}
opts.LabelIDs = labelIDs
// Parse ctx.Form("repos") and remember matched repo IDs for later.
// Parse ctx.FormString("repos") and remember matched repo IDs for later.
// Gets set when clicking filters on the issues overview page.
repoIDs := getRepoIDs(ctx.Form("repos"))
repoIDs := getRepoIDs(ctx.FormString("repos"))
if len(repoIDs) > 0 {
opts.RepoIDs = repoIDs
}
@@ -658,7 +658,7 @@ func buildIssueOverview(ctx *context.Context, unitType models.UnitType) {
ctx.Data["IsShowClosed"] = isShowClosed
ctx.Data["IssueRefEndNames"], ctx.Data["IssueRefURLs"] =
issue_service.GetRefEndNamesAndURLs(issues, ctx.Form("RepoLink"))
issue_service.GetRefEndNamesAndURLs(issues, ctx.FormString("RepoLink"))
ctx.Data["Issues"] = issues
@@ -900,7 +900,7 @@ func ShowGPGKeys(ctx *context.Context, uid int64) {
// Email2User show user page via email
func Email2User(ctx *context.Context) {
u, err := models.GetUserByEmail(ctx.Form("email"))
u, err := models.GetUserByEmail(ctx.FormString("email"))
if err != nil {
if models.IsErrUserNotExist(err) {
ctx.NotFound("GetUserByEmail", err)

View File

@@ -50,7 +50,7 @@ func Notifications(c *context.Context) {
return
}
if c.FormBool("div-only") {
c.Data["SequenceNumber"] = c.Form("sequence-number")
c.Data["SequenceNumber"] = c.FormString("sequence-number")
c.HTML(http.StatusOK, tplNotificationDiv)
return
}
@@ -59,7 +59,7 @@ func Notifications(c *context.Context) {
func getNotifications(c *context.Context) {
var (
keyword = strings.Trim(c.Form("q"), " ")
keyword = strings.Trim(c.FormString("q"), " ")
status models.NotificationStatus
page = c.FormInt("page")
perPage = c.FormInt("perPage")
@@ -87,7 +87,7 @@ func getNotifications(c *context.Context) {
// redirect to last page if request page is more than total pages
pager := context.NewPagination(int(total), perPage, page, 5)
if pager.Paginater.Current() < page {
c.Redirect(fmt.Sprintf("/notifications?q=%s&page=%d", c.Form("q"), pager.Paginater.Current()))
c.Redirect(fmt.Sprintf("/notifications?q=%s&page=%d", c.FormString("q"), pager.Paginater.Current()))
return
}
@@ -167,7 +167,7 @@ func NotificationStatusPost(c *context.Context) {
}
if !c.FormBool("noredirect") {
url := fmt.Sprintf("%s/notifications?page=%s", setting.AppSubURL, c.Form("page"))
url := fmt.Sprintf("%s/notifications?page=%s", setting.AppSubURL, c.FormString("page"))
c.Redirect(url, http.StatusSeeOther)
}

View File

@@ -143,7 +143,7 @@ func Profile(ctx *context.Context) {
ctx.Data["Orgs"] = orgs
ctx.Data["HasOrgsVisible"] = models.HasOrgsVisible(orgs, ctx.User)
tab := ctx.Form("tab")
tab := ctx.FormString("tab")
ctx.Data["TabName"] = tab
page := ctx.FormInt("page")
@@ -160,8 +160,8 @@ func Profile(ctx *context.Context) {
orderBy models.SearchOrderBy
)
ctx.Data["SortType"] = ctx.Form("sort")
switch ctx.Form("sort") {
ctx.Data["SortType"] = ctx.FormString("sort")
switch ctx.FormString("sort") {
case "newest":
orderBy = models.SearchOrderByNewest
case "oldest":
@@ -187,7 +187,7 @@ func Profile(ctx *context.Context) {
orderBy = models.SearchOrderByRecentUpdated
}
keyword := strings.Trim(ctx.Form("q"), " ")
keyword := strings.Trim(ctx.FormString("q"), " ")
ctx.Data["Keyword"] = keyword
switch tab {
case "followers":
@@ -220,7 +220,7 @@ func Profile(ctx *context.Context) {
IncludePrivate: showPrivate,
OnlyPerformedBy: true,
IncludeDeleted: false,
Date: ctx.Form("date"),
Date: ctx.FormString("date"),
})
if ctx.Written() {
return
@@ -332,5 +332,5 @@ func Action(ctx *context.Context) {
return
}
ctx.RedirectToFirst(ctx.Form("redirect_to"), u.HomeLink())
ctx.RedirectToFirst(ctx.FormString("redirect_to"), u.HomeLink())
}

View File

@@ -90,7 +90,7 @@ func EmailPost(ctx *context.Context) {
ctx.Data["PageIsSettingsAccount"] = true
// Make emailaddress primary.
if ctx.Form("_method") == "PRIMARY" {
if ctx.FormString("_method") == "PRIMARY" {
if err := models.MakeEmailPrimary(&models.EmailAddress{ID: ctx.FormInt64("id")}); err != nil {
ctx.ServerError("MakeEmailPrimary", err)
return
@@ -101,7 +101,7 @@ func EmailPost(ctx *context.Context) {
return
}
// Send activation Email
if ctx.Form("_method") == "SENDACTIVATION" {
if ctx.FormString("_method") == "SENDACTIVATION" {
var address string
if ctx.Cache.IsExist("MailResendLimit_" + ctx.User.LowerName) {
log.Error("Send activation: activation still pending")
@@ -147,8 +147,8 @@ func EmailPost(ctx *context.Context) {
return
}
// Set Email Notification Preference
if ctx.Form("_method") == "NOTIFICATION" {
preference := ctx.Form("preference")
if ctx.FormString("_method") == "NOTIFICATION" {
preference := ctx.FormString("preference")
if !(preference == models.EmailNotificationsEnabled ||
preference == models.EmailNotificationsOnMention ||
preference == models.EmailNotificationsDisabled) {
@@ -229,7 +229,7 @@ func DeleteAccount(ctx *context.Context) {
ctx.Data["Title"] = ctx.Tr("settings")
ctx.Data["PageIsSettingsAccount"] = true
if _, err := auth.UserSignIn(ctx.User.Name, ctx.Form("password")); err != nil {
if _, err := auth.UserSignIn(ctx.User.Name, ctx.FormString("password")); err != nil {
if models.IsErrUserNotExist(err) {
loadAccountData(ctx)

View File

@@ -23,8 +23,8 @@ func AdoptOrDeleteRepository(ctx *context.Context) {
allowDelete := ctx.IsUserSiteAdmin() || setting.Repository.AllowDeleteOfUnadoptedRepositories
ctx.Data["allowDelete"] = allowDelete
dir := ctx.Form("id")
action := ctx.Form("action")
dir := ctx.FormString("id")
action := ctx.FormString("action")
ctxUser := ctx.User
root := filepath.Join(models.UserPath(ctxUser.LowerName))

View File

@@ -193,7 +193,7 @@ func KeysPost(ctx *context.Context) {
// DeleteKey response for delete user's SSH/GPG key
func DeleteKey(ctx *context.Context) {
switch ctx.Form("type") {
switch ctx.FormString("type") {
case "gpg":
if err := models.DeleteGPGKey(ctx.User, ctx.FormInt64("id")); err != nil {
ctx.Flash.Error("DeleteGPGKey: " + err.Error())
@@ -265,5 +265,5 @@ func loadKeysData(ctx *context.Context) {
}
ctx.Data["Principals"] = principals
ctx.Data["VerifyingID"] = ctx.Form("verify_gpg")
ctx.Data["VerifyingID"] = ctx.FormString("verify_gpg")
}

View File

@@ -25,7 +25,7 @@ func Security(ctx *context.Context) {
ctx.Data["PageIsSettingsSecurity"] = true
ctx.Data["RequireU2F"] = true
if ctx.Form("openid.return_to") != "" {
if ctx.FormString("openid.return_to") != "" {
settingsOpenIDVerify(ctx)
return
}