lib/down/net_http.rb in down-4.1.0 vs lib/down/net_http.rb in down-4.1.1
- old
+ new
@@ -1,9 +1,9 @@
# frozen-string-literal: true
require "open-uri"
-require "net/http"
+require "net/https"
require "down/backend"
require "tempfile"
require "fileutils"
@@ -58,19 +58,22 @@
tries = max_redirects + 1
begin
uri = URI(uri)
+ raise Down::InvalidUrl, "URL scheme needs to be http or https" unless uri.is_a?(URI::HTTP)
+ rescue URI::InvalidURIError => exception
+ raise Down::InvalidUrl, exception.message
+ end
- fail Down::InvalidUrl, "URL scheme needs to be http or https" unless uri.is_a?(URI::HTTP)
+ if uri.user || uri.password
+ open_uri_options[:http_basic_authentication] ||= [uri.user, uri.password]
+ uri.user = nil
+ uri.password = nil
+ end
- if uri.user || uri.password
- open_uri_options[:http_basic_authentication] ||= [uri.user, uri.password]
- uri.user = nil
- uri.password = nil
- end
-
+ begin
downloaded_file = uri.open(open_uri_options)
rescue OpenURI::HTTPRedirect => exception
if (tries -= 1) > 0
uri = exception.uri
@@ -78,11 +81,11 @@
open_uri_options["Cookie"] = exception.io.meta["set-cookie"]
end
retry
else
- fail Down::TooManyRedirects, "too many redirects"
+ raise Down::TooManyRedirects, "too many redirects"
end
rescue OpenURI::HTTPError => exception
code, message = exception.io.status
response_class = Net::HTTPResponse::CODE_TO_OBJ.fetch(code)
response = response_class.new(nil, code, message)
@@ -108,13 +111,17 @@
downloaded_file
end
def open(uri, options = {})
options = @options.merge(options)
- uri = URI(uri)
- fail Down::InvalidUrl, "URL scheme needs to be http or https" unless uri.is_a?(URI::HTTP)
+ begin
+ uri = URI(uri)
+ raise Down::InvalidUrl, "URL scheme needs to be http or https" unless uri.is_a?(URI::HTTP)
+ rescue URI::InvalidURIError => exception
+ raise Down::InvalidUrl, exception.message
+ end
http_class = Net::HTTP
if options[:proxy]
proxy = URI(options[:proxy])
@@ -123,11 +130,10 @@
http = http_class.new(uri.host, uri.port)
# taken from open-uri implementation
if uri.is_a?(URI::HTTPS)
- require "net/https"
http.use_ssl = true
http.verify_mode = options[:ssl_verify_mode] || OpenSSL::SSL::VERIFY_PEER
store = OpenSSL::X509::Store.new
if options[:ssl_ca_cert]
Array(options[:ssl_ca_cert]).each do |cert|
@@ -155,11 +161,15 @@
response.instance_variable_set("@read", true)
end
end
end
- response = request.resume
+ begin
+ response = request.resume
+ rescue => exception
+ request_error!(exception)
+ end
response_error!(response) unless (200..299).cover?(response.code.to_i)
body_chunks = Enumerator.new do |yielder|
begin
@@ -182,12 +192,10 @@
headers.merge!(name => value)
},
response: response,
},
)
- rescue => exception
- request_error!(exception)
end
private
def copy_to_tempfile(basename, io)
@@ -217,27 +225,16 @@
end
end
def request_error!(exception)
case exception
- when URI::InvalidURIError
- raise Down::InvalidUrl, "URL was invalid"
- when Errno::ECONNREFUSED
- raise Down::ConnectionError, "connection was refused"
- when EOFError,
- IOError,
- Errno::ECONNABORTED,
- Errno::ECONNRESET,
- Errno::EPIPE,
- Errno::EINVAL,
- Errno::EHOSTUNREACH
+ when Errno::ETIMEDOUT, Net::OpenTimeout
+ raise Down::TimeoutError, "timed out waiting for connection to open"
+ when Net::ReadTimeout
+ raise Down::TimeoutError, "timed out while reading data"
+ when EOFError, IOError, SocketError, SystemCallError
raise Down::ConnectionError, exception.message
- when SocketError
- raise Down::ConnectionError, "domain name could not be resolved"
- when Errno::ETIMEDOUT,
- Timeout::Error
- raise Down::TimeoutError, "request timed out"
- when defined?(OpenSSL) && OpenSSL::SSL::SSLError
+ when OpenSSL::SSL::SSLError
raise Down::SSLError, exception.message
else
raise exception
end
end