From 1e18389f5fa7b25ccf016b01dafbc06bc260ee95 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafa=C5=82=20Wojsznis?= Date: Mon, 16 Jul 2018 04:54:36 +0200 Subject: [PATCH] Remove etag logic as it's not implemented in Crystal 0.25.1 (#470) --- spec/static_file_handler_spec.cr | 1 + src/kemal/static_file_handler.cr | 18 +++++++----------- 2 files changed, 8 insertions(+), 11 deletions(-) diff --git a/spec/static_file_handler_spec.cr b/spec/static_file_handler_spec.cr index 6da77a3..1aac161 100644 --- a/spec/static_file_handler_spec.cr +++ b/spec/static_file_handler_spec.cr @@ -30,6 +30,7 @@ describe Kemal::StaticFileHandler do headers = HTTP::Headers{"If-None-Match" => etag} response = handle HTTP::Request.new("GET", "/dir/test.txt", headers) + response.headers["Content-Type"]?.should be_nil response.status_code.should eq(304) response.body.should eq "" end diff --git a/src/kemal/static_file_handler.cr b/src/kemal/static_file_handler.cr index fc9e036..8fa802e 100644 --- a/src/kemal/static_file_handler.cr +++ b/src/kemal/static_file_handler.cr @@ -50,21 +50,17 @@ module Kemal return call_next(context) end 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) else call_next(context) 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