Skip to content

Commit c9b43c6

Browse files
committed
Improved download code
1 parent 59d6dd3 commit c9b43c6

1 file changed

Lines changed: 2 additions & 36 deletions

File tree

ext/or-tools/vendor.rb

Lines changed: 2 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
require "digest"
22
require "fileutils"
3-
require "net/http"
3+
require "open-uri"
44
require "tmpdir"
55

66
version = "9.15.6755"
@@ -72,45 +72,11 @@
7272

7373
$stdout.sync = true
7474

75-
def download_file(url, download_path, redirects = 0)
76-
raise "Too many redirects" if redirects > 10
77-
78-
uri = URI(url)
79-
location = nil
80-
81-
Net::HTTP.start(uri.host, uri.port, use_ssl: true) do |http|
82-
request = Net::HTTP::Get.new(uri)
83-
http.request(request) do |response|
84-
case response
85-
when Net::HTTPRedirection
86-
location = response["location"]
87-
when Net::HTTPSuccess
88-
i = 0
89-
File.open(download_path, "wb") do |f|
90-
response.read_body do |chunk|
91-
f.write(chunk)
92-
93-
# print progress
94-
putc "." if i % 50 == 0
95-
i += 1
96-
end
97-
end
98-
puts # newline
99-
else
100-
raise "Bad response"
101-
end
102-
end
103-
end
104-
105-
# outside of Net::HTTP block to close previous connection
106-
download_file(location, download_path, redirects + 1) if location
107-
end
108-
10975
# download
11076
download_path = "#{Dir.tmpdir}/#{filename}"
11177
unless File.exist?(download_path)
11278
puts "Downloading #{url}..."
113-
download_file(url, download_path)
79+
IO.copy_stream(URI.parse(url).open(max_redirects: 10), download_path)
11480
end
11581

11682
# check integrity - do this regardless of if just downloaded

0 commit comments

Comments
 (0)