Module: RubyProxyHeaders::Typhoeus

Defined in:
lib/ruby_proxy_headers/typhoeus.rb

Overview

Typhoeus/Ethon integration for proxy headers support. Typhoeus wraps libcurl, which has native support for CURLOPT_PROXYHEADER.

Examples:

response = RubyProxyHeaders::Typhoeus.get(
  'https://example.com',
  proxy: 'http://user:pass@proxy:8080',
  proxy_headers: { 'X-ProxyMesh-Country' => 'US' }
)
puts response.proxy_response_headers

Defined Under Namespace

Classes: ProxyResponse

Class Method Summary collapse

Class Method Details

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

Make a GET request with proxy headers.

Parameters:

  • url (String)

    Target URL

  • proxy (String)

    Proxy URL

  • proxy_headers (Hash) (defaults to: {})

    Custom headers to send to proxy

  • options (Hash)

    Additional Typhoeus options

Returns:



22
23
24
# File 'lib/ruby_proxy_headers/typhoeus.rb', line 22

def self.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

Make a POST request with proxy headers.



27
28
29
# File 'lib/ruby_proxy_headers/typhoeus.rb', line 27

def self.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) ⇒ ProxyResponse

Make a request with proxy headers.

Parameters:

  • method (Symbol)

    HTTP method

  • url (String)

    Target URL

  • proxy (String)

    Proxy URL

  • proxy_headers (Hash) (defaults to: {})

    Custom headers to send to proxy

  • options (Hash)

    Additional Typhoeus options

Returns:



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/ruby_proxy_headers/typhoeus.rb', line 38

def self.request(method, url, proxy:, proxy_headers: {}, **options)
  require 'typhoeus'

  uri = URI.parse(url)

  # For HTTPS, we need custom handling since Typhoeus doesn't expose CURLOPT_PROXYHEADER
  if uri.scheme == 'https' && !proxy_headers.empty?
    # Use our core connection for now
    # TODO: Extend Ethon to expose CURLOPT_PROXYHEADER for native support
    response = RubyProxyHeaders::NetHTTP.request(
      method,
      url,
      proxy: proxy,
      proxy_headers: proxy_headers,
      headers: options[:headers],
      body: options[:body]
    )
    return response
  end

  # For HTTP or no proxy headers, use standard Typhoeus
  typhoeus_options = options.merge(
    proxy: proxy,
    method: method
  )

  response = ::Typhoeus::Request.new(url, typhoeus_options).run
  ProxyResponse.new(response, {})
end