From a77c3d89c8bb96b909f4711f81fc64dadc8a383c Mon Sep 17 00:00:00 2001 From: DarkCat09 Date: Thu, 29 Aug 2024 11:08:38 +0400 Subject: [PATCH] refactor: move client I/O into a separate function --- src/client/mod.rs | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/client/mod.rs b/src/client/mod.rs index a498789..32d1f75 100644 --- a/src/client/mod.rs +++ b/src/client/mod.rs @@ -85,7 +85,7 @@ impl Client { /// - [`std::io::Error`] is returned in nearly all cases: /// could not open a TCP connection, perform a TLS handshake, /// write to or read from the TCP stream. - /// Check the inner error if you need to determine what exactly happened + /// Check the inner error if you need to determine what exactly happened. /// - [`LibError::StatusOutOfRange`] means that a server returned an incorrect status code /// (less than 10 or greater than 69). /// - [`LibError::DataNotUtf8`] is returned when metadata (the text after a status code) @@ -100,9 +100,12 @@ impl Client { .map_err(|_| InvalidUrl::ConvertError)? .to_owned(); + // TCP connection let stream = self.try_connect(host, port).await?; - let mut stream = self.connector.connect(domain, stream).await?; + // TLS connection via tokio-rustls + let stream = self.connector.connect(domain, stream).await?; + // certificate verification if let Some(ssv) = &self.ss_verifier { let cert = stream .get_ref() @@ -117,6 +120,14 @@ impl Client { } } + self.perform_io(url_str, stream).await + } + + pub(crate) async fn perform_io( + &self, + url_str: &str, + mut stream: IO, + ) -> Result>, LibError> { // Write URL, then CRLF stream.write_all(url_str.as_bytes()).await?; stream.write_all(b"\r\n").await?;