make local_addr syscall unconditional

This commit is contained in:
Devrim Şahin
2023-04-18 12:15:49 +03:00
committed by Martin Algesten
parent 7e32dbaaaf
commit 11a8eb470c
2 changed files with 2 additions and 37 deletions

View File

@@ -78,7 +78,6 @@ pub(crate) struct AgentConfig {
pub redirect_auth_headers: RedirectAuthHeaders, pub redirect_auth_headers: RedirectAuthHeaders,
pub user_agent: String, pub user_agent: String,
pub tls_config: TlsConfig, pub tls_config: TlsConfig,
pub get_local_addr: bool,
} }
/// Agents keep state between requests. /// Agents keep state between requests.
@@ -263,7 +262,6 @@ impl AgentBuilder {
redirect_auth_headers: RedirectAuthHeaders::Never, redirect_auth_headers: RedirectAuthHeaders::Never,
user_agent: format!("ureq/{}", env!("CARGO_PKG_VERSION")), user_agent: format!("ureq/{}", env!("CARGO_PKG_VERSION")),
tls_config: TlsConfig(crate::default_tls_config()), tls_config: TlsConfig(crate::default_tls_config()),
get_local_addr: false,
}, },
max_idle_connections: DEFAULT_MAX_IDLE_CONNECTIONS, max_idle_connections: DEFAULT_MAX_IDLE_CONNECTIONS,
max_idle_connections_per_host: DEFAULT_MAX_IDLE_CONNECTIONS_PER_HOST, max_idle_connections_per_host: DEFAULT_MAX_IDLE_CONNECTIONS_PER_HOST,
@@ -622,30 +620,6 @@ impl AgentBuilder {
self self
} }
/// Whether local_addr will be read from the tcp socket.
///
/// local_addr is the address from which the client sends the request.
/// Reading it invokes a syscall, therefore it is disabled by default.
///
/// Defaults to false.
///
/// ```
/// # fn main() -> Result<(), ureq::Error> {
/// # use std::net::SocketAddr;
/// # ureq::is_test(true);
/// let agent = ureq::builder()
/// .get_local_addr(true)
/// .build();
/// let response = agent.get("http://httpbin.org/get").call()?;
/// assert!(response.local_addr().is_some());
/// # Ok(())
/// # }
/// ```
pub fn get_local_addr(mut self, get_local_addr: bool) -> Self {
self.config.get_local_addr = get_local_addr;
self
}
/// Provide the cookie store to be used for all requests using this agent. /// Provide the cookie store to be used for all requests using this agent.
/// ///
/// This is useful in two cases. First when there is a need to persist cookies /// This is useful in two cases. First when there is a need to persist cookies

View File

@@ -78,8 +78,7 @@ pub struct Response {
pub(crate) reader: Box<dyn Read + Send + Sync + 'static>, pub(crate) reader: Box<dyn Read + Send + Sync + 'static>,
/// The socket address of the server that sent the response. /// The socket address of the server that sent the response.
pub(crate) remote_addr: SocketAddr, pub(crate) remote_addr: SocketAddr,
/// Local address request was sent from. Only set if necessary since /// The socket address of the client that sent the request.
/// it is an extra system call.
pub(crate) local_addr: Option<SocketAddr>, pub(crate) local_addr: Option<SocketAddr>,
/// The redirect history of this response, if any. The history starts with /// The redirect history of this response, if any. The history starts with
/// the first response received and ends with the response immediately /// the first response received and ends with the response immediately
@@ -236,8 +235,6 @@ impl Response {
} }
/// The local address the request was made from. /// The local address the request was made from.
///
/// This is only available if [`AgentBuilder::get_local_addr()`][crate::AgentBuilder] is set to true.
pub fn local_addr(&self) -> Option<SocketAddr> { pub fn local_addr(&self) -> Option<SocketAddr> {
self.local_addr self.local_addr
} }
@@ -544,13 +541,7 @@ impl Response {
pub(crate) fn do_from_stream(stream: Stream, unit: Unit) -> Result<Response, Error> { pub(crate) fn do_from_stream(stream: Stream, unit: Unit) -> Result<Response, Error> {
let remote_addr = stream.remote_addr; let remote_addr = stream.remote_addr;
// Only read local_addr if configured to do so, since it's another syscall. let local_addr = stream.socket().map(|s| s.local_addr().unwrap());
let mut local_addr = None;
if unit.agent.config.get_local_addr {
if let Some(socket) = stream.socket() {
local_addr = Some(socket.local_addr()?);
}
}
// //
// HTTP/1.1 200 OK\r\n // HTTP/1.1 200 OK\r\n