Skip to content

Commit 4c7afbe

Browse files
committed
web: remove trailing slashes from internal URLs
The StripSlashes middleware already normalizes incoming requests, so trailing slashes in links, redirects and form actions are unnecessary. Remove them for consistency. Signed-off-by: Robin Jarry <robin@jarry.cc>
1 parent 895e07a commit 4c7afbe

18 files changed

Lines changed: 72 additions & 72 deletions

‎pkg/web/auth.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ func (h *webHandler) RegisterSubmit(w http.ResponseWriter, r *http.Request) {
106106
if baseURL == "" {
107107
baseURL = "http://" + r.Host
108108
}
109-
link := fmt.Sprintf("%s/confirm/%s/", baseURL, conf.Key)
109+
link := fmt.Sprintf("%s/confirm/%s", baseURL, conf.Key)
110110
body := fmt.Sprintf(
111111
"Please click the following link to confirm your registration:\n\n%s\n\nThis link will expire in 7 days.\n",
112112
link,

‎pkg/web/bundles.go‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ func (h *webHandler) BundleUpdate(w http.ResponseWriter, r *http.Request) {
152152
bundlename := urlParam(r, "bundlename")
153153

154154
if !h.validateCSRF(r) {
155-
http.Redirect(w, r, fmt.Sprintf("/bundle/%s/%s/", username, bundlename), http.StatusFound)
155+
http.Redirect(w, r, fmt.Sprintf("/bundle/%s/%s", username, bundlename), http.StatusFound)
156156
return
157157
}
158158

@@ -183,7 +183,7 @@ func (h *webHandler) BundleUpdate(w http.ResponseWriter, r *http.Request) {
183183
serverErrorPage(w, "delete bundle", err)
184184
return
185185
}
186-
http.Redirect(w, r, "/user/bundles/", http.StatusFound)
186+
http.Redirect(w, r, "/user/bundles", http.StatusFound)
187187

188188
case "update":
189189
newName := strings.TrimSpace(r.FormValue("name"))
@@ -197,9 +197,9 @@ func (h *webHandler) BundleUpdate(w http.ResponseWriter, r *http.Request) {
197197
serverErrorPage(w, "update bundle", err)
198198
return
199199
}
200-
http.Redirect(w, r, fmt.Sprintf("/bundle/%s/%s/", username, newName), http.StatusFound)
200+
http.Redirect(w, r, fmt.Sprintf("/bundle/%s/%s", username, newName), http.StatusFound)
201201
} else {
202-
http.Redirect(w, r, fmt.Sprintf("/bundle/%s/%s/", username, bundlename), http.StatusFound)
202+
http.Redirect(w, r, fmt.Sprintf("/bundle/%s/%s", username, bundlename), http.StatusFound)
203203
}
204204

205205
case "remove-patches":
@@ -216,9 +216,9 @@ func (h *webHandler) BundleUpdate(w http.ResponseWriter, r *http.Request) {
216216
return
217217
}
218218
}
219-
http.Redirect(w, r, fmt.Sprintf("/bundle/%s/%s/", username, bundlename), http.StatusFound)
219+
http.Redirect(w, r, fmt.Sprintf("/bundle/%s/%s", username, bundlename), http.StatusFound)
220220

221221
default:
222-
http.Redirect(w, r, fmt.Sprintf("/bundle/%s/%s/", username, bundlename), http.StatusFound)
222+
http.Redirect(w, r, fmt.Sprintf("/bundle/%s/%s", username, bundlename), http.StatusFound)
223223
}
224224
}

