refactor: move client I/O into a separate function

This commit is contained in:
DarkCat09 2024-08-29 11:08:38 +04:00
parent 8cbeefb713
commit a77c3d89c8
Signed by: DarkCat09
GPG key ID: BD3CE9B65916CD82

View file

@ -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<IO: AsyncReadExt + AsyncWriteExt + Unpin>(
&self,
url_str: &str,
mut stream: IO,
) -> Result<Response<BufReader<IO>>, LibError> {
// Write URL, then CRLF
stream.write_all(url_str.as_bytes()).await?;
stream.write_all(b"\r\n").await?;