Merge pull request #134 from 5apps/feature/82-content_range_poc
Content-Range support
This commit is contained in:
		
						commit
						ab673f1d43
					
				@ -69,6 +69,11 @@ module RemoteStorage
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
      res = do_get_request(url)
 | 
					      res = do_get_request(url)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      if res.headers[:content_range]
 | 
				
			||||||
 | 
					        # Partial content
 | 
				
			||||||
 | 
					        server.headers["Content-Range"] = res.headers[:content_range]
 | 
				
			||||||
 | 
					        server.status 206
 | 
				
			||||||
 | 
					      end
 | 
				
			||||||
      set_response_headers(metadata)
 | 
					      set_response_headers(metadata)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      return res.body
 | 
					      return res.body
 | 
				
			||||||
@ -405,7 +410,9 @@ module RemoteStorage
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    def do_get_request(url, &block)
 | 
					    def do_get_request(url, &block)
 | 
				
			||||||
      deal_with_unauthorized_requests do
 | 
					      deal_with_unauthorized_requests do
 | 
				
			||||||
        RestClient.get(url, default_headers, &block)
 | 
					        headers = { }
 | 
				
			||||||
 | 
					        headers["Range"] = server.env["HTTP_RANGE"] if server.env["HTTP_RANGE"]
 | 
				
			||||||
 | 
					        RestClient.get(url, default_headers.merge(headers), &block)
 | 
				
			||||||
      end
 | 
					      end
 | 
				
			||||||
    end
 | 
					    end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
@ -32,8 +32,10 @@ module RemoteStorage
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    def do_get_request(url, &block)
 | 
					    def do_get_request(url, &block)
 | 
				
			||||||
      deal_with_unauthorized_requests do
 | 
					      deal_with_unauthorized_requests do
 | 
				
			||||||
 | 
					        headers = { }
 | 
				
			||||||
 | 
					        headers["Range"] = server.env["HTTP_RANGE"] if server.env["HTTP_RANGE"]
 | 
				
			||||||
        authorization_headers = authorization_headers_for("GET", url)
 | 
					        authorization_headers = authorization_headers_for("GET", url)
 | 
				
			||||||
        RestClient.get(url, authorization_headers, &block)
 | 
					        RestClient.get(url, authorization_headers.merge(headers), &block)
 | 
				
			||||||
      end
 | 
					      end
 | 
				
			||||||
    end
 | 
					    end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
