From 49f15713d6d6337139dbd730cdffa0df25120546 Mon Sep 17 00:00:00 2001 From: Felix Wiedemann Date: Tue, 14 Apr 2020 19:50:24 +0200 Subject: [PATCH] Move empty host handling to the DB specific code --- sqlx-core/src/io/tls.rs | 9 ++++----- sqlx-core/src/mysql/stream.rs | 4 +++- sqlx-core/src/mysql/tls.rs | 5 ++++- sqlx-core/src/postgres/stream.rs | 4 +++- sqlx-core/src/postgres/tls.rs | 3 ++- sqlx-core/src/url.rs | 11 ++++------- 6 files changed, 20 insertions(+), 16 deletions(-) diff --git a/sqlx-core/src/io/tls.rs b/sqlx-core/src/io/tls.rs index 0e5f136f..a81c4f7d 100644 --- a/sqlx-core/src/io/tls.rs +++ b/sqlx-core/src/io/tls.rs @@ -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 { - let conn = TcpStream::connect((url.host(), url.port(default_port))).await?; + pub async fn connect(host: &str, port: u16) -> crate::Result { + 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(()) } diff --git a/sqlx-core/src/mysql/stream.rs b/sqlx-core/src/mysql/stream.rs index a5b38de9..5e0b4d13 100644 --- a/sqlx-core/src/mysql/stream.rs +++ b/sqlx-core/src/mysql/stream.rs @@ -34,7 +34,9 @@ pub(crate) struct MySqlStream { impl MySqlStream { pub(super) async fn new(url: &Url) -> crate::Result { - 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 diff --git a/sqlx-core/src/mysql/tls.rs b/sqlx-core/src/mysql/tls.rs index d5cf3fbc..49a21849 100644 --- a/sqlx-core/src/mysql/tls.rs +++ b/sqlx-core/src/mysql/tls.rs @@ -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 } diff --git a/sqlx-core/src/postgres/stream.rs b/sqlx-core/src/postgres/stream.rs index a47de9ac..6beb5ee2 100644 --- a/sqlx-core/src/postgres/stream.rs +++ b/sqlx-core/src/postgres/stream.rs @@ -23,7 +23,9 @@ pub struct PgStream { impl PgStream { pub(super) async fn new(url: &Url) -> crate::Result { - 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, diff --git a/sqlx-core/src/postgres/tls.rs b/sqlx-core/src/postgres/tls.rs index fd2f8b27..a9590125 100644 --- a/sqlx-core/src/postgres/tls.rs +++ b/sqlx-core/src/postgres/tls.rs @@ -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) } diff --git a/sqlx-core/src/url.rs b/sqlx-core/src/url.rs index fc6383c6..ace27fdc 100644 --- a/sqlx-core/src/url.rs +++ b/sqlx-core/src/url.rs @@ -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), } }