diff options
author | Antoni Sawicki <tenox@google.com> | 2022-08-29 00:06:26 -0700 |
---|---|---|
committer | Antoni Sawicki <tenox@google.com> | 2022-08-29 00:06:26 -0700 |
commit | 7ca2ad44a634ed0c6bb97018d0d7dd65d8e9fa7e (patch) | |
tree | 6677c9f62df711a146564daabe6dc8f7cffc2b9d | |
parent | 4e11bb00c7f33d8503f539304b5906e0a7ceb4a9 (diff) | |
download | wfm-7ca2ad44a634ed0c6bb97018d0d7dd65d8e9fa7e.tar.gz |
use gorilla mux for handling prefixes
-rw-r--r-- | TODO.md | 1 | ||||
-rw-r--r-- | go.mod | 1 | ||||
-rw-r--r-- | go.sum | 2 | ||||
-rw-r--r-- | handlers.go | 5 | ||||
-rw-r--r-- | wfm.go | 5 |
5 files changed, 8 insertions, 6 deletions
@@ -9,7 +9,6 @@ * Use custom FS implementation to resolve and deny symlinks outside of srv directory https://github.com/crazcalm/go/commit/8b0b644cd02c59fe2461908304c44d64e8be431e * use direct url path instead of ?dir=xxx&file=yyyy use /prefix/dir/file -* use different mux/router with glob/regex patterns, required for above ## Security * seperate prefix for admin/rw users eg /admin with readonly on / @@ -7,6 +7,7 @@ require ( github.com/breml/rootcerts v0.2.4 github.com/dustin/go-humanize v1.0.0 github.com/gabriel-vasile/mimetype v1.4.0 + github.com/gorilla/mux v1.8.0 github.com/juju/ratelimit v1.0.1 github.com/kdomanski/iso9660 v0.3.1 github.com/mholt/archiver/v4 v4.0.0-alpha.7 @@ -77,6 +77,8 @@ github.com/google/pprof v0.0.0-20200212024743-f11f1df84d12/go.mod h1:ZgVRPoUq/hf github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI= github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+vpHVxEJEs9eg= github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5mhpdKc/us6bOk= +github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI= +github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So= github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4= github.com/hashicorp/errwrap v1.1.0 h1:OxrOeh75EUXMY8TBjag2fzXGZ40LB6IKw45YeGUDY2I= github.com/hashicorp/errwrap v1.1.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4= diff --git a/handlers.go b/handlers.go index 71b2e24..7edde82 100644 --- a/handlers.go +++ b/handlers.go @@ -37,11 +37,10 @@ func wfmMain(w http.ResponseWriter, r *http.Request) { } wfm.uFbn = filepath.Base(r.FormValue("file")) wfm.uDir = filepath.Clean(r.FormValue("dir")) + // directory can come from form value or URI if wfm.uDir == "" || wfm.uDir == "." { - // this only works with empty/default prefix - // TODO(tenox): use different mux/router with glob/regex patterns u, _ := url.QueryUnescape(r.RequestURI) - wfm.uDir = filepath.Clean(u) + wfm.uDir = filepath.Clean("/" + strings.TrimPrefix(u, *wfmPfx)) } if wfm.uDir == "" || wfm.uDir == "." { wfm.uDir = "/" @@ -15,6 +15,7 @@ import ( "syscall" _ "github.com/breml/rootcerts" + "github.com/gorilla/mux" "github.com/juju/ratelimit" "golang.org/x/crypto/acme/autocert" ) @@ -193,8 +194,8 @@ func main() { } // http stuff - mux := http.NewServeMux() - mux.HandleFunc(*wfmPfx, wfmMain) + mux := mux.NewRouter() + mux.PathPrefix(*wfmPfx).HandlerFunc(wfmMain) mux.HandleFunc("/favicon.ico", dispFavIcon) mux.HandleFunc("/robots.txt", dispRobots) if *f2bDump != "" { |