Skip to content

Commit

Permalink
fix: filesystem handler returned incorrect error when file not found
Browse files Browse the repository at this point in the history
  • Loading branch information
a-h committed Mar 13, 2021
1 parent 057d923 commit 77bffd7
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
dist
cmd/gemini
4 changes: 4 additions & 0 deletions filesystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ func FileSystemHandler(fs FileSystem) Handler {
}
f, err := fs.Open(r.URL.Path)
if err != nil {
if os.IsNotExist(err) {
NotFoundHandler().ServeGemini(w, r)
return
}
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
Expand Down
9 changes: 9 additions & 0 deletions filesystem_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,15 @@ func TestFileSystemHandler(t *testing.T) {
expectedHeader: geminiSuccessHeader,
expectedBody: "# /tests/a/index.gmi\n",
},
{
name: "non-existent files return a 51 status code",
url: "/a/non-existent.gmi",
expectedHeader: Header{
Code: CodeNotFound,
Meta: "",
},
expectedBody: "",
},
{
name: "a slash prefix is added if missing",
url: "a/index.gmi",
Expand Down
2 changes: 1 addition & 1 deletion server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ func TestServer(t *testing.T) {
expectedBodyErr: nil,
},
{
name: "panics in handlers return a GCI error",
name: "panics in handlers return a CGI error",
request: "gemini://sensible\r\n",
handler: func(w ResponseWriter, r *Request) {
panic("oops")
Expand Down

0 comments on commit 77bffd7

Please sign in to comment.