Module: RubyProxyHeaders::Typhoeus

Defined in:
lib/ruby_proxy_headers/typhoeus.rb

Overview

Typhoeus adapter using libcurl's proxy-header list.

Typhoeus 1.7 / Ethon 0.19 expose :proxyheader (CURLOPT_PROXYHEADER) and :headeropt (CURLOPT_HEADEROPT). With headeropt: :separate, those headers are sent only to the proxy. On HTTPS that is the CONNECT request. :headers stay on the tunneled origin request.

libcurl records the CONNECT response as the first header block of response_headers and the origin response after it. Typhoeus's response.headers is only the last block. On HTTPS through a proxy, proxy_response_headers is that first block when its status is 2xx.

Examples:

response = RubyProxyHeaders::Typhoeus.get(
  'https://api.ipify.org?format=json',
  proxy: 'http://user:[email protected]:31280',
  proxy_headers: { 'X-ProxyMesh-Country' => 'US' }
)
puts response.proxy_response_headers['X-ProxyMesh-IP']

Defined Under Namespace

Classes: ProxyResponse

Constant Summary collapse

HTTP_STATUS =
%r{\AHTTP/\d+(?:\.\d+)?\s+(\d{3})\b}i

Class Method Summary collapse

Class Method Details

.get(url, proxy:, proxy_headers: {}, **options) ⇒ Object



37
38
39
# File 'lib/ruby_proxy_headers/typhoeus.rb', line 37

def get(url, proxy:, proxy_headers: {}, **options)
  request(:get, url, proxy: proxy, proxy_headers: proxy_headers, **options)
end

.post(url, proxy:, proxy_headers: {}, body: nil, **options) ⇒ Object



41
42
43
# File 'lib/ruby_proxy_headers/typhoeus.rb', line 41

def post(url, proxy:, proxy_headers: {}, body: nil, **options)
  request(:post, url, proxy: proxy, proxy_headers: proxy_headers, body: body, **options)
end

.request(method, url, proxy:, proxy_headers: {}, **options) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/ruby_proxy_headers/typhoeus.rb', line 45

def request(method, url, proxy:, proxy_headers: {}, **options)
  typhoeus_options = options.merge(proxy: proxy, method: method)
  unless proxy_headers.empty?
    lines = proxy_headers.map do |name, value|
      RubyProxyHeaders.validate_header!(name, value)
      "#{name}: #{value}"
    end
    # The Slist has to stay in the options Typhoeus keeps. libcurl does not
    # copy CURLOPT_PROXYHEADER, and Ethon clears its own reference on reset.
    typhoeus_options[:proxyheader] = ::Ethon::Curl::Slist.new(::Ethon::Curl::Slist.build(lines))
    typhoeus_options[:headeropt] = :separate
  end

  response = ::Typhoeus::Request.new(url, typhoeus_options).run
  ProxyResponse.new(response, connect_headers(response, url, proxy))
end