diff options
author | Wim <wim@42.be> | 2022-03-12 19:41:07 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-03-12 19:41:07 +0100 |
commit | b3be2e208cb373207d6199cac5a9fc92be073e7a (patch) | |
tree | 6de6d444737edee8f0476eea87c233fa980f0002 /vendor/github.com/labstack/echo/v4/echo_fs.go | |
parent | c30e90ff3f7e9ff96ac79ed4b7d90d6346216a15 (diff) | |
download | matterbridge-msglm-b3be2e208cb373207d6199cac5a9fc92be073e7a.tar.gz matterbridge-msglm-b3be2e208cb373207d6199cac5a9fc92be073e7a.tar.bz2 matterbridge-msglm-b3be2e208cb373207d6199cac5a9fc92be073e7a.zip |
Update dependencies and vendor (#1761)
Diffstat (limited to 'vendor/github.com/labstack/echo/v4/echo_fs.go')
-rw-r--r-- | vendor/github.com/labstack/echo/v4/echo_fs.go | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/vendor/github.com/labstack/echo/v4/echo_fs.go b/vendor/github.com/labstack/echo/v4/echo_fs.go new file mode 100644 index 00000000..c3790545 --- /dev/null +++ b/vendor/github.com/labstack/echo/v4/echo_fs.go @@ -0,0 +1,62 @@ +//go:build !go1.16 +// +build !go1.16 + +package echo + +import ( + "net/http" + "net/url" + "os" + "path/filepath" +) + +type filesystem struct { +} + +func createFilesystem() filesystem { + return filesystem{} +} + +// Static registers a new route with path prefix to serve static files from the +// provided root directory. +func (e *Echo) Static(prefix, root string) *Route { + if root == "" { + root = "." // For security we want to restrict to CWD. + } + return e.static(prefix, root, e.GET) +} + +func (common) static(prefix, root string, get func(string, HandlerFunc, ...MiddlewareFunc) *Route) *Route { + h := func(c Context) error { + p, err := url.PathUnescape(c.Param("*")) + if err != nil { + return err + } + + name := filepath.Join(root, filepath.Clean("/"+p)) // "/"+ for security + fi, err := os.Stat(name) + if err != nil { + // The access path does not exist + return NotFoundHandler(c) + } + + // If the request is for a directory and does not end with "/" + p = c.Request().URL.Path // path must not be empty. + if fi.IsDir() && p[len(p)-1] != '/' { + // Redirect to ends with "/" + return c.Redirect(http.StatusMovedPermanently, p+"/") + } + return c.File(name) + } + // Handle added routes based on trailing slash: + // /prefix => exact route "/prefix" + any route "/prefix/*" + // /prefix/ => only any route "/prefix/*" + if prefix != "" { + if prefix[len(prefix)-1] == '/' { + // Only add any route for intentional trailing slash + return get(prefix+"*", h) + } + get(prefix, h) + } + return get(prefix+"/*", h) +} |