@ -68,8 +68,9 @@ class LiquorCabinet < Sinatra::Base
 | 
				
			|||||||
    before path do
 | 
					    before path do
 | 
				
			||||||
      headers 'Access-Control-Allow-Origin' => '*',
 | 
					      headers 'Access-Control-Allow-Origin' => '*',
 | 
				
			||||||
              'Access-Control-Allow-Methods' => 'GET, PUT, DELETE',
 | 
					              'Access-Control-Allow-Methods' => 'GET, PUT, DELETE',
 | 
				
			||||||
              'Access-Control-Allow-Headers' => 'Authorization, Content-Type, Origin, If-Match, If-None-Match',
 | 
					              'Access-Control-Allow-Headers' => 'Authorization, Content-Type, Origin, If-Match, If-None-Match, Range',
 | 
				
			||||||
              'Access-Control-Expose-Headers' => 'ETag, Content-Length'
 | 
					              'Access-Control-Expose-Headers' => 'ETag, Content-Length, Content-Range',
 | 
				
			||||||
 | 
					              'Accept-Ranges' => 'bytes'
 | 
				
			||||||
      headers['Access-Control-Allow-Origin'] = env["HTTP_ORIGIN"] if env["HTTP_ORIGIN"]
 | 
					      headers['Access-Control-Allow-Origin'] = env["HTTP_ORIGIN"] if env["HTTP_ORIGIN"]
 | 
				
			||||||
      headers['Cache-Control'] = 'no-cache'
 | 
					      headers['Cache-Control'] = 'no-cache'
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
@ -16,12 +16,20 @@ describe "S3 provider" do
 | 
				
			|||||||
    stub_request(:put, "#{container_url_for("phil")}/food/aguacate").
 | 
					    stub_request(:put, "#{container_url_for("phil")}/food/aguacate").
 | 
				
			||||||
      with(body: "aye").
 | 
					      with(body: "aye").
 | 
				
			||||||
      to_return(status: 200, headers: { etag: '"0915etag"', date: "Fri, 04 Mar 2016 12:20:18 GMT" })
 | 
					      to_return(status: 200, headers: { etag: '"0915etag"', date: "Fri, 04 Mar 2016 12:20:18 GMT" })
 | 
				
			||||||
 | 
					    stub_request(:put, "#{container_url_for("phil")}/public/shares/example.jpg").
 | 
				
			||||||
 | 
					      to_return(status: 200, headers: { etag: '"0817etag"', content_type: "image/jpeg", date: "Fri, 04 Mar 2016 12:20:18 GMT" })
 | 
				
			||||||
 | 
					    stub_request(:put, "#{container_url_for("phil")}/public/shares/example_partial.jpg").
 | 
				
			||||||
 | 
					      to_return(status: 200, headers: { etag: '"0817etag"', content_type: "image/jpeg", date: "Fri, 04 Mar 2016 12:20:18 GMT" })
 | 
				
			||||||
    stub_request(:head, "#{container_url_for("phil")}/food/aguacate").
 | 
					    stub_request(:head, "#{container_url_for("phil")}/food/aguacate").
 | 
				
			||||||
      to_return(status: 200, headers: { last_modified: "Fri, 04 Mar 2016 12:20:18 GMT" })
 | 
					      to_return(status: 200, headers: { last_modified: "Fri, 04 Mar 2016 12:20:18 GMT" })
 | 
				
			||||||
    stub_request(:get, "#{container_url_for("phil")}/food/aguacate").
 | 
					    stub_request(:get, "#{container_url_for("phil")}/food/aguacate").
 | 
				
			||||||
      to_return(status: 200, body: "rootbody", headers: { etag: '"0817etag"', content_type: "text/plain; charset=utf-8" })
 | 
					      to_return(status: 200, body: "rootbody", headers: { etag: '"0817etag"', content_type: "text/plain; charset=utf-8" })
 | 
				
			||||||
    stub_request(:delete, "#{container_url_for("phil")}/food/aguacate").
 | 
					    stub_request(:delete, "#{container_url_for("phil")}/food/aguacate").
 | 
				
			||||||
      to_return(status: 200, headers: { etag: '"0815etag"' })
 | 
					      to_return(status: 200, headers: { etag: '"0815etag"' })
 | 
				
			||||||
 | 
					    stub_request(:get, "#{container_url_for("phil")}/public/shares/example.jpg").
 | 
				
			||||||
 | 
					      to_return(status: 200, body: "", headers: { etag: '"0817etag"', content_type: "image/jpeg" })
 | 
				
			||||||
 | 
					    stub_request(:get, "#{container_url_for("phil")}/public/shares/example_partial.jpg").
 | 
				
			||||||
 | 
					      to_return(status: 206, body: "", headers: { etag: '"0817etag"', content_type: "image/jpeg", content_range: "bytes 0-16/128" })
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    # Write new content to check the metadata in Redis
 | 
					    # Write new content to check the metadata in Redis
 | 
				
			||||||
    stub_request(:put, "#{container_url_for("phil")}/food/banano").
 | 
					    stub_request(:put, "#{container_url_for("phil")}/food/banano").
 | 
				
			||||||
 | 
				
			|||||||
@ -397,6 +397,47 @@ shared_examples_for 'a REST adapter' do
 | 
				
			|||||||
      purge_redis
 | 
					      purge_redis
 | 
				
			||||||
    end
 | 
					    end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    context "requests to public resources" do
 | 
				
			||||||
 | 
					      before do
 | 
				
			||||||
 | 
					        redis.sadd "authorizations:phil:amarillo", [":rw"]
 | 
				
			||||||
 | 
					        header "Authorization", "Bearer amarillo"
 | 
				
			||||||
 | 
					      end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      describe "normal request" do
 | 
				
			||||||
 | 
					        before do
 | 
				
			||||||
 | 
					          header "Content-Type", "image/jpeg"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					          put "/phil/public/shares/example.jpg", ""
 | 
				
			||||||
 | 
					        end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        it "returns the required response headers" do
 | 
				
			||||||
 | 
					          get "/phil/public/shares/example.jpg"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					          last_response.status.must_equal 200
 | 
				
			||||||
 | 
					          last_response.headers["Content-Type"].must_equal "image/jpeg"
 | 
				
			||||||
 | 
					        end
 | 
				
			||||||
 | 
					      end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      describe "partial request" do
 | 
				
			||||||
 | 
					        before do
 | 
				
			||||||
 | 
					          header "Content-Type", "image/jpeg"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					          put "/phil/public/shares/example_partial.jpg", <<-EOF
 | 
				
			||||||
 | 
					JFIFddDuckyA␍⎺␉␊␍
 | 
				
			||||||
 | 
					#%'%#//33//@@@@@@@@@@@@@@@&&0##0+.'''.+550055@@?@@@@@@@@@@@>"!1AQaq"2B
 | 
				
			||||||
 | 
					          EOF
 | 
				
			||||||
 | 
					        end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        it "returns the required response headers" do
 | 
				
			||||||
 | 
					          header 'Range', 'bytes=0-16'
 | 
				
			||||||
 | 
					          get "/phil/public/shares/example_partial.jpg"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					          last_response.status.must_equal 206
 | 
				
			||||||
 | 
					          last_response.headers["Content-Type"].must_equal "image/jpeg"
 | 
				
			||||||
 | 
					        end
 | 
				
			||||||
 | 
					      end
 | 
				
			||||||
 | 
					    end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    context "not authorized" do
 | 
					    context "not authorized" do
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      describe "without token" do
 | 
					      describe "without token" do
 | 
				
			||||||
 | 
				
			|||||||
