Previously, ReadWrite had methods `is_poolable` and `written_bytes`, which were solely for the use of unittests. This replaces `written_bytes` and `TestStream` with a `struct Recorder` that implements `ReadWrite` and allows unittests to access its recorded bytes via an `Arc<Mutex<Vec<u8>>>`. It eliminates `is_poolable`; it's fine to pool a Stream of any kind. The new `Recorder` also has some convenience methods that abstract away boilerplate code from many of our unittests. I got rid of `Stream::from_vec` and `Stream::from_vec_poolable` because they depended on `TestStream`. They've been replaced by `NoopStream` for the pool.rs tests, and `ReadOnlyStream` for constructing `Response`s from `&str` and some test cases.
35 lines
1.1 KiB
Rust
35 lines
1.1 KiB
Rust
use crate::error::Error;
|
|
use crate::error::ErrorKind;
|
|
use crate::stream::{ReadWrite, TlsConnector};
|
|
|
|
use std::net::TcpStream;
|
|
use std::sync::Arc;
|
|
|
|
#[allow(dead_code)]
|
|
pub(crate) fn default_tls_config() -> std::sync::Arc<dyn TlsConnector> {
|
|
Arc::new(native_tls::TlsConnector::new().unwrap())
|
|
}
|
|
|
|
impl TlsConnector for native_tls::TlsConnector {
|
|
fn connect(&self, dns_name: &str, io: Box<dyn ReadWrite>) -> Result<Box<dyn ReadWrite>, Error> {
|
|
let stream =
|
|
native_tls::TlsConnector::connect(self, dns_name, io).map_err(|e| match e {
|
|
native_tls::HandshakeError::Failure(e) => ErrorKind::ConnectionFailed
|
|
.msg("native_tls connect failed")
|
|
.src(e),
|
|
native_tls::HandshakeError::WouldBlock(_) => {
|
|
ErrorKind::Io.msg("Unexpected native_tls::HandshakeError::WouldBlock")
|
|
}
|
|
})?;
|
|
|
|
Ok(Box::new(stream))
|
|
}
|
|
}
|
|
|
|
#[cfg(feature = "native-tls")]
|
|
impl ReadWrite for native_tls::TlsStream<Box<dyn ReadWrite>> {
|
|
fn socket(&self) -> Option<&TcpStream> {
|
|
self.get_ref().socket()
|
|
}
|
|
}
|