Move empty host handling to the DB specific code

This commit is contained in:
Felix Wiedemann 2020-04-14 19:50:24 +02:00 committed by Ryan Leckey
parent f1c1d9ae07
commit 49f15713d6
6 changed files with 20 additions and 16 deletions

View File

@ -4,7 +4,6 @@ use std::pin::Pin;
use std::task::{Context, Poll};
use crate::runtime::{AsyncRead, AsyncWrite, TcpStream};
use crate::url::Url;
use self::Inner::*;
@ -21,8 +20,8 @@ enum Inner {
}
impl MaybeTlsStream {
pub async fn connect(url: &Url, default_port: u16) -> crate::Result<Self> {
let conn = TcpStream::connect((url.host(), url.port(default_port))).await?;
pub async fn connect(host: &str, port: u16) -> crate::Result<Self> {
let conn = TcpStream::connect((host, port)).await?;
Ok(Self {
inner: Inner::NotTls(conn),
})
@ -43,7 +42,7 @@ impl MaybeTlsStream {
#[cfg_attr(docsrs, doc(cfg(feature = "tls")))]
pub async fn upgrade(
&mut self,
url: &Url,
host: &str,
connector: async_native_tls::TlsConnector,
) -> crate::Result<()> {
let conn = match std::mem::replace(&mut self.inner, Upgrading) {
@ -52,7 +51,7 @@ impl MaybeTlsStream {
Upgrading => return Err(tls_err!("connection already failed to upgrade").into()),
};
self.inner = Tls(connector.connect(url.host(), conn).await?);
self.inner = Tls(connector.connect(host, conn).await?);
Ok(())
}

View File

@ -34,7 +34,9 @@ pub(crate) struct MySqlStream {
impl MySqlStream {
pub(super) async fn new(url: &Url) -> crate::Result<Self> {
let stream = MaybeTlsStream::connect(&url, 3306).await?;
let host = url.host().unwrap_or("localhost");
let port = url.port(3306);
let stream = MaybeTlsStream::connect(host, port).await?;
let mut capabilities = Capabilities::PROTOCOL_41
| Capabilities::IGNORE_SPACE

View File

@ -116,5 +116,8 @@ async fn try_upgrade(
)
.await?;
stream.stream.upgrade(url, connector).await
stream
.stream
.upgrade(url.host().unwrap_or("localhost"), connector)
.await
}

View File

@ -23,7 +23,9 @@ pub struct PgStream {
impl PgStream {
pub(super) async fn new(url: &Url) -> crate::Result<Self> {
let stream = MaybeTlsStream::connect(&url, 5432).await?;
let host = url.host().unwrap_or("localhost");
let port = url.port(5432);
let stream = MaybeTlsStream::connect(host, port).await?;
Ok(Self {
notifications: None,

View File

@ -105,7 +105,8 @@ async fn try_upgrade(
}
}
stream.stream.upgrade(url, connector).await?;
let host = url.host().unwrap_or("localhost");
stream.stream.upgrade(host, connector).await?;
Ok(true)
}

View File

@ -34,13 +34,10 @@ impl Url {
self.0.as_str()
}
pub fn host(&self) -> &str {
let host = self.0.host_str();
match host {
Some(host) if !host.is_empty() => host,
_ => "localhost",
pub fn host(&self) -> Option<&str> {
match self.0.host_str()? {
"" => None,
host => Some(host),
}
}