diff --git a/lib/remote_storage/rest_provider.rb b/lib/remote_storage/rest_provider.rb index c353389..9034533 100644 --- a/lib/remote_storage/rest_provider.rb +++ b/lib/remote_storage/rest_provider.rb @@ -46,17 +46,18 @@ module RemoteStorage end def get_data(user, directory, key) + 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}") + url = url_for_key(user, directory, key) res = do_get_request(url) set_response_headers(res) - none_match = (server.env["HTTP_IF_NONE_MATCH"] || "").split(",") - .map(&:strip) - .map { |s| s.gsub(/^"?W\//, "") } - server.halt 304 if none_match.include? format_etag(res.headers[:etag]) - return res.body rescue RestClient::ResourceNotFound server.halt 404, "Not Found" diff --git a/spec/s3/app_spec.rb b/spec/s3/app_spec.rb index 3842a3b..17188ed 100644 --- a/spec/s3/app_spec.rb +++ b/spec/s3/app_spec.rb @@ -639,7 +639,7 @@ describe "App" do header "Authorization", "Bearer amarillo" put_stub = OpenStruct.new(headers: { - etag: '"bla"' + etag: '"0815etag"' }) RestClient.stub :put, put_stub do @@ -684,16 +684,7 @@ describe "App" do it "responds with 304 when IF_NONE_MATCH header contains the ETag" do header "If-None-Match", "\"0815etag\"" - get_stub = OpenStruct.new(body: "si", headers: { - etag: '"0815etag"', - last_modified: "Fri, 04 Mar 2016 12:20:18 GMT", - content_type: "text/plain; charset=utf-8", - content_length: 2 - }) - - RestClient.stub :get, get_stub do - get "/phil/food/aguacate" - end + get "/phil/food/aguacate" last_response.status.must_equal 304 end @@ -701,16 +692,7 @@ describe "App" do it "responds with 304 when IF_NONE_MATCH header contains weak ETAG matching the current ETag" do header "If-None-Match", "W/\"0815etag\"" - get_stub = OpenStruct.new(body: "si", headers: { - etag: '"0815etag"', - last_modified: "Fri, 04 Mar 2016 12:20:18 GMT", - content_type: "text/plain; charset=utf-8", - content_length: 2 - }) - - RestClient.stub :get, get_stub do - get "/phil/food/aguacate" - end + get "/phil/food/aguacate" last_response.status.must_equal 304 end @@ -758,11 +740,11 @@ describe "App" do content["items"]["aguacate"].wont_be_nil content["items"]["aguacate"]["Content-Type"].must_equal "text/plain; charset=utf-8" content["items"]["aguacate"]["Content-Length"].must_equal 2 - content["items"]["aguacate"]["ETag"].must_equal "bla" + content["items"]["aguacate"]["ETag"].must_equal "0815etag" content["items"]["camaron"].wont_be_nil content["items"]["camaron"]["Content-Type"].must_equal "text/plain; charset=utf-8" content["items"]["camaron"]["Content-Length"].must_equal 5 - content["items"]["camaron"]["ETag"].must_equal "bla" + content["items"]["camaron"]["ETag"].must_equal "0815etag" content["items"]["desayunos/"].wont_be_nil content["items"]["desayunos/"]["ETag"].must_equal "dd36e3cfe52b5f33421150b289a7d48d" end diff --git a/spec/swift/app_spec.rb b/spec/swift/app_spec.rb index ae207c7..1cb6aae 100644 --- a/spec/swift/app_spec.rb +++ b/spec/swift/app_spec.rb @@ -597,7 +597,7 @@ describe "App" do header "Authorization", "Bearer amarillo" put_stub = OpenStruct.new(headers: { - etag: "bla", + etag: "0815etag", last_modified: "Fri, 04 Mar 2016 12:20:18 GMT" }) @@ -639,18 +639,10 @@ describe "App" do end it "responds with 304 when IF_NONE_MATCH header contains the ETag" do + header "If-None-Match", "\"0815etag\"" - get_stub = OpenStruct.new(body: "si", headers: { - etag: "0815etag", - last_modified: "Fri, 04 Mar 2016 12:20:18 GMT", - content_type: "text/plain; charset=utf-8", - content_length: 2 - }) - - RestClient.stub :get, get_stub do - get "/phil/food/aguacate" - end + get "/phil/food/aguacate" last_response.status.must_equal 304 end @@ -658,16 +650,7 @@ describe "App" do it "responds with 304 when IF_NONE_MATCH header contains weak ETAG matching the current ETag" do header "If-None-Match", "W/\"0815etag\"" - get_stub = OpenStruct.new(body: "si", headers: { - etag: "0815etag", - last_modified: "Fri, 04 Mar 2016 12:20:18 GMT", - content_type: "text/plain; charset=utf-8", - content_length: 2 - }) - - RestClient.stub :get, get_stub do - get "/phil/food/aguacate" - end + get "/phil/food/aguacate" last_response.status.must_equal 304 end @@ -715,11 +698,11 @@ describe "App" do content["items"]["aguacate"].wont_be_nil content["items"]["aguacate"]["Content-Type"].must_equal "text/plain; charset=utf-8" content["items"]["aguacate"]["Content-Length"].must_equal 2 - content["items"]["aguacate"]["ETag"].must_equal "bla" + content["items"]["aguacate"]["ETag"].must_equal "0815etag" content["items"]["camaron"].wont_be_nil content["items"]["camaron"]["Content-Type"].must_equal "text/plain; charset=utf-8" content["items"]["camaron"]["Content-Length"].must_equal 5 - content["items"]["camaron"]["ETag"].must_equal "bla" + content["items"]["camaron"]["ETag"].must_equal "0815etag" content["items"]["desayunos/"].wont_be_nil content["items"]["desayunos/"]["ETag"].must_equal "dd36e3cfe52b5f33421150b289a7d48d" end