Skip to content

Commit

Permalink
Do not pass state in Config
Browse files Browse the repository at this point in the history
  • Loading branch information
algesten committed Feb 12, 2025
1 parent baf3f8d commit 21e3705
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 36 deletions.
7 changes: 1 addition & 6 deletions src/agent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,12 +245,7 @@ impl Agent {
}

pub(crate) fn new_request_level_config(&self) -> RequestLevelConfig {
let mut config = self.config.as_ref().clone();

// Set flag indicating this is request level.
config.request_level = true;

RequestLevelConfig(config)
RequestLevelConfig(self.config.as_ref().clone())
}

/// Make a GET request using this agent.
Expand Down
9 changes: 0 additions & 9 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,13 +159,6 @@ pub struct Config {

// Chain built for middleware.
pub(crate) middleware: MiddlewareChain,

// Techically not config, but here to pass as argument from
// RequestBuilder::force_send_body() to run()
pub(crate) force_send_body: bool,

// If this config instance is request level.
pub(crate) request_level: bool,
}

impl Config {
Expand Down Expand Up @@ -852,8 +845,6 @@ impl Default for Config {
max_idle_connections_per_host: 3,
max_idle_age: Duration::from_secs(15),
middleware: MiddlewareChain::default(),
force_send_body: false,
request_level: false,
}
}
}
Expand Down
10 changes: 6 additions & 4 deletions src/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -402,10 +402,9 @@ impl RequestBuilder<WithoutBody> {
/// # Ok::<_, ureq::Error>(())
/// ```
pub fn force_send_body(mut self) -> RequestBuilder<WithBody> {
// This is how we communicate to run() that we want to disable
// the method-body-compliance check.
let config = self.request_level_config();
config.force_send_body = true;
if let Some(exts) = self.extensions_mut() {
exts.insert(ForceSendBody);
}

RequestBuilder {
agent: self.agent,
Expand All @@ -417,6 +416,9 @@ impl RequestBuilder<WithoutBody> {
}
}

#[derive(Debug, Clone)]
pub(crate) struct ForceSendBody;

impl RequestBuilder<WithBody> {
pub(crate) fn new<T>(agent: Agent, method: Method, uri: T) -> Self
where
Expand Down
18 changes: 12 additions & 6 deletions src/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use crate::body::ResponseInfo;
use crate::config::{Config, RequestLevelConfig, DEFAULT_USER_AGENT};
use crate::http;
use crate::pool::Connection;
use crate::request::ForceSendBody;
use crate::response::{RedirectHistory, ResponseUri};
use crate::timings::{CallTimings, CurrentTime};
use crate::transport::time::{Duration, Instant};
Expand All @@ -33,12 +34,13 @@ pub(crate) fn run(
let mut redirect_count = 0;

// Configuration on the request level overrides the agent level.
let config = request
let (config, request_level) = request
.extensions_mut()
.remove::<RequestLevelConfig>()
.map(|rl| rl.0)
.map(Arc::new)
.unwrap_or_else(|| agent.config.clone());
.map(|rl| (Arc::new(rl.0), true))
.unwrap_or_else(|| (agent.config.clone(), false));

let force_send_body = request.extensions_mut().remove::<ForceSendBody>().is_some();

let mut redirect_history: Option<Vec<Uri>> =
config.save_redirect_history().then_some(Vec::new());
Expand All @@ -49,7 +51,7 @@ pub(crate) fn run(

let mut flow = Flow::new(request)?;

if config.force_send_body {
if force_send_body {
flow.send_body_despite_method();
}

Expand All @@ -66,6 +68,7 @@ pub(crate) fn run(
match flow_run(
agent,
&config,
request_level,
flow,
&mut body,
redirect_count,
Expand Down Expand Up @@ -112,6 +115,7 @@ pub(crate) fn run(
fn flow_run(
agent: &Agent,
config: &Config,
request_level: bool,
mut flow: Flow<Prepare>,
body: &mut SendBody,
redirect_count: u32,
Expand All @@ -127,7 +131,7 @@ fn flow_run(

add_headers(&mut flow, agent, config, body, &uri)?;

let mut connection = connect(agent, config, &uri, timings)?;
let mut connection = connect(agent, config, request_level, &uri, timings)?;

let mut flow = flow.proceed();

Expand Down Expand Up @@ -336,6 +340,7 @@ fn add_headers(
fn connect(
agent: &Agent,
config: &Config,
request_level: bool,
wanted_uri: &Uri,
timings: &mut CallTimings,
) -> Result<Connection, Error> {
Expand Down Expand Up @@ -363,6 +368,7 @@ fn connect(
addrs,
resolver: &*agent.resolver,
config,
request_level,
now: timings.now(),
timeout: timings.next_timeout(Timeout::Connect),
proxied,
Expand Down
12 changes: 7 additions & 5 deletions src/tls/native_tls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use std::fmt;
use std::io::{Read, Write};
use std::sync::{Arc, OnceLock};

use crate::config::Config;
use crate::tls::{RootCerts, TlsProvider};
use crate::{transport::*, Error};
use der::pem::LineEnding;
Expand Down Expand Up @@ -52,7 +51,7 @@ impl<In: Transport> Connector<In> for NativeTlsConnector {

trace!("Try wrap TLS");

let connector = self.get_cached_native_tls_connector(details.config)?;
let connector = self.get_cached_native_tls_connector(details)?;

let domain = details
.uri
Expand All @@ -78,10 +77,13 @@ impl<In: Transport> Connector<In> for NativeTlsConnector {
}

impl NativeTlsConnector {
fn get_cached_native_tls_connector(&self, config: &Config) -> Result<Arc<TlsConnector>, Error> {
let tls_config = config.tls_config();
fn get_cached_native_tls_connector(
&self,
details: &ConnectionDetails,
) -> Result<Arc<TlsConnector>, Error> {
let tls_config = details.config.tls_config();

let connector = if config.request_level {
let connector = if details.request_level {
// If the TlsConfig is request level, it is not allowed to
// initialize the self.config OnceLock, but it should
// reuse the cached value if it is the same TlsConfig
Expand Down
9 changes: 4 additions & 5 deletions src/tls/rustls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use rustls::{ClientConfig, ClientConnection, RootCertStore, StreamOwned, ALL_VER
use rustls_pki_types::{CertificateDer, PrivateKeyDer, PrivatePkcs1KeyDer, PrivatePkcs8KeyDer};
use rustls_pki_types::{PrivateSec1KeyDer, ServerName};

use crate::config::Config;
use crate::tls::cert::KeyKind;
use crate::tls::{RootCerts, TlsProvider};
use crate::transport::{Buffers, ConnectionDetails, Connector, LazyBuffers};
Expand Down Expand Up @@ -57,7 +56,7 @@ impl<In: Transport> Connector<In> for RustlsConnector {

trace!("Try wrap in TLS");

let config = self.get_cached_config(details.config);
let config = self.get_cached_config(details);

let name_borrowed: ServerName<'_> = details
.uri
Expand Down Expand Up @@ -92,10 +91,10 @@ impl<In: Transport> Connector<In> for RustlsConnector {
}

impl RustlsConnector {
fn get_cached_config(&self, config: &Config) -> Arc<ClientConfig> {
let tls_config = config.tls_config();
fn get_cached_config(&self, details: &ConnectionDetails) -> Arc<ClientConfig> {
let tls_config = details.config.tls_config();

if config.request_level {
if details.request_level {
// If the TlsConfig is request level, it is not allowed to
// initialize the self.config OnceLock, but it should
// reuse the cached value if it is the same TlsConfig
Expand Down
7 changes: 6 additions & 1 deletion src/unversioned/transport/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,9 +183,14 @@ pub struct ConnectionDetails<'a> {
/// For CONNECT proxy, this is the address of the proxy server.
pub addrs: ResolvedSocketAddrs,

/// The Agent configuration.
/// The configuration.
///
/// Agent or Request level.
pub config: &'a Config,

/// Whether the config is request level.
pub request_level: bool,

/// The resolver configured on [`Agent`](crate::Agent).
///
/// Typically the IP address of the host in the uri is already resolved to the `addr`
Expand Down

0 comments on commit 21e3705

Please sign in to comment.