Skip to content

Commit

Permalink
FIX: Fix handling of multi-cookie responses
Browse files Browse the repository at this point in the history
FIX: Correctly parse relative redirects using Addressable

Also rename 'header' to 'redir_header' to deconflict with 'headers' parameter
  • Loading branch information
riking committed Jun 29, 2020
1 parent 5fd30f8 commit 01e062d
Showing 1 changed file with 8 additions and 10 deletions.
18 changes: 8 additions & 10 deletions lib/onebox/helpers.rb
Expand Up @@ -51,17 +51,14 @@ def self.fetch_response(location, limit = nil, domain = nil, headers = nil)

raise Net::HTTPError.new('HTTP redirect too deep', location) if limit == 0

uri = URI(location)
uri = URI("#{domain}#{location}") if !uri.host
uri = Addressable::URI.parse(location)
uri = Addressable::URI.join(domain, uri) if !uri.host

result = StringIO.new
Net::HTTP.start(uri.host, uri.port, use_ssl: uri.is_a?(URI::HTTPS)) do |http|
Net::HTTP.start(uri.host, uri.port, use_ssl: uri.normalized_scheme == 'https') do |http|
http.open_timeout = Onebox.options.connect_timeout
http.read_timeout = Onebox.options.timeout
if uri.is_a?(URI::HTTPS)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
end
http.verify_mode = OpenSSL::SSL::VERIFY_NONE # Work around path building bugs

headers ||= {}

Expand All @@ -76,10 +73,11 @@ def self.fetch_response(location, limit = nil, domain = nil, headers = nil)
http.request(request) do |response|

if cookie = response.get_fields('set-cookie')
header = { 'Cookie' => cookie.join }
# HACK: If this breaks again in the future, use HTTP::CookieJar from gem 'http-cookie'
redir_header = { 'Cookie' => cookie.join('; ') }
end

header = nil unless header.is_a? Hash
redir_header = nil unless redir_header.is_a? Hash

code = response.code.to_i
unless code === 200
Expand All @@ -88,7 +86,7 @@ def self.fetch_response(location, limit = nil, domain = nil, headers = nil)
response['location'],
limit - 1,
"#{uri.scheme}://#{uri.host}",
header
redir_header
)
end

Expand Down

0 comments on commit 01e062d

Please sign in to comment.