refactor: allow any IO in Response

This commit is contained in:
DarkCat09 2024-08-29 10:51:02 +04:00
parent 35a4672340
commit 8cbeefb713
Signed by: DarkCat09
GPG key ID: BD3CE9B65916CD82
2 changed files with 12 additions and 12 deletions

View file

@ -22,12 +22,14 @@ use builder::ClientBuilder;
use std::sync::Arc;
use tokio::{
io::{AsyncBufReadExt, AsyncReadExt, AsyncWriteExt},
io::{AsyncBufReadExt, AsyncReadExt, AsyncWriteExt, BufReader},
net::TcpStream,
};
use tokio_rustls::{rustls, TlsConnector};
use tokio_rustls::{client::TlsStream, rustls, TlsConnector};
use url::Url;
pub type ThisResponse = Response<BufReader<TlsStream<TcpStream>>>;
pub struct Client {
pub(crate) connector: TlsConnector,
pub(crate) ss_verifier: Option<Arc<dyn SelfsignedCertVerifier>>,
@ -56,7 +58,7 @@ impl Client {
/// - [`InvalidUrl::UserinfoPresent`] is returned when the URL contains userinfo -- `user:password@` --
/// which is forbidden by the Gemini specs.
/// - For the rest, see [`Client::request_with_host`].
pub async fn request(&self, url_str: &str) -> Result<Response, LibError> {
pub async fn request(&self, url_str: &str) -> Result<ThisResponse, LibError> {
let url = Url::parse(url_str).map_err(InvalidUrl::ParseError)?;
// deny non-Gemini requests
if url.scheme() != "gemini" {
@ -93,7 +95,7 @@ impl Client {
url_str: &str,
host: &str,
port: u16,
) -> Result<Response, LibError> {
) -> Result<ThisResponse, LibError> {
let domain = ServerName::try_from(host)
.map_err(|_| InvalidUrl::ConvertError)?
.to_owned();
@ -126,7 +128,7 @@ impl Client {
Status::parse_status(&buf)?
};
let mut stream = tokio::io::BufReader::new(stream);
let mut stream = BufReader::new(stream);
let message = {
let mut result: Vec<u8> = Vec::new();

View file

@ -5,19 +5,17 @@ use crate::{status::Status, LibError, ReplyType};
use bytes::Bytes;
use tokio::io::AsyncReadExt;
type BodyStream = tokio::io::BufReader<tokio_rustls::client::TlsStream<tokio::net::TcpStream>>;
/// Client-side response structure wrapping a [`Status`],
/// a metadata string and a TLS stream
#[derive(Debug)]
pub struct Response {
pub struct Response<IO: AsyncReadExt + Unpin> {
status: Status,
message: String,
stream: BodyStream,
stream: IO,
}
impl Response {
pub fn new(status: Status, message: String, stream: BodyStream) -> Self {
impl<IO: AsyncReadExt + Unpin> Response<IO> {
pub fn new(status: Status, message: String, stream: IO) -> Self {
Response {
status,
message,
@ -82,7 +80,7 @@ impl Response {
/// so calling `.bytes()` after `.stream().read_to_end(…)`
/// (or `.bytes()` twice, or `.text()` after `.bytes()` and vice versa)
/// on the same response will result in empty output.
pub fn stream(&mut self) -> &mut BodyStream {
pub fn stream(&mut self) -> &mut IO {
&mut self.stream
}