Skip to content

Commit

Permalink
feat: added redirects from bare directories to including a trailing s…
Browse files Browse the repository at this point in the history
…lash
  • Loading branch information
a-h committed Mar 8, 2021
1 parent c659de2 commit 626ec5f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
9 changes: 5 additions & 4 deletions filesystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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) {
Expand Down
10 changes: 8 additions & 2 deletions filesystem_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
},
Expand All @@ -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/
Expand Down

0 comments on commit 626ec5f

Please sign in to comment.