fix: folders in FileSystems are now correctly returned as 404 instead of 200 without content
This commit is contained in:
parent
144d640578
commit
773209071c
2 changed files with 77 additions and 4 deletions
39
fs.go
39
fs.go
|
@ -51,18 +51,24 @@ func (fs *FS) ServeHTTP(w http.ResponseWriter, r *http.Request, info RoutingInfo
|
||||||
}
|
}
|
||||||
|
|
||||||
// search for file in static file system
|
// search for file in static file system
|
||||||
file, err := fs.findFile(url)
|
file, filePath, err := fs.findFile(url)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.NotFound(w, r)
|
http.NotFound(w, r)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
// check, if the file is a folder
|
||||||
|
file, ok := fs.checkFolder(file, filePath)
|
||||||
|
if !ok {
|
||||||
|
http.NotFound(w, r)
|
||||||
|
return
|
||||||
|
}
|
||||||
defer file.Close()
|
defer file.Close()
|
||||||
|
|
||||||
// serve file content
|
// serve file content
|
||||||
fs.serve(w, r, info, file)
|
fs.serve(w, r, info, file)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (fs *FS) findFile(url string) (fs.File, error) {
|
func (fs *FS) findFile(url string) (fs.File, string, error) {
|
||||||
// build file path
|
// build file path
|
||||||
filePath := path.Join(fs.FolderPrefix, url)
|
filePath := path.Join(fs.FolderPrefix, url)
|
||||||
filePath = strings.TrimPrefix(filePath, "/")
|
filePath = strings.TrimPrefix(filePath, "/")
|
||||||
|
@ -71,15 +77,40 @@ func (fs *FS) findFile(url string) (fs.File, error) {
|
||||||
if fs.UseLocalFolder {
|
if fs.UseLocalFolder {
|
||||||
localFilePath := path.Join(fs.LocalFolderPrefix, filePath)
|
localFilePath := path.Join(fs.LocalFolderPrefix, filePath)
|
||||||
localFilePath = strings.TrimPrefix(localFilePath, "/")
|
localFilePath = strings.TrimPrefix(localFilePath, "/")
|
||||||
|
if localFilePath == "" {
|
||||||
|
localFilePath = "."
|
||||||
|
}
|
||||||
|
|
||||||
file, err := os.Open(localFilePath)
|
file, err := os.Open(localFilePath)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
return file, nil
|
return file, localFilePath, nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// use static file system
|
// use static file system
|
||||||
return fs.StaticFiles.Open(filePath)
|
if filePath == "" {
|
||||||
|
filePath = "."
|
||||||
|
}
|
||||||
|
file, err := fs.StaticFiles.Open(filePath)
|
||||||
|
return file, filePath, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (fs *FS) checkFolder(file fs.File, url string) (fs.File, bool) {
|
||||||
|
fileInfo, err := file.Stat()
|
||||||
|
if err != nil {
|
||||||
|
_ = file.Close()
|
||||||
|
return file, false
|
||||||
|
}
|
||||||
|
|
||||||
|
// check for folder / root location
|
||||||
|
if fileInfo.IsDir() {
|
||||||
|
// close folder handler (we don't need it anymore)
|
||||||
|
_ = file.Close()
|
||||||
|
|
||||||
|
return file, false
|
||||||
|
}
|
||||||
|
|
||||||
|
return file, true
|
||||||
}
|
}
|
||||||
|
|
||||||
func (fs *FS) serve(w http.ResponseWriter, r *http.Request, info RoutingInfo, file fs.File) {
|
func (fs *FS) serve(w http.ResponseWriter, r *http.Request, info RoutingInfo, file fs.File) {
|
||||||
|
|
42
fs_test.go
42
fs_test.go
|
@ -114,6 +114,48 @@ func TestFSLicense(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestFSFolder(t *testing.T) {
|
||||||
|
// create new FS
|
||||||
|
fs := NewFS(&staticFiles)
|
||||||
|
|
||||||
|
// serve the request
|
||||||
|
tw := &testWriter{}
|
||||||
|
tr := &http.Request{
|
||||||
|
Method: http.MethodHead,
|
||||||
|
URL: &url.URL{
|
||||||
|
Path: "/",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
fs.ServeHTTP(tw, tr, RoutingInfo{})
|
||||||
|
|
||||||
|
// check data
|
||||||
|
if tw.statusCode != http.StatusNotFound {
|
||||||
|
t.Error("received invalid http status code", tw.statusCode)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestFSFolderLocal(t *testing.T) {
|
||||||
|
// create new FS
|
||||||
|
fs := NewFS(&staticFiles)
|
||||||
|
fs.UseLocalFolder = true
|
||||||
|
fs.LocalFolderPrefix = "/"
|
||||||
|
|
||||||
|
// serve the request
|
||||||
|
tw := &testWriter{}
|
||||||
|
tr := &http.Request{
|
||||||
|
Method: http.MethodHead,
|
||||||
|
URL: &url.URL{
|
||||||
|
Path: "/",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
fs.ServeHTTP(tw, tr, RoutingInfo{})
|
||||||
|
|
||||||
|
// check data
|
||||||
|
if tw.statusCode != http.StatusNotFound {
|
||||||
|
t.Error("received invalid http status code", tw.statusCode)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestFSNotFound(t *testing.T) {
|
func TestFSNotFound(t *testing.T) {
|
||||||
// create new FS
|
// create new FS
|
||||||
fs := NewFS(&staticFiles)
|
fs := NewFS(&staticFiles)
|
||||||
|
|
Loading…
Add table
Reference in a new issue