Path: blob/main/crates/wasi-http/src/p3/conv.rs
3050 views
use crate::p3::bindings::http::types::{ErrorCode, Method, Scheme};1use core::convert::Infallible;2use core::error::Error as _;3use tracing::warn;45impl From<Infallible> for ErrorCode {6fn from(x: Infallible) -> Self {7match x {}8}9}1011impl ErrorCode {12/// Translate a [`hyper::Error`] to a wasi-http [ErrorCode] in the context of a request.13pub fn from_hyper_request_error(err: hyper::Error) -> Self {14// If there's a source, we might be able to extract a wasi-http error from it.15if let Some(cause) = err.source() {16if let Some(err) = cause.downcast_ref::<Self>() {17return err.clone();18}19}2021warn!("hyper request error: {err:?}");2223Self::HttpProtocolError24}2526/// Translate a [`hyper::Error`] to a wasi-http [ErrorCode] in the context of a response.27#[cfg(feature = "default-send-request")]28pub(crate) fn from_hyper_response_error(err: hyper::Error) -> Self {29if err.is_timeout() {30return ErrorCode::HttpResponseTimeout;31}3233// If there's a source, we might be able to extract a wasi-http error from it.34if let Some(cause) = err.source() {35if let Some(err) = cause.downcast_ref::<Self>() {36return err.clone();37}38}3940warn!("hyper response error: {err:?}");4142ErrorCode::HttpProtocolError43}44}4546impl From<http::Method> for Method {47fn from(method: http::Method) -> Self {48Self::from(&method)49}50}5152impl From<&http::Method> for Method {53fn from(method: &http::Method) -> Self {54if method == http::Method::GET {55Self::Get56} else if method == http::Method::HEAD {57Self::Head58} else if method == http::Method::POST {59Self::Post60} else if method == http::Method::PUT {61Self::Put62} else if method == http::Method::DELETE {63Self::Delete64} else if method == http::Method::CONNECT {65Self::Connect66} else if method == http::Method::OPTIONS {67Self::Options68} else if method == http::Method::TRACE {69Self::Trace70} else if method == http::Method::PATCH {71Self::Patch72} else {73Self::Other(method.as_str().into())74}75}76}7778impl TryFrom<Method> for http::Method {79type Error = http::method::InvalidMethod;8081fn try_from(method: Method) -> Result<Self, Self::Error> {82Self::try_from(&method)83}84}8586impl TryFrom<&Method> for http::Method {87type Error = http::method::InvalidMethod;8889fn try_from(method: &Method) -> Result<Self, Self::Error> {90match method {91Method::Get => Ok(Self::GET),92Method::Head => Ok(Self::HEAD),93Method::Post => Ok(Self::POST),94Method::Put => Ok(Self::PUT),95Method::Delete => Ok(Self::DELETE),96Method::Connect => Ok(Self::CONNECT),97Method::Options => Ok(Self::OPTIONS),98Method::Trace => Ok(Self::TRACE),99Method::Patch => Ok(Self::PATCH),100Method::Other(s) => s.parse(),101}102}103}104105impl From<http::uri::Scheme> for Scheme {106fn from(scheme: http::uri::Scheme) -> Self {107Self::from(&scheme)108}109}110111impl From<&http::uri::Scheme> for Scheme {112fn from(scheme: &http::uri::Scheme) -> Self {113if *scheme == http::uri::Scheme::HTTP {114Self::Http115} else if *scheme == http::uri::Scheme::HTTPS {116Self::Https117} else {118Self::Other(scheme.as_str().into())119}120}121}122123impl TryFrom<Scheme> for http::uri::Scheme {124type Error = http::uri::InvalidUri;125126fn try_from(scheme: Scheme) -> Result<Self, Self::Error> {127Self::try_from(&scheme)128}129}130131impl TryFrom<&Scheme> for http::uri::Scheme {132type Error = http::uri::InvalidUri;133134fn try_from(scheme: &Scheme) -> Result<Self, Self::Error> {135match scheme {136Scheme::Http => Ok(Self::HTTP),137Scheme::Https => Ok(Self::HTTPS),138Scheme::Other(s) => s.parse(),139}140}141}142143144