Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions spec/std/http/server/handlers/static_file_handler_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions src/http/server/handlers/static_file_handler.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down