Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion down.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Gem::Specification.new do |spec|
spec.add_development_dependency "mocha", "~> 1.5"
spec.add_development_dependency "rake"
spec.add_development_dependency "httpx", "~> 1.0", "< 1.4.4"
spec.add_development_dependency "http", "~> 5.0"
spec.add_development_dependency "http", "~> 6.0"
spec.add_development_dependency "warning"
spec.add_development_dependency "csv"
end
16 changes: 12 additions & 4 deletions lib/down/http.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
# frozen-string-literal: true

gem "http", ">= 2.1.0", "< 6"
gem "http", ">= 2.1.0", "< 7"

require "http"
require "addressable/uri"

require "down/backend"

Expand All @@ -19,7 +20,10 @@ def initialize(**options, &block)
.follow(max_hops: 2)
.timeout(connect: 30, write: 30, read: 30)

@client = HTTP::Client.new(@client.default_options.merge(options)) if options.any?
if options.any?
client_class = defined?(HTTP::Session) ? HTTP::Session : HTTP::Client
@client = client_class.new(@client.default_options.merge(options))
end
@client = block.call(@client) if block
end

Expand Down Expand Up @@ -94,7 +98,7 @@ def send_request(method, url, **options, &block)
client = client.basic_auth(user: uri.user, pass: uri.password) if uri.user || uri.password
client = block.call(client) if block

client.request(method, url, options)
client.request(method, url, **options)
rescue => exception
request_error!(exception)
end
Expand Down Expand Up @@ -123,7 +127,7 @@ def response_error!(response)
# Re-raise HTTP.rb exceptions as Down::Error exceptions.
def request_error!(exception)
case exception
when HTTP::Request::UnsupportedSchemeError, Addressable::URI::InvalidURIError
when HTTP::Request::UnsupportedSchemeError, Addressable::URI::InvalidURIError, *invalid_url_errors
raise Down::InvalidUrl, exception.message
when HTTP::ConnectionError
raise Down::ConnectionError, exception.message
Expand All @@ -138,6 +142,10 @@ def request_error!(exception)
end
end

def invalid_url_errors
defined?(HTTP::URI::InvalidError) ? [HTTP::URI::InvalidError] : []
end

# Defines some additional attributes for the returned Tempfile.
module DownloadedFile
attr_accessor :url, :headers
Expand Down
4 changes: 2 additions & 2 deletions test/http_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -312,8 +312,8 @@
assert_equal "500 Internal Server Error", error.message
assert_instance_of HTTP::Response, error.response

error = assert_raises(Down::ResponseError) { Down::Http.open("#{$httpbin}/status/100") }
assert_equal "100 Continue", error.message
error = assert_raises(Down::ResponseError) { Down::Http.open("#{$httpbin}/status/305") }
assert_equal "305 Use Proxy", error.message
assert_instance_of HTTP::Response, error.response
end

Expand Down
Loading