diff options
Diffstat (limited to 'vendor/github.com/labstack/echo/middleware/static.go')
-rw-r--r-- | vendor/github.com/labstack/echo/middleware/static.go | 39 |
1 files changed, 31 insertions, 8 deletions
diff --git a/vendor/github.com/labstack/echo/middleware/static.go b/vendor/github.com/labstack/echo/middleware/static.go index 793c1445..e715c1c4 100644 --- a/vendor/github.com/labstack/echo/middleware/static.go +++ b/vendor/github.com/labstack/echo/middleware/static.go @@ -3,7 +3,9 @@ package middleware import ( "fmt" "os" + "path" "path/filepath" + "strings" "github.com/labstack/echo" ) @@ -53,6 +55,9 @@ func Static(root string) echo.MiddlewareFunc { // See `Static()`. func StaticWithConfig(config StaticConfig) echo.MiddlewareFunc { // Defaults + if config.Root == "" { + config.Root = "." // For security we want to restrict to CWD. + } if config.Skipper == nil { config.Skipper = DefaultStaticConfig.Skipper } @@ -62,26 +67,44 @@ func StaticWithConfig(config StaticConfig) echo.MiddlewareFunc { return func(next echo.HandlerFunc) echo.HandlerFunc { return func(c echo.Context) error { - p := c.Param("*") - name := filepath.Join(config.Root, p) - fi, err := os.Stat(name) + if config.Skipper(c) { + return next(c) + } + p := c.Request().URL.Path + if strings.HasSuffix(c.Path(), "*") { // When serving from a group, e.g. `/static*`. + p = c.Param("*") + } + name := filepath.Join(config.Root, path.Clean("/"+p)) // "/"+ for security + + fi, err := os.Stat(name) if err != nil { if os.IsNotExist(err) { - if config.HTML5 { + if config.HTML5 && path.Ext(p) == "" { return c.File(filepath.Join(config.Root, config.Index)) } - return echo.ErrNotFound + return next(c) } return err } if fi.IsDir() { - if config.Browse { - return listDir(name, c.Response()) + index := filepath.Join(name, config.Index) + fi, err = os.Stat(index) + + if err != nil { + if config.Browse { + return listDir(name, c.Response()) + } + if os.IsNotExist(err) { + return next(c) + } + return err } - return c.File(filepath.Join(name, config.Index)) + + return c.File(index) } + return c.File(name) } } |