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 CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Require Ruby >= 3.2. Dependabot resolves Bundler updates against the gemspec floor, and Ruby 3.1 cannot select nokogiri 1.19 (the release line that installs on the integration job's current Ruby).
- Development dependencies for the Typhoeus adapter are `typhoeus` `~> 1.7` and `ethon` `>= 0.19.1`, `< 0.20`. The gem still has no runtime dependency on Typhoeus.
- Gem metadata includes homepage, changelog, and bug tracker URIs. The published gem ships the guides under `docs/` and leaves the internal research notes out.
- Faraday, HTTParty, Excon, and Net::HTTP guides describe `FaradayIntegration`, `ProxyHeadersConnectionAdapter`, `ExconIntegration`, and `NetHTTP.patch!`.

### Fixed

Expand Down
81 changes: 13 additions & 68 deletions docs/excon.md
Original file line number Diff line number Diff line change
@@ -1,84 +1,29 @@
# Excon

[Excon](https://github.com/excon/excon) is a fast, simple HTTP(S) client. The `RubyProxyHeaders::Excon` module adds proxy header support.
[Excon](https://github.com/excon/excon) already sends extra CONNECT headers as `:ssl_proxy_headers`. `RubyProxyHeaders::ExconIntegration.get` passes them through.

Excon does not expose the CONNECT response on the origin response, so `X-ProxyMesh-IP` is not readable from this client. See [DEFERRED.md](../DEFERRED.md).

## Installation

```ruby
gem 'excon'
gem 'ruby_proxy_headers'
gem 'excon', '~> 1.4'
gem 'ruby-proxy-headers'
```

## Basic Usage
## Usage

```ruby
require 'ruby_proxy_headers'
require 'ruby_proxy_headers/excon'

response = RubyProxyHeaders::Excon.get(
response = RubyProxyHeaders::ExconIntegration.get(
'https://api.ipify.org?format=json',
proxy: 'http://user:pass@proxy.example.com:8080',
proxy_headers: { 'X-ProxyMesh-Country' => 'US' }
)

puts "Status: #{response.code}"
puts "Body: #{response.body}"
puts "Proxy IP: #{response.proxy_response_headers['x-proxymesh-ip']}"
```

## API Reference

### `RubyProxyHeaders::Excon.get(url, options)`

Make a GET request.

**Parameters:**

- `url` (String) - Target URL
- `options` (Hash):
- `:proxy` (String) - Proxy URL (required)
- `:proxy_headers` (Hash) - Headers to send to proxy
- `:headers` (Hash) - Request headers

**Returns:** `ProxyResponse`

### `RubyProxyHeaders::Excon.post(url, options)`

Make a POST request.

**Parameters:**

- Same as `get`, plus:
- `:body` (String) - Request body

### `RubyProxyHeaders::Excon.request(method, url, options)`

Make a request with any HTTP method.

## Response Object

```ruby
response.status # HTTP status code (alias: code)
response.body # Response body
response.headers # Response headers
response.proxy_response_headers # Proxy CONNECT response headers
```

## Example: AWS SDK Compatible

Excon is commonly used with AWS SDKs. Here's how to use proxy headers with AWS:

```ruby
require 'ruby_proxy_headers'

response = RubyProxyHeaders::Excon.get(
'https://sts.amazonaws.com/?Action=GetCallerIdentity&Version=2011-06-15',
proxy: ENV['PROXY_URL'],
proxy_headers: { 'X-ProxyMesh-Country' => 'US' },
headers: {
'Authorization' => aws_signature,
'X-Amz-Date' => Time.now.utc.strftime('%Y%m%dT%H%M%SZ')
}
proxy_url: 'http://user:pass@proxy.example.com:31280',
proxy_connect_headers: { 'X-ProxyMesh-Country' => 'US' }
)

puts response.status
puts response.body
```

Header names are documented at <https://docs.proxymesh.com/>.
121 changes: 14 additions & 107 deletions docs/faraday.md
Original file line number Diff line number Diff line change
@@ -1,122 +1,29 @@
# Faraday

[Faraday](https://lostisland.github.io/faraday/) is a popular HTTP client library with a middleware architecture. The `RubyProxyHeaders::Faraday` module provides middleware for proxy header support.
[Faraday](https://lostisland.github.io/faraday/) 2.x with `faraday-net_http` 3.4 or 3.5. `RubyProxyHeaders::FaradayIntegration` builds a connection whose adapter merges CONNECT response headers into `response.headers`.

## Installation

Add Faraday to your Gemfile:

```ruby
gem 'faraday'
gem 'ruby_proxy_headers'
```
Proxy header names are documented at <https://docs.proxymesh.com/>.

## Basic Usage

### Using Middleware
## Installation

```ruby
require 'faraday'
require 'ruby_proxy_headers'

conn = Faraday.new(url: 'https://api.ipify.org') do |f|
f.use RubyProxyHeaders::Faraday::Middleware,
proxy: 'http://user:pass@proxy.example.com:8080',
proxy_headers: { 'X-ProxyMesh-Country' => 'US' }
f.adapter Faraday.default_adapter
end

response = conn.get('/?format=json')

puts "Status: #{response.status}"
puts "Body: #{response.body}"
puts "Proxy IP: #{response.env[:proxy_response_headers]['x-proxymesh-ip']}"
gem 'faraday', '~> 2.14'
gem 'faraday-net_http', '~> 3.4'
gem 'ruby-proxy-headers'
```

### Using Helper Method
## Usage

```ruby
require 'ruby_proxy_headers'
require 'ruby_proxy_headers/faraday'

conn = RubyProxyHeaders::Faraday.create_connection(
'https://api.ipify.org',
proxy: 'http://user:pass@proxy:8080',
proxy_headers: { 'X-ProxyMesh-Country' => 'US' }
conn = RubyProxyHeaders::FaradayIntegration.connection(
proxy: 'http://user:pass@proxy.example.com:31280',
proxy_connect_headers: { 'X-ProxyMesh-Country' => 'US' }
)

response = conn.get('/?format=json')
puts response.body
```

## Middleware Options

### `RubyProxyHeaders::Faraday::Middleware`

**Options:**

- `:proxy` (String) - Proxy URL (required)
- `:proxy_headers` (Hash) - Headers to send to proxy during CONNECT
- `:on_proxy_connect` (Proc) - Callback when proxy connection is established

### Callback Example

```ruby
conn = Faraday.new do |f|
f.use RubyProxyHeaders::Faraday::Middleware,
proxy: 'http://proxy:8080',
proxy_headers: { 'X-Country' => 'US' },
on_proxy_connect: ->(headers) {
puts "Connected via: #{headers['x-proxymesh-ip']}"
}
f.adapter Faraday.default_adapter
end
```

## Accessing Proxy Headers

Proxy response headers are stored in the Faraday environment:

```ruby
response = conn.get('/')
proxy_headers = response.env[:proxy_response_headers]

puts proxy_headers['x-proxymesh-ip']
puts proxy_headers['x-proxymesh-country']
response = conn.get('https://api.ipify.org?format=json')
puts response.headers['X-ProxyMesh-IP']
```

## API Reference

### `RubyProxyHeaders::Faraday.create_connection(url, options)`

Create a Faraday connection with proxy header support.

**Parameters:**

- `url` (String) - Base URL
- `options` (Hash):
- `:proxy` (String) - Proxy URL (required)
- `:proxy_headers` (Hash) - Headers to send to proxy
- Additional Faraday options

**Returns:** `Faraday::Connection`

## Example: Session Persistence

```ruby
require 'faraday'
require 'ruby_proxy_headers'

session_id = "session-#{SecureRandom.hex(8)}"

conn = Faraday.new do |f|
f.use RubyProxyHeaders::Faraday::Middleware,
proxy: 'http://user:pass@proxyhost:port',
proxy_headers: { 'X-ProxyMesh-Session' => session_id }
f.adapter Faraday.default_adapter
end

5.times do |i|
response = conn.get('https://api.ipify.org?format=json')
puts "Request #{i + 1}: #{response.body}"
end
```
`proxy_connect_headers` are sent only on the CONNECT request. Origin headers are the normal Faraday request headers. When both sides use the same header name, the origin value is kept.
Loading