Skip to content

Commit

Permalink
Add an option to allow empty root in the fsHandler (#1299)
Browse files Browse the repository at this point in the history
* Add an option to allow empty root in the fsHandler

this is necessary to restore the capabilities before the commit c7576cc
and to allow further functionality to be docked from the outside which is not affected by setting the root dir afterwards

gofiber/fiber#1882 (comment)

* Add an option to allow empty root in the fsHandler

this is necessary to restore the capabilities before the commit c7576cc
and to allow further functionality to be docked from the outside which is not affected by setting the root dir afterwards

gofiber/fiber#1882 (comment)

* Update fs.go

Co-authored-by: Erik Dubbelboer <[email protected]>

* Update fs.go

Co-authored-by: Erik Dubbelboer <[email protected]>

Co-authored-by: Erik Dubbelboer <[email protected]>
  • Loading branch information
ReneWerner87 and erikdubbelboer authored May 16, 2022
1 parent 9961079 commit 66bc61e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 12 deletions.
26 changes: 15 additions & 11 deletions fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ var (
rootFSOnce sync.Once
rootFS = &FS{
Root: "",
AllowEmptyRoot: true,
GenerateIndexPages: true,
Compress: true,
CompressBrotli: true,
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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 {
Expand Down
1 change: 0 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ golang.org/x/sys v0.0.0-20220227234510-4e6760a101f9 h1:nhht2DYV/Sn3qOayu8lM+cU1i
golang.org/x/sys v0.0.0-20220227234510-4e6760a101f9/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/text v0.3.6 h1:aRYxNxv6iGQlyVaZmk6ZgYEDa+Jg18DxebPSrd6bg1M=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
Expand Down

0 comments on commit 66bc61e

Please sign in to comment.