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