Skip to content

Commit f1a810e

Browse files
authored
Related refactors to ctx.FormX functions (#16567)
* use FormTrim if posible * speedup goGet * only convert if nessesary
1 parent 2d25b7d commit f1a810e

22 files changed

+36
-52
lines changed

routers/api/v1/notify/repo.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ func ReadRepoNotifications(ctx *context.APIContext) {
171171
// "$ref": "#/responses/empty"
172172

173173
lastRead := int64(0)
174-
qLastRead := strings.Trim(ctx.FormString("last_read_at"), " ")
174+
qLastRead := ctx.FormTrim("last_read_at")
175175
if len(qLastRead) > 0 {
176176
tmpLastRead, err := time.Parse(time.RFC3339, qLastRead)
177177
if err != nil {

routers/api/v1/notify/user.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ package notify
66

77
import (
88
"net/http"
9-
"strings"
109
"time"
1110

1211
"code.gitea.io/gitea/models"
@@ -122,7 +121,7 @@ func ReadNotifications(ctx *context.APIContext) {
122121
// "$ref": "#/responses/empty"
123122

124123
lastRead := int64(0)
125-
qLastRead := strings.Trim(ctx.FormString("last_read_at"), " ")
124+
qLastRead := ctx.FormTrim("last_read_at")
126125
if len(qLastRead) > 0 {
127126
tmpLastRead, err := time.Parse(time.RFC3339, qLastRead)
128127
if err != nil {

routers/api/v1/org/team.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ package org
88
import (
99
"fmt"
1010
"net/http"
11-
"strings"
1211

1312
"code.gitea.io/gitea/models"
1413
"code.gitea.io/gitea/modules/context"
@@ -658,7 +657,7 @@ func SearchTeam(ctx *context.APIContext) {
658657

659658
opts := &models.SearchTeamOptions{
660659
UserID: ctx.User.ID,
661-
Keyword: strings.TrimSpace(ctx.FormString("q")),
660+
Keyword: ctx.FormTrim("q"),
662661
OrgID: ctx.Org.Organization.ID,
663662
IncludeDesc: ctx.FormString("include_desc") == "" || ctx.FormBool("include_desc"),
664663
ListOptions: listOptions,

routers/api/v1/repo/issue.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ func SearchIssues(ctx *context.APIContext) {
140140
var issues []*models.Issue
141141
var filteredCount int64
142142

143-
keyword := strings.Trim(ctx.FormString("q"), " ")
143+
keyword := ctx.FormTrim("q")
144144
if strings.IndexByte(keyword, 0) >= 0 {
145145
keyword = ""
146146
}
@@ -162,13 +162,13 @@ func SearchIssues(ctx *context.APIContext) {
162162
isPull = util.OptionalBoolNone
163163
}
164164

165-
labels := strings.TrimSpace(ctx.FormString("labels"))
165+
labels := ctx.FormTrim("labels")
166166
var includedLabelNames []string
167167
if len(labels) > 0 {
168168
includedLabelNames = strings.Split(labels, ",")
169169
}
170170

171-
milestones := strings.TrimSpace(ctx.FormString("milestones"))
171+
milestones := ctx.FormTrim("milestones")
172172
var includedMilestones []string
173173
if len(milestones) > 0 {
174174
includedMilestones = strings.Split(milestones, ",")
@@ -331,7 +331,7 @@ func ListIssues(ctx *context.APIContext) {
331331
var issues []*models.Issue
332332
var filteredCount int64
333333

334-
keyword := strings.Trim(ctx.FormString("q"), " ")
334+
keyword := ctx.FormTrim("q")
335335
if strings.IndexByte(keyword, 0) >= 0 {
336336
keyword = ""
337337
}

routers/api/v1/repo/issue_tracked_time.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ package repo
77
import (
88
"fmt"
99
"net/http"
10-
"strings"
1110
"time"
1211

1312
"code.gitea.io/gitea/models"
@@ -90,7 +89,7 @@ func ListTrackedTimes(ctx *context.APIContext) {
9089
IssueID: issue.ID,
9190
}
9291

93-
qUser := strings.Trim(ctx.FormString("user"), " ")
92+
qUser := ctx.FormTrim("user")
9493
if qUser != "" {
9594
user, err := models.GetUserByName(qUser)
9695
if models.IsErrUserNotExist(err) {
@@ -500,7 +499,7 @@ func ListTrackedTimesByRepository(ctx *context.APIContext) {
500499
}
501500

502501
// Filters
503-
qUser := strings.Trim(ctx.FormString("user"), " ")
502+
qUser := ctx.FormTrim("user")
504503
if qUser != "" {
505504
user, err := models.GetUserByName(qUser)
506505
if models.IsErrUserNotExist(err) {

routers/api/v1/repo/repo.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ func Search(ctx *context.APIContext) {
135135
opts := &models.SearchRepoOptions{
136136
ListOptions: utils.GetListOptions(ctx),
137137
Actor: ctx.User,
138-
Keyword: strings.Trim(ctx.FormString("q"), " "),
138+
Keyword: ctx.FormTrim("q"),
139139
OwnerID: ctx.FormInt64("uid"),
140140
PriorityOwnerID: ctx.FormInt64("priority_owner_id"),
141141
TeamID: ctx.FormInt64("team_id"),

routers/api/v1/user/user.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ package user
88
import (
99
"fmt"
1010
"net/http"
11-
"strings"
1211

1312
"code.gitea.io/gitea/models"
1413
"code.gitea.io/gitea/modules/context"
@@ -58,7 +57,7 @@ func Search(ctx *context.APIContext) {
5857

5958
opts := &models.SearchUserOptions{
6059
Actor: ctx.User,
61-
Keyword: strings.Trim(ctx.FormString("q"), " "),
60+
Keyword: ctx.FormTrim("q"),
6261
UID: ctx.FormInt64("uid"),
6362
Type: models.UserTypeIndividual,
6463
ListOptions: listOptions,

routers/web/admin/repos.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ package admin
77
import (
88
"net/http"
99
"net/url"
10-
"strconv"
1110
"strings"
1211

1312
"code.gitea.io/gitea/models"
@@ -111,7 +110,7 @@ func UnadoptedRepos(ctx *context.Context) {
111110
func AdoptOrDeleteRepository(ctx *context.Context) {
112111
dir := ctx.FormString("id")
113112
action := ctx.FormString("action")
114-
page := ctx.FormInt("page")
113+
page := ctx.FormString("page")
115114
q := ctx.FormString("q")
116115

117116
dirSplit := strings.SplitN(dir, "/", 2)
@@ -162,5 +161,5 @@ func AdoptOrDeleteRepository(ctx *context.Context) {
162161
}
163162
ctx.Flash.Success(ctx.Tr("repo.delete_preexisting_success", dir))
164163
}
165-
ctx.Redirect(setting.AppSubURL + "/admin/repos/unadopted?search=true&q=" + url.QueryEscape(q) + "&page=" + strconv.Itoa(page))
164+
ctx.Redirect(setting.AppSubURL + "/admin/repos/unadopted?search=true&q=" + url.QueryEscape(q) + "&page=" + page)
166165
}

routers/web/explore/code.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ package explore
66

77
import (
88
"net/http"
9-
"strings"
109

1110
"code.gitea.io/gitea/models"
1211
"code.gitea.io/gitea/modules/base"
@@ -33,14 +32,14 @@ func Code(ctx *context.Context) {
3332
ctx.Data["PageIsExplore"] = true
3433
ctx.Data["PageIsExploreCode"] = true
3534

36-
language := strings.TrimSpace(ctx.FormString("l"))
37-
keyword := strings.TrimSpace(ctx.FormString("q"))
35+
language := ctx.FormTrim("l")
36+
keyword := ctx.FormTrim("q")
3837
page := ctx.FormInt("page")
3938
if page <= 0 {
4039
page = 1
4140
}
4241

43-
queryType := strings.TrimSpace(ctx.FormString("t"))
42+
queryType := ctx.FormTrim("t")
4443
isMatch := queryType == "match"
4544

4645
var (

routers/web/explore/repo.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ package explore
66

77
import (
88
"net/http"
9-
"strings"
109

1110
"code.gitea.io/gitea/models"
1211
"code.gitea.io/gitea/modules/base"
@@ -73,7 +72,7 @@ func RenderRepoSearch(ctx *context.Context, opts *RepoSearchOptions) {
7372
orderBy = models.SearchOrderByRecentUpdated
7473
}
7574

76-
keyword := strings.Trim(ctx.FormString("q"), " ")
75+
keyword := ctx.FormTrim("q")
7776
topicOnly := ctx.FormBool("topic")
7877
ctx.Data["TopicOnly"] = topicOnly
7978

routers/web/explore/user.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ package explore
77
import (
88
"bytes"
99
"net/http"
10-
"strings"
1110

1211
"code.gitea.io/gitea/models"
1312
"code.gitea.io/gitea/modules/base"
@@ -63,7 +62,7 @@ func RenderUserSearch(ctx *context.Context, opts *models.SearchUserOptions, tplN
6362
orderBy = models.SearchOrderByAlphabetically
6463
}
6564

66-
opts.Keyword = strings.Trim(ctx.FormString("q"), " ")
65+
opts.Keyword = ctx.FormTrim("q")
6766
opts.OrderBy = orderBy
6867
if len(opts.Keyword) == 0 || isKeywordValid(opts.Keyword) {
6968
users, count, err = models.SearchUsers(opts)

routers/web/goget.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import (
1818
)
1919

2020
func goGet(ctx *context.Context) {
21-
if ctx.Req.Method != "GET" || ctx.FormString("go-get") != "1" || len(ctx.Req.URL.Query()) > 1 {
21+
if ctx.Req.Method != "GET" || len(ctx.Req.URL.RawQuery) < 8 || ctx.FormString("go-get") != "1" {
2222
return
2323
}
2424

routers/web/org/home.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ package org
66

77
import (
88
"net/http"
9-
"strings"
109

1110
"code.gitea.io/gitea/models"
1211
"code.gitea.io/gitea/modules/base"
@@ -78,7 +77,7 @@ func Home(ctx *context.Context) {
7877
orderBy = models.SearchOrderByRecentUpdated
7978
}
8079

81-
keyword := strings.Trim(ctx.FormString("q"), " ")
80+
keyword := ctx.FormTrim("q")
8281
ctx.Data["Keyword"] = keyword
8382

8483
page := ctx.FormInt("page")

routers/web/repo/commit.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ func SearchCommits(ctx *context.Context) {
177177
ctx.Data["PageIsCommits"] = true
178178
ctx.Data["PageIsViewCode"] = true
179179

180-
query := strings.Trim(ctx.FormString("q"), " ")
180+
query := ctx.FormTrim("q")
181181
if len(query) == 0 {
182182
ctx.Redirect(ctx.Repo.RepoLink + "/commits/" + ctx.Repo.BranchNameSubURL())
183183
return

routers/web/repo/issue.go

+3-6
Original file line numberDiff line numberDiff line change
@@ -1722,14 +1722,12 @@ func UpdateIssueContent(ctx *context.Context) {
17221722
return
17231723
}
17241724

1725-
content := ctx.FormString("content")
1726-
if err := issue_service.ChangeContent(issue, ctx.User, content); err != nil {
1725+
if err := issue_service.ChangeContent(issue, ctx.User, ctx.Req.FormValue("content")); err != nil {
17271726
ctx.ServerError("ChangeContent", err)
17281727
return
17291728
}
17301729

1731-
files := ctx.FormStrings("files[]")
1732-
if err := updateAttachments(issue, files); err != nil {
1730+
if err := updateAttachments(issue, ctx.FormStrings("files[]")); err != nil {
17331731
ctx.ServerError("UpdateAttachments", err)
17341732
return
17351733
}
@@ -2157,8 +2155,7 @@ func UpdateCommentContent(ctx *context.Context) {
21572155
return
21582156
}
21592157

2160-
files := ctx.FormStrings("files[]")
2161-
if err := updateAttachments(comment, files); err != nil {
2158+
if err := updateAttachments(comment, ctx.FormStrings("files[]")); err != nil {
21622159
ctx.ServerError("UpdateAttachments", err)
21632160
return
21642161
}

routers/web/repo/milestone.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ package repo
66

77
import (
88
"net/http"
9-
"strings"
109
"time"
1110

1211
"code.gitea.io/gitea/models"
@@ -47,7 +46,7 @@ func Milestones(ctx *context.Context) {
4746

4847
sortType := ctx.FormString("sort")
4948

50-
keyword := strings.Trim(ctx.FormString("q"), " ")
49+
keyword := ctx.FormTrim("q")
5150

5251
page := ctx.FormInt("page")
5352
if page <= 1 {

routers/web/repo/search.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ package repo
66

77
import (
88
"net/http"
9-
"strings"
109

1110
"code.gitea.io/gitea/modules/base"
1211
"code.gitea.io/gitea/modules/context"
@@ -22,13 +21,13 @@ func Search(ctx *context.Context) {
2221
ctx.Redirect(ctx.Repo.RepoLink, 302)
2322
return
2423
}
25-
language := strings.TrimSpace(ctx.FormString("l"))
26-
keyword := strings.TrimSpace(ctx.FormString("q"))
24+
language := ctx.FormTrim("l")
25+
keyword := ctx.FormTrim("q")
2726
page := ctx.FormInt("page")
2827
if page <= 0 {
2928
page = 1
3029
}
31-
queryType := strings.TrimSpace(ctx.FormString("t"))
30+
queryType := ctx.FormTrim("t")
3231
isMatch := queryType == "match"
3332

3433
total, searchResults, searchResultLanguages, err := code_indexer.PerformSearch([]int64{ctx.Repo.Repository.ID},

routers/web/repo/topic.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ func TopicsPost(ctx *context.Context) {
2323
}
2424

2525
var topics = make([]string, 0)
26-
var topicsStr = strings.TrimSpace(ctx.FormString("topics"))
26+
var topicsStr = ctx.FormTrim("topics")
2727
if len(topicsStr) > 0 {
2828
topics = strings.Split(topicsStr, ",")
2929
}

routers/web/user/auth.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -1491,8 +1491,7 @@ func ForgotPasswd(ctx *context.Context) {
14911491
return
14921492
}
14931493

1494-
email := ctx.FormString("email")
1495-
ctx.Data["Email"] = email
1494+
ctx.Data["Email"] = ctx.FormString("email")
14961495

14971496
ctx.Data["IsResetRequest"] = true
14981497
ctx.HTML(http.StatusOK, tplForgotPassword)

routers/web/user/home.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ func Milestones(ctx *context.Context) {
204204
isShowClosed = ctx.FormString("state") == "closed"
205205
sortType = ctx.FormString("sort")
206206
page = ctx.FormInt("page")
207-
keyword = strings.Trim(ctx.FormString("q"), " ")
207+
keyword = ctx.FormTrim("q")
208208
)
209209

210210
if page <= 1 {

routers/web/user/notification.go

+4-5
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"errors"
99
"fmt"
1010
"net/http"
11-
"strconv"
1211
"strings"
1312

1413
"code.gitea.io/gitea/models"
@@ -59,7 +58,7 @@ func Notifications(c *context.Context) {
5958

6059
func getNotifications(c *context.Context) {
6160
var (
62-
keyword = strings.Trim(c.FormString("q"), " ")
61+
keyword = c.FormTrim("q")
6362
status models.NotificationStatus
6463
page = c.FormInt("page")
6564
perPage = c.FormInt("perPage")
@@ -144,9 +143,9 @@ func getNotifications(c *context.Context) {
144143
// NotificationStatusPost is a route for changing the status of a notification
145144
func NotificationStatusPost(c *context.Context) {
146145
var (
147-
notificationID, _ = strconv.ParseInt(c.Req.PostFormValue("notification_id"), 10, 64)
148-
statusStr = c.Req.PostFormValue("status")
149-
status models.NotificationStatus
146+
notificationID = c.FormInt64("notification_id")
147+
statusStr = c.FormString("status")
148+
status models.NotificationStatus
150149
)
151150

152151
switch statusStr {

routers/web/user/profile.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ func Profile(ctx *context.Context) {
187187
orderBy = models.SearchOrderByRecentUpdated
188188
}
189189

190-
keyword := strings.Trim(ctx.FormString("q"), " ")
190+
keyword := ctx.FormTrim("q")
191191
ctx.Data["Keyword"] = keyword
192192
switch tab {
193193
case "followers":

0 commit comments

Comments
 (0)