Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .agents/skills/ruby-proxy-headers/SKILL.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ Install faraday, httparty, excon, or typhoeus (~> 1.7) as needed.
| HTTParty | `ruby_proxy_headers/httparty` | `ProxyHeadersConnectionAdapter` |
| Excon | `ruby_proxy_headers/excon` | `ExconIntegration.get` (send-only) |
| Typhoeus | `ruby_proxy_headers/typhoeus` | `Typhoeus.get` (`proxy_response_headers`) |
| HTTP.rb | `ruby_proxy_headers/http_gem` | `HTTPGem.create_client` (http gem >= 6) |
| RestClient | `ruby_proxy_headers/rest_client` | `RestClient.get` |

## Usage Patterns

Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed

- HTTPS requests through `RubyProxyHeaders::Typhoeus` no longer call `RubyProxyHeaders::NetHTTP.request`, which is not defined.
- HTTP.rb and RestClient no longer call that missing method. HTTP.rb uses its own `proxy_headers` on CONNECT. RestClient keeps using Net::HTTP, with CONNECT headers attached by the Net::HTTP patch.
- The Net::HTTP patch no longer replaces `Net::HTTP#connect` with a copy of the Ruby 3.2 method. Socket setup, `timeouted_connect`, and HTTPS proxies stay in the installed net-http.
- The Faraday adapter no longer overrides faraday-net_http's private `request_with_wrapped_block`. CONNECT response headers are merged from `build_connection` and `call`.
- `NetHTTP.patch!` still applies after WebMock swaps `Net::HTTP` out and restores it.

## [0.1.0] - 2024-02-28

