aboutsummaryrefslogtreecommitdiff
path: root/fs.go
diff options
context:
space:
mode:
authorGravatar RW <rene@gofiber.io> 2022-05-16 12:33:47 +0200
committerGravatar GitHub <noreply@github.com> 2022-05-16 12:33:47 +0200
commit66bc61ee646a8c1bdd7121afe1cd9c27ecc682a9 (patch)
tree298fd10158a5e2bde8226b4eaf39aa2697e50b22 /fs.go
parentFix userdata re-use after Remove (diff)
downloadfasthttp-66bc61ee646a8c1bdd7121afe1cd9c27ecc682a9.tar.gz
fasthttp-66bc61ee646a8c1bdd7121afe1cd9c27ecc682a9.tar.bz2
fasthttp-66bc61ee646a8c1bdd7121afe1cd9c27ecc682a9.zip
Add an option to allow empty root in the fsHandler (#1299)v1.37.0
* Add an option to allow empty root in the fsHandler this is necessary to restore the capabilities before the commit https://github.com/valyala/fasthttp/commit/c7576cc10cabfc9c993317a2d3f8355497bea156 and to allow further functionality to be docked from the outside which is not affected by setting the root dir afterwards https://github.com/gofiber/fiber/pull/1882#issuecomment-1120832500 * Add an option to allow empty root in the fsHandler this is necessary to restore the capabilities before the commit https://github.com/valyala/fasthttp/commit/c7576cc10cabfc9c993317a2d3f8355497bea156 and to allow further functionality to be docked from the outside which is not affected by setting the root dir afterwards https://github.com/gofiber/fiber/pull/1882#issuecomment-1120832500 * Update fs.go Co-authored-by: Erik Dubbelboer <erik@dubbelboer.com> * Update fs.go Co-authored-by: Erik Dubbelboer <erik@dubbelboer.com> Co-authored-by: Erik Dubbelboer <erik@dubbelboer.com>
Diffstat (limited to 'fs.go')
-rw-r--r--fs.go26
1 files changed, 15 insertions, 11 deletions
diff --git a/fs.go b/fs.go
index d924848..90bbf12 100644
--- a/fs.go
+++ b/fs.go
@@ -128,6 +128,7 @@ var (
rootFSOnce sync.Once
rootFS = &FS{
Root: "",
+ AllowEmptyRoot: true,
GenerateIndexPages: true,
Compress: true,
CompressBrotli: true,
@@ -229,6 +230,12 @@ type FS struct {
// Path to the root directory to serve files from.
Root string
+ // AllowEmptyRoot controls what happens when Root is empty. When false (default) it will default to the
+ // current working directory. An empty root is mostly useful when you want to use absolute paths
+ // on windows that are on different filesystems. On linux setting your Root to "/" already allows you to use
+ // absolute paths on any filesystem.
+ AllowEmptyRoot bool
+
// List of index file names to try opening during directory access.
//
// For example:
@@ -384,19 +391,16 @@ func (fs *FS) NewRequestHandler() RequestHandler {
func (fs *FS) initRequestHandler() {
root := fs.Root
- // rootFS' handleRequest will do special treatment of absolute and relative paths
- if fs != rootFS {
- // serve files from the current working directory if root is empty
- if len(root) == 0 || !filepath.IsAbs(root) {
- path, err := os.Getwd()
- if err != nil {
- path = "."
- }
- root = path + "/" + root
+ // Serve files from the current working directory if Root is empty or if Root is a relative path.
+ if (!fs.AllowEmptyRoot && len(root) == 0) || (len(root) > 0 && !filepath.IsAbs(root)) {
+ path, err := os.Getwd()
+ if err != nil {
+ path = "."
}
- // convert the root directory slashes to the native format
- root = filepath.FromSlash(root)
+ root = path + "/" + root
}
+ // convert the root directory slashes to the native format
+ root = filepath.FromSlash(root)
// strip trailing slashes from the root path
for len(root) > 0 && root[len(root)-1] == os.PathSeparator {