7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
# File 'lib/cifrado/streaming_downloader.rb', line 7
def self.get url, output, options = {}
if output.nil? and !options[:stream]
raise ArgumentError.new('Invalid output file')
end
uri = URI.parse url
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true if uri.scheme == "https"
copts = options[:connection_options]
if copts[:ssl_verify_peer] == false
Log.debug "SSL verification DISABLED"
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
end
Log.debug "Request URL #{uri.request_uri}"
request = Net::HTTP::Get.new(uri.request_uri)
= options[:headers]
request.
rate_limit = nil
rate_limit = Cifrado::RateLimit.new(options[:bwlimit]) if options[:bwlimit]
unless options[:stream]
file = File.open(output, "wb")
Log.debug "Downloading file to #{output}"
end
callback = options[:progress_callback]
http.request(request) do |response|
clength = response['Content-Length'].to_i
response.read_body do |segment|
rate_limit.limit(segment.size) if rate_limit
callback.call(clength, segment.length, segment) if callback
file.write(segment) if file
end
end
ensure
file.close if file
end
|