class Tor::HTTP

Attributes

redirects_made[RW]

Public Class Methods

get(uri_or_host, path = nil, port = nil, max_redirects = 3) click to toggle source
# File lib/tor/http.rb, line 13
def self.get(uri_or_host, path = nil, port = nil, max_redirects = 3)
  res, host = "", nil
  self.redirects_made = 0

  if path
    host = uri_or_host
  else
    host = uri_or_host.host
    port = uri_or_host.port
  end

  start_params = start_parameters(uri_or_host, host, port)
  start_socks_proxy(start_params) do |http|
    request = Net::HTTP::Get.new(path || uri_or_host.path)
    Tor.configuration.headers.each do |header, value|
      request.delete(header)
      request.add_field(header, value)
    end

    res = http.request(request)
    res = follow_redirect(res, http, max_redirects) # Follow redirects
  end

  res
end
post(uri_or_host, post_options = {}, path = nil, port = nil) click to toggle source
# File lib/tor/http.rb, line 39
def self.post(uri_or_host, post_options = {}, path = nil, port = nil)
  res, host = "", nil
  if path
    host = uri_or_host
  else
    host = uri_or_host.host
    port = uri_or_host.port
    path = uri_or_host.request_uri
  end

  start_params = start_parameters(uri_or_host, host, port)
  start_socks_proxy(start_params) do |http|
    request = Net::HTTP::Post.new(path)
    request.set_form_data(post_options)
    Tor.configuration.headers.each do |header, value|
      request.delete(header)
      request.add_field(header, value)
    end
    res = http.request(request)
  end

  res
end

Private Class Methods

fetch_redirect_url(response) click to toggle source

Get the redirect url from the response. It searches in the “location” header and response body

# File lib/tor/http.rb, line 94
def self.fetch_redirect_url(response)
  if response['location'].nil?
    response.body.match(/<a href=\"([^>]+)\">/i)[1]
  else
    response['location']
  end
end
follow_redirect(response, http, max_redirects) click to toggle source
# File lib/tor/http.rb, line 79
def self.follow_redirect(response, http, max_redirects)
  if response.kind_of?(Net::HTTPRedirection)
    raise TooManyRedirects if self.redirects_made >= max_redirects
    request  = Net::HTTP::Get.new(fetch_redirect_url(response))
    response = http.request(request)
    self.redirects_made += 1
    response = follow_redirect(response, http, max_redirects)
  else
    response
  end

end
start_parameters(uri_or_host, host, port) click to toggle source
# File lib/tor/http.rb, line 70
def self.start_parameters(uri_or_host, host, port)
  uri_or_host = URI.parse(uri_or_host) if uri_or_host.is_a? String
  [
    host, port,
    :use_ssl     => uri_or_host.scheme == 'https',
    :verify_mode => OpenSSL::SSL::VERIFY_NONE
  ]
end
start_socks_proxy(start_params, &code_block) click to toggle source
# File lib/tor/http.rb, line 65
def self.start_socks_proxy(start_params, &code_block)
  Net::HTTP.SOCKSProxy(Tor.configuration.ip, Tor.configuration.port).
    start(*start_params) { |http| code_block.call(http) }
end