pub mod bindings;
mod body;
mod conv;
mod helpers;
mod host;
mod proxy;
mod request;
mod response;
pub use request::Request;
pub use response::Response;
pub const DEFAULT_OUTGOING_BODY_CHUNK_SIZE: usize = 1024 * 1024;
use crate::{FieldMapError, WasiHttp, WasiHttpView};
use bindings::http::{client, types};
use core::ops::Deref;
use std::sync::Arc;
use wasmtime::component::Linker;
use wasmtime_wasi::TrappableError;
pub(crate) type HttpResult<T> = Result<T, HttpError>;
pub(crate) type HttpError = TrappableError<types::ErrorCode>;
pub(crate) type HeaderResult<T> = Result<T, HeaderError>;
pub(crate) type HeaderError = TrappableError<types::HeaderError>;
impl From<FieldMapError> for HeaderError {
fn from(e: FieldMapError) -> Self {
match e {
FieldMapError::Immutable => types::HeaderError::Immutable.into(),
FieldMapError::InvalidHeaderName | FieldMapError::InvalidHeaderValue => {
types::HeaderError::InvalidSyntax.into()
}
FieldMapError::TooManyFields | FieldMapError::TotalSizeTooBig => {
types::HeaderError::SizeExceeded.into()
}
FieldMapError::Forbidden => types::HeaderError::Forbidden.into(),
}
}
}
pub(crate) type RequestOptionsResult<T> = Result<T, RequestOptionsError>;
pub(crate) type RequestOptionsError = TrappableError<types::RequestOptionsError>;
pub fn add_to_linker<T>(linker: &mut Linker<T>) -> wasmtime::Result<()>
where
T: WasiHttpView + 'static,
{
client::add_to_linker::<_, WasiHttp>(linker, T::http)?;
types::add_to_linker::<_, WasiHttp>(linker, T::http)?;
Ok(())
}
pub enum MaybeMutable<T> {
Mutable(Arc<T>),
Immutable(Arc<T>),
}
impl<T> From<MaybeMutable<T>> for Arc<T> {
fn from(v: MaybeMutable<T>) -> Self {
v.into_arc()
}
}
impl<T> Deref for MaybeMutable<T> {
type Target = Arc<T>;
fn deref(&self) -> &Self::Target {
match self {
Self::Mutable(v) | Self::Immutable(v) => v,
}
}
}
impl<T> MaybeMutable<T> {
pub fn new_mutable(v: impl Into<Arc<T>>) -> Self {
Self::Mutable(v.into())
}
pub fn new_mutable_default() -> Self
where
T: Default,
{
Self::new_mutable(T::default())
}
pub fn new_immutable(v: impl Into<Arc<T>>) -> Self {
Self::Immutable(v.into())
}
pub fn into_arc(self) -> Arc<T> {
match self {
Self::Mutable(v) | Self::Immutable(v) => v,
}
}
pub fn get_mut(&mut self) -> Option<&mut T>
where
T: Clone,
{
match self {
Self::Mutable(v) => Some(Arc::make_mut(v)),
Self::Immutable(..) => None,
}
}
}