refactor: move client I/O into a separate function
This commit is contained in:
parent
8cbeefb713
commit
a77c3d89c8
1 changed files with 13 additions and 2 deletions
|
@ -85,7 +85,7 @@ impl Client {
|
||||||
/// - [`std::io::Error`] is returned in nearly all cases:
|
/// - [`std::io::Error`] is returned in nearly all cases:
|
||||||
/// could not open a TCP connection, perform a TLS handshake,
|
/// could not open a TCP connection, perform a TLS handshake,
|
||||||
/// write to or read from the TCP stream.
|
/// 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
|
/// - [`LibError::StatusOutOfRange`] means that a server returned an incorrect status code
|
||||||
/// (less than 10 or greater than 69).
|
/// (less than 10 or greater than 69).
|
||||||
/// - [`LibError::DataNotUtf8`] is returned when metadata (the text after a status code)
|
/// - [`LibError::DataNotUtf8`] is returned when metadata (the text after a status code)
|
||||||
|
@ -100,9 +100,12 @@ impl Client {
|
||||||
.map_err(|_| InvalidUrl::ConvertError)?
|
.map_err(|_| InvalidUrl::ConvertError)?
|
||||||
.to_owned();
|
.to_owned();
|
||||||
|
|
||||||
|
// TCP connection
|
||||||
let stream = self.try_connect(host, port).await?;
|
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 {
|
if let Some(ssv) = &self.ss_verifier {
|
||||||
let cert = stream
|
let cert = stream
|
||||||
.get_ref()
|
.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
|
// Write URL, then CRLF
|
||||||
stream.write_all(url_str.as_bytes()).await?;
|
stream.write_all(url_str.as_bytes()).await?;
|
||||||
stream.write_all(b"\r\n").await?;
|
stream.write_all(b"\r\n").await?;
|
||||||
|
|
Loading…
Reference in a new issue