From a2673f788034574829c146a93c39cdb0f043bb8e Mon Sep 17 00:00:00 2001 From: Felix Wiedemann Date: Tue, 14 Apr 2020 20:51:17 +0200 Subject: [PATCH] postgres: Add support for `postgres:///?host=...` connection strings --- sqlx-core/src/postgres/stream.rs | 1 + sqlx-core/src/postgres/tls.rs | 9 +++++++-- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/sqlx-core/src/postgres/stream.rs b/sqlx-core/src/postgres/stream.rs index 3cf2a0bb..332ee75e 100644 --- a/sqlx-core/src/postgres/stream.rs +++ b/sqlx-core/src/postgres/stream.rs @@ -33,6 +33,7 @@ impl PgStream { .decode_utf8() .expect("percent-encoded hostname contained non-UTF-8 bytes") }) + .or_else(|| url.param("host")) .unwrap_or("/var/run/postgresql".into()); if host.starts_with("/") { let path = format!("{}/.s.PGSQL.{}", host, port); diff --git a/sqlx-core/src/postgres/tls.rs b/sqlx-core/src/postgres/tls.rs index a9590125..5b2c0d4c 100644 --- a/sqlx-core/src/postgres/tls.rs +++ b/sqlx-core/src/postgres/tls.rs @@ -69,6 +69,7 @@ async fn try_upgrade( accept_invalid_host_names: bool, ) -> crate::Result { use async_native_tls::TlsConnector; + use std::borrow::Cow; stream.write(crate::postgres::protocol::SslRequest); stream.flush().await?; @@ -105,8 +106,12 @@ async fn try_upgrade( } } - let host = url.host().unwrap_or("localhost"); - stream.stream.upgrade(host, connector).await?; + let host = url + .host() + .map(Cow::Borrowed) + .or_else(|| url.param("host")) + .unwrap_or("localhost".into()); + stream.stream.upgrade(&host, connector).await?; Ok(true) }