lib/unwind.rb in unwind-0.10.0 vs lib/unwind.rb in unwind-0.11.0
- old
+ new
@@ -1,7 +1,8 @@
require "unwind/version"
require 'faraday'
+require 'addressable/uri'
module Unwind
class TooManyRedirects < StandardError; end
class MissingRedirectLocation < StandardError; end
@@ -25,11 +26,11 @@
ok_to_continue?
current_url ||= self.original_url
#adding this header because we really only care about resolving the url
headers = (options || {}).merge({"accept-encoding" => "none"})
- response = Faraday.get(current_url, headers)
+ response = Faraday.get(current_url, {}, headers)
if is_response_redirect?(response)
handle_redirect(redirect_url(response), current_url, response, headers)
elsif meta_uri = meta_refresh?(response)
handle_redirect(meta_uri, current_url, response, headers)
@@ -60,13 +61,20 @@
resolve(uri_to_redirect.normalize, apply_cookie(response, headers))
end
def handle_final_response(current_url, response)
current_url = current_url.dup.to_s
- if response.status == 200 && canonical = canonical_link?(response)
+ if response.status == 200 && canonical = canonical_link?(response)
@redirects << current_url
- @final_url = canonical
+ if Addressable::URI.parse(canonical).relative?
+ current_uri = Addressable::URI.parse(current_url)
+ # Is there a cleaner way of doing this?
+ @final_url = "#{current_uri.scheme}://#{current_uri.host}#{canonical}"
+ else
+ @final_url = canonical
+ end
+
else
@final_url = current_url
end
@response = response
end
@@ -80,11 +88,11 @@
body_match = response.body.match(/<a href=\"([^>]+)\">/i)
raise MissingRedirectLocation unless body_match
Addressable::URI.parse(body_match[0])
else
redirect_uri = Addressable::URI.parse(response['location'])
- redirect_uri.relative? ? response.env[:url].join(response['location']) : redirect_uri
+ redirect_uri.relative? ? Addressable::URI.parse(response.env[:url]).join(response['location']) : redirect_uri
end
end
def meta_refresh?(response)
if response.status == 200
@@ -92,10 +100,10 @@
Addressable::URI.parse(body_match[1]) if body_match
end
end
def canonical_link?(response)
- body_match = response.body.match(/<link rel=[\'\"]canonical[\'\"] href=[\'\"](.*)[\'\"]/i)
+ body_match = response.body.match(/<link rel=[\'\"]canonical[\'\"] href=[\'\"](.*?)[\'\"]/i)
body_match ? Addressable::URI.parse(body_match[1]).to_s : false
end
def apply_cookie(response, headers)
if response.status == 302 && response['set-cookie']