mirror of
https://github.com/labstack/echo.git
synced 2025-02-03 13:11:39 +02:00
The directory path does not end with '/', it needs to be redirected (#1572)
* The directory path does not end with '/', it needs to be redirected * changed guide highlighting to shell (#1593) * Fix recover print stack trace log level (#1604) * Fix recover print stack trace log level * Add recover log level test * Add default LogLevel to DefaultRecoverConfig Co-authored-by: solym <ymwh@foxmai.com> Co-authored-by: roz3x <52892437+roz3x@users.noreply.github.com> Co-authored-by: Masahiro Furudate <178inaba.git@gmail.com>
This commit is contained in:
parent
6463bcb190
commit
28020c2a47
14
echo.go
14
echo.go
@ -48,6 +48,7 @@ import (
|
|||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
"os"
|
||||||
"path"
|
"path"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"reflect"
|
"reflect"
|
||||||
@ -479,7 +480,20 @@ func (common) static(prefix, root string, get func(string, HandlerFunc, ...Middl
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
name := filepath.Join(root, path.Clean("/"+p)) // "/"+ for security
|
name := filepath.Join(root, path.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)
|
return c.File(name)
|
||||||
}
|
}
|
||||||
if prefix == "/" {
|
if prefix == "/" {
|
||||||
|
13
echo_test.go
13
echo_test.go
@ -76,9 +76,17 @@ func TestEchoStatic(t *testing.T) {
|
|||||||
|
|
||||||
// Directory
|
// Directory
|
||||||
e.Static("/images", "_fixture/images")
|
e.Static("/images", "_fixture/images")
|
||||||
c, _ = request(http.MethodGet, "/images", e)
|
c, _ = request(http.MethodGet, "/images/", e)
|
||||||
assert.Equal(http.StatusNotFound, c)
|
assert.Equal(http.StatusNotFound, c)
|
||||||
|
|
||||||
|
// Directory Redirect
|
||||||
|
e.Static("/", "_fixture")
|
||||||
|
req := httptest.NewRequest(http.MethodGet, "/folder", nil)
|
||||||
|
rec := httptest.NewRecorder()
|
||||||
|
e.ServeHTTP(rec, req)
|
||||||
|
assert.Equal(http.StatusMovedPermanently, rec.Code)
|
||||||
|
assert.Equal("/folder/", rec.HeaderMap["Location"][0])
|
||||||
|
|
||||||
// Directory with index.html
|
// Directory with index.html
|
||||||
e.Static("/", "_fixture")
|
e.Static("/", "_fixture")
|
||||||
c, r := request(http.MethodGet, "/", e)
|
c, r := request(http.MethodGet, "/", e)
|
||||||
@ -86,9 +94,10 @@ func TestEchoStatic(t *testing.T) {
|
|||||||
assert.Equal(true, strings.HasPrefix(r, "<!doctype html>"))
|
assert.Equal(true, strings.HasPrefix(r, "<!doctype html>"))
|
||||||
|
|
||||||
// Sub-directory with index.html
|
// Sub-directory with index.html
|
||||||
c, r = request(http.MethodGet, "/folder", e)
|
c, r = request(http.MethodGet, "/folder/", e)
|
||||||
assert.Equal(http.StatusOK, c)
|
assert.Equal(http.StatusOK, c)
|
||||||
assert.Equal(true, strings.HasPrefix(r, "<!doctype html>"))
|
assert.Equal(true, strings.HasPrefix(r, "<!doctype html>"))
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestEchoFile(t *testing.T) {
|
func TestEchoFile(t *testing.T) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user