unwind 0.11.0 → 0.11.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,3 @@
1
1
  module Unwind
2
- VERSION = "0.11.0"
2
+ VERSION = "0.11.1"
3
3
  end
data/lib/unwind.rb CHANGED
@@ -32,7 +32,7 @@ module Unwind
32
32
 
33
33
  if is_response_redirect?(response)
34
34
  handle_redirect(redirect_url(response), current_url, response, headers)
35
- elsif meta_uri = meta_refresh?(response)
35
+ elsif meta_uri = meta_refresh?(current_url, response)
36
36
  handle_redirect(meta_uri, current_url, response, headers)
37
37
  else
38
38
  handle_final_response(current_url, response)
@@ -66,9 +66,7 @@ module Unwind
66
66
  if response.status == 200 && canonical = canonical_link?(response)
67
67
  @redirects << current_url
68
68
  if Addressable::URI.parse(canonical).relative?
69
- current_uri = Addressable::URI.parse(current_url)
70
- # Is there a cleaner way of doing this?
71
- @final_url = "#{current_uri.scheme}://#{current_uri.host}#{canonical}"
69
+ @final_url = make_url_absolute(current_url, Addressable::URI.parse(canonical)).to_s
72
70
  else
73
71
  @final_url = canonical
74
72
  end
@@ -94,10 +92,13 @@ module Unwind
94
92
  end
95
93
  end
96
94
 
97
- def meta_refresh?(response)
95
+ def meta_refresh?(current_url, response)
98
96
  if response.status == 200
99
- body_match = response.body.match(/<meta http-equiv=\"refresh\" content=\"0; URL=(.*)\">/i)
100
- Addressable::URI.parse(body_match[1]) if body_match
97
+ body_match = response.body.match(/<meta http-equiv=\"refresh\" content=\"0; URL=(.*?)\"\s*\/*>/i)
98
+ if body_match
99
+ uri = Addressable::URI.parse(body_match[1])
100
+ make_url_absolute(current_url, uri)
101
+ end
101
102
  end
102
103
  end
103
104
 
@@ -115,6 +116,23 @@ module Unwind
115
116
  end
116
117
  end
117
118
 
119
+ def make_url_absolute(current_url, relative_url)
120
+ current_uri = Addressable::URI.parse(current_url)
121
+ if (relative_url.relative?)
122
+ url = Addressable::URI.new(
123
+ :scheme => current_uri.scheme,
124
+ :user => current_uri.user,
125
+ :password => current_uri.password,
126
+ :host => current_uri.host,
127
+ :port => current_uri.port,
128
+ :path => relative_url.path,
129
+ :query => relative_url.query,
130
+ :fragment => relative_url.fragment)
131
+ else
132
+ relative_url
133
+ end
134
+ end
135
+
118
136
  end
119
137
 
120
138
  #borrowed (stolen) from HTTParty with minor updates
@@ -109,5 +109,21 @@ describe Unwind::RedirectFollower do
109
109
  assert_equal 'http://www.google.com/', follower.final_url
110
110
  end
111
111
  end
112
+
113
+ it 'should handle a relative meta-refresh' do
114
+ VCR.use_cassette('relative meta refresh') do
115
+ follower = Unwind::RedirectFollower.resolve('http://fb.me/2JYu23acx')
116
+ assert follower.redirected?
117
+ assert_equal 'https://www.facebook.com/londonswf/posts/696389650411604?_fb_noscript=1', follower.final_url
118
+ end
119
+ end
120
+
121
+ it 'should handle a relative canonical url' do
122
+ VCR.use_cassette('relative canonical url') do
123
+ follower = Unwind::RedirectFollower.resolve('http://youtu.be/hPJ0oLahGDg')
124
+ assert follower.redirected?
125
+ assert_equal 'http://www.youtube.com/watch?v=hPJ0oLahGDg', follower.final_url
126
+ end
127
+ end
112
128
 
113
129
  end