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,321 changes: 1,923 additions & 398 deletions Cargo.lock

Large diffs are not rendered by default.

26 changes: 13 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ resp = Wreq.get("https://httpbin.org/bearer", bearer: "my-token")
resp = Wreq.get("https://httpbin.org/basic-auth/user/pass", basic: ["user", "pass"])

# Browser emulation (enabled by default)
resp = Wreq.get("https://tls.peet.ws/api/all", emulation: "chrome_143")
resp = Wreq.get("https://tls.peet.ws/api/all", emulation: "chrome_148")
```

## Using a Client
Expand Down Expand Up @@ -148,25 +148,25 @@ Pass an options hash as the second argument to any HTTP method:

## Browser Emulation

wreq-rb emulates real browser TLS fingerprints, HTTP/2 settings, and headers by default. **The lastest supported Chrome is used when no emulation is specified.**
wreq-rb emulates real browser TLS fingerprints, HTTP/2 settings, and headers by default. **The latest supported Chrome is used when no emulation is specified.**

```ruby
resp = Wreq.get("https://tls.peet.ws/api/all")

# Explicit browser emulation
client = Wreq::Client.new(emulation: "firefox_146")
client = Wreq::Client.new(emulation: "firefox_151")
client = Wreq::Client.new(emulation: "safari_18.5")
client = Wreq::Client.new(emulation: "edge_142")
client = Wreq::Client.new(emulation: "edge_148")

# Disable emulation entirely
client = Wreq::Client.new(emulation: false)

# Emulate a specific OS (default is macOS)
client = Wreq::Client.new(emulation: "chrome_145", emulation_os: "windows")
client = Wreq::Client.new(emulation: "chrome_145", emulation_os: "linux")
client = Wreq::Client.new(emulation: "chrome_148", emulation_os: "windows")
client = Wreq::Client.new(emulation: "chrome_148", emulation_os: "linux")

# Emulation + custom user-agent (user_agent overrides emulation's UA)
client = Wreq::Client.new(emulation: "chrome_143", user_agent: "MyBot/1.0")
client = Wreq::Client.new(emulation: "chrome_148", user_agent: "MyBot/1.0")

# Per-request emulation override
resp = client.get("https://example.com", emulation: "safari_26.2")
Expand All @@ -176,12 +176,12 @@ resp = client.get("https://example.com", emulation: "safari_26.2")

| Browser | Example values |
|---------|---------------|
| Chrome | `chrome_100` .. `chrome_145` |
| Firefox | `firefox_109` .. `firefox_147`, `firefox_private_135` |
| Safari | `safari_15.3` .. `safari_26.2`, `safari_ios_26`, `safari_ipad_18` |
| Edge | `edge_101` .. `edge_145` |
| Opera | `opera_116` .. `opera_119` |
| OkHttp | `okhttp_3_9` .. `okhttp_5` |
| Chrome | `chrome_100` .. `chrome_148` |
| Firefox | `firefox_109` .. `firefox_151`, `firefox_private_135` |
| Safari | `safari_15.3` .. `safari_26.4`, `safari_ios_26`, `safari_ipad_18` |
| Edge | `edge_101` .. `edge_148` |
| Opera | `opera_116` .. `opera_131` |
| OkHttp | `okhttp_3.9` .. `okhttp_5` |

## Response

Expand Down
2 changes: 1 addition & 1 deletion cmake/aarch64-apple-darwin.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
set(CMAKE_SYSTEM_NAME Darwin)
set(CMAKE_SYSTEM_PROCESSOR arm64)
set(CMAKE_OSX_ARCHITECTURES arm64)
set(CMAKE_OSX_DEPLOYMENT_TARGET "10.13")
set(CMAKE_OSX_DEPLOYMENT_TARGET "10.14")

if(CMAKE_HOST_SYSTEM_NAME STREQUAL "Linux")
# osxcross compiler wrappers (available inside the rb-sys-dock container)
Expand Down
2 changes: 1 addition & 1 deletion cmake/x86_64-apple-darwin.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
set(CMAKE_SYSTEM_NAME Darwin)
set(CMAKE_SYSTEM_PROCESSOR x86_64)
set(CMAKE_OSX_ARCHITECTURES x86_64)
set(CMAKE_OSX_DEPLOYMENT_TARGET "10.13")
set(CMAKE_OSX_DEPLOYMENT_TARGET "10.14")