Expand Down
25 changes: 18 additions & 7 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ GEM
ffi (1.17.4-x86_64-linux-gnu)
ffi (1.17.4-x86_64-linux-musl)
hashdiff (1.2.1)
http (6.0.4)
http-cookie (~> 1.0)
llhttp (~> 0.6.1)
http-accept (1.7.0)
http-cookie (1.1.6)
domain_name (~> 0.5)
httparty (0.24.3)
Expand All @@ -48,6 +52,7 @@ GEM
json (3.1.0)
language_server-protocol (3.17.0.6)
lint_roller (1.1.0)
llhttp (0.6.2)
logger (1.7.0)
mechanize (2.14.2)
addressable (~> 2.8)
Expand All @@ -67,7 +72,6 @@ GEM
mime-types-data (~> 3.2025, >= 3.2025.0507)
mime-types-data (3.2026.1027)
mini_mime (1.1.5)
mini_portile2 (2.8.9)
multi_xml (0.6.0)
net-http (0.10.0)
net-protocol (>= 0.3.0)
Expand All @@ -77,10 +81,8 @@ GEM
connection_pool (>= 2.2.4, < 4)
net-protocol (0.4.0)
timeout
netrc (0.11.0)
nkf (0.3.0)
nokogiri (1.19.6)
mini_portile2 (~> 2.8.2)
racc (~> 1.4)
nokogiri (1.19.6-aarch64-linux-gnu)
racc (~> 1.4)
nokogiri (1.19.6-aarch64-linux-musl)
Expand All @@ -107,6 +109,11 @@ GEM
rainbow (3.1.1)
rake (13.4.2)
regexp_parser (2.13.1)
rest-client (2.1.0)
http-accept (>= 1.7.0, < 2.0)
http-cookie (>= 1.0.2, < 2.0)
mime-types (>= 1.16, < 4.0)
netrc (~> 0.8)
rexml (3.4.4)
rspec (3.13.2)
rspec-core (~> 3.13.0)
Expand Down Expand Up @@ -159,7 +166,6 @@ PLATFORMS
arm-linux-gnu
arm-linux-musl
arm64-darwin
ruby
x86_64-darwin
x86_64-linux-gnu
x86_64-linux-musl
Expand All @@ -170,9 +176,11 @@ DEPENDENCIES
excon (~> 1.4)
faraday (~> 2.14)
faraday-net_http (~> 3.4)
http (~> 6.0)
httparty (~> 0.24)
mechanize (~> 2.14)
rake (~> 13.0)
rest-client (~> 2.1)
rspec (~> 3.12)
rubocop (~> 1.50)
ruby-proxy-headers!
Expand Down Expand Up @@ -204,24 +212,26 @@ CHECKSUMS
ffi (1.17.4-x86_64-linux-gnu) sha256=9d3db14c2eae074b382fa9c083fe95aec6e0a1451da249eab096c34002bc752d
ffi (1.17.4-x86_64-linux-musl) sha256=3fdf9888483de005f8ef8d1cf2d3b20d86626af206cbf780f6a6a12439a9c49e
hashdiff (1.2.1) sha256=9c079dbc513dfc8833ab59c0c2d8f230fa28499cc5efb4b8dd276cf931457cd1
http (6.0.4) sha256=3768f902489ea8309424f1881c668af8d3ca73675648a755dfe2cb0030d42d26
http-accept (1.7.0) sha256=c626860682bfbb3b46462f8c39cd470fd7b0584f61b3cc9df5b2e9eb9972a126
http-cookie (1.1.6) sha256=ba4b82be64de61dc281243dac70e3c382c45142f20268ed9276a3670c93feaa9
httparty (0.24.3) sha256=5c95cf29ca70d5bfd834e56140ddfca6e94166c3b870cde9c25d157cdb5a00d2
json (3.1.0) sha256=3ce7ccf8fde58960177a3a756c31f292a4c1e1dedee884547096a5b0604e3bd8
language_server-protocol (3.17.0.6) sha256=5ef2c0c138f8267e1bc631d3328347d354f96724b0af22f2c79516120443b7f0
lint_roller (1.1.0) sha256=2c0c845b632a7d172cb849cc90c1bce937a28c5c8ccccb50dfd46a485003cc87
llhttp (0.6.2) sha256=3c3c59aafb1e1594ab2f45293478f697458f5a91eb0f0db4b27f61a064024982
logger (1.7.0) sha256=196edec7cc44b66cfb40f9755ce11b392f21f7967696af15d274dde7edff0203
mechanize (2.14.2) sha256=aaf0a91dad80a2a72f51054780731d42ffed564fc530d6cffa8022717508e23e
mime-types (3.7.0) sha256=dcebf61c246f08e15a4de34e386ebe8233791e868564a470c3fe77c00eed5e56
mime-types-data (3.2026.1027) sha256=4568729e44974c1c8a8ceb76f7a5fc164e8c9553335455688bb3c00020f5d83f
mini_mime (1.1.5) sha256=8681b7e2e4215f2a159f9400b5816d85e9d8c6c6b491e96a12797e798f8bccef
mini_portile2 (2.8.9) sha256=0cd7c7f824e010c072e33f68bc02d85a00aeb6fce05bb4819c03dfd3c140c289
multi_xml (0.6.0) sha256=d24393cf958adb226db884b976b007914a89c53ad88718e25679d7008823ad52
net-http (0.10.0) sha256=1a2ff6e37a05724973c43f2bf7bf105f59c36a4cd0a7e9b1c021a0a566987c27
net-http-digest_auth (1.4.1) sha256=4b8ad50ed8d180a58db5d6c49449b987dd0466fe01e24037945bc007562a08db
net-http-persistent (4.0.8) sha256=ef3de8319d691537b329053fae3a33195f8b070bbbfae8bf1a58c796081960e6
net-protocol (0.4.0) sha256=f9f90c8c6f64794a45160bf422177a336db12809de3beeeaa26b88fcd2e8c853
netrc (0.11.0) sha256=de1ce33da8c99ab1d97871726cba75151113f117146becbe45aa85cb3dabee3f
nkf (0.3.0) sha256=357a8dbeba38b727b75930f665146546076a394a1c243faf634ff176e3588895
nokogiri (1.19.6) sha256=a61e7192fc2691c23bc0241907c4dbcb70ada6ab904796ce2426a57171022c8f
nokogiri (1.19.6-aarch64-linux-gnu) sha256=746fca23be3cd3f121cabab65cb44c52049e4cf9f0d36e7e8c3c0eb6cc298c57
nokogiri (1.19.6-aarch64-linux-musl) sha256=4a5620da1c52150e0343bd2bba7dbd9b3899b5eb41687dc1942c9092d81e52a3
nokogiri (1.19.6-arm-linux-gnu) sha256=574e50ddd99ed71aaa868027d84eb3b9f051e55fe07ddc844e4fdd654be953fc
Expand All @@ -238,6 +248,7 @@ CHECKSUMS
rainbow (3.1.1) sha256=039491aa3a89f42efa1d6dec2fc4e62ede96eb6acd95e52f1ad581182b79bc6a
rake (13.4.2) sha256=cb825b2bd5f1f8e91ca37bddb4b9aaf345551b4731da62949be002fa89283701
regexp_parser (2.13.1) sha256=5aedb6b7c35688f51e86eef17a15374dfd287c83f4ca644b2c5f3ec50a33b44b
rest-client (2.1.0) sha256=35a6400bdb14fae28596618e312776c158f7ebbb0ccad752ff4fa142bf2747e3
rexml (3.4.4) sha256=19e0a2c3425dfbf2d4fc1189747bdb2f849b6c5e74180401b15734bc97b5d142
rspec (3.13.2) sha256=206284a08ad798e61f86d7ca3e376718d52c0bc944626b2349266f239f820587
rspec-core (3.13.6) sha256=a8823c6411667b60a8bca135364351dda34cd55e44ff94c4be4633b37d828b2d
Expand Down
30 changes: 30 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,36 @@ HTTParty.get(
puts RubyProxyHeaders.proxy_connect_response_headers['X-ProxyMesh-IP']
```

## HTTP.rb

Requires the `http` gem 6 or newer. Proxy headers are sent on CONNECT by HTTP.rb itself.

```ruby
require 'ruby_proxy_headers/http_gem'

client = RubyProxyHeaders::HTTPGem.create_client(
proxy: 'http://user:pass@proxy.example.com:31280',
proxy_headers: { 'X-ProxyMesh-Country' => 'US' }
)
response = client.get('https://api.ipify.org?format=json')
puts response.proxy_response_headers['X-ProxyMesh-IP']
```

## RestClient

RestClient performs the request. The Net::HTTP patch adds the CONNECT headers.

```ruby
require 'ruby_proxy_headers/rest_client'

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

## Typhoeus

Requires Typhoeus 1.7. Proxy headers go to libcurl as `CURLOPT_PROXYHEADER` and are not copied onto the origin request. Header names are documented at <https://docs.proxymesh.com/>.
Expand Down
35 changes: 19 additions & 16 deletions lib/ruby_proxy_headers/faraday.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,31 @@
module RubyProxyHeaders
# Faraday adapter that merges proxy CONNECT response headers into Faraday response headers.
# Use with {RubyProxyHeaders::NetHTTP.patch!}.
#
# faraday-net_http 3.4 and 3.5 perform the request in the private method
# +request_with_wrapped_block+. This adapter does not override that method.
# It uses the public +build_connection+ and +call+ methods.
module FaradayAdapter
# Copies CONNECT response headers onto the Faraday response.
class NetHttp < ::Faraday::Adapter::NetHttp
def request_with_wrapped_block(http, env, &block)
res = super
merge_proxy_headers(env, http)
res
def build_connection(env)
super.tap { |http| Thread.current[:ruby_proxy_headers_faraday_http] = http }
end

private
def call(env)
response = super
http = Thread.current[:ruby_proxy_headers_faraday_http]
headers = http.last_proxy_connect_response_headers if http
if env[:response_headers] && headers
headers.each do |key, value|
next if value.nil?

def merge_proxy_headers(env, http)
return unless env[:response_headers]
return unless http.respond_to?(:last_proxy_connect_response_headers)

ph = http.last_proxy_connect_response_headers
return unless ph.is_a?(Hash)

ph.each do |k, v|
next if v.nil?

env[:response_headers][k] ||= v
env[:response_headers][key] ||= value
end
end
response
ensure
Thread.current[:ruby_proxy_headers_faraday_http] = nil
end
end
end
Expand Down
101 changes: 61 additions & 40 deletions lib/ruby_proxy_headers/http_gem.rb
Original file line number Diff line number Diff line change
@@ -1,48 +1,53 @@
# frozen_string_literal: true

require 'delegate'
require 'openssl'
require 'uri'
require_relative '../ruby_proxy_headers'

begin
require 'http'
rescue LoadError => e
raise LoadError, "the http gem >= 6 is required for ruby_proxy_headers/http_gem (#{e.message})"
end

module RubyProxyHeaders
# HTTP.rb (http gem) integration for proxy headers support.
# HTTP.rb (the +http+ gem) sends +proxy_headers+ on CONNECT and returns them
# as +response.proxy_headers+. This wrapper adds +proxy_response_headers+,
# matching the other adapters.
#
# @example
# client = RubyProxyHeaders::HTTPGem.create_client(
# proxy: 'http://user:pass@proxy:8080',
# proxy: 'http://user:pass@proxy.example.com:31280',
# proxy_headers: { 'X-ProxyMesh-Country' => 'US' }
# )
#
# response = client.get('https://example.com')
# puts response.proxy_response_headers
#
# puts response.proxy_response_headers['X-ProxyMesh-IP']
module HTTPGem
# Create an HTTP client with proxy header support.
# @param proxy [String] Proxy URL
# @param proxy_headers [Hash] Custom headers to send to proxy
# @param options [Hash] Additional HTTP.rb options
# @return [ProxyClient]
def self.create_client(proxy:, proxy_headers: {}, **options)
ProxyClient.new(proxy: proxy, proxy_headers: proxy_headers, **options)
end

# Make a GET request with proxy headers.
def self.get(url, proxy:, proxy_headers: {}, **options)
create_client(proxy: proxy, proxy_headers: proxy_headers).get(url, **options)
end

# Make a POST request with proxy headers.
def self.post(url, proxy:, proxy_headers: {}, body: nil, **options)
create_client(proxy: proxy, proxy_headers: proxy_headers).post(url, body: body, **options)
end

# Proxy client wrapper for HTTP.rb
# HTTP.rb client that sends proxy_headers on CONNECT.
class ProxyClient
attr_reader :last_proxy_response_headers

REQUEST_OPTIONS = %i[open_timeout read_timeout headers body verify_ssl].freeze

def initialize(proxy:, proxy_headers: {}, **options)
@proxy = proxy
@proxy_headers = proxy_headers
@options = options
@last_proxy_response_headers = nil
@default_options = options
end

attr_reader :last_proxy_response_headers

def get(url, **options)
request(:get, url, **options)
end
Expand All @@ -62,32 +67,48 @@ def delete(url, **options)
private

def request(method, url, **options)
uri = URI.parse(url)

# Use our core connection for HTTPS
if uri.scheme == 'https'
response = RubyProxyHeaders::NetHTTP.request(
method,
url,
proxy: @proxy,
proxy_headers: @proxy_headers,
headers: options[:headers],
body: options[:body]
)
@last_proxy_response_headers = response.proxy_response_headers
return response
options = @default_options.merge(options)
unknown = options.keys - REQUEST_OPTIONS
raise ArgumentError, "unknown option: #{unknown.first}" unless unknown.empty?

@proxy_headers.each { |key, value| RubyProxyHeaders.validate_header!(key, value) }
proxy_uri = URI.parse(@proxy)
user = proxy_uri.user && URI.decode_www_form_component(proxy_uri.user)
password = proxy_uri.password && URI.decode_www_form_component(proxy_uri.password)
client = ::HTTP.via(proxy_uri.host, proxy_uri.port, user, password, @proxy_headers)
timeouts = {}
timeouts[:connect] = options[:open_timeout] if options[:open_timeout]
timeouts[:read] = options[:read_timeout] if options[:read_timeout]
client = client.timeout(timeouts) unless timeouts.empty?

request_options = {}
request_options[:headers] = options[:headers] if options[:headers]
request_options[:body] = options[:body] unless options[:body].nil?
if options[:verify_ssl] == false
context = OpenSSL::SSL::SSLContext.new
context.verify_mode = OpenSSL::SSL::VERIFY_NONE
request_options[:ssl_context] = context
end

response = client.request(method, url, **request_options)
# HTTP::Headers is not a Hash. Copy into HeaderMap so callers can
# read X-ProxyMesh-IP the same way as the other adapters.
mapped = HeaderMap.new
response.proxy_headers.each do |name, value|
mapped[name] = mapped.key?(name) ? "#{mapped[name]}, #{value}" : value
end
@last_proxy_response_headers = mapped
ProxyResponse.new(response, mapped)
end
end

# For HTTP, use standard http gem
require 'http'
# HTTP::Response plus +proxy_response_headers+.
class ProxyResponse < SimpleDelegator
attr_reader :proxy_response_headers

proxy_uri = URI.parse(@proxy)
::HTTP.via(
proxy_uri.host,
proxy_uri.port,
proxy_uri.user,
proxy_uri.password
).send(method, url, **options)
def initialize(response, proxy_headers)
super(response)
@proxy_response_headers = proxy_headers
end
end
end
Expand Down
Loading