Allow downloads with a HTTP range header

This commit is contained in:
Frederic Merizen 2018-04-24 14:00:09 +02:00 committed by gregoirenovel
parent 47e06812b1
commit b0037d50c8
2 changed files with 30 additions and 1 deletions

View file

@ -47,8 +47,11 @@ module Cellar
end
end
def download(key)
def download(key, range: nil)
request = Net::HTTP::Get.new("/#{key}")
if range.present?
add_range_header(request, range)
end
@signer.sign(request, key)
if block_given?
@http.request(request) do |response|
@ -106,6 +109,12 @@ module Cellar
private
def add_range_header(request, range)
bytes_end = range.exclude_end? ? range.end - 1 : range.end
request['range'] = "bytes=#{range.begin}-#{bytes_end}"
end
def parse_bucket_listing(bucket_listing_xml)
doc = Nokogiri::XML(bucket_listing_xml)
doc

View file

@ -4,6 +4,26 @@ describe 'CellarAdapter' do
before { Timecop.freeze(Time.gm(2016, 10, 2)) }
after { Timecop.return }
describe 'add_range_header' do
let(:request) { Net::HTTP::Get.new('/whatever') }
before { session.send(:add_range_header, request, range) }
subject { request['range'] }
context 'with end included' do
let(:range) { 100..500 }
it { is_expected.to eq('bytes=100-500') }
end
context 'with end excluded' do
let(:range) { 10...50 }
it { is_expected.to eq('bytes=10-49') }
end
end
describe 'parse_bucket_listing' do
let(:response) do
<<~EOS