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

Add Response::remote_addr() #489

Merged
merged 5 commits into from
Oct 3, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
10 changes: 10 additions & 0 deletions src/response.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::io::{self, Read};
use std::net::SocketAddr;
use std::str::FromStr;
use std::sync::Mutex;
use std::{fmt, io::BufRead};
Expand Down Expand Up @@ -70,6 +71,8 @@ pub struct Response {
unit: Box<Unit>,
// Boxed to avoid taking up too much size.
stream: Mutex<Box<Stream>>,
/// The socket address of the server that sent the response.
remote_addr: SocketAddr,
/// The redirect history of this response, if any. The history starts with
/// the first response received and ends with the response immediately
/// previous to this one.
Expand Down Expand Up @@ -224,6 +227,11 @@ impl Response {
charset_from_content_type(self.header("content-type"))
}

/// The socket address of the server that sent the response.
pub fn remote_addr(&self) -> SocketAddr {
self.remote_addr
}

/// Turn this response into a `impl Read` of the body.
///
/// 1. If `Transfer-Encoding: chunked`, the returned reader will unchunk it
Expand Down Expand Up @@ -470,6 +478,7 @@ impl Response {
///
/// assert_eq!(resp.status(), 401);
pub(crate) fn do_from_stream(stream: Stream, unit: Unit) -> Result<Response, Error> {
let remote_addr = stream.remote_addr;
//
// HTTP/1.1 200 OK\r\n
let mut stream = stream::DeadlineStream::new(stream, unit.deadline);
Expand Down Expand Up @@ -515,6 +524,7 @@ impl Response {
headers,
unit: Box::new(unit),
stream: Mutex::new(Box::new(stream.into())),
remote_addr,
history: vec![],
length,
compression,
Expand Down
43 changes: 26 additions & 17 deletions src/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ pub trait TlsConnector: Send + Sync {

pub(crate) struct Stream {
inner: BufReader<Box<dyn ReadWrite>>,
/// The remote address the stream is connected to.
pub(crate) remote_addr: SocketAddr,
}

impl<T: ReadWrite + ?Sized> ReadWrite for Box<T> {
Expand Down Expand Up @@ -197,9 +199,10 @@ impl fmt::Debug for Stream {
}

impl Stream {
fn new(t: impl ReadWrite) -> Stream {
fn new(t: impl ReadWrite, remote_addr: SocketAddr) -> Stream {
Stream::logged_create(Stream {
inner: BufReader::new(Box::new(t)),
remote_addr,
})
}

Expand All @@ -215,19 +218,15 @@ impl Stream {
vec![],
false,
))),
remote_addr: remote_addr_for_test(),
})
}

#[cfg(test)]
pub(crate) fn from_vec_poolable(v: Vec<u8>) -> Stream {
Stream::logged_create(Stream {
inner: BufReader::new(Box::new(TestStream(Box::new(Cursor::new(v)), vec![], true))),
})
}

fn from_tcp_stream(t: TcpStream) -> Stream {
Stream::logged_create(Stream {
inner: BufReader::new(Box::new(t)),
remote_addr: remote_addr_for_test(),
})
}

Expand Down Expand Up @@ -348,20 +347,25 @@ pub(crate) fn connect_http(unit: &Unit, hostname: &str) -> Result<Stream, Error>
//
let port = unit.url.port().unwrap_or(80);

connect_host(unit, hostname, port).map(Stream::from_tcp_stream)
connect_host(unit, hostname, port).map(|(t, r)| Stream::new(t, r))
}

pub(crate) fn connect_https(unit: &Unit, hostname: &str) -> Result<Stream, Error> {
let port = unit.url.port().unwrap_or(443);

let sock = connect_host(unit, hostname, port)?;
let (sock, remote_addr) = connect_host(unit, hostname, port)?;

let tls_conf = &unit.agent.config.tls_config;
let https_stream = tls_conf.connect(hostname, Box::new(sock))?;
Ok(Stream::new(https_stream))
Ok(Stream::new(https_stream, remote_addr))
}

pub(crate) fn connect_host(unit: &Unit, hostname: &str, port: u16) -> Result<TcpStream, Error> {
/// If successful, returns a `TcpStream` and the remote address it is connected to.
pub(crate) fn connect_host(
unit: &Unit,
hostname: &str,
port: u16,
) -> Result<(TcpStream, SocketAddr), Error> {
let connect_deadline: Option<Instant> =
if let Some(timeout_connect) = unit.agent.config.timeout_connect {
Instant::now().checked_add(timeout_connect)
Expand All @@ -388,7 +392,7 @@ pub(crate) fn connect_host(unit: &Unit, hostname: &str, port: u16) -> Result<Tcp
let proto = proxy.as_ref().map(|proxy| proxy.proto);

let mut any_err = None;
let mut any_stream = None;
let mut any_stream_and_addr = None;
// Find the first sock_addr that accepts a connection
for sock_addr in sock_addrs {
// ensure connect timeout or overall timeout aren't yet hit.
Expand Down Expand Up @@ -417,15 +421,15 @@ pub(crate) fn connect_host(unit: &Unit, hostname: &str, port: u16) -> Result<Tcp
};

if let Ok(stream) = stream {
any_stream = Some(stream);
any_stream_and_addr = Some((stream, sock_addr));
break;
} else if let Err(err) = stream {
any_err = Some(err);
}
}

let mut stream = if let Some(stream) = any_stream {
stream
let (mut stream, remote_addr) = if let Some(stream_and_addr) = any_stream_and_addr {
stream_and_addr
} else if let Some(e) = any_err {
return Err(ErrorKind::ConnectionFailed.msg("Connect error").src(e));
} else {
Expand Down Expand Up @@ -466,7 +470,7 @@ pub(crate) fn connect_host(unit: &Unit, hostname: &str, port: u16) -> Result<Tcp
}
}

Ok(stream)
Ok((stream, remote_addr))
}

#[cfg(feature = "socks-proxy")]
Expand Down Expand Up @@ -652,6 +656,11 @@ pub(crate) fn connect_test(unit: &Unit) -> Result<Stream, Error> {
Err(ErrorKind::UnknownScheme.msg(format!("unknown scheme '{}'", unit.url.scheme())))
}

pub(crate) fn remote_addr_for_test() -> SocketAddr {
use std::net::{Ipv4Addr, SocketAddrV4};
SocketAddrV4::new(Ipv4Addr::new(0, 0, 0, 0), 0).into()
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down Expand Up @@ -707,7 +716,7 @@ mod tests {
let recorder = ReadRecorder {
reads: reads.clone(),
};
let stream = Stream::new(recorder);
let stream = Stream::new(recorder, remote_addr_for_test());
let mut deadline_stream = DeadlineStream::new(stream, None);
let mut buf = [0u8; 1];
for _ in 0..8193 {
Expand Down