Permalink
Browse files

Adds the ability to set a max_redirects option on both Typhoeus::Easy

and Typhoeus::Request instances to prevent the client getting hung on
a request that doesn't redirect properly.

* Added bad redirect action to the test server, that redirects to itself, and
so hangs infinitely unless called with a max_redirects option.
* Added option to allow the passing of a max_redirects option to Typhoeus::Easy.
* Added max_redirects option to Typhoeus::Request.
* Added code to Typhoeus::Hydra to pass the max_redirects option from
Typhoeus::Request through to the Typhoeus::Easy instance that performs the request.
  • Loading branch information...
1 parent 458477d commit 0bd731b5da92147c92ee786cba8c52945bb4022b @smulube smulube committed Nov 16, 2009
@@ -16,7 +16,8 @@ class Easy
:CURLOPT_TIMEOUT_MS => 155,
:CURLOPT_NOSIGNAL => 99,
:CURLOPT_HTTPHEADER => 10023,
- :CURLOPT_FOLLOWLOCATION => 52
+ :CURLOPT_FOLLOWLOCATION => 52,
+ :CURLOPT_MAXREDIRS => 68
}
INFO_VALUES = {
:CURLINFO_RESPONSE_CODE => 2097154,
@@ -48,6 +49,10 @@ def follow_location=(boolean)
set_option(OPTION_VALUES[:CURLOPT_FOLLOWLOCATION], 0)
end
end
+
+ def max_redirects=(redirects)
+ set_option(OPTION_VALUES[:CURLOPT_MAXREDIRS], redirects)
+ end
def timeout=(milliseconds)
@timeout = milliseconds
@@ -123,6 +123,7 @@ def get_easy_object(request)
easy.request_body = request.body if request.body
easy.timeout = request.timeout if request.timeout
easy.follow_location = request.follow_location if request.follow_location
+ easy.max_redirects = request.max_redirects if request.max_redirects
easy.on_success do |easy|
queue_next
handle_request(request, response_from_easy(easy, request))
@@ -1,6 +1,6 @@
module Typhoeus
class Request
- attr_accessor :method, :params, :body, :headers, :timeout, :user_agent, :response, :cache_timeout, :follow_location
+ attr_accessor :method, :params, :body, :headers, :timeout, :user_agent, :response, :cache_timeout, :follow_location, :max_redirects
attr_reader :url
def initialize(url, options = {})
@@ -12,6 +12,7 @@ def initialize(url, options = {})
@user_agent = options[:user_agent] || Typhoeus::USER_AGENT
@cache_timeout = options[:cache_timeout]
@follow_location = options[:follow_location]
+ @max_redirects = options[:max_redirects]
if @method == :post
@url = url
else
View
@@ -21,6 +21,10 @@
redirect '/'
end
+get '/bad_redirect' do
+ redirect '/bad_redirect'
+end
+
get '/**' do
sleep params["delay"].to_i if params.has_key?("delay")
request.env.merge!(:body => request.body.read).to_json
@@ -39,6 +39,26 @@
# this doesn't work on a mac for some reason
# e.timed_out?.should == true
end
+
+ it "should allow the setting of the max redirects to follow" do
+ e = Typhoeus::Easy.new
+ e.url = "http://localhost:3001/redirect"
+ e.method = :get
+ e.follow_location = true
+ e.max_redirects = 5
+ e.perform
+ e.response_code.should == 200
+ end
+
+ it "should handle our bad redirect action, provided we've set max_redirects properly" do
+ e = Typhoeus::Easy.new
+ e.url = "http://localhost:3001/bad_redirect"
+ e.method = :get
+ e.follow_location = true
+ e.max_redirects = 5
+ e.perform
+ e.response_code.should == 302
+ end
end
describe "get" do
@@ -265,4 +265,13 @@ def set(key, object, timeout = 0)
request.response.code.should == 200
end
+
+ it "should pass through the max_redirects option when set on a request" do
+ hydra = Typhoeus::Hydra.new
+ request = Typhoeus::Request.new("http://localhost:3000/bad_redirect", :max_redirects => 5)
+ hydra.queue request
+ hydra.run
+
+ request.response.code.should == 302
+ end
end
@@ -69,6 +69,10 @@
Typhoeus::Request.new("http://localhost:3000", :follow_location => true).follow_location.should == true
end
+ it "takes max_redirects as an option" do
+ Typhoeus::Request.new("http://localhost:3000", :max_redirects => 10).max_redirects.should == 10
+ end
+
it "has the associated response object" do
request = Typhoeus::Request.new("http://localhost:3000")
request.response = :foo

0 comments on commit 0bd731b

Please sign in to comment.