Adapt StaticFileHandler for Crystal 1.6 (#3269)
This commit is contained in:
commit
b0bb156918
|
@ -111,7 +111,7 @@ module Kemal
|
||||||
if @fallthrough
|
if @fallthrough
|
||||||
call_next(context)
|
call_next(context)
|
||||||
else
|
else
|
||||||
context.response.status_code = 405
|
context.response.status = HTTP::Status::METHOD_NOT_ALLOWED
|
||||||
context.response.headers.add("Allow", "GET, HEAD")
|
context.response.headers.add("Allow", "GET, HEAD")
|
||||||
end
|
end
|
||||||
return
|
return
|
||||||
|
@ -124,7 +124,7 @@ module Kemal
|
||||||
# File path cannot contains '\0' (NUL) because all filesystem I know
|
# File path cannot contains '\0' (NUL) because all filesystem I know
|
||||||
# don't accept '\0' character as file name.
|
# don't accept '\0' character as file name.
|
||||||
if request_path.includes? '\0'
|
if request_path.includes? '\0'
|
||||||
context.response.status_code = 400
|
context.response.status = HTTP::Status::BAD_REQUEST
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -143,13 +143,15 @@ module Kemal
|
||||||
add_cache_headers(context.response.headers, last_modified)
|
add_cache_headers(context.response.headers, last_modified)
|
||||||
|
|
||||||
if cache_request?(context, last_modified)
|
if cache_request?(context, last_modified)
|
||||||
context.response.status_code = 304
|
context.response.status = HTTP::Status::NOT_MODIFIED
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
send_file(context, file_path, file[:data], file[:filestat])
|
send_file(context, file_path, file[:data], file[:filestat])
|
||||||
else
|
else
|
||||||
is_dir = Dir.exists? file_path
|
file_info = File.info?(file_path)
|
||||||
|
is_dir = file_info.try &.directory? || false
|
||||||
|
is_file = file_info.try &.file? || false
|
||||||
|
|
||||||
if request_path != expanded_path
|
if request_path != expanded_path
|
||||||
redirect_to context, expanded_path
|
redirect_to context, expanded_path
|
||||||
|
@ -157,19 +159,21 @@ module Kemal
|
||||||
redirect_to context, expanded_path + '/'
|
redirect_to context, expanded_path + '/'
|
||||||
end
|
end
|
||||||
|
|
||||||
if Dir.exists?(file_path)
|
return call_next(context) if file_info.nil?
|
||||||
|
|
||||||
|
if is_dir
|
||||||
if config.is_a?(Hash) && config["dir_listing"] == true
|
if config.is_a?(Hash) && config["dir_listing"] == true
|
||||||
context.response.content_type = "text/html"
|
context.response.content_type = "text/html"
|
||||||
directory_listing(context.response, request_path, file_path)
|
directory_listing(context.response, request_path, file_path)
|
||||||
else
|
else
|
||||||
call_next(context)
|
call_next(context)
|
||||||
end
|
end
|
||||||
elsif File.exists?(file_path)
|
elsif is_file
|
||||||
last_modified = modification_time(file_path)
|
last_modified = file_info.modification_time
|
||||||
add_cache_headers(context.response.headers, last_modified)
|
add_cache_headers(context.response.headers, last_modified)
|
||||||
|
|
||||||
if cache_request?(context, last_modified)
|
if cache_request?(context, last_modified)
|
||||||
context.response.status_code = 304
|
context.response.status = HTTP::Status::NOT_MODIFIED
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -177,14 +181,12 @@ module Kemal
|
||||||
data = Bytes.new(size)
|
data = Bytes.new(size)
|
||||||
File.open(file_path, &.read(data))
|
File.open(file_path, &.read(data))
|
||||||
|
|
||||||
filestat = File.info(file_path)
|
@cached_files[file_path] = {data: data, filestat: file_info}
|
||||||
|
send_file(context, file_path, data, file_info)
|
||||||
@cached_files[file_path] = {data: data, filestat: filestat}
|
|
||||||
send_file(context, file_path, data, filestat)
|
|
||||||
else
|
else
|
||||||
send_file(context, file_path)
|
send_file(context, file_path)
|
||||||
end
|
end
|
||||||
else
|
else # Not a normal file (FIFO/device/socket)
|
||||||
call_next(context)
|
call_next(context)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue