aboutsummaryrefslogtreecommitdiff
path: root/fs.go
diff options
context:
space:
mode:
authorGravatar Moritz Marquardt <git@mo-mar.de> 2020-05-18 18:30:13 +0200
committerGravatar GitHub <noreply@github.com> 2020-05-18 18:30:13 +0200
commit5bd1b0cf2c661162f6855f63f09c4a72f7c7fa45 (patch)
treee7a282bec0524971f5a787a2795f2950178a093c /fs.go
parentFix race condition in test (diff)
downloadfasthttp-5bd1b0cf2c661162f6855f63f09c4a72f7c7fa45.tar.gz
fasthttp-5bd1b0cf2c661162f6855f63f09c4a72f7c7fa45.tar.bz2
fasthttp-5bd1b0cf2c661162f6855f63f09c4a72f7c7fa45.zip
Make FS return a redirect for directories without trailing slash (#802)
* Make FS return a redirect for directories without trailing slash Fixes #792 * Add a test for the directory redirect * Fix directory redirects for ServeFile * Fix error message Co-authored-by: Erik Dubbelboer <erik@dubbelboer.com>
Diffstat (limited to 'fs.go')
-rw-r--r--fs.go9
1 files changed, 9 insertions, 0 deletions
diff --git a/fs.go b/fs.go
index ca70f72..28b34f8 100644
--- a/fs.go
+++ b/fs.go
@@ -84,12 +84,16 @@ func ServeFile(ctx *RequestCtx, path string) {
})
if len(path) == 0 || path[0] != '/' {
// extend relative path to absolute path
+ hasTrailingSlash := path[len(path) - 1] == '/'
var err error
if path, err = filepath.Abs(path); err != nil {
ctx.Logger().Printf("cannot resolve path %q to absolute file path: %s", path, err)
ctx.Error("Internal Server Error", StatusInternalServerError)
return
}
+ if hasTrailingSlash {
+ path += "/"
+ }
}
ctx.Request.SetRequestURI(path)
rootFSHandler(ctx)
@@ -684,6 +688,7 @@ func (h *fsHandler) handleRequest(ctx *RequestCtx) {
} else {
path = ctx.Path()
}
+ hasTrailingSlash := path[len(path) - 1] == '/'
path = stripTrailingSlashes(path)
if n := bytes.IndexByte(path, 0); n >= 0 {
@@ -729,6 +734,10 @@ func (h *fsHandler) handleRequest(ctx *RequestCtx) {
ff, err = h.openFSFile(filePath, mustCompress)
}
if err == errDirIndexRequired {
+ if !hasTrailingSlash {
+ ctx.RedirectBytes(append(path, '/'), StatusFound)
+ return
+ }
ff, err = h.openIndexFile(ctx, filePath, mustCompress)
if err != nil {
ctx.Logger().Printf("cannot open dir index %q: %s", filePath, err)