@ -16,12 +16,20 @@ describe "Swift provider" do
 | 
				
			|||||||
    stub_request(:put, "#{container_url_for("phil")}/food/aguacate").
 | 
					    stub_request(:put, "#{container_url_for("phil")}/food/aguacate").
 | 
				
			||||||
      with(body: "aye").
 | 
					      with(body: "aye").
 | 
				
			||||||
      to_return(status: 200, headers: { etag: "0915etag", last_modified: "Fri, 04 Mar 2016 12:20:18 GMT" })
 | 
					      to_return(status: 200, headers: { etag: "0915etag", last_modified: "Fri, 04 Mar 2016 12:20:18 GMT" })
 | 
				
			||||||
 | 
					    stub_request(:put, "#{container_url_for("phil")}/public/shares/example.jpg").
 | 
				
			||||||
 | 
					      to_return(status: 200, headers: { etag: '"0817etag"', content_type: "image/jpeg", last_modified: "Fri, 04 Mar 2016 12:20:18 GMT" })
 | 
				
			||||||
 | 
					    stub_request(:put, "#{container_url_for("phil")}/public/shares/example_partial.jpg").
 | 
				
			||||||
 | 
					      to_return(status: 200, headers: { etag: '"0817etag"', content_type: "image/jpeg", last_modified: "Fri, 04 Mar 2016 12:20:18 GMT" })
 | 
				
			||||||
    stub_request(:head, "#{container_url_for("phil")}/food/aguacate").
 | 
					    stub_request(:head, "#{container_url_for("phil")}/food/aguacate").
 | 
				
			||||||
      to_return(status: 200, headers: { last_modified: "Fri, 04 Mar 2016 12:20:18 GMT" })
 | 
					      to_return(status: 200, headers: { last_modified: "Fri, 04 Mar 2016 12:20:18 GMT" })
 | 
				
			||||||
    stub_request(:get, "#{container_url_for("phil")}/food/aguacate").
 | 
					    stub_request(:get, "#{container_url_for("phil")}/food/aguacate").
 | 
				
			||||||
      to_return(status: 200, body: "rootbody", headers: { etag: "0817etag", content_type: "text/plain; charset=utf-8" })
 | 
					      to_return(status: 200, body: "rootbody", headers: { etag: "0817etag", content_type: "text/plain; charset=utf-8" })
 | 
				
			||||||
    stub_request(:delete, "#{container_url_for("phil")}/food/aguacate").
 | 
					    stub_request(:delete, "#{container_url_for("phil")}/food/aguacate").
 | 
				
			||||||
      to_return(status: 200, headers: { etag: "0815etag" })
 | 
					      to_return(status: 200, headers: { etag: "0815etag" })
 | 
				
			||||||
 | 
					    stub_request(:get, "#{container_url_for("phil")}/public/shares/example.jpg").
 | 
				
			||||||
 | 
					      to_return(status: 200, body: "", headers: { etag: '"0817etag"', content_type: "image/jpeg" })
 | 
				
			||||||
 | 
					    stub_request(:get, "#{container_url_for("phil")}/public/shares/example_partial.jpg").
 | 
				
			||||||
 | 
					      to_return(status: 206, body: "", headers: { etag: '"0817etag"', content_type: "image/jpeg", content_range: "bytes 0-16/128" })
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    # Write new content to check the metadata in Redis
 | 
					    # Write new content to check the metadata in Redis
 | 
				
			||||||
    stub_request(:put, "#{container_url_for("phil")}/food/banano").
 | 
					    stub_request(:put, "#{container_url_for("phil")}/food/banano").
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user