Skip to content

Commit f5ca9f2

Browse files
committed
Raise an error if there are too many redirects
1 parent 61efefb commit f5ca9f2

File tree

4 files changed

+8
-16
lines changed

4 files changed

+8
-16
lines changed

lib/excon/connection.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,7 @@ def response(datum={})
440440
datum[:stack].response_call(datum)
441441
rescue => error
442442
case error
443-
when Excon::Errors::HTTPStatusError, Excon::Errors::Timeout
443+
when Excon::Errors::HTTPStatusError, Excon::Errors::Timeout, Excon::Errors::TooManyRedirects
444444
raise(error)
445445
else
446446
raise_socket_error(error)

lib/excon/error.rb

+1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ class InvalidHeaderValue < Error; end
5151
class Timeout < Error; end
5252
class ResponseParse < Error; end
5353
class ProxyParse < Error; end
54+
class TooManyRedirects < Error; end
5455

5556
# Base class for HTTP Error classes
5657
class HTTPStatus < Error

lib/excon/middlewares/redirect_follower.rb

+4-4
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,13 @@ def get_header(datum, header)
2323
end
2424

2525
def response_call(datum)
26-
if datum[:redirects_remaining] <= 0
27-
return @stack.response_call(datum)
28-
end
29-
3026
if datum.has_key?(:response)
3127
case datum[:response][:status]
3228
when 301, 302, 303, 307, 308
29+
if datum[:redirects_remaining] <= 0
30+
raise Excon::Errors::TooManyRedirects
31+
end
32+
3333
datum[:redirects_remaining] -= 1
3434

3535
uri_parser = datum[:uri_parser] || Excon.defaults[:uri_parser]

tests/middlewares/redirect_follower_tests.rb

+2-11
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,9 @@
3333
Shindo.tests('Excon redirector support with redirect loop') do
3434
env_init
3535

36-
tests("request(:method => :get, :path => '/old').body").returns('redirecting') do
36+
tests("request(:method => :get, :path => '/old')").raises(Excon::Errors::TooManyRedirects) do
3737
Excon.stub(
3838
{ :path => '/old' },
39-
{
40-
:headers => { 'Location' => 'http://127.0.0.1:9292/new' },
41-
:body => 'redirecting',
42-
:status => 301
43-
}
44-
)
45-
46-
Excon.stub(
47-
{ :path => '/new' },
4839
{
4940
:headers => { 'Location' => 'http://127.0.0.1:9292/old' },
5041
:body => 'redirecting',
@@ -57,7 +48,7 @@
5748
:path => '/old',
5849
:middlewares => Excon.defaults[:middlewares] + [Excon::Middleware::RedirectFollower],
5950
:mock => true
60-
).body
51+
)
6152
end
6253

6354
env_restore

0 commit comments

Comments
 (0)