diff options
author | Moritz Marquardt <git@mo-mar.de> | 2020-05-18 18:30:13 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-05-18 18:30:13 +0200 |
commit | 5bd1b0cf2c661162f6855f63f09c4a72f7c7fa45 (patch) | |
tree | e7a282bec0524971f5a787a2795f2950178a093c /fs.go | |
parent | Fix race condition in test (diff) | |
download | fasthttp-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.go | 9 |
1 files changed, 9 insertions, 0 deletions
@@ -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) |