diff --git a/filesystem.go b/filesystem.go index a97cc00..f7e01c5 100644 --- a/filesystem.go +++ b/filesystem.go @@ -50,7 +50,7 @@ func DirectoryListingHandler(path string, f File) Handler { return HandlerFunc(func(w ResponseWriter, r *Request) { files, err := f.Readdir(-1) if err != nil { - log.Error("DirectoryListingHandler: readdir failed", err, log.String("path", r.URL.Path), log.String("url", r.URL.String())) + log.Warn("DirectoryListingHandler: readdir failed", log.String("reason", err.Error()), log.String("path", r.URL.Path), log.String("url", r.URL.String())) w.SetHeader(CodeTemporaryFailure, "readdir failed") return } @@ -92,20 +92,21 @@ func FileSystemHandler(fs FileSystem) Handler { } f, err := fs.Open(r.URL.Path) if err != nil { - log.Error("FileSystemHandler: file open failed", err, log.String("path", r.URL.Path), log.String("url", r.URL.String())) + log.Warn("FileSystemHandler: file open failed", log.String("reason", err.Error()), log.String("path", r.URL.Path), log.String("url", r.URL.String())) w.SetHeader(CodeTemporaryFailure, "file open failed") return } stat, err := f.Stat() if err != nil { - log.Error("FileSystemHandler: file stat failed", err, log.String("path", r.URL.Path), log.String("url", r.URL.String())) + log.Warn("FileSystemHandler: file stat failed", log.String("reason", err.Error()), log.String("path", r.URL.Path), log.String("url", r.URL.String())) w.SetHeader(CodeTemporaryFailure, "file stat failed") return } if stat.IsDir() { // Look for index.gmi first before listing contents. if !strings.HasSuffix(r.URL.Path, "/") { - r.URL.Path += "/" + RedirectPermanentHandler(r.URL.Path+"/").ServeGemini(w, r) + return } index, err := fs.Open(r.URL.Path + "index.gmi") if errors.Is(err, os.ErrNotExist) { diff --git a/filesystem_test.go b/filesystem_test.go index 97977e6..bd00306 100644 --- a/filesystem_test.go +++ b/filesystem_test.go @@ -20,8 +20,14 @@ func TestFileSystemHandler(t *testing.T) { expectedBody string }{ { - name: "if a directory contains index.gmi, it is used", + name: "directories without a trailing slash are redirected", url: "/a", + expectedHeader: Header{Code: CodeRedirectPermanent, Meta: "/a/"}, + expectedBody: "", + }, + { + name: "if a directory contains index.gmi, it is used", + url: "/a/", expectedHeader: geminiSuccessHeader, expectedBody: "# /tests/a/index.gmi\n", }, @@ -39,7 +45,7 @@ func TestFileSystemHandler(t *testing.T) { }, { name: "if a directory does not contain an index, a listing is returned", - url: "/b", + url: "/b/", expectedHeader: geminiSuccessHeader, expectedBody: `# Index of /b/