Skip to content

Commit b575745

Browse files
committed
fix test
1 parent f7a2644 commit b575745

File tree

7 files changed

+26
-24
lines changed

7 files changed

+26
-24
lines changed

tokio-postgres/src/cancel_query.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,10 @@ where
2424
}
2525
};
2626

27-
let tls = config
28-
.hostname
29-
.map(|s| tls.make_tls_connect(&s))
30-
.transpose()
27+
let tls = tls
28+
.make_tls_connect(config.hostname.as_deref().unwrap_or(""))
3129
.map_err(|e| Error::tls(e.into()))?;
30+
let has_hostname = config.hostname.is_some();
3231

3332
let socket = connect_socket::connect_socket(
3433
&config.host,
@@ -39,5 +38,6 @@ where
3938
)
4039
.await?;
4140

42-
cancel_query_raw::cancel_query_raw(socket, ssl_mode, tls, process_id, secret_key).await
41+
cancel_query_raw::cancel_query_raw(socket, ssl_mode, tls, has_hostname, process_id, secret_key)
42+
.await
4343
}

tokio-postgres/src/cancel_query_raw.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,16 @@ use tokio::io::{AsyncRead, AsyncWrite, AsyncWriteExt};
88
pub async fn cancel_query_raw<S, T>(
99
stream: S,
1010
mode: SslMode,
11-
tls: Option<T>,
11+
tls: T,
12+
has_hostname: bool,
1213
process_id: i32,
1314
secret_key: i32,
1415
) -> Result<(), Error>
1516
where
1617
S: AsyncRead + AsyncWrite + Unpin,
1718
T: TlsConnect<S>,
1819
{
19-
let mut stream = connect_tls::connect_tls(stream, mode, tls).await?;
20+
let mut stream = connect_tls::connect_tls(stream, mode, tls, has_hostname).await?;
2021

2122
let mut buf = BytesMut::new();
2223
frontend::cancel_request(process_id, secret_key, &mut buf);

tokio-postgres/src/cancel_token.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ impl CancelToken {
5454
cancel_query_raw::cancel_query_raw(
5555
stream,
5656
self.ssl_mode,
57-
Some(tls),
57+
tls,
58+
true,
5859
self.process_id,
5960
self.secret_key,
6061
)

tokio-postgres/src/config.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -645,7 +645,7 @@ impl Config {
645645
S: AsyncRead + AsyncWrite + Unpin,
646646
T: TlsConnect<S>,
647647
{
648-
connect_raw(stream, Some(tls), self).await
648+
connect_raw(stream, tls, true, self).await
649649
}
650650
}
651651

tokio-postgres/src/connect.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,8 @@ where
5959
Some(Host::Unix(_)) => None,
6060
None => None,
6161
};
62-
let tls = hostname
63-
.as_ref()
64-
.map(|s| tls.make_tls_connect(s))
65-
.transpose()
62+
let tls = tls
63+
.make_tls_connect(hostname.as_deref().unwrap_or(""))
6664
.map_err(|e| Error::tls(e.into()))?;
6765

6866
// Try to use the value of hostaddr to establish the TCP connection,
@@ -92,7 +90,7 @@ async fn connect_once<T>(
9290
host: Host,
9391
hostname: Option<String>,
9492
port: u16,
95-
tls: Option<T>,
93+
tls: T,
9694
config: &Config,
9795
) -> Result<(Client, Connection<Socket, T::Stream>), Error>
9896
where
@@ -110,7 +108,8 @@ where
110108
},
111109
)
112110
.await?;
113-
let (mut client, mut connection) = connect_raw(socket, tls, config).await?;
111+
let has_hostname = hostname.is_some();
112+
let (mut client, mut connection) = connect_raw(socket, tls, has_hostname, config).await?;
114113

115114
if let TargetSessionAttrs::ReadWrite = config.target_session_attrs {
116115
let rows = client.simple_query_raw("SHOW transaction_read_only");

tokio-postgres/src/connect_raw.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,14 +80,15 @@ where
8080

8181
pub async fn connect_raw<S, T>(
8282
stream: S,
83-
tls: Option<T>,
83+
tls: T,
84+
has_hostname: bool,
8485
config: &Config,
8586
) -> Result<(Client, Connection<S, T::Stream>), Error>
8687
where
8788
S: AsyncRead + AsyncWrite + Unpin,
8889
T: TlsConnect<S>,
8990
{
90-
let stream = connect_tls(stream, config.ssl_mode, tls).await?;
91+
let stream = connect_tls(stream, config.ssl_mode, tls, has_hostname).await?;
9192

9293
let mut stream = StartupStream {
9394
inner: Framed::new(stream, PostgresCodec),

tokio-postgres/src/connect_tls.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,16 @@ use tokio::io::{AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt};
1010
pub async fn connect_tls<S, T>(
1111
mut stream: S,
1212
mode: SslMode,
13-
tls: Option<T>,
13+
tls: T,
14+
has_hostname: bool,
1415
) -> Result<MaybeTlsStream<S, T::Stream>, Error>
1516
where
1617
S: AsyncRead + AsyncWrite + Unpin,
1718
T: TlsConnect<S>,
1819
{
1920
match mode {
2021
SslMode::Disable => return Ok(MaybeTlsStream::Raw(stream)),
21-
SslMode::Prefer
22-
if tls
23-
.as_ref()
24-
.map_or(false, |tls| !tls.can_connect(ForcePrivateApi)) =>
25-
{
22+
SslMode::Prefer if !tls.can_connect(ForcePrivateApi) => {
2623
return Ok(MaybeTlsStream::Raw(stream))
2724
}
2825
SslMode::Prefer | SslMode::Require => {}
@@ -43,8 +40,11 @@ where
4340
}
4441
}
4542

43+
if !has_hostname {
44+
return Err(Error::tls("no hostname provided for TLS handshake".into()));
45+
}
46+
4647
let stream = tls
47-
.ok_or_else(|| Error::tls("no hostname provided for TLS handshake".into()))?
4848
.connect(stream)
4949
.await
5050
.map_err(|e| Error::tls(e.into()))?;

0 commit comments

Comments
 (0)