rename to connectinfo

This commit is contained in:
Rob Ede 2021-11-29 20:58:50 +00:00
parent b26115d7c5
commit 3891745a6e
No known key found for this signature in database
GPG Key ID: 97C636207D3EF933
10 changed files with 57 additions and 61 deletions

View File

@ -16,7 +16,7 @@
* Rename `accept::native_tls::{NativeTlsAcceptorService => AcceptorService}`. [#422]
* Rename `connect::{Address => Host}` trait. [#422]
* Rename method `connect::Connection::{host => hostname}`. [#422]
* Rename struct `connect::{Connect => ConnectionInfo}`. [#422]
* Rename struct `connect::{Connect => ConnectInfo}`. [#422]
* Rename struct `connect::{ConnectService => ConnectorService}`. [#422]
* Rename struct `connect::{ConnectServiceFactory => Connector}`. [#422]
* Rename TLS acceptor service future types and hide from docs. [#422]

30
actix-tls/src/connect/connector.rs Executable file → Normal file
View File

@ -13,7 +13,7 @@ use super::{
error::ConnectError,
resolver::{Resolver, ResolverService},
tcp::{TcpConnector, TcpConnectorService},
Connection, ConnectionInfo, Host,
ConnectInfo, Connection, Host,
};
/// Combined resolver and TCP connector service factory.
@ -40,7 +40,7 @@ impl Connector {
}
}
impl<R: Host> ServiceFactory<ConnectionInfo<R>> for Connector {
impl<R: Host> ServiceFactory<ConnectInfo<R>> for Connector {
type Response = Connection<R, TcpStream>;
type Error = ConnectError;
type Config = ();
@ -63,43 +63,43 @@ pub struct ConnectorService {
resolver: ResolverService,
}
impl<R: Host> Service<ConnectionInfo<R>> for ConnectorService {
impl<R: Host> Service<ConnectInfo<R>> for ConnectorService {
type Response = Connection<R, TcpStream>;
type Error = ConnectError;
type Future = ConnectServiceResponse<R>;
actix_service::always_ready!();
fn call(&self, req: ConnectionInfo<R>) -> Self::Future {
fn call(&self, req: ConnectInfo<R>) -> Self::Future {
ConnectServiceResponse {
fut: ConnectFuture::Resolve(self.resolver.call(req)),
fut: ConnectFut::Resolve(self.resolver.call(req)),
tcp: self.tcp,
}
}
}
// helper enum to generic over futures of resolve and connect phase.
pub(crate) enum ConnectFuture<R: Host> {
Resolve(<ResolverService as Service<ConnectionInfo<R>>>::Future),
Connect(<TcpConnectorService as Service<ConnectionInfo<R>>>::Future),
/// Helper enum to generic over futures of resolve and connect steps.
pub(crate) enum ConnectFut<R: Host> {
Resolve(<ResolverService as Service<ConnectInfo<R>>>::Future),
Connect(<TcpConnectorService as Service<ConnectInfo<R>>>::Future),
}
/// Helper enum to contain the future output of `ConnectFuture`.
pub(crate) enum ConnectOutput<R: Host> {
Resolved(ConnectionInfo<R>),
Resolved(ConnectInfo<R>),
Connected(Connection<R, TcpStream>),
}
impl<R: Host> ConnectFuture<R> {
impl<R: Host> ConnectFut<R> {
fn poll_connect(
&mut self,
cx: &mut Context<'_>,
) -> Poll<Result<ConnectOutput<R>, ConnectError>> {
match self {
ConnectFuture::Resolve(ref mut fut) => {
ConnectFut::Resolve(ref mut fut) => {
Pin::new(fut).poll(cx).map_ok(ConnectOutput::Resolved)
}
ConnectFuture::Connect(ref mut fut) => {
ConnectFut::Connect(ref mut fut) => {
Pin::new(fut).poll(cx).map_ok(ConnectOutput::Connected)
}
}
@ -107,7 +107,7 @@ impl<R: Host> ConnectFuture<R> {
}
pub struct ConnectServiceResponse<R: Host> {
fut: ConnectFuture<R>,
fut: ConnectFut<R>,
tcp: TcpConnectorService,
}
@ -118,7 +118,7 @@ impl<R: Host> Future for ConnectServiceResponse<R> {
loop {
match ready!(self.fut.poll_connect(cx))? {
ConnectOutput::Resolved(res) => {
self.fut = ConnectFuture::Connect(self.tcp.call(res));
self.fut = ConnectFut::Connect(self.tcp.call(res));
}
ConnectOutput::Connected(res) => return Poll::Ready(Ok(res)),
}

28
actix-tls/src/connect/info.rs Executable file → Normal file
View File

@ -17,19 +17,19 @@ use super::{
///
/// May contain known/pre-resolved socket address(es) or a host that needs resolving with DNS.
#[derive(Debug, PartialEq, Eq, Hash)]
pub struct ConnectionInfo<R> {
pub struct ConnectInfo<R> {
pub(crate) request: R,
pub(crate) port: u16,
pub(crate) addr: ConnectAddrs,
pub(crate) local_addr: Option<IpAddr>,
}
impl<R: Host> ConnectionInfo<R> {
impl<R: Host> ConnectInfo<R> {
/// Constructs new connection info using a request.
pub fn new(request: R) -> ConnectionInfo<R> {
pub fn new(request: R) -> ConnectInfo<R> {
let port = request.port();
ConnectionInfo {
ConnectInfo {
request,
port: port.unwrap_or(0),
addr: ConnectAddrs::None,
@ -41,8 +41,8 @@ impl<R: Host> ConnectionInfo<R> {
///
/// Since socket address is known, [`Connector`](super::Connector) will skip the DNS
/// resolution step.
pub fn with_addr(request: R, addr: SocketAddr) -> ConnectionInfo<R> {
ConnectionInfo {
pub fn with_addr(request: R, addr: SocketAddr) -> ConnectInfo<R> {
ConnectInfo {
request,
port: 0,
addr: ConnectAddrs::One(addr),
@ -165,13 +165,13 @@ impl<R: Host> ConnectionInfo<R> {
}
}
impl<R: Host> From<R> for ConnectionInfo<R> {
impl<R: Host> From<R> for ConnectInfo<R> {
fn from(addr: R) -> Self {
ConnectionInfo::new(addr)
ConnectInfo::new(addr)
}
}
impl<R: Host> fmt::Display for ConnectionInfo<R> {
impl<R: Host> fmt::Display for ConnectInfo<R> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}:{}", self.hostname(), self.port())
}
@ -217,7 +217,7 @@ mod tests {
#[test]
fn test_local_addr() {
let conn = ConnectionInfo::new("hello").set_local_addr([127, 0, 0, 1]);
let conn = ConnectInfo::new("hello").set_local_addr([127, 0, 0, 1]);
assert_eq!(
conn.local_addr.unwrap(),
IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1))
@ -226,7 +226,7 @@ mod tests {
#[test]
fn request_ref() {
let conn = ConnectionInfo::new("hello");
let conn = ConnectInfo::new("hello");
assert_eq!(conn.request(), &"hello")
}
@ -234,15 +234,15 @@ mod tests {
fn set_connect_addr_into_option() {
let addr = SocketAddr::from(([127, 0, 0, 1], 4242));
let conn = ConnectionInfo::new("hello").set_addr(None);
let conn = ConnectInfo::new("hello").set_addr(None);
let mut addrs = conn.addrs();
assert!(addrs.next().is_none());
let conn = ConnectionInfo::new("hello").set_addr(addr);
let conn = ConnectInfo::new("hello").set_addr(addr);
let mut addrs = conn.addrs();
assert_eq!(addrs.next().unwrap(), addr);
let conn = ConnectionInfo::new("hello").set_addr(Some(addr));
let conn = ConnectInfo::new("hello").set_addr(Some(addr));
let mut addrs = conn.addrs();
assert_eq!(addrs.next().unwrap(), addr);
}

View File

@ -41,6 +41,6 @@ pub use self::connection::Connection;
pub use self::connector::{Connector, ConnectorService};
pub use self::error::ConnectError;
pub use self::host::Host;
pub use self::info::ConnectionInfo;
pub use self::info::ConnectInfo;
pub use self::resolve::Resolve;
pub use self::resolver::{Resolver, ResolverService};

0
actix-tls/src/connect/openssl.rs Executable file → Normal file
View File

24
actix-tls/src/connect/resolver.rs Executable file → Normal file
View File

@ -14,7 +14,7 @@ use actix_utils::future::{ok, Ready};
use futures_core::{future::LocalBoxFuture, ready};
use log::trace;
use super::{ConnectError, ConnectionInfo, Host, Resolve};
use super::{ConnectError, ConnectInfo, Host, Resolve};
/// DNS resolver service factory.
#[derive(Clone, Default)]
@ -36,8 +36,8 @@ impl Resolver {
}
}
impl<R: Host> ServiceFactory<ConnectionInfo<R>> for Resolver {
type Response = ConnectionInfo<R>;
impl<R: Host> ServiceFactory<ConnectInfo<R>> for Resolver {
type Response = ConnectInfo<R>;
type Error = ConnectError;
type Config = ();
type Service = ResolverService;
@ -81,9 +81,7 @@ impl ResolverService {
}
/// Resolve DNS with default resolver.
fn look_up<R: Host>(
req: &ConnectionInfo<R>,
) -> JoinHandle<io::Result<IntoIter<SocketAddr>>> {
fn look_up<R: Host>(req: &ConnectInfo<R>) -> JoinHandle<io::Result<IntoIter<SocketAddr>>> {
let host = req.hostname();
// TODO: Connect should always return host(name?) with port if possible; basically try to
// reduce ability to create conflicting lookup info by having port in host string being
@ -109,14 +107,14 @@ impl ResolverService {
}
}
impl<R: Host> Service<ConnectionInfo<R>> for ResolverService {
type Response = ConnectionInfo<R>;
impl<R: Host> Service<ConnectInfo<R>> for ResolverService {
type Response = ConnectInfo<R>;
type Error = ConnectError;
type Future = ResolverFut<R>;
actix_service::always_ready!();
fn call(&self, req: ConnectionInfo<R>) -> Self::Future {
fn call(&self, req: ConnectInfo<R>) -> Self::Future {
if req.addr.is_some() {
ResolverFut::Connected(Some(req))
} else if let Ok(ip) = req.hostname().parse() {
@ -157,16 +155,16 @@ impl<R: Host> Service<ConnectionInfo<R>> for ResolverService {
/// Future for resolver service.
pub enum ResolverFut<R: Host> {
Connected(Option<ConnectionInfo<R>>),
Connected(Option<ConnectInfo<R>>),
LookUp(
JoinHandle<io::Result<IntoIter<SocketAddr>>>,
Option<ConnectionInfo<R>>,
Option<ConnectInfo<R>>,
),
LookupCustom(LocalBoxFuture<'static, Result<ConnectionInfo<R>, ConnectError>>),
LookupCustom(LocalBoxFuture<'static, Result<ConnectInfo<R>, ConnectError>>),
}
impl<R: Host> Future for ResolverFut<R> {
type Output = Result<ConnectionInfo<R>, ConnectError>;
type Output = Result<ConnectInfo<R>, ConnectError>;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
match self.get_mut() {

0
actix-tls/src/connect/rustls.rs Executable file → Normal file
View File

14
actix-tls/src/connect/tcp.rs Executable file → Normal file
View File

@ -18,9 +18,7 @@ use futures_core::ready;
use log::{error, trace};
use tokio_util::sync::ReusableBoxFuture;
use super::{
connect_addrs::ConnectAddrs, error::ConnectError, Connection, ConnectionInfo, Host,
};
use super::{connect_addrs::ConnectAddrs, error::ConnectError, ConnectInfo, Connection, Host};
/// TCP connector service factory.
#[derive(Debug, Copy, Clone)]
@ -33,7 +31,7 @@ impl TcpConnector {
}
}
impl<R: Host> ServiceFactory<ConnectionInfo<R>> for TcpConnector {
impl<R: Host> ServiceFactory<ConnectInfo<R>> for TcpConnector {
type Response = Connection<R, TcpStream>;
type Error = ConnectError;
type Config = ();
@ -50,17 +48,17 @@ impl<R: Host> ServiceFactory<ConnectionInfo<R>> for TcpConnector {
#[derive(Debug, Copy, Clone)]
pub struct TcpConnectorService;
impl<R: Host> Service<ConnectionInfo<R>> for TcpConnectorService {
impl<R: Host> Service<ConnectInfo<R>> for TcpConnectorService {
type Response = Connection<R, TcpStream>;
type Error = ConnectError;
type Future = TcpConnectorFut<R>;
actix_service::always_ready!();
fn call(&self, req: ConnectionInfo<R>) -> Self::Future {
fn call(&self, req: ConnectInfo<R>) -> Self::Future {
let port = req.port();
let ConnectionInfo {
let ConnectInfo {
request: req,
addr,
local_addr,
@ -178,7 +176,7 @@ impl<R: Host> Future for TcpConnectorFut<R> {
}
async fn connect(addr: SocketAddr, local_addr: Option<IpAddr>) -> io::Result<TcpStream> {
// use local addr if connect asks for it.
// use local addr if connect asks for it
match local_addr {
Some(ip_addr) => {
let socket = match ip_addr {

12
actix-tls/tests/test_connect.rs Executable file → Normal file
View File

@ -12,7 +12,7 @@ use actix_service::{fn_service, Service, ServiceFactory};
use bytes::Bytes;
use futures_util::sink::SinkExt;
use actix_tls::connect::{ConnectError, Connection, ConnectionInfo, Connector, Host};
use actix_tls::connect::{ConnectError, ConnectInfo, Connection, Connector, Host};
#[cfg(feature = "openssl")]
#[actix_rt::test]
@ -58,12 +58,12 @@ async fn test_static_str() {
})
});
let info = ConnectionInfo::with_addr("10", srv.addr());
let info = ConnectInfo::with_addr("10", srv.addr());
let connector = Connector::default().service();
let conn = connector.call(info).await.unwrap();
assert_eq!(conn.peer_addr().unwrap(), srv.addr());
let info = ConnectionInfo::new(srv.host().to_owned());
let info = ConnectInfo::new(srv.host().to_owned());
let connector = Connector::default().service();
let conn = connector.call(info).await;
assert!(conn.is_err());
@ -72,7 +72,7 @@ async fn test_static_str() {
#[actix_rt::test]
async fn service_factory() {
pub fn default_connector_factory<T: Host + 'static>() -> impl ServiceFactory<
ConnectionInfo<T>,
ConnectInfo<T>,
Config = (),
Response = Connection<T, TcpStream>,
Error = ConnectError,
@ -89,7 +89,7 @@ async fn service_factory() {
})
});
let info = ConnectionInfo::with_addr("10", srv.addr());
let info = ConnectInfo::with_addr("10", srv.addr());
let factory = default_connector_factory();
let connector = factory.new_service(()).await.unwrap();
let con = connector.call(info).await;
@ -148,7 +148,7 @@ async fn test_local_addr() {
let local = IpAddr::V4(Ipv4Addr::new(127, 0, 0, 3));
let (con, _) = conn
.call(ConnectionInfo::with_addr("10", srv.addr()).set_local_addr(local))
.call(ConnectInfo::with_addr("10", srv.addr()).set_local_addr(local))
.await
.unwrap()
.into_parts();

View File

@ -11,7 +11,7 @@ use actix_service::{fn_service, Service, ServiceFactory};
use futures_core::future::LocalBoxFuture;
use actix_tls::connect::{
ConnectError, Connection, ConnectionInfo, Connector, Host, Resolve, Resolver,
ConnectError, ConnectInfo, Connection, Connector, Host, Resolve, Resolver,
};
#[actix_rt::test]
@ -41,7 +41,7 @@ async fn custom_resolver_connect() {
pub fn connector_factory<T: Host + 'static>(
resolver: Resolver,
) -> impl ServiceFactory<
ConnectionInfo<T>,
ConnectInfo<T>,
Config = (),
Response = Connection<T, TcpStream>,
Error = ConnectError,
@ -86,7 +86,7 @@ async fn custom_resolver_connect() {
let conn = factory.new_service(()).await.unwrap();
let con = conn
.call(ConnectionInfo::with_addr("example.com", srv.addr()))
.call(ConnectInfo::with_addr("example.com", srv.addr()))
.await
.unwrap();
assert_eq!(con.peer_addr().unwrap(), srv.addr());