mirror of
https://gitea.invidious.io/iv-org/shard-kemal.git
synced 2024-08-15 00:53:36 +00:00
Enable option for index.html to be a directories default (#640)
This commit is contained in:
parent
268e501a63
commit
05d55540b9
3 changed files with 24 additions and 4 deletions
|
@ -23,6 +23,15 @@ describe Kemal::StaticFileHandler do
|
|||
response.body.should eq(File.read("#{__DIR__}/static/dir/test.txt"))
|
||||
end
|
||||
|
||||
it "should serve the 'index.html' file when a directory is requested and index serving is enabled" do
|
||||
serve_static({"dir_index" => true})
|
||||
response = handle HTTP::Request.new("GET", "/dir/")
|
||||
response.status_code.should eq(200)
|
||||
response.headers["Content-Type"].should eq "text/html"
|
||||
response.headers["Etag"].should contain "W/\""
|
||||
response.body.should eq(File.read("#{__DIR__}/static/dir/index.html"))
|
||||
end
|
||||
|
||||
it "should respond with 304 if file has not changed" do
|
||||
response = handle HTTP::Request.new("GET", "/dir/test.txt")
|
||||
response.status_code.should eq(200)
|
||||
|
|
|
@ -31,7 +31,7 @@ module Kemal
|
|||
@host_binding = "0.0.0.0"
|
||||
@port = 3000
|
||||
@env = ENV["KEMAL_ENV"]? || "development"
|
||||
@serve_static = {"dir_listing" => false, "gzip" => true}
|
||||
@serve_static = {"dir_listing" => false, "gzip" => true, "dir_index" => false}
|
||||
@public_folder = "./public"
|
||||
@logging = true
|
||||
@logger = nil
|
||||
|
|
|
@ -36,7 +36,7 @@ module Kemal
|
|||
end
|
||||
|
||||
file_path = File.join(@public_dir, expanded_path)
|
||||
is_dir = Dir.exists? file_path
|
||||
is_dir = Dir.exists?(file_path)
|
||||
|
||||
if request_path != expanded_path
|
||||
redirect_to context, expanded_path
|
||||
|
@ -44,8 +44,19 @@ module Kemal
|
|||
redirect_to context, expanded_path + '/'
|
||||
end
|
||||
|
||||
if Dir.exists?(file_path)
|
||||
if config.is_a?(Hash) && config["dir_listing"] == true
|
||||
if is_dir
|
||||
if config.is_a?(Hash) && config.fetch("dir_index", false) && File.exists?(File.join(file_path, "index.html"))
|
||||
file_path = File.join(@public_dir, expanded_path, "index.html")
|
||||
|
||||
last_modified = modification_time(file_path)
|
||||
add_cache_headers(context.response.headers, last_modified)
|
||||
|
||||
if cache_request?(context, last_modified)
|
||||
context.response.status_code = 304
|
||||
return
|
||||
end
|
||||
send_file(context, file_path)
|
||||
elsif config.is_a?(Hash) && config.fetch("dir_listing", false)
|
||||
context.response.content_type = "text/html"
|
||||
directory_listing(context.response, request_path, file_path)
|
||||
else
|
||||
|
|
Loading…
Reference in a new issue