Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

golink: strip trailing punctuation when resolving links #159

Merged
merged 1 commit into from
Jan 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions golink.go
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,15 @@ func serveGo(w http.ResponseWriter, r *http.Request) {
}

link, err := db.Load(short)
if errors.Is(err, fs.ErrNotExist) {
// Trim common punctuation from the end and try again.
// This catches auto-linking and copy/paste issues that include punctuation.
if s := strings.TrimRight(short, ".,()[]{}"); short != s {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even though it's valid, it might be worth taking ? too, since such a URL's query is empty anyway at that point (the same argument could apply to ., since it's valid in paths). YMMV.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm... I tried adding ?, but turns out it's not really needed. Since this is based solely on the r.URL.Path value, it seems that the only way a question mark could appear is if it was url encoded as %3F. But then we never url-decode, so it wouldn't actually get stripped off. Since we're just trying to handle accidental punctuation, I don't think we need to worry about ?, since it would get stripped already by the URL parsing.

short = s
link, err = db.Load(short)
}
}

if errors.Is(err, fs.ErrNotExist) {
w.WriteHeader(http.StatusNotFound)
serveHome(w, r, short)
Expand Down
25 changes: 25 additions & 0 deletions golink_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,31 @@ func TestServeGo(t *testing.T) {
wantStatus: http.StatusFound,
wantLink: "http://who/http://host",
},
{
name: "simple link, trailing period",
link: "/who.",
wantStatus: http.StatusFound,
wantLink: "http://who/",
},
{
name: "simple link, trailing comma",
link: "/who,",
wantStatus: http.StatusFound,
wantLink: "http://who/",
},
{
// This seems like an incredibly unlikely typo, but test it anyway.
name: "simple link, trailing comma and path",
link: "/who,/p",
wantStatus: http.StatusFound,
wantLink: "http://who/p",
},
{
name: "simple link, trailing paren",
link: "/who)",
wantStatus: http.StatusFound,
wantLink: "http://who/",
},
{
name: "user link",
link: "/me",
Expand Down
Loading