From 172ed66cc383be35ba83604d12afbc895367a2ff Mon Sep 17 00:00:00 2001 From: Will Norris Date: Thu, 2 Jun 2022 15:23:46 -0700 Subject: [PATCH] golinks: append remaining path onto destination allow for links like http://go/who/will to resolve to http://who/will Change-Id: I79e502eb9d9e874a863c7062b6449197b03cbabc Signed-off-by: Will Norris --- golink.go | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/golink.go b/golink.go index 5343fa4..16f7d48 100644 --- a/golink.go +++ b/golink.go @@ -14,6 +14,7 @@ import ( "net/http" "net/url" "os" + "path" "path/filepath" "sort" "strings" @@ -161,7 +162,7 @@ func serveGo(w http.ResponseWriter, r *http.Request) { return } - short := strings.ToLower(strings.TrimPrefix(r.RequestURI, "/")) + short, remainder, _ := strings.Cut(strings.ToLower(strings.TrimPrefix(r.RequestURI, "/")), "/") dl, err := loadLink(short) if os.IsNotExist(err) { @@ -176,7 +177,14 @@ func serveGo(w http.ResponseWriter, r *http.Request) { return } - http.Redirect(w, r, dl.Long, http.StatusFound) + dest, err := url.Parse(dl.Long) + if err != nil { + log.Printf("error parsing URL %q: %v", dl.Long, err) + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + dest.Path = path.Join(dest.RequestURI(), remainder) + http.Redirect(w, r, dest.String(), http.StatusFound) } func devMode() bool { return *dev != "" }