1
0
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:
风吹过 2020-08-28 08:53:48 +08:00 committed by GitHub
parent 6463bcb190
commit 28020c2a47
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 2 deletions

14
echo.go
View File

@ -48,6 +48,7 @@ import (
"net"
"net/http"
"net/url"
"os"
"path"
"path/filepath"
"reflect"
@ -479,7 +480,20 @@ func (common) static(prefix, root string, get func(string, HandlerFunc, ...Middl
if err != nil {
return err
}
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)
}
if prefix == "/" {

View File

@ -76,9 +76,17 @@ func TestEchoStatic(t *testing.T) {
// Directory
e.Static("/images", "_fixture/images")
c, _ = request(http.MethodGet, "/images", e)
c, _ = request(http.MethodGet, "/images/", e)
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
e.Static("/", "_fixture")
c, r := request(http.MethodGet, "/", e)
@ -86,9 +94,10 @@ func TestEchoStatic(t *testing.T) {
assert.Equal(true, strings.HasPrefix(r, "<!doctype 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(true, strings.HasPrefix(r, "<!doctype html>"))
}
func TestEchoFile(t *testing.T) {