The client module is irrelevant
This commit is contained in:
parent
aeb94687b5
commit
1f10156ebb
|
@ -1,94 +0,0 @@
|
|||
use futures_util::stream::StreamExt;
|
||||
use magnetar_activity_pub::Resolver;
|
||||
use magnetar_core::web_model::content_type::ContentActivityStreams;
|
||||
use reqwest::Client;
|
||||
use serde::Deserialize;
|
||||
use serde_json::Value;
|
||||
use thiserror::Error;
|
||||
use url::Url;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct DereferencingClient {
|
||||
pub client: Client,
|
||||
pub body_limit: usize,
|
||||
}
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
pub enum DereferencingClientBuilderError {
|
||||
#[error("Reqwest error: {0}")]
|
||||
ReqwestError(#[from] reqwest::Error),
|
||||
}
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
pub enum DereferencingClientError {
|
||||
#[error("Reqwest error: {0}")]
|
||||
ReqwestError(#[from] reqwest::Error),
|
||||
#[error("JSON error: {0}")]
|
||||
JsonError(#[from] serde_json::Error),
|
||||
#[error("Body limit exceeded error")]
|
||||
BodyLimitExceededError,
|
||||
#[error("Invalid URL: {0}")]
|
||||
InvalidUrl(#[from] url::ParseError),
|
||||
#[error("Client error: {0}")]
|
||||
Other(String),
|
||||
}
|
||||
|
||||
impl DereferencingClient {
|
||||
pub fn new(
|
||||
force_https: bool,
|
||||
body_limit: usize,
|
||||
) -> Result<DereferencingClient, reqwest::Error> {
|
||||
let client = Client::builder().https_only(force_https).build()?;
|
||||
|
||||
Ok(DereferencingClient { client, body_limit })
|
||||
}
|
||||
|
||||
pub async fn dereference(&self, url: Url) -> Result<Value, DereferencingClientError> {
|
||||
let mut headers = reqwest::header::HeaderMap::new();
|
||||
|
||||
headers.insert(
|
||||
reqwest::header::ACCEPT,
|
||||
reqwest::header::HeaderValue::from_static(ContentActivityStreams.as_ref()),
|
||||
);
|
||||
|
||||
let response = self
|
||||
.client
|
||||
.get(url)
|
||||
.headers(headers)
|
||||
.send()
|
||||
.await
|
||||
.map_err(DereferencingClientError::ReqwestError)?;
|
||||
|
||||
let mut body = response.bytes_stream();
|
||||
|
||||
let mut data = Vec::with_capacity(4096);
|
||||
|
||||
while let Some(buf) = body.next().await {
|
||||
let chunk = buf.map_err(DereferencingClientError::ReqwestError)?;
|
||||
|
||||
if data.len() + chunk.len() > self.body_limit {
|
||||
return Err(DereferencingClientError::BodyLimitExceededError);
|
||||
}
|
||||
|
||||
data.extend_from_slice(&chunk);
|
||||
}
|
||||
|
||||
let json =
|
||||
serde_json::from_slice::<Value>(&data).map_err(DereferencingClientError::JsonError)?;
|
||||
|
||||
Ok(json)
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl Resolver for DereferencingClient {
|
||||
type Error = DereferencingClientError;
|
||||
|
||||
async fn resolve<T: for<'a> Deserialize<'a>>(&self, id: &str) -> Result<T, Self::Error> {
|
||||
let url = id.parse().map_err(DereferencingClientError::InvalidUrl)?;
|
||||
|
||||
let json = self.dereference(url).await?;
|
||||
|
||||
serde_json::from_value(json).map_err(DereferencingClientError::JsonError)
|
||||
}
|
||||
}
|
|
@ -1,72 +0,0 @@
|
|||
use axum::http::{HeaderMap, Method};
|
||||
use hyper::client::HttpConnector;
|
||||
use hyper::http::uri;
|
||||
use hyper::{header, Body, Client, Request, Response, Uri};
|
||||
use thiserror::Error;
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
pub enum ProxyClientError {
|
||||
#[error("Client error: {0}")]
|
||||
ClientError(String),
|
||||
#[error("URL error: {0}")]
|
||||
UriError(#[from] uri::InvalidUri),
|
||||
#[error("HTTP error: {0}")]
|
||||
HttpError(#[from] axum::http::Error),
|
||||
#[error("Hyper error: {0}")]
|
||||
HyperError(#[from] hyper::Error),
|
||||
}
|
||||
|
||||
pub struct ProxyClient {
|
||||
pub client: Client<HttpConnector, Body>,
|
||||
pub upstream: String,
|
||||
}
|
||||
|
||||
impl ProxyClient {
|
||||
pub fn new(upstream: String) -> ProxyClient {
|
||||
let client = Client::builder().set_host(false).build_http();
|
||||
|
||||
ProxyClient { client, upstream }
|
||||
}
|
||||
|
||||
pub async fn send(
|
||||
&self,
|
||||
host: &str,
|
||||
path: &str,
|
||||
method: Method,
|
||||
body: Body,
|
||||
headers_in: &HeaderMap,
|
||||
) -> Result<Response<Body>, ProxyClientError> {
|
||||
let mut builder = Request::builder();
|
||||
|
||||
let Some(headers) = builder.headers_mut() else {
|
||||
return Err(ProxyClientError::ClientError("No headers".to_owned()));
|
||||
};
|
||||
|
||||
*headers = headers_in.clone();
|
||||
|
||||
headers.insert(
|
||||
header::HOST,
|
||||
host.parse().map_err(|e| {
|
||||
ProxyClientError::ClientError(format!("Invalid header value: {e:?}"))
|
||||
})?,
|
||||
);
|
||||
|
||||
let uri = format!("{}/{}", self.upstream, path)
|
||||
.parse::<Uri>()
|
||||
.map_err(ProxyClientError::UriError)?;
|
||||
|
||||
let request = builder
|
||||
.method(method)
|
||||
.uri(uri)
|
||||
.body(body)
|
||||
.map_err(ProxyClientError::HttpError)?;
|
||||
|
||||
let response = self
|
||||
.client
|
||||
.request(request)
|
||||
.await
|
||||
.map_err(ProxyClientError::HyperError)?;
|
||||
|
||||
Ok(response)
|
||||
}
|
||||
}
|
|
@ -1,2 +0,0 @@
|
|||
pub mod dereferencing_client;
|
||||
pub mod forwarding_client;
|
Loading…
Reference in New Issue