Get the ETag from Redis on a GET request

This prevents doing a network request when we would return a 304 anyway
This commit is contained in:
Greg Karékinian 2018-04-16 16:40:04 +02:00
parent 86dc45f444
commit f083022e6d
3 changed files with 17 additions and 51 deletions

View File

@ -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"

View File

@ -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

View File

@ -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