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:
		
							parent
							
								
									86dc45f444
								
							
						
					
					
						commit
						f083022e6d
					
				@ -46,17 +46,18 @@ module RemoteStorage
 | 
				
			|||||||
    end
 | 
					    end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def get_data(user, directory, key)
 | 
					    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)
 | 
					      url = url_for_key(user, directory, key)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      res = do_get_request(url)
 | 
					      res = do_get_request(url)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      set_response_headers(res)
 | 
					      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
 | 
					      return res.body
 | 
				
			||||||
    rescue RestClient::ResourceNotFound
 | 
					    rescue RestClient::ResourceNotFound
 | 
				
			||||||
      server.halt 404, "Not Found"
 | 
					      server.halt 404, "Not Found"
 | 
				
			||||||
 | 
				
			|||||||
@ -639,7 +639,7 @@ describe "App" do
 | 
				
			|||||||
        header "Authorization", "Bearer amarillo"
 | 
					        header "Authorization", "Bearer amarillo"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        put_stub = OpenStruct.new(headers: {
 | 
					        put_stub = OpenStruct.new(headers: {
 | 
				
			||||||
          etag: '"bla"'
 | 
					          etag: '"0815etag"'
 | 
				
			||||||
        })
 | 
					        })
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        RestClient.stub :put, put_stub do
 | 
					        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
 | 
					        it "responds with 304 when IF_NONE_MATCH header contains the ETag" do
 | 
				
			||||||
          header "If-None-Match", "\"0815etag\""
 | 
					          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"
 | 
					          get "/phil/food/aguacate"
 | 
				
			||||||
          end
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
          last_response.status.must_equal 304
 | 
					          last_response.status.must_equal 304
 | 
				
			||||||
        end
 | 
					        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
 | 
					        it "responds with 304 when IF_NONE_MATCH header contains weak ETAG matching the current ETag" do
 | 
				
			||||||
          header "If-None-Match", "W/\"0815etag\""
 | 
					          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"
 | 
					          get "/phil/food/aguacate"
 | 
				
			||||||
          end
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
          last_response.status.must_equal 304
 | 
					          last_response.status.must_equal 304
 | 
				
			||||||
        end
 | 
					        end
 | 
				
			||||||
@ -758,11 +740,11 @@ describe "App" do
 | 
				
			|||||||
          content["items"]["aguacate"].wont_be_nil
 | 
					          content["items"]["aguacate"].wont_be_nil
 | 
				
			||||||
          content["items"]["aguacate"]["Content-Type"].must_equal "text/plain; charset=utf-8"
 | 
					          content["items"]["aguacate"]["Content-Type"].must_equal "text/plain; charset=utf-8"
 | 
				
			||||||
          content["items"]["aguacate"]["Content-Length"].must_equal 2
 | 
					          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"].wont_be_nil
 | 
				
			||||||
          content["items"]["camaron"]["Content-Type"].must_equal "text/plain; charset=utf-8"
 | 
					          content["items"]["camaron"]["Content-Type"].must_equal "text/plain; charset=utf-8"
 | 
				
			||||||
          content["items"]["camaron"]["Content-Length"].must_equal 5
 | 
					          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/"].wont_be_nil
 | 
				
			||||||
          content["items"]["desayunos/"]["ETag"].must_equal "dd36e3cfe52b5f33421150b289a7d48d"
 | 
					          content["items"]["desayunos/"]["ETag"].must_equal "dd36e3cfe52b5f33421150b289a7d48d"
 | 
				
			||||||
        end
 | 
					        end
 | 
				
			||||||
 | 
				
			|||||||
@ -597,7 +597,7 @@ describe "App" do
 | 
				
			|||||||
        header "Authorization", "Bearer amarillo"
 | 
					        header "Authorization", "Bearer amarillo"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        put_stub = OpenStruct.new(headers: {
 | 
					        put_stub = OpenStruct.new(headers: {
 | 
				
			||||||
          etag: "bla",
 | 
					          etag: "0815etag",
 | 
				
			||||||
          last_modified: "Fri, 04 Mar 2016 12:20:18 GMT"
 | 
					          last_modified: "Fri, 04 Mar 2016 12:20:18 GMT"
 | 
				
			||||||
        })
 | 
					        })
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -639,18 +639,10 @@ describe "App" do
 | 
				
			|||||||
        end
 | 
					        end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        it "responds with 304 when IF_NONE_MATCH header contains the ETag" do
 | 
					        it "responds with 304 when IF_NONE_MATCH header contains the ETag" do
 | 
				
			||||||
 | 
					
 | 
				
			||||||
          header "If-None-Match", "\"0815etag\""
 | 
					          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"
 | 
					          get "/phil/food/aguacate"
 | 
				
			||||||
          end
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
          last_response.status.must_equal 304
 | 
					          last_response.status.must_equal 304
 | 
				
			||||||
        end
 | 
					        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
 | 
					        it "responds with 304 when IF_NONE_MATCH header contains weak ETAG matching the current ETag" do
 | 
				
			||||||
          header "If-None-Match", "W/\"0815etag\""
 | 
					          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"
 | 
					          get "/phil/food/aguacate"
 | 
				
			||||||
          end
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
          last_response.status.must_equal 304
 | 
					          last_response.status.must_equal 304
 | 
				
			||||||
        end
 | 
					        end
 | 
				
			||||||
@ -715,11 +698,11 @@ describe "App" do
 | 
				
			|||||||
          content["items"]["aguacate"].wont_be_nil
 | 
					          content["items"]["aguacate"].wont_be_nil
 | 
				
			||||||
          content["items"]["aguacate"]["Content-Type"].must_equal "text/plain; charset=utf-8"
 | 
					          content["items"]["aguacate"]["Content-Type"].must_equal "text/plain; charset=utf-8"
 | 
				
			||||||
          content["items"]["aguacate"]["Content-Length"].must_equal 2
 | 
					          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"].wont_be_nil
 | 
				
			||||||
          content["items"]["camaron"]["Content-Type"].must_equal "text/plain; charset=utf-8"
 | 
					          content["items"]["camaron"]["Content-Type"].must_equal "text/plain; charset=utf-8"
 | 
				
			||||||
          content["items"]["camaron"]["Content-Length"].must_equal 5
 | 
					          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/"].wont_be_nil
 | 
				
			||||||
          content["items"]["desayunos/"]["ETag"].must_equal "dd36e3cfe52b5f33421150b289a7d48d"
 | 
					          content["items"]["desayunos/"]["ETag"].must_equal "dd36e3cfe52b5f33421150b289a7d48d"
 | 
				
			||||||
        end
 | 
					        end
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user