1
0
Fork 0
mirror of https://github.com/rest-client/rest-client.git synced 2022-11-09 13:49:40 -05:00

Limit number of redirects followed to 10 by default or by :max_redirects, if specified.

This commit is contained in:
Chris Dinn 2010-12-16 12:41:12 -05:00
parent 2caea9b9f0
commit bce9ed458a
4 changed files with 21 additions and 2 deletions

View file

@ -67,9 +67,11 @@ module RestClient
end
args[:url] = url
if request
raise MaxRedirectsReached if request.max_redirects == 0
args[:password] = request.password
args[:user] = request.user
args[:headers] = request.headers
args[:max_redirects] = request.max_redirects - 1
# pass any cookie set in the result
if result && result['set-cookie']
args[:headers][:cookies] = (args[:headers][:cookies] || {}).merge(parse_cookie(result['set-cookie']))

View file

@ -163,6 +163,10 @@ module RestClient
end
end
class MaxRedirectsReached < Exception
message = 'Maximum number of redirect reached'
end
# The server broke the connection prior to the request completing. Usually
# this means it crashed, or sometimes that your network connection was
# severed before it could complete.

View file

@ -23,7 +23,7 @@ module RestClient
class Request
attr_reader :method, :url, :headers, :cookies,
:payload, :user, :password, :timeout,
:payload, :user, :password, :timeout, :max_redirects,
:open_timeout, :raw_response, :verify_ssl, :ssl_client_cert,
:ssl_client_key, :ssl_ca_file, :processed_headers, :args
@ -51,6 +51,7 @@ module RestClient
@ssl_client_key = args[:ssl_client_key] || nil
@ssl_ca_file = args[:ssl_ca_file] || nil
@tf = nil # If you are a raw request, this is your tempfile
@max_redirects = args[:max_redirects] || 10
@processed_headers = make_headers headers
@args = args
end

View file

@ -149,8 +149,20 @@ describe RestClient::Response do
stub_request(:get, 'http://new/resource').to_return(:body => 'Foo')
RestClient::Request.execute(:url => 'http://some/resource', :method => :get).body.should == 'Foo'
end
it "follows no more than 10 redirections before raising error" do
stub_request(:get, 'http://some/redirect-1').to_return(:body => '', :status => 301, :headers => {'Location' => 'http://some/redirect-2'})
stub_request(:get, 'http://some/redirect-2').to_return(:body => '', :status => 301, :headers => {'Location' => 'http://some/redirect-2'})
lambda { RestClient::Request.execute(:url => 'http://some/redirect-1', :method => :get) }.should raise_error(RestClient::MaxRedirectsReached)
WebMock.should have_requested(:get, 'http://some/redirect-2').times(10)
end
it "follows no more than max_redirects redirections, if specified" do
stub_request(:get, 'http://some/redirect-1').to_return(:body => '', :status => 301, :headers => {'Location' => 'http://some/redirect-2'})
stub_request(:get, 'http://some/redirect-2').to_return(:body => '', :status => 301, :headers => {'Location' => 'http://some/redirect-2'})
lambda { RestClient::Request.execute(:url => 'http://some/redirect-1', :method => :get, :max_redirects => 5) }.should raise_error(RestClient::MaxRedirectsReached)
WebMock.should have_requested(:get, 'http://some/redirect-2').times(5)
end
end