diff --git a/spec/std/http/server/handlers/static_file_handler_spec.cr b/spec/std/http/server/handlers/static_file_handler_spec.cr index 036e53eef2cc..dc87febc9e43 100644 --- a/spec/std/http/server/handlers/static_file_handler_spec.cr +++ b/spec/std/http/server/handlers/static_file_handler_spec.cr @@ -425,6 +425,17 @@ describe HTTP::StaticFileHandler do response.status_code.should eq(404) end + it "does not redirect directory when directory_listing=false" do + response = handle HTTP::Request.new("GET", "/foo"), directory_listing: false + response.status_code.should eq(404) + end + + it "redirect directory when directory_listing=true" do + response = handle HTTP::Request.new("GET", "/foo"), directory_listing: true + response.status_code.should eq(302) + response.headers["Location"].should eq "/foo/" + end + it "does not serve a not found file" do response = handle HTTP::Request.new("GET", "/not_found_file.txt") response.status_code.should eq(404) diff --git a/src/http/server/handlers/static_file_handler.cr b/src/http/server/handlers/static_file_handler.cr index cba0ff993ad2..665f466c7e46 100644 --- a/src/http/server/handlers/static_file_handler.cr +++ b/src/http/server/handlers/static_file_handler.cr @@ -68,7 +68,7 @@ class HTTP::StaticFileHandler file_path = @public_dir.join(expanded_path.to_kind(Path::Kind.native)) file_info = File.info? file_path - is_dir = file_info && file_info.directory? + is_dir = @directory_listing && file_info && file_info.directory? is_file = file_info && file_info.file? if request_path != expanded_path || is_dir && !is_dir_path @@ -85,7 +85,7 @@ class HTTP::StaticFileHandler context.response.headers["Accept-Ranges"] = "bytes" - if @directory_listing && is_dir + if is_dir context.response.content_type = "text/html; charset=utf-8" directory_listing(context.response, request_path, file_path) elsif is_file