postgres: Add support for postgres:///?host=... connection strings

This commit is contained in:
Felix Wiedemann 2020-04-14 20:51:17 +02:00 committed by Ryan Leckey
parent fc78f15ebf
commit a2673f7880
2 changed files with 8 additions and 2 deletions

View file

@ -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);

View file

@ -69,6 +69,7 @@ async fn try_upgrade(
accept_invalid_host_names: bool,
) -> crate::Result<bool> {
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)
}