mirror of
https://github.com/labstack/echo.git
synced 2024-12-24 20:14:31 +02:00
Adds IgnoreBase parameter to static middleware
Adds IgnoreBase parameter to static middleware to support the use case of nested route groups
This commit is contained in:
parent
502cce28d5
commit
3206527cfe
1
_fixture/_fixture/README.md
Normal file
1
_fixture/_fixture/README.md
Normal file
@ -0,0 +1 @@
|
|||||||
|
This directory is used for the static middleware test
|
@ -36,6 +36,12 @@ type (
|
|||||||
// Enable directory browsing.
|
// Enable directory browsing.
|
||||||
// Optional. Default value false.
|
// Optional. Default value false.
|
||||||
Browse bool `yaml:"browse"`
|
Browse bool `yaml:"browse"`
|
||||||
|
|
||||||
|
// Enable ignoring of the base of the URL path.
|
||||||
|
// Example: when assigning a static middleware to a non root path group,
|
||||||
|
// the filesystem path is not doubled
|
||||||
|
// Optional. Default value false.
|
||||||
|
IgnoreBase bool `yaml:"ignoreBase"`
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -163,6 +169,15 @@ func StaticWithConfig(config StaticConfig) echo.MiddlewareFunc {
|
|||||||
}
|
}
|
||||||
name := filepath.Join(config.Root, path.Clean("/"+p)) // "/"+ for security
|
name := filepath.Join(config.Root, path.Clean("/"+p)) // "/"+ for security
|
||||||
|
|
||||||
|
if config.IgnoreBase {
|
||||||
|
routePath := path.Base(strings.TrimRight(c.Path(), "/*"))
|
||||||
|
baseURLPath := path.Base(p)
|
||||||
|
if baseURLPath == routePath {
|
||||||
|
i := strings.LastIndex(name, routePath)
|
||||||
|
name = name[:i] + strings.Replace(name[i:], routePath, "", 1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fi, err := os.Stat(name)
|
fi, err := os.Stat(name)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if os.IsNotExist(err) {
|
if os.IsNotExist(err) {
|
||||||
|
@ -67,4 +67,27 @@ func TestStatic(t *testing.T) {
|
|||||||
assert.Equal(http.StatusOK, rec.Code)
|
assert.Equal(http.StatusOK, rec.Code)
|
||||||
assert.Contains(rec.Body.String(), "cert.pem")
|
assert.Contains(rec.Body.String(), "cert.pem")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// IgnoreBase
|
||||||
|
req = httptest.NewRequest(http.MethodGet, "/_fixture", nil)
|
||||||
|
rec = httptest.NewRecorder()
|
||||||
|
config.Root = "../_fixture"
|
||||||
|
config.IgnoreBase = true
|
||||||
|
static = StaticWithConfig(config)
|
||||||
|
c.Echo().Group("_fixture", static)
|
||||||
|
e.ServeHTTP(rec, req)
|
||||||
|
|
||||||
|
assert.Equal(http.StatusOK, rec.Code)
|
||||||
|
assert.Equal(rec.Header().Get(echo.HeaderContentLength), "122")
|
||||||
|
|
||||||
|
req = httptest.NewRequest(http.MethodGet, "/_fixture", nil)
|
||||||
|
rec = httptest.NewRecorder()
|
||||||
|
config.Root = "../_fixture"
|
||||||
|
config.IgnoreBase = false
|
||||||
|
static = StaticWithConfig(config)
|
||||||
|
c.Echo().Group("_fixture", static)
|
||||||
|
e.ServeHTTP(rec, req)
|
||||||
|
|
||||||
|
assert.Equal(http.StatusOK, rec.Code)
|
||||||
|
assert.Contains(rec.Body.String(), "..\\_fixture\\_fixture")
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user