Remove etag logic as it's not implemented in Crystal 0.25.1 (#470)

This commit is contained in:
Rafał Wojsznis 2018-07-16 04:54:36 +02:00 committed by Serdar Dogruyol
parent b8ec6ee328
commit 1e18389f5f
2 changed files with 8 additions and 11 deletions

View file

@ -30,6 +30,7 @@ describe Kemal::StaticFileHandler do
headers = HTTP::Headers{"If-None-Match" => etag} headers = HTTP::Headers{"If-None-Match" => etag}
response = handle HTTP::Request.new("GET", "/dir/test.txt", headers) response = handle HTTP::Request.new("GET", "/dir/test.txt", headers)
response.headers["Content-Type"]?.should be_nil
response.status_code.should eq(304) response.status_code.should eq(304)
response.body.should eq "" response.body.should eq ""
end end

View file

@ -50,21 +50,17 @@ module Kemal
return call_next(context) return call_next(context)
end end
elsif File.exists?(file_path) elsif File.exists?(file_path)
return if etag(context, file_path) 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) send_file(context, file_path)
else else
call_next(context) call_next(context)
end end
end end
private def etag(context : HTTP::Server::Context, file_path : String)
etag = %{W/"#{File.info(file_path).modification_time.epoch.to_s}"}
context.response.headers["ETag"] = etag
return false if !context.request.headers["If-None-Match"]? || context.request.headers["If-None-Match"] != etag
context.response.headers.delete "Content-Type"
context.response.content_length = 0
context.response.status_code = 304 # not modified
return true
end
end end
end end