Skip to content

Commit c21529a

Browse files
committed
golink: strip trailing punctuation when resolving links
It's not uncommon for auto-linkers or simple copy/paste errors to accidentally include trailing punctuation in a golink. When resolving links, if the initial link was not found, then try again with common punctuation (that is invalid in link names anyway) removed. Fixes #148 Change-Id: Ia101a4a3005adb9118051b3416f5a64a4a45987d Signed-off-by: Will Norris <[email protected]>
1 parent d55b2a3 commit c21529a

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

golink.go

+9
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,15 @@ func serveGo(w http.ResponseWriter, r *http.Request) {
527527
}
528528

529529
link, err := db.Load(short)
530+
if errors.Is(err, fs.ErrNotExist) {
531+
// Trim common punctuation from the end and try again.
532+
// This catches auto-linking and copy/paste issues that include punctuation.
533+
if s := strings.TrimRight(short, ".,()[]{}"); short != s {
534+
short = s
535+
link, err = db.Load(short)
536+
}
537+
}
538+
530539
if errors.Is(err, fs.ErrNotExist) {
531540
w.WriteHeader(http.StatusNotFound)
532541
serveHome(w, r, short)

golink_test.go

+25
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,31 @@ func TestServeGo(t *testing.T) {
7575
wantStatus: http.StatusFound,
7676
wantLink: "http://who/http://host",
7777
},
78+
{
79+
name: "simple link, trailing period",
80+
link: "/who.",
81+
wantStatus: http.StatusFound,
82+
wantLink: "http://who/",
83+
},
84+
{
85+
name: "simple link, trailing comma",
86+
link: "/who,",
87+
wantStatus: http.StatusFound,
88+
wantLink: "http://who/",
89+
},
90+
{
91+
// This seems like an incredibly unlikely typo, but test it anyway.
92+
name: "simple link, trailing comma and path",
93+
link: "/who,/p",
94+
wantStatus: http.StatusFound,
95+
wantLink: "http://who/p",
96+
},
97+
{
98+
name: "simple link, trailing paren",
99+
link: "/who)",
100+
wantStatus: http.StatusFound,
101+
wantLink: "http://who/",
102+
},
78103
{
79104
name: "user link",
80105
link: "/me",

0 commit comments

Comments
 (0)