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
6 changes: 4 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ name = "wreq_ruby"
magnus = { version = "0.8", features = ["bytes"] }
rb-sys = { version = "0.9.110", default-features = false }
tokio = { version = "1", features = ["full"] }
wreq = { version = "6.0.0-rc.24", features = [
wreq = { version = "6.0.0-rc.23", features = [
"json",
"socks",
"stream",
Expand Down Expand Up @@ -49,5 +49,7 @@ debug = false
incremental = false
lto = "fat"
opt-level = 3
panic = "abort"
strip = true

[patch.crates-io]
wreq = { git = "https://github.com/0x676e67/wreq" }
19 changes: 1 addition & 18 deletions lib/wreq.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@ module Wreq
# @param cookies [Array<String>, nil] Cookies to send
# @param allow_redirects [Boolean, nil] Whether to follow redirects
# @param max_redirects [Integer, nil] Maximum number of redirects to follow
# @param referer [Boolean, nil] Whether to send Referer header on redirects
# @param history [Boolean, nil] Track full redirect chain
# @param gzip [Boolean, nil] Enable gzip compression
# @param brotli [Boolean, nil] Enable Brotli compression
# @param deflate [Boolean, nil] Enable deflate compression
Expand Down Expand Up @@ -71,8 +69,6 @@ def self.request(method, url, **options)
# @param cookies [Array<String>, nil] Cookies to send
# @param allow_redirects [Boolean, nil] Whether to follow redirects
# @param max_redirects [Integer, nil] Maximum number of redirects to follow
# @param referer [Boolean, nil] Whether to send Referer header on redirects
# @param history [Boolean, nil] Track full redirect chain
# @param gzip [Boolean, nil] Enable gzip compression
# @param brotli [Boolean, nil] Enable Brotli compression
# @param deflate [Boolean, nil] Enable deflate compression
Expand Down Expand Up @@ -105,8 +101,7 @@ def self.get(url, **options)
# @param cookies [Array<String>, nil] Cookies to send
# @param allow_redirects [Boolean, nil] Whether to follow redirects
# @param max_redirects [Integer, nil] Maximum number of redirects to follow
# @param referer [Boolean, nil] Whether to send Referer header on redirects
# @param history [Boolean, nil] Track full redirect chain

# @param gzip [Boolean, nil] Enable gzip compression
# @param brotli [Boolean, nil] Enable Brotli compression
# @param deflate [Boolean, nil] Enable deflate compression
Expand Down Expand Up @@ -139,8 +134,6 @@ def self.head(url, **options)
# @param cookies [Array<String>, nil] Cookies to send
# @param allow_redirects [Boolean, nil] Whether to follow redirects
# @param max_redirects [Integer, nil] Maximum number of redirects to follow
# @param referer [Boolean, nil] Whether to send Referer header on redirects
# @param history [Boolean, nil] Track full redirect chain
# @param gzip [Boolean, nil] Enable gzip compression
# @param brotli [Boolean, nil] Enable Brotli compression
# @param deflate [Boolean, nil] Enable deflate compression
Expand Down Expand Up @@ -173,8 +166,6 @@ def self.post(url, **options)
# @param cookies [Array<String>, nil] Cookies to send
# @param allow_redirects [Boolean, nil] Whether to follow redirects
# @param max_redirects [Integer, nil] Maximum number of redirects to follow
# @param referer [Boolean, nil] Whether to send Referer header on redirects
# @param history [Boolean, nil] Track full redirect chain
# @param gzip [Boolean, nil] Enable gzip compression
# @param brotli [Boolean, nil] Enable Brotli compression
# @param deflate [Boolean, nil] Enable deflate compression
Expand Down Expand Up @@ -207,8 +198,6 @@ def self.put(url, **options)
# @param cookies [Array<String>, nil] Cookies to send
# @param allow_redirects [Boolean, nil] Whether to follow redirects
# @param max_redirects [Integer, nil] Maximum number of redirects to follow
# @param referer [Boolean, nil] Whether to send Referer header on redirects
# @param history [Boolean, nil] Track full redirect chain
# @param gzip [Boolean, nil] Enable gzip compression
# @param brotli [Boolean, nil] Enable Brotli compression
# @param deflate [Boolean, nil] Enable deflate compression
Expand Down Expand Up @@ -241,8 +230,6 @@ def self.delete(url, **options)
# @param cookies [Array<String>, nil] Cookies to send
# @param allow_redirects [Boolean, nil] Whether to follow redirects
# @param max_redirects [Integer, nil] Maximum number of redirects to follow
# @param referer [Boolean, nil] Whether to send Referer header on redirects
# @param history [Boolean, nil] Track full redirect chain
# @param gzip [Boolean, nil] Enable gzip compression
# @param brotli [Boolean, nil] Enable Brotli compression
# @param deflate [Boolean, nil] Enable deflate compression
Expand Down Expand Up @@ -275,8 +262,6 @@ def self.options(url, **options)
# @param cookies [Array<String>, nil] Cookies to send
# @param allow_redirects [Boolean, nil] Whether to follow redirects
# @param max_redirects [Integer, nil] Maximum number of redirects to follow
# @param referer [Boolean, nil] Whether to send Referer header on redirects
# @param history [Boolean, nil] Track full redirect chain
# @param gzip [Boolean, nil] Enable gzip compression
# @param brotli [Boolean, nil] Enable Brotli compression
# @param deflate [Boolean, nil] Enable deflate compression
Expand Down Expand Up @@ -309,8 +294,6 @@ def self.trace(url, **options)
# @param cookies [Array<String>, nil] Cookies to send
# @param allow_redirects [Boolean, nil] Whether to follow redirects
# @param max_redirects [Integer, nil] Maximum number of redirects to follow
# @param referer [Boolean, nil] Whether to send Referer header on redirects
# @param history [Boolean, nil] Track full redirect chain
# @param gzip [Boolean, nil] Enable gzip compression
# @param brotli [Boolean, nil] Enable Brotli compression
# @param deflate [Boolean, nil] Enable deflate compression
Expand Down
21 changes: 0 additions & 21 deletions lib/wreq_ruby/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,6 @@ class Client
# headers when following redirects. When true, the previous URL will
# be sent as the Referer header.
#
# @param history [Boolean, nil] Whether to track the full redirect chain
# for each request. Useful for debugging redirect issues.
#
# @param allow_redirects [Boolean, nil] Enable automatic following of
# HTTP redirects (3xx status codes). When false, redirect responses
# will be returned directly to the caller.
Expand Down Expand Up @@ -252,8 +249,6 @@ def self.new(**options)
# @param cookies [Array<String>, nil] Cookies to send
# @param allow_redirects [Boolean, nil] Whether to follow redirects
# @param max_redirects [Integer, nil] Maximum number of redirects to follow
# @param referer [Boolean, nil] Whether to send Referer header on redirects
# @param history [Boolean, nil] Track full redirect chain
# @param gzip [Boolean, nil] Enable gzip compression
# @param brotli [Boolean, nil] Enable Brotli compression
# @param deflate [Boolean, nil] Enable deflate compression
Expand Down Expand Up @@ -285,8 +280,6 @@ def request(method, url, **options)
# @param cookies [Array<String>, nil] Cookies to send
# @param allow_redirects [Boolean, nil] Whether to follow redirects
# @param max_redirects [Integer, nil] Maximum number of redirects to follow
# @param referer [Boolean, nil] Whether to send Referer header on redirects
# @param history [Boolean, nil] Track full redirect chain
# @param gzip [Boolean, nil] Enable gzip compression
# @param brotli [Boolean, nil] Enable Brotli compression
# @param deflate [Boolean, nil] Enable deflate compression
Expand Down Expand Up @@ -318,8 +311,6 @@ def get(url, **options)
# @param cookies [Array<String>, nil] Cookies to send
# @param allow_redirects [Boolean, nil] Whether to follow redirects
# @param max_redirects [Integer, nil] Maximum number of redirects to follow
# @param referer [Boolean, nil] Whether to send Referer header on redirects
# @param history [Boolean, nil] Track full redirect chain
# @param gzip [Boolean, nil] Enable gzip compression
# @param brotli [Boolean, nil] Enable Brotli compression
# @param deflate [Boolean, nil] Enable deflate compression
Expand Down Expand Up @@ -351,8 +342,6 @@ def head(url, **options)
# @param cookies [Array<String>, nil] Cookies to send
# @param allow_redirects [Boolean, nil] Whether to follow redirects
# @param max_redirects [Integer, nil] Maximum number of redirects to follow
# @param referer [Boolean, nil] Whether to send Referer header on redirects
# @param history [Boolean, nil] Track full redirect chain
# @param gzip [Boolean, nil] Enable gzip compression
# @param brotli [Boolean, nil] Enable Brotli compression
# @param deflate [Boolean, nil] Enable deflate compression
Expand Down Expand Up @@ -384,8 +373,6 @@ def post(url, **options)
# @param cookies [Array<String>, nil] Cookies to send
# @param allow_redirects [Boolean, nil] Whether to follow redirects
# @param max_redirects [Integer, nil] Maximum number of redirects to follow
# @param referer [Boolean, nil] Whether to send Referer header on redirects
# @param history [Boolean, nil] Track full redirect chain
# @param gzip [Boolean, nil] Enable gzip compression
# @param brotli [Boolean, nil] Enable Brotli compression
# @param deflate [Boolean, nil] Enable deflate compression
Expand Down Expand Up @@ -417,8 +404,6 @@ def put(url, **options)
# @param cookies [Array<String>, nil] Cookies to send
# @param allow_redirects [Boolean, nil] Whether to follow redirects
# @param max_redirects [Integer, nil] Maximum number of redirects to follow
# @param referer [Boolean, nil] Whether to send Referer header on redirects
# @param history [Boolean, nil] Track full redirect chain
# @param gzip [Boolean, nil] Enable gzip compression
# @param brotli [Boolean, nil] Enable Brotli compression
# @param deflate [Boolean, nil] Enable deflate compression
Expand Down Expand Up @@ -450,8 +435,6 @@ def delete(url, **options)
# @param cookies [Array<String>, nil] Cookies to send
# @param allow_redirects [Boolean, nil] Whether to follow redirects
# @param max_redirects [Integer, nil] Maximum number of redirects to follow
# @param referer [Boolean, nil] Whether to send Referer header on redirects
# @param history [Boolean, nil] Track full redirect chain
# @param gzip [Boolean, nil] Enable gzip compression
# @param brotli [Boolean, nil] Enable Brotli compression
# @param deflate [Boolean, nil] Enable deflate compression
Expand Down Expand Up @@ -483,8 +466,6 @@ def options(url, **options)
# @param cookies [Array<String>, nil] Cookies to send
# @param allow_redirects [Boolean, nil] Whether to follow redirects
# @param max_redirects [Integer, nil] Maximum number of redirects to follow
# @param referer [Boolean, nil] Whether to send Referer header on redirects
# @param history [Boolean, nil] Track full redirect chain
# @param gzip [Boolean, nil] Enable gzip compression
# @param brotli [Boolean, nil] Enable Brotli compression
# @param deflate [Boolean, nil] Enable deflate compression
Expand Down Expand Up @@ -516,8 +497,6 @@ def trace(url, **options)
# @param cookies [Array<String>, nil] Cookies to send
# @param allow_redirects [Boolean, nil] Whether to follow redirects
# @param max_redirects [Integer, nil] Maximum number of redirects to follow
# @param referer [Boolean, nil] Whether to send Referer header on redirects
# @param history [Boolean, nil] Track full redirect chain
# @param gzip [Boolean, nil] Enable gzip compression
# @param brotli [Boolean, nil] Enable Brotli compression
# @param deflate [Boolean, nil] Enable deflate compression
Expand Down
12 changes: 12 additions & 0 deletions lib/wreq_ruby/error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,18 @@ class MemoryError < StandardError; end
# end
class ConnectionError < StandardError; end

# Proxy Connection to the server failed.
#
# Raised when the client cannot establish a connection to the proxy server.
# @example
# begin
# client.get("http://example.com", proxy: "http://invalid-proxy:8080")
# rescue Wreq::ProxyConnectionError => e
# puts "Proxy connection failed: #{e.message}"
# retry_with_different_proxy
# end
class ProxyConnectionError < StandardError; end

# Connection was reset by the server.
#
# Raised when the server closes the connection unexpectedly.
Expand Down
5 changes: 1 addition & 4 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@ struct Builder {
orig_headers: Option<OrigHeaderMap>,
/// Whether to use referer.
referer: Option<bool>,
/// Whether to keep track of request history.
history: Option<bool>,
/// Whether to allow redirects.
allow_redirects: Option<bool>,
/// The maximum number of redirects to follow.
Expand Down Expand Up @@ -184,7 +182,6 @@ impl Client {

// Allow redirects options.
apply_option!(set_if_some, builder, params.referer, referer);
apply_option!(set_if_some, builder, params.history, history);
apply_option!(
set_if_true_with,
builder,
Expand All @@ -201,7 +198,7 @@ impl Client {
// Cookie options.
apply_option!(set_if_some, builder, params.cookie_store, cookie_store);
apply_option!(
set_if_some,
set_if_some_inner,
builder,
params.cookie_provider,
cookie_provider
Expand Down
2 changes: 1 addition & 1 deletion src/client/req.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ pub fn execute_request<U: AsRef<str>>(
// Cookies options.
if let Some(cookies) = request.cookies.take() {
for cookie in cookies {
builder = builder.header_append(header::COOKIE, cookie);
builder = builder.header(header::COOKIE, cookie);
}
}

Expand Down
20 changes: 2 additions & 18 deletions src/cookie.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,7 @@ use magnus::{
Error, Module, Object, RModule, Ruby, Value, function, method, typed_data::Obj,
value::ReprValue,
};
use wreq::{
Uri,
cookie::CookieStore,
header::{self, HeaderMap, HeaderValue},
};
use wreq::header::{self, HeaderMap, HeaderValue};

use crate::gvl;

Expand All @@ -36,7 +32,7 @@ pub struct Cookie(RawCookie<'static>);
/// existing cookies more easily, before creating a `Client`.
#[derive(Clone, Default)]
#[magnus::wrap(class = "Wreq::Jar", free_immediately, size)]
pub struct Jar(Arc<wreq::cookie::Jar>);
pub struct Jar(pub Arc<wreq::cookie::Jar>);

// ===== impl Cookie =====

Expand Down Expand Up @@ -203,18 +199,6 @@ impl fmt::Display for Cookie {

// ===== impl Jar =====

impl CookieStore for Jar {
#[inline]
fn set_cookies(&self, cookie_headers: &mut dyn Iterator<Item = &HeaderValue>, uri: &Uri) {
self.0.set_cookies(cookie_headers, uri);
}

#[inline]
fn cookies(&self, uri: &Uri) -> Vec<HeaderValue> {
self.0.cookies(uri)
}
}

impl Jar {
/// Create a new [`Jar`] with an empty cookie store.
pub fn new() -> Self {
Expand Down
7 changes: 7 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ define_exception!(MEMORY, "MemoryError", exception_runtime_error);

// Network connection errors
define_exception!(CONNECTION_ERROR, "ConnectionError", exception_runtime_error);
define_exception!(
PROXY_CONNECTION_ERROR,
"ProxyConnectionError",
exception_runtime_error
);
define_exception!(
CONNECTION_RESET_ERROR,
"ConnectionResetError",
Expand Down Expand Up @@ -116,6 +121,7 @@ pub fn wreq_error_to_magnus(err: wreq::Error) -> MagnusError {
is_tls => TLS_ERROR,
is_connection_reset => CONNECTION_RESET_ERROR,
is_connect => CONNECTION_ERROR,
is_proxy_connect => PROXY_CONNECTION_ERROR,
is_decode => DECODING_ERROR,
is_redirect => REDIRECT_ERROR,
is_timeout => TIMEOUT_ERROR,
Expand All @@ -127,6 +133,7 @@ pub fn wreq_error_to_magnus(err: wreq::Error) -> MagnusError {
pub fn include(ruby: &Ruby) {
Lazy::force(&MEMORY, ruby);
Lazy::force(&CONNECTION_ERROR, ruby);
Lazy::force(&PROXY_CONNECTION_ERROR, ruby);
Lazy::force(&CONNECTION_RESET_ERROR, ruby);
Lazy::force(&TLS_ERROR, ruby);
Lazy::force(&REQUEST_ERROR, ruby);
Expand Down
21 changes: 21 additions & 0 deletions test/error_handling_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,25 @@ def test_empty_response_json
assert_instance_of Wreq::DecodingError, e
end
end

def test_proxy_error_handling
invalid_proxies = [
"http://invalid.proxy:8080",
"https://invalid.proxy:8080",
"socks4://invalid.proxy:8080",
"socks4a://invalid.proxy:8080",
"socks5://invalid.proxy:8080",
"socks5h://invalid.proxy:8080"
]
target_urls = ["https://example.com", "http://example.com"]

invalid_proxies.each do |proxy|
target_urls.each do |url|
Wreq.get(url, proxy: proxy, timeout: 5)
flunk "Expected proxy connection error but got response"
rescue => e
assert_instance_of Wreq::ProxyConnectionError, e
end
end
end
end