if(CMAKE_HOST_SYSTEM_NAME STREQUAL "Linux")
# osxcross compiler wrappers (available inside the rb-sys-dock container)
Expand Down
8 changes: 3 additions & 5 deletions ext/wreq_rb/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ crate-type = ["cdylib"]
[dependencies]
magnus = { version = "0.8", features = ["rb-sys"] }
rb-sys = "0.9"
wreq = { path = "../../vendor/wreq", features = [
wreq = { path = "../../vendor/wreq", version = "=6.0.0-rc.29", features = [
"cookies",
"json",
"gzip",
Expand All @@ -24,16 +24,14 @@ wreq = { path = "../../vendor/wreq", features = [
"query",
"form",
] }
wreq-util = { version = "=3.0.0-rc.10", features = ["emulation", "emulation-serde", "emulation-compression"] }
wreq-util = { version = "=3.0.0-rc.12", features = ["emulation", "emulation-serde", "emulation-compression"] }
tokio = { version = "1", features = ["full"] }
tokio-util = "0.7"
serde_json = "1.0"
bytes = "1"
http = "1"

[target.'cfg(target_os = "linux")'.dependencies]
wreq = { path = "../../vendor/wreq", features = [
wreq = { path = "../../vendor/wreq", version = "=6.0.0-rc.29", features = [
"prefix-symbols",
] }


55 changes: 24 additions & 31 deletions ext/wreq_rb/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use tokio_util::sync::CancellationToken;
use std::net::IpAddr;
use wreq::header::{HeaderMap, HeaderName, HeaderValue, OrigHeaderMap};
use wreq::tls::TlsVersion;
use wreq_util::{Emulation as BrowserEmulation, EmulationOS, EmulationOption};
use wreq_util::{Emulation as BrowserEmulation, Platform as EmulationPlatform, Profile as BrowserProfile};

use crate::error::{generic_error, to_magnus_error};
use crate::response::Response;
Expand Down Expand Up @@ -141,35 +141,35 @@ async fn execute_request(req: wreq::RequestBuilder) -> Result<ResponseData, wreq
// Emulation helpers
// --------------------------------------------------------------------------

/// The default emulation to apply when none is specified.
const DEFAULT_EMULATION: BrowserEmulation = BrowserEmulation::Chrome145;
/// The default browser profile to apply when none is specified.
const DEFAULT_EMULATION: BrowserProfile = BrowserProfile::Chrome148;

/// Parse a Ruby string like "chrome_143" into a BrowserEmulation variant.
fn parse_emulation(name: &str) -> Result<BrowserEmulation, magnus::Error> {
/// Parse a Ruby string like "chrome_143" into a BrowserProfile variant.
fn parse_emulation(name: &str) -> Result<BrowserProfile, magnus::Error> {
let json_val = serde_json::Value::String(name.to_string());
serde_json::from_value::<BrowserEmulation>(json_val)
.map_err(|_| generic_error(format!("unknown emulation: '{}'. Use names like 'chrome_145', 'firefox_147', 'safari_18.5', etc.", name)))
serde_json::from_value::<BrowserProfile>(json_val)
.map_err(|_| generic_error(format!("unknown emulation: '{}'. Use names like 'chrome_148', 'firefox_151', 'safari_18.5', etc.", name)))
}

/// Parse a Ruby string like "windows" into an EmulationOS variant.
fn parse_emulation_os(name: &str) -> Result<EmulationOS, magnus::Error> {
/// Parse a Ruby string like "windows" into an EmulationPlatform variant.
fn parse_emulation_os(name: &str) -> Result<EmulationPlatform, magnus::Error> {
let json_val = serde_json::Value::String(name.to_string());
serde_json::from_value::<EmulationOS>(json_val)
serde_json::from_value::<EmulationPlatform>(json_val)
.map_err(|_| generic_error("unknown emulation_os. Use: 'windows', 'macos', 'linux', 'android', 'ios'"))
}

/// Build an EmulationOption from an Emulation and an optional OS from the opts hash.
/// Build a BrowserEmulation from a profile and an optional platform from the opts hash.
fn build_emulation_option(
emu: BrowserEmulation,
profile: BrowserProfile,
opts: &RHash,
) -> Result<EmulationOption, magnus::Error> {
let os = match hash_get_string(opts, "emulation_os")? {
Some(os_name) => parse_emulation_os(&os_name)?,
None => EmulationOS::default(),
) -> Result<BrowserEmulation, magnus::Error> {
let platform = match hash_get_string(opts, "emulation_os")? {
Some(platform_name) => parse_emulation_os(&platform_name)?,
None => EmulationPlatform::default(),
};
Ok(EmulationOption::builder()
.emulation(emu)
.emulation_os(os)
Ok(BrowserEmulation::builder()
.profile(profile)
.platform(platform)
.build())
}

Expand Down Expand Up @@ -277,11 +277,11 @@ impl Client {
}

if let Some(v) = hash_get_bool(&opts, "verify_host")? {
builder = builder.verify_hostname(v);
builder = builder.tls_verify_hostname(v);
}

if let Some(v) = hash_get_bool(&opts, "verify_cert")? {
builder = builder.cert_verification(v);
builder = builder.tls_cert_verification(v);
}

if let Some(true) = hash_get_bool(&opts, "http1_only")? {
Expand Down Expand Up @@ -312,7 +312,7 @@ impl Client {
builder = builder.pool_max_idle_per_host(n);
}

if let Some(n) = hash_get_u32(&opts, "pool_max_size")? {
if let Some(n) = hash_get_usize(&opts, "pool_max_size")? {
builder = builder.pool_max_size(n);
}

Expand All @@ -335,11 +335,11 @@ impl Client {
}

if let Some(s) = hash_get_string(&opts, "min_tls_version")? {
builder = builder.min_tls_version(parse_tls_version(&s)?);
builder = builder.tls_min_version(parse_tls_version(&s)?);
}

if let Some(s) = hash_get_string(&opts, "max_tls_version")? {
builder = builder.max_tls_version(parse_tls_version(&s)?);
builder = builder.tls_max_version(parse_tls_version(&s)?);
}
} else {
builder = builder.emulation(DEFAULT_EMULATION);
Expand Down Expand Up @@ -599,13 +599,6 @@ fn hash_get_usize(hash: &RHash, key: &str) -> Result<Option<usize>, magnus::Erro
}
}

fn hash_get_u32(hash: &RHash, key: &str) -> Result<Option<u32>, magnus::Error> {
match hash_get_value(hash, key)? {
Some(v) => Ok(Some(TryConvert::try_convert(v)?)),
None => Ok(None),
}
}

fn parse_tls_version(s: &str) -> Result<TlsVersion, magnus::Error> {
match s {
"tls1.0" | "tls_1_0" | "1.0" => Ok(TlsVersion::TLS_1_0),
Expand Down
Loading
Loading