Skip to content

Commit f00de63

Browse files
committed
parse link short name just from request path
Previously, we were parsing from r.RequestURI, which includes both the path and query string. This causes problem for requests like go/who?q which try to lookup a link named "who?q" rather than "who" (see #77). For now, this just ignores the request query string. Eventually we should probably retain the query string, but this begins by parsing out the short name properly. Updates #77 Signed-off-by: Will Norris <[email protected]>
1 parent 3ec5bd9 commit f00de63

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

golink.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ func serveOpenSearch(w http.ResponseWriter, _ *http.Request) {
351351
}
352352

353353
func serveGo(w http.ResponseWriter, r *http.Request) {
354-
if r.RequestURI == "/" {
354+
if r.URL.Path == "/" {
355355
switch r.Method {
356356
case "GET":
357357
serveHome(w, "")
@@ -361,7 +361,7 @@ func serveGo(w http.ResponseWriter, r *http.Request) {
361361
return
362362
}
363363

364-
short, remainder, _ := strings.Cut(strings.TrimPrefix(r.RequestURI, "/"), "/")
364+
short, remainder, _ := strings.Cut(strings.TrimPrefix(r.URL.Path, "/"), "/")
365365

366366
// redirect {name}+ links to /.detail/{name}
367367
if strings.HasSuffix(short, "+") {
@@ -420,7 +420,7 @@ type detailData struct {
420420
}
421421

422422
func serveDetail(w http.ResponseWriter, r *http.Request) {
423-
short := strings.TrimPrefix(r.RequestURI, "/.detail/")
423+
short := strings.TrimPrefix(r.URL.Path, "/.detail/")
424424

425425
link, err := db.Load(short)
426426
if errors.Is(err, fs.ErrNotExist) {
@@ -568,7 +568,7 @@ func userExists(ctx context.Context, login string) (bool, error) {
568568
var reShortName = regexp.MustCompile(`^\w[\w\-\.]*$`)
569569

570570
func serveDelete(w http.ResponseWriter, r *http.Request) {
571-
short := strings.TrimPrefix(r.RequestURI, "/.delete/")
571+
short := strings.TrimPrefix(r.URL.Path, "/.delete/")
572572
if short == "" {
573573
http.Error(w, "short required", http.StatusBadRequest)
574574
return

golink_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,24 @@ func TestServeGo(t *testing.T) {
5050
wantStatus: http.StatusFound,
5151
wantLink: "http://who/",
5252
},
53+
{
54+
name: "simple link with path",
55+
link: "/who/p",
56+
wantStatus: http.StatusFound,
57+
wantLink: "http://who/p",
58+
},
59+
{
60+
name: "simple link with query",
61+
link: "/who?q",
62+
wantStatus: http.StatusFound,
63+
wantLink: "http://who/", // TODO: eventually http://who/?q
64+
},
65+
{
66+
name: "simple link with path and query",
67+
link: "/who/p?q",
68+
wantStatus: http.StatusFound,
69+
wantLink: "http://who/p", // TODO: eventually http://who/p?q
70+
},
5371
{
5472
name: "user link",
5573
link: "/me",

0 commit comments

Comments
 (0)