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 <will@tailscale.com>
This commit is contained in:
Will Norris
2022-06-02 15:23:46 -07:00
committed by Will Norris
parent f9c85e6b51
commit 172ed66cc3
+10 -2
View File
@@ -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 != "" }