From 7bd45543f6a0e51d9dc96ad9bbecb57e05dad8af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Greg=20Kar=C3=A9kinian?= Date: Mon, 16 Apr 2018 17:23:53 +0200 Subject: [PATCH] Set headers from the Redis metadata on a GET that results in a 304 Also add specs to check for the response headers --- lib/remote_storage/rest_provider.rb | 8 ++++++-- spec/s3/app_spec.rb | 4 ++++ spec/swift/app_spec.rb | 4 ++++ 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/lib/remote_storage/rest_provider.rb b/lib/remote_storage/rest_provider.rb index 9034533..37ebe10 100644 --- a/lib/remote_storage/rest_provider.rb +++ b/lib/remote_storage/rest_provider.rb @@ -49,8 +49,12 @@ module RemoteStorage none_match = (server.env["HTTP_IF_NONE_MATCH"] || "").split(",") .map(&:strip) .map { |s| s.gsub(/^"?W\//, "") } - etag = redis.hget redis_metadata_object_key(user, directory, key), "e" - server.halt 304 if none_match.include? %Q("#{etag}") + existing_metadata = redis.hgetall redis_metadata_object_key(user, directory, key) + if none_match.include? %Q("#{existing_metadata["e"]}") + server.headers["ETag"] = %Q("#{existing_metadata["e"]}") + server.headers["Last-Modified"] = Time.at(existing_metadata["m"].to_i / 1000).httpdate + server.halt 304 + end url = url_for_key(user, directory, key) diff --git a/spec/s3/app_spec.rb b/spec/s3/app_spec.rb index 17188ed..8f818f1 100644 --- a/spec/s3/app_spec.rb +++ b/spec/s3/app_spec.rb @@ -687,6 +687,8 @@ describe "App" do get "/phil/food/aguacate" last_response.status.must_equal 304 + last_response.headers["ETag"].must_equal "\"0815etag\"" + last_response.headers["Last-Modified"].must_equal "Fri, 04 Mar 2016 12:20:18 GMT" end it "responds with 304 when IF_NONE_MATCH header contains weak ETAG matching the current ETag" do @@ -695,6 +697,8 @@ describe "App" do get "/phil/food/aguacate" last_response.status.must_equal 304 + last_response.headers["ETag"].must_equal "\"0815etag\"" + last_response.headers["Last-Modified"].must_equal "Fri, 04 Mar 2016 12:20:18 GMT" end end diff --git a/spec/swift/app_spec.rb b/spec/swift/app_spec.rb index 1cb6aae..c400af4 100644 --- a/spec/swift/app_spec.rb +++ b/spec/swift/app_spec.rb @@ -645,6 +645,8 @@ describe "App" do get "/phil/food/aguacate" last_response.status.must_equal 304 + last_response.headers["ETag"].must_equal "\"0815etag\"" + last_response.headers["Last-Modified"].must_equal "Fri, 04 Mar 2016 12:20:18 GMT" end it "responds with 304 when IF_NONE_MATCH header contains weak ETAG matching the current ETag" do @@ -653,6 +655,8 @@ describe "App" do get "/phil/food/aguacate" last_response.status.must_equal 304 + last_response.headers["ETag"].must_equal "\"0815etag\"" + last_response.headers["Last-Modified"].must_equal "Fri, 04 Mar 2016 12:20:18 GMT" end end