From f1a810e0901b80eb6bc21103434fc0737af17eaa Mon Sep 17 00:00:00 2001
From: 6543 <6543@obermui.de>
Date: Wed, 11 Aug 2021 17:08:52 +0200
Subject: [PATCH] Related refactors to ctx.FormX functions (#16567)

* use FormTrim if posible

* speedup goGet

* only convert if nessesary
---
 routers/api/v1/notify/repo.go             | 2 +-
 routers/api/v1/notify/user.go             | 3 +--
 routers/api/v1/org/team.go                | 3 +--
 routers/api/v1/repo/issue.go              | 8 ++++----
 routers/api/v1/repo/issue_tracked_time.go | 5 ++---
 routers/api/v1/repo/repo.go               | 2 +-
 routers/api/v1/user/user.go               | 3 +--
 routers/web/admin/repos.go                | 5 ++---
 routers/web/explore/code.go               | 7 +++----
 routers/web/explore/repo.go               | 3 +--
 routers/web/explore/user.go               | 3 +--
 routers/web/goget.go                      | 2 +-
 routers/web/org/home.go                   | 3 +--
 routers/web/repo/commit.go                | 2 +-
 routers/web/repo/issue.go                 | 9 +++------
 routers/web/repo/milestone.go             | 3 +--
 routers/web/repo/search.go                | 7 +++----
 routers/web/repo/topic.go                 | 2 +-
 routers/web/user/auth.go                  | 3 +--
 routers/web/user/home.go                  | 2 +-
 routers/web/user/notification.go          | 9 ++++-----
 routers/web/user/profile.go               | 2 +-
 22 files changed, 36 insertions(+), 52 deletions(-)

diff --git a/routers/api/v1/notify/repo.go b/routers/api/v1/notify/repo.go
index 0bc48aeb16..af4507f896 100644
--- a/routers/api/v1/notify/repo.go
+++ b/routers/api/v1/notify/repo.go
@@ -171,7 +171,7 @@ func ReadRepoNotifications(ctx *context.APIContext) {
 	//     "$ref": "#/responses/empty"
 
 	lastRead := int64(0)
-	qLastRead := strings.Trim(ctx.FormString("last_read_at"), " ")
+	qLastRead := ctx.FormTrim("last_read_at")
 	if len(qLastRead) > 0 {
 		tmpLastRead, err := time.Parse(time.RFC3339, qLastRead)
 		if err != nil {
diff --git a/routers/api/v1/notify/user.go b/routers/api/v1/notify/user.go
index c4b126c567..c2178b4dee 100644
--- a/routers/api/v1/notify/user.go
+++ b/routers/api/v1/notify/user.go
@@ -6,7 +6,6 @@ package notify
 
 import (
 	"net/http"
-	"strings"
 	"time"
 
 	"code.gitea.io/gitea/models"
@@ -122,7 +121,7 @@ func ReadNotifications(ctx *context.APIContext) {
 	//     "$ref": "#/responses/empty"
 
 	lastRead := int64(0)
-	qLastRead := strings.Trim(ctx.FormString("last_read_at"), " ")
+	qLastRead := ctx.FormTrim("last_read_at")
 	if len(qLastRead) > 0 {
 		tmpLastRead, err := time.Parse(time.RFC3339, qLastRead)
 		if err != nil {
diff --git a/routers/api/v1/org/team.go b/routers/api/v1/org/team.go
index 7802bede1b..7998bb249f 100644
--- a/routers/api/v1/org/team.go
+++ b/routers/api/v1/org/team.go
@@ -8,7 +8,6 @@ package org
 import (
 	"fmt"
 	"net/http"
-	"strings"
 
 	"code.gitea.io/gitea/models"
 	"code.gitea.io/gitea/modules/context"
@@ -658,7 +657,7 @@ func SearchTeam(ctx *context.APIContext) {
 
 	opts := &models.SearchTeamOptions{
 		UserID:      ctx.User.ID,
-		Keyword:     strings.TrimSpace(ctx.FormString("q")),
+		Keyword:     ctx.FormTrim("q"),
 		OrgID:       ctx.Org.Organization.ID,
 		IncludeDesc: ctx.FormString("include_desc") == "" || ctx.FormBool("include_desc"),
 		ListOptions: listOptions,
diff --git a/routers/api/v1/repo/issue.go b/routers/api/v1/repo/issue.go
index 6add20cb09..7395d4fdd4 100644
--- a/routers/api/v1/repo/issue.go
+++ b/routers/api/v1/repo/issue.go
@@ -140,7 +140,7 @@ func SearchIssues(ctx *context.APIContext) {
 	var issues []*models.Issue
 	var filteredCount int64
 
-	keyword := strings.Trim(ctx.FormString("q"), " ")
+	keyword := ctx.FormTrim("q")
 	if strings.IndexByte(keyword, 0) >= 0 {
 		keyword = ""
 	}
@@ -162,13 +162,13 @@ func SearchIssues(ctx *context.APIContext) {
 		isPull = util.OptionalBoolNone
 	}
 
-	labels := strings.TrimSpace(ctx.FormString("labels"))
+	labels := ctx.FormTrim("labels")
 	var includedLabelNames []string
 	if len(labels) > 0 {
 		includedLabelNames = strings.Split(labels, ",")
 	}
 
-	milestones := strings.TrimSpace(ctx.FormString("milestones"))
+	milestones := ctx.FormTrim("milestones")
 	var includedMilestones []string
 	if len(milestones) > 0 {
 		includedMilestones = strings.Split(milestones, ",")
@@ -331,7 +331,7 @@ func ListIssues(ctx *context.APIContext) {
 	var issues []*models.Issue
 	var filteredCount int64
 
-	keyword := strings.Trim(ctx.FormString("q"), " ")
+	keyword := ctx.FormTrim("q")
 	if strings.IndexByte(keyword, 0) >= 0 {
 		keyword = ""
 	}
diff --git a/routers/api/v1/repo/issue_tracked_time.go b/routers/api/v1/repo/issue_tracked_time.go
index e2935fd13d..b27b746eb2 100644
--- a/routers/api/v1/repo/issue_tracked_time.go
+++ b/routers/api/v1/repo/issue_tracked_time.go
@@ -7,7 +7,6 @@ package repo
 import (
 	"fmt"
 	"net/http"
-	"strings"
 	"time"
 
 	"code.gitea.io/gitea/models"
@@ -90,7 +89,7 @@ func ListTrackedTimes(ctx *context.APIContext) {
 		IssueID:      issue.ID,
 	}
 
-	qUser := strings.Trim(ctx.FormString("user"), " ")
+	qUser := ctx.FormTrim("user")
 	if qUser != "" {
 		user, err := models.GetUserByName(qUser)
 		if models.IsErrUserNotExist(err) {
@@ -500,7 +499,7 @@ func ListTrackedTimesByRepository(ctx *context.APIContext) {
 	}
 
 	// Filters
-	qUser := strings.Trim(ctx.FormString("user"), " ")
+	qUser := ctx.FormTrim("user")
 	if qUser != "" {
 		user, err := models.GetUserByName(qUser)
 		if models.IsErrUserNotExist(err) {
diff --git a/routers/api/v1/repo/repo.go b/routers/api/v1/repo/repo.go
index fb1472226f..d222c9b080 100644
--- a/routers/api/v1/repo/repo.go
+++ b/routers/api/v1/repo/repo.go
@@ -135,7 +135,7 @@ func Search(ctx *context.APIContext) {
 	opts := &models.SearchRepoOptions{
 		ListOptions:        utils.GetListOptions(ctx),
 		Actor:              ctx.User,
-		Keyword:            strings.Trim(ctx.FormString("q"), " "),
+		Keyword:            ctx.FormTrim("q"),
 		OwnerID:            ctx.FormInt64("uid"),
 		PriorityOwnerID:    ctx.FormInt64("priority_owner_id"),
 		TeamID:             ctx.FormInt64("team_id"),
diff --git a/routers/api/v1/user/user.go b/routers/api/v1/user/user.go
index db950bd1ee..e00c8d476d 100644
--- a/routers/api/v1/user/user.go
+++ b/routers/api/v1/user/user.go
@@ -8,7 +8,6 @@ package user
 import (
 	"fmt"
 	"net/http"
-	"strings"
 
 	"code.gitea.io/gitea/models"
 	"code.gitea.io/gitea/modules/context"
@@ -58,7 +57,7 @@ func Search(ctx *context.APIContext) {
 
 	opts := &models.SearchUserOptions{
 		Actor:       ctx.User,
-		Keyword:     strings.Trim(ctx.FormString("q"), " "),
+		Keyword:     ctx.FormTrim("q"),
 		UID:         ctx.FormInt64("uid"),
 		Type:        models.UserTypeIndividual,
 		ListOptions: listOptions,
diff --git a/routers/web/admin/repos.go b/routers/web/admin/repos.go
index 15d4554957..4c3f2ad614 100644
--- a/routers/web/admin/repos.go
+++ b/routers/web/admin/repos.go
@@ -7,7 +7,6 @@ package admin
 import (
 	"net/http"
 	"net/url"
-	"strconv"
 	"strings"
 
 	"code.gitea.io/gitea/models"
@@ -111,7 +110,7 @@ func UnadoptedRepos(ctx *context.Context) {
 func AdoptOrDeleteRepository(ctx *context.Context) {
 	dir := ctx.FormString("id")
 	action := ctx.FormString("action")
-	page := ctx.FormInt("page")
+	page := ctx.FormString("page")
 	q := ctx.FormString("q")
 
 	dirSplit := strings.SplitN(dir, "/", 2)
@@ -162,5 +161,5 @@ func AdoptOrDeleteRepository(ctx *context.Context) {
 		}
 		ctx.Flash.Success(ctx.Tr("repo.delete_preexisting_success", dir))
 	}
-	ctx.Redirect(setting.AppSubURL + "/admin/repos/unadopted?search=true&q=" + url.QueryEscape(q) + "&page=" + strconv.Itoa(page))
+	ctx.Redirect(setting.AppSubURL + "/admin/repos/unadopted?search=true&q=" + url.QueryEscape(q) + "&page=" + page)
 }
diff --git a/routers/web/explore/code.go b/routers/web/explore/code.go
index 55a4409145..a9b159468d 100644
--- a/routers/web/explore/code.go
+++ b/routers/web/explore/code.go
@@ -6,7 +6,6 @@ package explore
 
 import (
 	"net/http"
-	"strings"
 
 	"code.gitea.io/gitea/models"
 	"code.gitea.io/gitea/modules/base"
@@ -33,14 +32,14 @@ func Code(ctx *context.Context) {
 	ctx.Data["PageIsExplore"] = true
 	ctx.Data["PageIsExploreCode"] = true
 
-	language := strings.TrimSpace(ctx.FormString("l"))
-	keyword := strings.TrimSpace(ctx.FormString("q"))
+	language := ctx.FormTrim("l")
+	keyword := ctx.FormTrim("q")
 	page := ctx.FormInt("page")
 	if page <= 0 {
 		page = 1
 	}
 
-	queryType := strings.TrimSpace(ctx.FormString("t"))
+	queryType := ctx.FormTrim("t")
 	isMatch := queryType == "match"
 
 	var (
diff --git a/routers/web/explore/repo.go b/routers/web/explore/repo.go
index 83fb721a7d..dfc6261b33 100644
--- a/routers/web/explore/repo.go
+++ b/routers/web/explore/repo.go
@@ -6,7 +6,6 @@ package explore
 
 import (
 	"net/http"
-	"strings"
 
 	"code.gitea.io/gitea/models"
 	"code.gitea.io/gitea/modules/base"
@@ -73,7 +72,7 @@ func RenderRepoSearch(ctx *context.Context, opts *RepoSearchOptions) {
 		orderBy = models.SearchOrderByRecentUpdated
 	}
 
-	keyword := strings.Trim(ctx.FormString("q"), " ")
+	keyword := ctx.FormTrim("q")
 	topicOnly := ctx.FormBool("topic")
 	ctx.Data["TopicOnly"] = topicOnly
 
diff --git a/routers/web/explore/user.go b/routers/web/explore/user.go
index 06dcdb3c6a..aeaaf92c12 100644
--- a/routers/web/explore/user.go
+++ b/routers/web/explore/user.go
@@ -7,7 +7,6 @@ package explore
 import (
 	"bytes"
 	"net/http"
-	"strings"
 
 	"code.gitea.io/gitea/models"
 	"code.gitea.io/gitea/modules/base"
@@ -63,7 +62,7 @@ func RenderUserSearch(ctx *context.Context, opts *models.SearchUserOptions, tplN
 		orderBy = models.SearchOrderByAlphabetically
 	}
 
-	opts.Keyword = strings.Trim(ctx.FormString("q"), " ")
+	opts.Keyword = ctx.FormTrim("q")
 	opts.OrderBy = orderBy
 	if len(opts.Keyword) == 0 || isKeywordValid(opts.Keyword) {
 		users, count, err = models.SearchUsers(opts)
diff --git a/routers/web/goget.go b/routers/web/goget.go
index 02adf9c5ea..9f367a9277 100644
--- a/routers/web/goget.go
+++ b/routers/web/goget.go
@@ -18,7 +18,7 @@ import (
 )
 
 func goGet(ctx *context.Context) {
-	if ctx.Req.Method != "GET" || ctx.FormString("go-get") != "1" || len(ctx.Req.URL.Query()) > 1 {
+	if ctx.Req.Method != "GET" || len(ctx.Req.URL.RawQuery) < 8 || ctx.FormString("go-get") != "1" {
 		return
 	}
 
diff --git a/routers/web/org/home.go b/routers/web/org/home.go
index de131d27b6..da53053c7f 100644
--- a/routers/web/org/home.go
+++ b/routers/web/org/home.go
@@ -6,7 +6,6 @@ package org
 
 import (
 	"net/http"
-	"strings"
 
 	"code.gitea.io/gitea/models"
 	"code.gitea.io/gitea/modules/base"
@@ -78,7 +77,7 @@ func Home(ctx *context.Context) {
 		orderBy = models.SearchOrderByRecentUpdated
 	}
 
-	keyword := strings.Trim(ctx.FormString("q"), " ")
+	keyword := ctx.FormTrim("q")
 	ctx.Data["Keyword"] = keyword
 
 	page := ctx.FormInt("page")
diff --git a/routers/web/repo/commit.go b/routers/web/repo/commit.go
index 57ee7a2043..6fbf11a1a0 100644
--- a/routers/web/repo/commit.go
+++ b/routers/web/repo/commit.go
@@ -177,7 +177,7 @@ func SearchCommits(ctx *context.Context) {
 	ctx.Data["PageIsCommits"] = true
 	ctx.Data["PageIsViewCode"] = true
 
-	query := strings.Trim(ctx.FormString("q"), " ")
+	query := ctx.FormTrim("q")
 	if len(query) == 0 {
 		ctx.Redirect(ctx.Repo.RepoLink + "/commits/" + ctx.Repo.BranchNameSubURL())
 		return
diff --git a/routers/web/repo/issue.go b/routers/web/repo/issue.go
index 248ae5b132..6050bb5c23 100644
--- a/routers/web/repo/issue.go
+++ b/routers/web/repo/issue.go
@@ -1722,14 +1722,12 @@ func UpdateIssueContent(ctx *context.Context) {
 		return
 	}
 
-	content := ctx.FormString("content")
-	if err := issue_service.ChangeContent(issue, ctx.User, content); err != nil {
+	if err := issue_service.ChangeContent(issue, ctx.User, ctx.Req.FormValue("content")); err != nil {
 		ctx.ServerError("ChangeContent", err)
 		return
 	}
 
-	files := ctx.FormStrings("files[]")
-	if err := updateAttachments(issue, files); err != nil {
+	if err := updateAttachments(issue, ctx.FormStrings("files[]")); err != nil {
 		ctx.ServerError("UpdateAttachments", err)
 		return
 	}
@@ -2157,8 +2155,7 @@ func UpdateCommentContent(ctx *context.Context) {
 		return
 	}
 
-	files := ctx.FormStrings("files[]")
-	if err := updateAttachments(comment, files); err != nil {
+	if err := updateAttachments(comment, ctx.FormStrings("files[]")); err != nil {
 		ctx.ServerError("UpdateAttachments", err)
 		return
 	}
diff --git a/routers/web/repo/milestone.go b/routers/web/repo/milestone.go
index 675cfef0aa..ca77e0976e 100644
--- a/routers/web/repo/milestone.go
+++ b/routers/web/repo/milestone.go
@@ -6,7 +6,6 @@ package repo
 
 import (
 	"net/http"
-	"strings"
 	"time"
 
 	"code.gitea.io/gitea/models"
@@ -47,7 +46,7 @@ func Milestones(ctx *context.Context) {
 
 	sortType := ctx.FormString("sort")
 
-	keyword := strings.Trim(ctx.FormString("q"), " ")
+	keyword := ctx.FormTrim("q")
 
 	page := ctx.FormInt("page")
 	if page <= 1 {
diff --git a/routers/web/repo/search.go b/routers/web/repo/search.go
index 02dd257cda..67539c3d7e 100644
--- a/routers/web/repo/search.go
+++ b/routers/web/repo/search.go
@@ -6,7 +6,6 @@ package repo
 
 import (
 	"net/http"
-	"strings"
 
 	"code.gitea.io/gitea/modules/base"
 	"code.gitea.io/gitea/modules/context"
@@ -22,13 +21,13 @@ func Search(ctx *context.Context) {
 		ctx.Redirect(ctx.Repo.RepoLink, 302)
 		return
 	}
-	language := strings.TrimSpace(ctx.FormString("l"))
-	keyword := strings.TrimSpace(ctx.FormString("q"))
+	language := ctx.FormTrim("l")
+	keyword := ctx.FormTrim("q")
 	page := ctx.FormInt("page")
 	if page <= 0 {
 		page = 1
 	}
-	queryType := strings.TrimSpace(ctx.FormString("t"))
+	queryType := ctx.FormTrim("t")
 	isMatch := queryType == "match"
 
 	total, searchResults, searchResultLanguages, err := code_indexer.PerformSearch([]int64{ctx.Repo.Repository.ID},
diff --git a/routers/web/repo/topic.go b/routers/web/repo/topic.go
index 2a2a04c111..41e3f995b6 100644
--- a/routers/web/repo/topic.go
+++ b/routers/web/repo/topic.go
@@ -23,7 +23,7 @@ func TopicsPost(ctx *context.Context) {
 	}
 
 	var topics = make([]string, 0)
-	var topicsStr = strings.TrimSpace(ctx.FormString("topics"))
+	var topicsStr = ctx.FormTrim("topics")
 	if len(topicsStr) > 0 {
 		topics = strings.Split(topicsStr, ",")
 	}
diff --git a/routers/web/user/auth.go b/routers/web/user/auth.go
index a9e60bb07c..313a583004 100644
--- a/routers/web/user/auth.go
+++ b/routers/web/user/auth.go
@@ -1491,8 +1491,7 @@ func ForgotPasswd(ctx *context.Context) {
 		return
 	}
 
-	email := ctx.FormString("email")
-	ctx.Data["Email"] = email
+	ctx.Data["Email"] = ctx.FormString("email")
 
 	ctx.Data["IsResetRequest"] = true
 	ctx.HTML(http.StatusOK, tplForgotPassword)
diff --git a/routers/web/user/home.go b/routers/web/user/home.go
index 285f1ff367..397850f18b 100644
--- a/routers/web/user/home.go
+++ b/routers/web/user/home.go
@@ -204,7 +204,7 @@ func Milestones(ctx *context.Context) {
 		isShowClosed = ctx.FormString("state") == "closed"
 		sortType     = ctx.FormString("sort")
 		page         = ctx.FormInt("page")
-		keyword      = strings.Trim(ctx.FormString("q"), " ")
+		keyword      = ctx.FormTrim("q")
 	)
 
 	if page <= 1 {
diff --git a/routers/web/user/notification.go b/routers/web/user/notification.go
index bc017db9d4..a444669b74 100644
--- a/routers/web/user/notification.go
+++ b/routers/web/user/notification.go
@@ -8,7 +8,6 @@ import (
 	"errors"
 	"fmt"
 	"net/http"
-	"strconv"
 	"strings"
 
 	"code.gitea.io/gitea/models"
@@ -59,7 +58,7 @@ func Notifications(c *context.Context) {
 
 func getNotifications(c *context.Context) {
 	var (
-		keyword = strings.Trim(c.FormString("q"), " ")
+		keyword = c.FormTrim("q")
 		status  models.NotificationStatus
 		page    = c.FormInt("page")
 		perPage = c.FormInt("perPage")
@@ -144,9 +143,9 @@ func getNotifications(c *context.Context) {
 // NotificationStatusPost is a route for changing the status of a notification
 func NotificationStatusPost(c *context.Context) {
 	var (
-		notificationID, _ = strconv.ParseInt(c.Req.PostFormValue("notification_id"), 10, 64)
-		statusStr         = c.Req.PostFormValue("status")
-		status            models.NotificationStatus
+		notificationID = c.FormInt64("notification_id")
+		statusStr      = c.FormString("status")
+		status         models.NotificationStatus
 	)
 
 	switch statusStr {
diff --git a/routers/web/user/profile.go b/routers/web/user/profile.go
index e6a8e5b5a8..c1d787a860 100644
--- a/routers/web/user/profile.go
+++ b/routers/web/user/profile.go
@@ -187,7 +187,7 @@ func Profile(ctx *context.Context) {
 		orderBy = models.SearchOrderByRecentUpdated
 	}
 
-	keyword := strings.Trim(ctx.FormString("q"), " ")
+	keyword := ctx.FormTrim("q")
 	ctx.Data["Keyword"] = keyword
 	switch tab {
 	case "followers":