‎pkg/web/bundles.templ‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ templ bundleListPage(d bundleListData) {
2828
<tr>
2929
<td>
3030
if b.Owner != nil {
31-
<a href={ templ.SafeURL(fmt.Sprintf("/bundle/%s/%s/", b.Owner.Username, b.Name)) }>
31+
<a href={ templ.SafeURL(fmt.Sprintf("/bundle/%s/%s", b.Owner.Username, b.Name)) }>
3232
{ b.Name }
3333
</a>
3434
} else {
@@ -37,7 +37,7 @@ templ bundleListPage(d bundleListData) {
3737
</td>
3838
<td>
3939
if b.Project != nil {
40-
<a href={ templ.SafeURL("/project/" + b.Project.Linkname + "/list/") }>
40+
<a href={ templ.SafeURL("/project/" + b.Project.Linkname + "/list") }>
4141
{ b.Project.Linkname }
4242
</a>
4343
}
@@ -52,11 +52,11 @@ templ bundleListPage(d bundleListData) {
5252
<td>{ intStr(b.PatchCount) }</td>
5353
<td>
5454
if b.Owner != nil {
55-
<a href={ templ.SafeURL(fmt.Sprintf("/bundle/%s/%s/mbox/", b.Owner.Username, b.Name)) }>mbox</a>
55+
<a href={ templ.SafeURL(fmt.Sprintf("/bundle/%s/%s/mbox", b.Owner.Username, b.Name)) }>mbox</a>
5656
}
5757
</td>
5858
<td>
59-
<form method="post" action={ templ.SafeURL(fmt.Sprintf("/bundle/%s/%s/", b.Owner.Username, b.Name)) }>
59+
<form method="post" action={ templ.SafeURL(fmt.Sprintf("/bundle/%s/%s", b.Owner.Username, b.Name)) }>
6060
<input type="hidden" name="csrftoken" value={ d.PC.CSRFToken }/>
6161
<input type="hidden" name="action" value="delete"/>
6262
<button type="submit"><danger-action>Delete</danger-action></button>
@@ -83,14 +83,14 @@ templ bundleListPage(d bundleListData) {
8383

8484
templ bundleDetailPage(d bundleDetailData) {
8585
@layout(d.Bundle.Name, []breadcrumb{
86-
{Label: "Bundles", URL: "/user/bundles/"},
86+
{Label: "Bundles", URL: "/user/bundles"},
8787
{Label: d.Bundle.Name},
8888
}, d.PC) {
8989
<title-bar>
9090
<h1>{ d.Bundle.Name }</h1>
9191
<download-links>
9292
if d.Bundle.Owner != nil {
93-
<a href={ templ.SafeURL(fmt.Sprintf("/bundle/%s/%s/mbox/", d.Bundle.Owner.Username, d.Bundle.Name)) }>mbox</a>
93+
<a href={ templ.SafeURL(fmt.Sprintf("/bundle/%s/%s/mbox", d.Bundle.Owner.Username, d.Bundle.Name)) }>mbox</a>
9494
}
9595
</download-links>
9696
</title-bar>

‎pkg/web/cover.templ‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ type coverDetailData struct {
2525

2626
templ coverDetailPage(d coverDetailData) {
2727
@layout(d.Cover.Name, []breadcrumb{
28-
{Label: d.Project.Name, URL: "/project/" + d.Project.Linkname + "/"},
29-
{Label: "Patches", URL: "/project/" + d.Project.Linkname + "/list/"},
28+
{Label: d.Project.Name, URL: "/project/" + d.Project.Linkname},
29+
{Label: "Patches", URL: "/project/" + d.Project.Linkname + "/list"},
3030
{Label: truncate(d.Cover.Name, 72)},
3131
}, d.PC) {
3232
<submission-meta>

‎pkg/web/layout.templ‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,15 @@ templ layout(title string, crumbs []breadcrumb, pc pageContext) {
5151
</ul>
5252
<span>
5353
if pc.User != nil {
54-
<a href="/user/bundles/">Bundles</a>
55-
<a href="/user/">{ pc.User.Username }</a>
56-
<form method="post" action="/user/logout/">
54+
<a href="/user/bundles">Bundles</a>
55+
<a href="/user">{ pc.User.Username }</a>
56+
<form method="post" action="/user/logout">
5757
<input type="hidden" name="csrftoken" value={ pc.CSRFToken }/>
5858
<button type="submit">Logout</button>
5959
</form>
6060
} else {
61-
<a href="/user/login/">Login</a>
62-
<a href="/register/">Register</a>
61+
<a href="/user/login">Login</a>
62+
<a href="/register">Register</a>
6363
}
6464
</span>
6565
</nav>

‎pkg/web/login.templ‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@ templ loginPage(pc pageContext, errMsg string, next string) {
1212
if errMsg != "" {
1313
<error-msg>{ errMsg }</error-msg>
1414
}
15-
<form method="post" action="/user/login/">
15+
<form method="post" action="/user/login">
1616
<input type="hidden" name="csrftoken" value={ pc.CSRFToken }/>
1717
<input type="text" name="username" placeholder="Username" required autofocus/>
1818
<input type="password" name="password" placeholder="Password" required/>
1919
<input type="hidden" name="next" value={ next }/>
2020
<button type="submit">Login</button>
2121
</form>
22-
<p><a href="/user/password-reset/">Forgot password?</a></p>
22+
<p><a href="/user/password-reset">Forgot password?</a></p>
2323
</auth-form>
2424
}
2525
}

‎pkg/web/patch.templ‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,17 @@ type patchDetailData struct {
4040
templ downloadLinks(submissionid int, linkname, msgid string, series *db.Series) {
4141
<download-links>
4242
<button>{ submissionid }</button>
43-
<a href={ templ.SafeURL(patchURL(linkname, msgid) + "mbox/") }>mbox</a>
43+
<a href={ templ.SafeURL(patchURL(linkname, msgid) + "/mbox") }>mbox</a>
4444
if series != nil {
45-
<a href={ templ.SafeURL(fmt.Sprintf("/series/%d/mbox/", series.ID)) }>series</a>
45+
<a href={ templ.SafeURL(fmt.Sprintf("/series/%d/mbox", series.ID)) }>series</a>
4646
}
4747
</download-links>
4848
}
4949

5050
templ patchDetailPage(d patchDetailData) {
5151
@layout(d.Patch.Name, []breadcrumb{
52-
{Label: d.Project.Name, URL: "/project/" + d.Project.Linkname + "/"},
53-
{Label: "Patches", URL: "/project/" + d.Project.Linkname + "/list/"},
52+
{Label: d.Project.Name, URL: "/project/" + d.Project.Linkname},
53+
{Label: "Patches", URL: "/project/" + d.Project.Linkname + "/list"},
5454
{Label: truncate(d.Patch.Name, 72)},
5555
}, d.PC) {
5656
<title-bar>
@@ -259,7 +259,7 @@ templ patchDetailPage(d patchDetailData) {
259259
<ul>
260260
for _, r := range d.Related {
261261
<li>
262-
<a href={ templ.SafeURL(fmt.Sprintf("/patch/%d/", r.ID)) }>
262+
<a href={ templ.SafeURL(fmt.Sprintf("/patch/%d", r.ID)) }>
263263
{ r.Name }
264264
</a>
265265
</li>
@@ -293,7 +293,7 @@ templ patchDetailPage(d patchDetailData) {
293293
}
294294
}
295295
if d.CanEdit {
296-
<form method="post" action={ templ.SafeURL(fmt.Sprintf("%scomment/%d/addressed", patchURL(d.Project.Linkname, d.Patch.Msgid), c.ID)) } style="display:inline">
296+
<form method="post" action={ templ.SafeURL(fmt.Sprintf("%s/comment/%d/addressed", patchURL(d.Project.Linkname, d.Patch.Msgid), c.ID)) } style="display:inline">
297297
<input type="hidden" name="csrftoken" value={ d.PC.CSRFToken }/>
298298
if c.Addressed != nil && *c.Addressed {
299299
<input type="hidden" name="addressed" value="false"/>

‎pkg/web/patches.go‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func (h *webHandler) PatchList(w http.ResponseWriter, r *http.Request) {
5050
Where("project_id = ?", project.ID)
5151

5252
var filters []appliedFilter
53-
baseQuery := fmt.Sprintf("/project/%s/list/", linkname)
53+
baseQuery := fmt.Sprintf("/project/%s/list", linkname)
5454

5555
sq, filters = applyWebFilters(q.Ctx, q.DB, sq, params, baseQuery)
5656

@@ -178,7 +178,7 @@ func (h *webHandler) PatchListAction(w http.ResponseWriter, r *http.Request) {
178178
linkname := urlParam(r, "linkname")
179179

180180
if !h.validateCSRF(r) {
181-
http.Redirect(w, r, "/project/"+linkname+"/list/", http.StatusFound)
181+
http.Redirect(w, r, "/project/"+linkname+"/list", http.StatusFound)
182182
return
183183
}
184184

@@ -187,7 +187,7 @@ func (h *webHandler) PatchListAction(w http.ResponseWriter, r *http.Request) {
187187
patchIDs := r.Form["patch_id"]
188188

189189
if len(patchIDs) == 0 {
190-
http.Redirect(w, r, "/project/"+linkname+"/list/", http.StatusFound)
190+
http.Redirect(w, r, "/project/"+linkname+"/list", http.StatusFound)
191191
return
192192
}
193193

@@ -294,7 +294,7 @@ func (h *webHandler) PatchListAction(w http.ResponseWriter, r *http.Request) {
294294
}
295295
}
296296

297-
http.Redirect(w, r, "/project/"+linkname+"/list/", http.StatusFound)
297+
http.Redirect(w, r, "/project/"+linkname+"/list", http.StatusFound)
298298
}
299299

300300
func (h *webHandler) PatchDetailPage(w http.ResponseWriter, r *http.Request) {

‎pkg/web/patches.templ‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ type appliedFilter struct {
3838

3939
templ patchListPage(d patchListData) {
4040
@layout(d.Project.Name, []breadcrumb{
41-
{Label: d.Project.Name, URL: "/project/" + d.Project.Linkname + "/"},
41+
{Label: d.Project.Name, URL: "/project/" + d.Project.Linkname},
4242
{Label: "Patches"},
4343
}, d.PC) {
4444
<patch-count>
@@ -55,7 +55,7 @@ templ patchListPage(d patchListData) {
5555
}
5656
{ intStr(d.Total) } patches
5757
{ " | " }
58-
<a href={ templ.SafeURL("/project/" + d.Project.Linkname + "/bundles/") }>Bundles</a>
58+
<a href={ templ.SafeURL("/project/" + d.Project.Linkname + "/bundles") }>Bundles</a>
5959
{ " | " }
6060
<button type="button" id="toggle-toolbar">Filters &amp; Actions &#9662;</button>
6161
</patch-count>
@@ -85,7 +85,7 @@ templ patchListPage(d patchListData) {
8585
<button type="submit">Filter</button>
8686
</form>
8787
</patch-filters>
88-
<form method="post" action={ templ.SafeURL("/project/" + d.Project.Linkname + "/list/") }>
88+
<form method="post" action={ templ.SafeURL("/project/" + d.Project.Linkname + "/list") }>
8989
<input type="hidden" name="csrftoken" value={ d.PC.CSRFToken }/>
9090
if d.PC.User != nil {
9191
<patch-actions class="toolbar" style="display:none">

‎pkg/web/profile.go‎

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ func (h *webHandler) ProfileUpdate(w http.ResponseWriter, r *http.Request) {
116116
user := getWebUser(r)
117117

118118
if !h.validateCSRF(r) {
119-
http.Redirect(w, r, "/user/", http.StatusFound)
119+
http.Redirect(w, r, "/user", http.StatusFound)
120120
return
121121
}
122122

@@ -133,7 +133,7 @@ func (h *webHandler) ProfileUpdate(w http.ResponseWriter, r *http.Request) {
133133
Set("show_ids = ?", showIds).
134134
Exec(ctx)
135135

136-
http.Redirect(w, r, "/user/", http.StatusFound)
136+
http.Redirect(w, r, "/user", http.StatusFound)
137137
}
138138

139139
func (h *webHandler) LinkEmail(w http.ResponseWriter, r *http.Request) {
@@ -173,7 +173,7 @@ func (h *webHandler) LinkEmail(w http.ResponseWriter, r *http.Request) {
173173
if baseURL == "" {
174174
baseURL = "http://" + r.Host
175175
}
176-
link := fmt.Sprintf("%s/confirm/%s/", baseURL, conf.Key)
176+
link := fmt.Sprintf("%s/confirm/%s", baseURL, conf.Key)
177177
body := fmt.Sprintf(
178178
"Please click the following link to link this email to your Patchwork account:\n\n%s\n",
179179
link,
@@ -198,13 +198,13 @@ func (h *webHandler) UnlinkEmail(w http.ResponseWriter, r *http.Request) {
198198
user := getWebUser(r)
199199

200200
if !h.validateCSRF(r) {
201-
http.Redirect(w, r, "/user/", http.StatusFound)
201+
http.Redirect(w, r, "/user", http.StatusFound)
202202
return
203203
}
204204

205205
personID, err := strconv.ParseInt(chi.URLParam(r, "id"), 10, 32)
206206
if err != nil {
207-
http.Redirect(w, r, "/user/", http.StatusFound)
207+
http.Redirect(w, r, "/user", http.StatusFound)
208208
return
209209
}
210210

@@ -214,12 +214,12 @@ func (h *webHandler) UnlinkEmail(w http.ResponseWriter, r *http.Request) {
214214
Where("user_id = ?", user.ID).
215215
Scan(ctx)
216216
if err != nil {
217-
http.Redirect(w, r, "/user/", http.StatusFound)
217+
http.Redirect(w, r, "/user", http.StatusFound)
218218
return
219219
}
220220

221221
if person.Email == user.Email {
222-
http.Redirect(w, r, "/user/", http.StatusFound)
222+
http.Redirect(w, r, "/user", http.StatusFound)
223223
return
224224
}
225225

@@ -228,7 +228,7 @@ func (h *webHandler) UnlinkEmail(w http.ResponseWriter, r *http.Request) {
228228
Set("user_id = NULL").
229229
Exec(ctx)
230230

231-
http.Redirect(w, r, "/user/", http.StatusFound)
231+
http.Redirect(w, r, "/user", http.StatusFound)
232232
}
233233

234234
func (h *webHandler) ChangePassword(w http.ResponseWriter, r *http.Request) {
@@ -273,7 +273,7 @@ func (h *webHandler) ChangePassword(w http.ResponseWriter, r *http.Request) {
273273
Set("password = ?", db.HashPassword(newPassword)).
274274
Exec(ctx)
275275

276-
http.Redirect(w, r, "/user/", http.StatusFound)
276+
http.Redirect(w, r, "/user", http.StatusFound)
277277
}
278278

279279
func (h *webHandler) GenerateToken(w http.ResponseWriter, r *http.Request) {
@@ -285,7 +285,7 @@ func (h *webHandler) GenerateToken(w http.ResponseWriter, r *http.Request) {
285285
user := getWebUser(r)
286286

287287
if !h.validateCSRF(r) {
288-
http.Redirect(w, r, "/user/", http.StatusFound)
288+
http.Redirect(w, r, "/user", http.StatusFound)
289289
return
290290
}
291291

@@ -306,7 +306,7 @@ func (h *webHandler) GenerateToken(w http.ResponseWriter, r *http.Request) {
306306
return
307307
}
308308

309-
http.Redirect(w, r, "/user/", http.StatusFound)
309+
http.Redirect(w, r, "/user", http.StatusFound)
310310
}
311311

312312
func (h *webHandler) PasswordReset(w http.ResponseWriter, r *http.Request) {
@@ -351,7 +351,7 @@ func (h *webHandler) PasswordReset(w http.ResponseWriter, r *http.Request) {
351351
if baseURL == "" {
352352
baseURL = "http://" + r.Host
353353
}
354-
link := fmt.Sprintf("%s/password-reset/%s/", baseURL, conf.Key)
354+
link := fmt.Sprintf("%s/password-reset/%s", baseURL, conf.Key)
355355
body := fmt.Sprintf(
356356
"Please click the following link to reset your password:\n\n%s\n\nThis link will expire in 7 days.\n",
357357
link,

0 commit comments

Comments
 (0)