mirror of
https://github.com/wahyd4/golink.git
synced 2026-08-09 05:05:56 +10:00
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:
@@ -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
|
||||
|
||||
@@ -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",
|
||||
|
||||
Reference in New Issue
Block a user