Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disable directory path redirect when directory_listing=false #15393

Open
wants to merge 3 commits into
base: release/1.15
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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=true" do
straight-shoota marked this conversation as resolved.
Show resolved Hide resolved
response = handle HTTP::Request.new("GET", "/foo"), directory_listing: false
response.status_code.should eq(404)
end

it "redirect directory when directory_listing=false" do
straight-shoota marked this conversation as resolved.
Show resolved Hide resolved
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
Loading