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 <will@tailscale.com>
This commit is contained in:
Will Norris
2023-05-16 10:17:35 -07:00
committed by Will Norris
parent 3ec5bd9693
commit f00de63b45
2 changed files with 22 additions and 4 deletions
+4 -4
View File
@@ -351,7 +351,7 @@ func serveOpenSearch(w http.ResponseWriter, _ *http.Request) {
}
func serveGo(w http.ResponseWriter, r *http.Request) {
if r.RequestURI == "/" {
if r.URL.Path == "/" {
switch r.Method {
case "GET":
serveHome(w, "")
@@ -361,7 +361,7 @@ func serveGo(w http.ResponseWriter, r *http.Request) {
return
}
short, remainder, _ := strings.Cut(strings.TrimPrefix(r.RequestURI, "/"), "/")
short, remainder, _ := strings.Cut(strings.TrimPrefix(r.URL.Path, "/"), "/")
// redirect {name}+ links to /.detail/{name}
if strings.HasSuffix(short, "+") {
@@ -420,7 +420,7 @@ type detailData struct {
}
func serveDetail(w http.ResponseWriter, r *http.Request) {
short := strings.TrimPrefix(r.RequestURI, "/.detail/")
short := strings.TrimPrefix(r.URL.Path, "/.detail/")
link, err := db.Load(short)
if errors.Is(err, fs.ErrNotExist) {
@@ -568,7 +568,7 @@ func userExists(ctx context.Context, login string) (bool, error) {
var reShortName = regexp.MustCompile(`^\w[\w\-\.]*$`)
func serveDelete(w http.ResponseWriter, r *http.Request) {
short := strings.TrimPrefix(r.RequestURI, "/.delete/")
short := strings.TrimPrefix(r.URL.Path, "/.delete/")
if short == "" {
http.Error(w, "short required", http.StatusBadRequest)
return
+18
View File
@@ -50,6 +50,24 @@ func TestServeGo(t *testing.T) {
wantStatus: http.StatusFound,
wantLink: "http://who/",
},
{
name: "simple link with path",
link: "/who/p",
wantStatus: http.StatusFound,
wantLink: "http://who/p",
},
{
name: "simple link with query",
link: "/who?q",
wantStatus: http.StatusFound,
wantLink: "http://who/", // TODO: eventually http://who/?q
},
{
name: "simple link with path and query",
link: "/who/p?q",
wantStatus: http.StatusFound,
wantLink: "http://who/p", // TODO: eventually http://who/p?q
},
{
name: "user link",
link: "/me",