diff --git a/lib/remote_storage/rest_provider.rb b/lib/remote_storage/rest_provider.rb index c04ae59..7447c53 100644 --- a/lib/remote_storage/rest_provider.rb +++ b/lib/remote_storage/rest_provider.rb @@ -69,6 +69,11 @@ module RemoteStorage 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) return res.body @@ -405,7 +410,9 @@ module RemoteStorage def do_get_request(url, &block) 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 diff --git a/lib/remote_storage/s3.rb b/lib/remote_storage/s3.rb index 6233cff..c852006 100644 --- a/lib/remote_storage/s3.rb +++ b/lib/remote_storage/s3.rb @@ -32,8 +32,10 @@ module RemoteStorage def do_get_request(url, &block) deal_with_unauthorized_requests do + headers = { } + headers["Range"] = server.env["HTTP_RANGE"] if server.env["HTTP_RANGE"] authorization_headers = authorization_headers_for("GET", url) - RestClient.get(url, authorization_headers, &block) + RestClient.get(url, authorization_headers.merge(headers), &block) end end diff --git a/liquor-cabinet.rb b/liquor-cabinet.rb index 6f6324f..8746a42 100644 --- a/liquor-cabinet.rb +++ b/liquor-cabinet.rb @@ -68,8 +68,9 @@ class LiquorCabinet < Sinatra::Base before path do headers 'Access-Control-Allow-Origin' => '*', 'Access-Control-Allow-Methods' => 'GET, PUT, DELETE', - 'Access-Control-Allow-Headers' => 'Authorization, Content-Type, Origin, If-Match, If-None-Match', - 'Access-Control-Expose-Headers' => 'ETag, Content-Length' + 'Access-Control-Allow-Headers' => 'Authorization, Content-Type, Origin, If-Match, If-None-Match, Range', + '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['Cache-Control'] = 'no-cache' diff --git a/spec/s3/app_spec.rb b/spec/s3/app_spec.rb index 65950d2..64f2e3d 100644 --- a/spec/s3/app_spec.rb +++ b/spec/s3/app_spec.rb @@ -16,12 +16,20 @@ describe "S3 provider" do stub_request(:put, "#{container_url_for("phil")}/food/aguacate"). with(body: "aye"). 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"). to_return(status: 200, headers: { last_modified: "Fri, 04 Mar 2016 12:20:18 GMT" }) 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" }) stub_request(:delete, "#{container_url_for("phil")}/food/aguacate"). 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 stub_request(:put, "#{container_url_for("phil")}/food/banano"). diff --git a/spec/shared_examples.rb b/spec/shared_examples.rb index 07eba69..9255f9c 100644 --- a/spec/shared_examples.rb +++ b/spec/shared_examples.rb @@ -397,6 +397,47 @@ shared_examples_for 'a REST adapter' do purge_redis 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 describe "without token" do diff --git a/spec/swift/app_spec.rb b/spec/swift/app_spec.rb index eff8bd9..9871b4b 100644 --- a/spec/swift/app_spec.rb +++ b/spec/swift/app_spec.rb @@ -16,12 +16,20 @@ describe "Swift provider" do stub_request(:put, "#{container_url_for("phil")}/food/aguacate"). with(body: "aye"). 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"). to_return(status: 200, headers: { last_modified: "Fri, 04 Mar 2016 12:20:18 GMT" }) 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" }) stub_request(:delete, "#{container_url_for("phil")}/food/aguacate"). 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 stub_request(:put, "#{container_url_for("phil")}/food/banano").