mirror of
https://gitea.invidious.io/iv-org/shard-kemal.git
synced 2024-08-15 00:53:36 +00:00
Merge pull request #387 from kemalcr/static-headers
Allow headers in built-in static file server
This commit is contained in:
commit
c81f8f979a
4 changed files with 41 additions and 0 deletions
12
spec/static/dir/index.html
Normal file
12
spec/static/dir/index.html
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="utf-8">
|
||||||
|
<title>title</title>
|
||||||
|
<link rel="stylesheet" href="style.css">
|
||||||
|
<script src="script.js"></script>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<!-- page content -->
|
||||||
|
</body>
|
||||||
|
</html>
|
|
@ -130,4 +130,24 @@ describe Kemal::StaticFileHandler do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "should handle setting custom headers" do
|
||||||
|
headers = Proc(HTTP::Server::Response, String, File::Stat, Void).new do |response, path, stat|
|
||||||
|
if path =~ /\.html$/
|
||||||
|
response.headers.add("Access-Control-Allow-Origin", "*")
|
||||||
|
end
|
||||||
|
response.headers.add("Content-Size", stat.size.to_s)
|
||||||
|
end
|
||||||
|
|
||||||
|
static_headers(&headers)
|
||||||
|
|
||||||
|
response = handle HTTP::Request.new("GET", "/dir/test.txt")
|
||||||
|
response.headers.has_key?("Access-Control-Allow-Origin").should be_false
|
||||||
|
response.headers["Content-Size"].should eq(
|
||||||
|
File.stat("#{__DIR__}/static/dir/test.txt").size.to_s
|
||||||
|
)
|
||||||
|
|
||||||
|
response = handle HTTP::Request.new("GET", "/dir/index.html")
|
||||||
|
response.headers["Access-Control-Allow-Origin"].should eq("*")
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -21,6 +21,7 @@ module Kemal
|
||||||
always_rescue, serve_static : (Bool | Hash(String, Bool)), server, extra_options,
|
always_rescue, serve_static : (Bool | Hash(String, Bool)), server, extra_options,
|
||||||
shutdown_message
|
shutdown_message
|
||||||
getter custom_handler_position
|
getter custom_handler_position
|
||||||
|
property static_headers : (HTTP::Server::Response, String, File::Stat -> Void)?
|
||||||
|
|
||||||
def initialize
|
def initialize
|
||||||
@host_binding = "0.0.0.0"
|
@host_binding = "0.0.0.0"
|
||||||
|
|
|
@ -102,6 +102,10 @@ def send_file(env, path : String, mime_type : String? = nil)
|
||||||
minsize = 860 # http://webmasters.stackexchange.com/questions/31750/what-is-recommended-minimum-object-size-for-gzip-performance-benefits ??
|
minsize = 860 # http://webmasters.stackexchange.com/questions/31750/what-is-recommended-minimum-object-size-for-gzip-performance-benefits ??
|
||||||
request_headers = env.request.headers
|
request_headers = env.request.headers
|
||||||
filesize = File.size(file_path)
|
filesize = File.size(file_path)
|
||||||
|
filestat = File.stat(file_path)
|
||||||
|
|
||||||
|
Kemal.config.static_headers.try(&.call(env.response, file_path, filestat))
|
||||||
|
|
||||||
File.open(file_path) do |file|
|
File.open(file_path) do |file|
|
||||||
if env.request.method == "GET" && env.request.headers.has_key?("Range")
|
if env.request.method == "GET" && env.request.headers.has_key?("Range")
|
||||||
next multipart(file, env)
|
next multipart(file, env)
|
||||||
|
@ -196,3 +200,7 @@ end
|
||||||
def gzip(status : Bool = false)
|
def gzip(status : Bool = false)
|
||||||
add_handler HTTP::CompressHandler.new if status
|
add_handler HTTP::CompressHandler.new if status
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def static_headers(&headers : HTTP::Server::Response, String, File::Stat -> Void)
|
||||||
|
Kemal.config.static_headers = headers
|
||||||
|
end
|
||||||
|
|
Loading…
Reference in a new issue