From 11a8eb470ca5621bedcf4e9afc1623deee2674ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Devrim=20=C5=9Eahin?= Date: Tue, 18 Apr 2023 12:15:49 +0300 Subject: [PATCH] make local_addr syscall unconditional --- src/agent.rs | 26 -------------------------- src/response.rs | 13 ++----------- 2 files changed, 2 insertions(+), 37 deletions(-) diff --git a/src/agent.rs b/src/agent.rs index 9a6e724..4ed88ce 100644 --- a/src/agent.rs +++ b/src/agent.rs @@ -78,7 +78,6 @@ pub(crate) struct AgentConfig { pub redirect_auth_headers: RedirectAuthHeaders, pub user_agent: String, pub tls_config: TlsConfig, - pub get_local_addr: bool, } /// Agents keep state between requests. @@ -263,7 +262,6 @@ impl AgentBuilder { redirect_auth_headers: RedirectAuthHeaders::Never, user_agent: format!("ureq/{}", env!("CARGO_PKG_VERSION")), tls_config: TlsConfig(crate::default_tls_config()), - get_local_addr: false, }, max_idle_connections: DEFAULT_MAX_IDLE_CONNECTIONS, max_idle_connections_per_host: DEFAULT_MAX_IDLE_CONNECTIONS_PER_HOST, @@ -622,30 +620,6 @@ impl AgentBuilder { 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. /// /// This is useful in two cases. First when there is a need to persist cookies diff --git a/src/response.rs b/src/response.rs index bc32907..2cea7d7 100644 --- a/src/response.rs +++ b/src/response.rs @@ -78,8 +78,7 @@ pub struct Response { pub(crate) reader: Box, /// The socket address of the server that sent the response. pub(crate) remote_addr: SocketAddr, - /// Local address request was sent from. Only set if necessary since - /// it is an extra system call. + /// The socket address of the client that sent the request. pub(crate) local_addr: Option, /// The redirect history of this response, if any. The history starts with /// the first response received and ends with the response immediately @@ -236,8 +235,6 @@ impl Response { } /// 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 { self.local_addr } @@ -544,13 +541,7 @@ impl Response { pub(crate) fn do_from_stream(stream: Stream, unit: Unit) -> Result { let remote_addr = stream.remote_addr; - // Only read local_addr if configured to do so, since it's another syscall. - let mut local_addr = None; - if unit.agent.config.get_local_addr { - if let Some(socket) = stream.socket() { - local_addr = Some(socket.local_addr()?); - } - } + let local_addr = stream.socket().map(|s| s.local_addr().unwrap()); // // HTTP/1.1 200 OK\r\n