refactor: allow any IO in Response
This commit is contained in:
parent
35a4672340
commit
8cbeefb713
2 changed files with 12 additions and 12 deletions
|
@ -22,12 +22,14 @@ use builder::ClientBuilder;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
use tokio::{
|
use tokio::{
|
||||||
io::{AsyncBufReadExt, AsyncReadExt, AsyncWriteExt},
|
io::{AsyncBufReadExt, AsyncReadExt, AsyncWriteExt, BufReader},
|
||||||
net::TcpStream,
|
net::TcpStream,
|
||||||
};
|
};
|
||||||
use tokio_rustls::{rustls, TlsConnector};
|
use tokio_rustls::{client::TlsStream, rustls, TlsConnector};
|
||||||
use url::Url;
|
use url::Url;
|
||||||
|
|
||||||
|
pub type ThisResponse = Response<BufReader<TlsStream<TcpStream>>>;
|
||||||
|
|
||||||
pub struct Client {
|
pub struct Client {
|
||||||
pub(crate) connector: TlsConnector,
|
pub(crate) connector: TlsConnector,
|
||||||
pub(crate) ss_verifier: Option<Arc<dyn SelfsignedCertVerifier>>,
|
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@` --
|
/// - [`InvalidUrl::UserinfoPresent`] is returned when the URL contains userinfo -- `user:password@` --
|
||||||
/// which is forbidden by the Gemini specs.
|
/// which is forbidden by the Gemini specs.
|
||||||
/// - For the rest, see [`Client::request_with_host`].
|
/// - 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)?;
|
let url = Url::parse(url_str).map_err(InvalidUrl::ParseError)?;
|
||||||
// deny non-Gemini requests
|
// deny non-Gemini requests
|
||||||
if url.scheme() != "gemini" {
|
if url.scheme() != "gemini" {
|
||||||
|
@ -93,7 +95,7 @@ impl Client {
|
||||||
url_str: &str,
|
url_str: &str,
|
||||||
host: &str,
|
host: &str,
|
||||||
port: u16,
|
port: u16,
|
||||||
) -> Result<Response, LibError> {
|
) -> Result<ThisResponse, LibError> {
|
||||||
let domain = ServerName::try_from(host)
|
let domain = ServerName::try_from(host)
|
||||||
.map_err(|_| InvalidUrl::ConvertError)?
|
.map_err(|_| InvalidUrl::ConvertError)?
|
||||||
.to_owned();
|
.to_owned();
|
||||||
|
@ -126,7 +128,7 @@ impl Client {
|
||||||
Status::parse_status(&buf)?
|
Status::parse_status(&buf)?
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut stream = tokio::io::BufReader::new(stream);
|
let mut stream = BufReader::new(stream);
|
||||||
|
|
||||||
let message = {
|
let message = {
|
||||||
let mut result: Vec<u8> = Vec::new();
|
let mut result: Vec<u8> = Vec::new();
|
||||||
|
|
|
@ -5,19 +5,17 @@ use crate::{status::Status, LibError, ReplyType};
|
||||||
use bytes::Bytes;
|
use bytes::Bytes;
|
||||||
use tokio::io::AsyncReadExt;
|
use tokio::io::AsyncReadExt;
|
||||||
|
|
||||||
type BodyStream = tokio::io::BufReader<tokio_rustls::client::TlsStream<tokio::net::TcpStream>>;
|
|
||||||
|
|
||||||
/// Client-side response structure wrapping a [`Status`],
|
/// Client-side response structure wrapping a [`Status`],
|
||||||
/// a metadata string and a TLS stream
|
/// a metadata string and a TLS stream
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct Response {
|
pub struct Response<IO: AsyncReadExt + Unpin> {
|
||||||
status: Status,
|
status: Status,
|
||||||
message: String,
|
message: String,
|
||||||
stream: BodyStream,
|
stream: IO,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Response {
|
impl<IO: AsyncReadExt + Unpin> Response<IO> {
|
||||||
pub fn new(status: Status, message: String, stream: BodyStream) -> Self {
|
pub fn new(status: Status, message: String, stream: IO) -> Self {
|
||||||
Response {
|
Response {
|
||||||
status,
|
status,
|
||||||
message,
|
message,
|
||||||
|
@ -82,7 +80,7 @@ impl Response {
|
||||||
/// so calling `.bytes()` after `.stream().read_to_end(…)`
|
/// so calling `.bytes()` after `.stream().read_to_end(…)`
|
||||||
/// (or `.bytes()` twice, or `.text()` after `.bytes()` and vice versa)
|
/// (or `.bytes()` twice, or `.text()` after `.bytes()` and vice versa)
|
||||||
/// on the same response will result in empty output.
|
/// 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
|
&mut self.stream
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue