Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

style: format code style #314

Merged
merged 3 commits into from
Jan 13, 2025
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
170 changes: 117 additions & 53 deletions src/client/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ struct Config {
http2_max_retry_count: usize,
tls_info: bool,
connector_layers: Vec<BoxedConnectorLayer>,
http2: Option<Http2Settings>,
tls: TlsSettings,
}

Expand Down Expand Up @@ -170,11 +171,12 @@ impl ClientBuilder {
dns_overrides: HashMap::new(),
dns_resolver: None,
base_url: None,
builder: crate::util::client::Client::builder(TokioExecutor::new()),
builder: util::client::Client::builder(TokioExecutor::new()),
https_only: false,
http2_max_retry_count: 2,
tls_info: false,
connector_layers: Vec::new(),
http2: None,
tls: Default::default(),
},
}
Expand Down Expand Up @@ -237,7 +239,12 @@ impl ClientBuilder {
.pool_timer(TokioTimer::new())
.pool_idle_timeout(config.pool_idle_timeout)
.pool_max_idle_per_host(config.pool_max_idle_per_host)
.pool_max_size(config.pool_max_size);
.pool_max_size(config.pool_max_size)
.with_http2_builder(|builder| {
if let Some(http2) = config.http2 {
apply_http2_settings(builder, http2)
}
});

let hyper = config
.builder
Expand Down Expand Up @@ -891,39 +898,77 @@ impl ClientBuilder {

// TLS/HTTP2 mimic options

/// Sets the necessary values to mimic the specified impersonate version, including headers and TLS settings.
/// Configures the client to mimic the specified impersonate version.
/// This includes setting the necessary headers and TLS settings.
///
/// # Arguments
///
/// * `impersonate` - The impersonate version to mimic.
///
/// # Returns
///
/// A `ClientBuilder` instance with the applied settings.
#[inline]
pub fn impersonate(self, impersonate: Impersonate) -> ClientBuilder {
let settings = mimic::impersonate(impersonate, true, ImpersonateOs::default());
let settings = mimic::impersonate(impersonate, ImpersonateOs::default(), true);
self.apply_impersonate_settings(settings)
}

/// Sets the necessary values to mimic the specified impersonate version, including headers, TLS settings and OS.
/// Configures the client to mimic the specified impersonate version with a specific OS.
/// This includes setting the necessary headers, TLS settings, and OS.
///
/// # Arguments
///
/// * `impersonate` - The impersonate version to mimic.
/// * `impersonate_os` - The OS to mimic.
///
/// # Returns
///
/// A `ClientBuilder` instance with the applied settings.
#[inline]
pub fn impersonate_with_os(
self,
impersonate: Impersonate,
impersonate_os: ImpersonateOs,
) -> ClientBuilder {
let settings = mimic::impersonate(impersonate, true, impersonate_os);
let settings = mimic::impersonate(impersonate, impersonate_os, true);
self.apply_impersonate_settings(settings)
}

/// Sets the necessary values to mimic the specified impersonate version, skipping header configuration.
/// Configures the client to mimic the specified impersonate version, skipping header configuration.
/// This includes setting the necessary TLS settings.
///
/// # Arguments
///
/// * `impersonate` - The impersonate version to mimic.
///
/// # Returns
///
/// A `ClientBuilder` instance with the applied settings.
#[inline]
pub fn impersonate_skip_headers(self, impersonate: Impersonate) -> ClientBuilder {
let settings = mimic::impersonate(impersonate, false, ImpersonateOs::default());
let settings = mimic::impersonate(impersonate, ImpersonateOs::default(), false);
self.apply_impersonate_settings(settings)
}

/// Sets the necessary values to mimic the specified impersonate version, with os and skipping header configuration.
/// Configures the client to mimic the specified impersonate version with a specific OS, skipping header configuration.
/// This includes setting the necessary TLS settings and OS.
///
/// # Arguments
///
/// * `impersonate` - The impersonate version to mimic.
/// * `impersonate_os` - The OS to mimic.
///
/// # Returns
///
/// A `ClientBuilder` instance with the applied settings.
#[inline]
pub fn impersonate_with_os_skip_headers(
self,
impersonate: Impersonate,
impersonate_os: ImpersonateOs,
) -> ClientBuilder {
let settings = mimic::impersonate(impersonate, false, impersonate_os);
let settings = mimic::impersonate(impersonate, impersonate_os, false);
self.apply_impersonate_settings(settings)
}

Expand All @@ -935,24 +980,12 @@ impl ClientBuilder {

/// Apply the given TLS settings and header function.
fn apply_impersonate_settings(mut self, mut settings: ImpersonateSettings) -> ClientBuilder {
// Set the headers if needed
if let Some(mut headers) = settings.headers {
std::mem::swap(&mut self.config.headers, &mut headers);
}

// Set the headers order if needed
std::mem::swap(&mut self.config.headers_order, &mut settings.headers_order);

// Set the TLS settings
std::mem::swap(&mut self.config.tls, &mut settings.tls);

// Set the http2 preference
if let Some(http2) = settings.http2 {
self.config
.builder
.with_http2_builder(|builder| apply_http2_settings(builder, http2));
}

std::mem::swap(&mut self.config.http2, &mut settings.http2);
self
}

Expand Down Expand Up @@ -1504,21 +1537,20 @@ impl Client {

/// Set the cookie provider for this client.
#[cfg(feature = "cookies")]
pub fn set_cookie_provider<C>(&mut self, cookie_store: Arc<C>) -> &mut Self
pub fn set_cookie_provider<C>(&mut self, cookie_store: Arc<C>) -> &mut Client
where
C: cookie::CookieStore + 'static,
{
std::mem::swap(
&mut self.inner_mut().cookie_store,
&mut Some(cookie_store as _),
);

self
}

/// Set the proxies for this client.
#[inline]
pub fn set_proxies<P>(&mut self, proxies: P) -> &mut Self
pub fn set_proxies<P>(&mut self, proxies: P) -> &mut Client
where
P: Into<Option<Vec<Proxy>>>,
{
Expand All @@ -1533,7 +1565,6 @@ impl Client {
inner.proxies.clear();
}
}

self
}

Expand All @@ -1543,7 +1574,7 @@ impl Client {
///
/// Default is `None`.
#[inline]
pub fn set_local_address<T>(&mut self, addr: T) -> &mut Self
pub fn set_local_address<T>(&mut self, addr: T) -> &mut Client
where
T: Into<Option<IpAddr>>,
{
Expand All @@ -1554,7 +1585,7 @@ impl Client {
/// Set that all sockets are bound to the configured IPv4 or IPv6 address
/// (depending on host's preferences) before connection.
#[inline]
pub fn set_local_addresses<V4, V6>(&mut self, ipv4: V4, ipv6: V6) -> &mut Self
pub fn set_local_addresses<V4, V6>(&mut self, ipv4: V4, ipv6: V6) -> &mut Client
where
V4: Into<Option<Ipv4Addr>>,
V6: Into<Option<Ipv6Addr>>,
Expand All @@ -1566,7 +1597,7 @@ impl Client {
cfg_bindable_device! {
/// Bind to an interface by `SO_BINDTODEVICE`.
#[inline]
pub fn set_interface<T>(&mut self, interface: T) -> &mut Self
pub fn set_interface<T>(&mut self, interface: T) -> &mut Client
where
T: Into<Cow<'static, str>>,
{
Expand All @@ -1576,7 +1607,7 @@ impl Client {
}

/// Set the headers order for this client.
pub fn set_headers_order<T>(&mut self, order: T) -> &mut Self
pub fn set_headers_order<T>(&mut self, order: T) -> &mut Client
where
T: Into<Cow<'static, [HeaderName]>>,
{
Expand All @@ -1585,59 +1616,96 @@ impl Client {
}

/// Set the redirect policy for this client.
pub fn set_redirect(&mut self, mut policy: redirect::Policy) -> &mut Self {
pub fn set_redirect(&mut self, mut policy: redirect::Policy) -> &mut Client {
std::mem::swap(&mut self.inner_mut().redirect, &mut policy);
self
}

/// Set the cross-origin proxy authorization for this client.
pub fn set_redirect_with_proxy_auth(&mut self, enabled: bool) -> &mut Self {
pub fn set_redirect_with_proxy_auth(&mut self, enabled: bool) -> &mut Client {
self.inner_mut().redirect_with_proxy_auth = enabled;
self
}

/// Set the bash url for this client.
pub fn set_base_url<U: IntoUrl>(&mut self, url: U) -> &mut Self {
pub fn set_base_url<U: IntoUrl>(&mut self, url: U) -> &mut Client {
if let Ok(url) = url.into_url() {
std::mem::swap(&mut self.inner_mut().base_url, &mut Some(url));
}

self
}

/// Set the impersonate for this client.
/// Set the impersonate version for this client.
/// This includes setting the necessary headers and TLS settings.
///
/// # Arguments
///
/// * `var` - The impersonate version to set.
///
/// # Returns
///
/// A mutable reference to the `Client` instance with the applied settings.
#[inline]
pub fn set_impersonate(&mut self, var: Impersonate) -> crate::Result<&mut Self> {
let settings = mimic::impersonate(var, true, ImpersonateOs::default());
pub fn set_impersonate(&mut self, var: Impersonate) -> crate::Result<&mut Client> {
let settings = mimic::impersonate(var, ImpersonateOs::default(), true);
self.apply_impersonate_settings(settings)
}

/// Set the impersonate with os for this client.
/// Set the impersonate version with a specific OS for this client.
/// This includes setting the necessary headers, TLS settings, and OS.
///
/// # Arguments
///
/// * `var` - The impersonate version to set.
/// * `os` - The OS to set.
///
/// # Returns
///
/// A mutable reference to the `Client` instance with the applied settings.
#[inline]
pub fn set_impersonate_with_os(
&mut self,
var: Impersonate,
os: ImpersonateOs,
) -> crate::Result<&mut Self> {
let settings = mimic::impersonate(var, true, os);
) -> crate::Result<&mut Client> {
let settings = mimic::impersonate(var, os, true);
self.apply_impersonate_settings(settings)
}

/// Set the impersonate for this client skip setting the headers.
/// Set the impersonate version for this client, skipping header configuration.
/// This includes setting the necessary TLS settings.
///
/// # Arguments
///
/// * `var` - The impersonate version to set.
///
/// # Returns
///
/// A mutable reference to the `Client` instance with the applied settings.
#[inline]
pub fn set_impersonate_skip_headers(&mut self, var: Impersonate) -> crate::Result<&mut Self> {
let settings = mimic::impersonate(var, false, ImpersonateOs::default());
pub fn set_impersonate_skip_headers(&mut self, var: Impersonate) -> crate::Result<&mut Client> {
let settings = mimic::impersonate(var, ImpersonateOs::default(), false);
self.apply_impersonate_settings(settings)
}

/// Set the impersonate for this client skip setting the headers.
/// Set the impersonate version with a specific OS for this client, skipping header configuration.
/// This includes setting the necessary TLS settings and OS.
///
/// # Arguments
///
/// * `var` - The impersonate version to set.
/// * `os` - The OS to set.
///
/// # Returns
///
/// A mutable reference to the `Client` instance with the applied settings.
#[inline]
pub fn set_impersonate_with_os_skip_headers(
&mut self,
var: Impersonate,
os: ImpersonateOs,
) -> crate::Result<&mut Self> {
let settings = mimic::impersonate(var, false, os);
) -> crate::Result<&mut Client> {
let settings = mimic::impersonate(var, os, false);
self.apply_impersonate_settings(settings)
}

Expand All @@ -1647,7 +1715,7 @@ impl Client {
pub fn set_impersonate_settings(
&mut self,
settings: ImpersonateSettings,
) -> crate::Result<&mut Self> {
) -> crate::Result<&mut Client> {
self.apply_impersonate_settings(settings)
}

Expand All @@ -1656,22 +1724,18 @@ impl Client {
fn apply_impersonate_settings(
&mut self,
mut settings: ImpersonateSettings,
) -> crate::Result<&mut Self> {
) -> crate::Result<&mut Client> {
let inner = self.inner_mut();

// Set the headers
if let Some(mut headers) = settings.headers {
std::mem::swap(&mut inner.headers, &mut headers);
}

// Set the headers order if needed
std::mem::swap(&mut inner.headers_order, &mut settings.headers_order);

// Set the connector
let connector = BoringTlsConnector::new(settings.tls)?;
inner.hyper.with_connector(|c| c.set_connector(connector));

// Set the http2 preference
if let Some(http2) = settings.http2 {
inner
.hyper
Expand Down
2 changes: 1 addition & 1 deletion src/mimic/chrome.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ macro_rules! mod_generator {
use super::*;

#[inline(always)]
pub fn settings(with_headers: bool, os_choice: ImpersonateOs) -> ImpersonateSettings {
pub fn settings(os_choice: ImpersonateOs, with_headers: bool) -> ImpersonateSettings {
#[allow(unreachable_patterns)]
match os_choice {
$(
Expand Down
2 changes: 1 addition & 1 deletion src/mimic/firefox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ macro_rules! mod_generator {
use super::*;

#[inline(always)]
pub fn settings(with_headers: bool, os_choice: ImpersonateOs) -> ImpersonateSettings {
pub fn settings(os_choice: ImpersonateOs, with_headers: bool) -> ImpersonateSettings {
#[allow(unreachable_patterns)]
match os_choice {
$(
Expand Down
4 changes: 2 additions & 2 deletions src/mimic/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,13 @@ pub struct ImpersonateSettings {
#[inline]
pub fn impersonate(
ver: Impersonate,
with_headers: bool,
impersonate_os: ImpersonateOs,
with_headers: bool,
) -> ImpersonateSettings {
impersonate_match!(
ver,
with_headers,
impersonate_os,
with_headers,
Chrome100 => v100::settings,
Chrome101 => v101::settings,
Chrome104 => v104::settings,
Expand Down
Loading
Loading