Skip to content

Commit

Permalink
customizable redirect limit
Browse files Browse the repository at this point in the history
  • Loading branch information
Razvan Secara committed Jun 17, 2014
1 parent b09c7bc commit bb68a15
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions lib/tor/http.rb
Expand Up @@ -7,12 +7,12 @@ class HTTP
class TooManyRedirects < StandardError; end

class << self
attr_accessor :redirects_count
attr_accessor :redirects_made
end

def self.get(uri_or_host, path = nil, port = nil, max_redirects = 3)
res, host = "", nil
self.redirects_count = 0
self.redirects_made = 0

if path
host = uri_or_host
Expand All @@ -32,6 +32,7 @@ def self.get(uri_or_host, path = nil, port = nil, max_redirects = 3)
res = http.request(request)
res = follow_redirect(res, http, max_redirects) # Follow redirects
end

res
end

Expand Down Expand Up @@ -77,12 +78,11 @@ def self.start_parameters(uri_or_host, host, port)

def self.follow_redirect(response, http, max_redirects)
if response.kind_of?(Net::HTTPRedirection)
raise TooManyRedirects if self.redirects_count >= max_redirects
raise TooManyRedirects if self.redirects_made >= max_redirects
request = Net::HTTP::Get.new(fetch_redirect_url(response))
response = http.request(request)
self.redirects_count += 1
self.redirects_made += 1
response = follow_redirect(response, http, max_redirects)

else
response
end
Expand Down

0 comments on commit bb68a15

Please sign in to comment.