Skip to main content

wasi_hyperium/
hyperium1.rs

1mod incoming;
2mod outgoing;
3mod send;
4mod service;
5
6pub use incoming::{incoming_request, incoming_response};
7pub use outgoing::{outgoing_request, outgoing_response, Hyperium1OutgoingBodyCopier};
8pub use send::{block_on_outbound_request, send_outbound_request};
9pub use service::handle_service_call;
10
11use crate::wasi::{FieldEntries, Method, Scheme};
12
13impl TryFrom<Method> for http1::Method {
14    type Error = http1::Error;
15
16    fn try_from(method: Method) -> Result<Self, Self::Error> {
17        Ok(match method {
18            Method::Get => Self::GET,
19            Method::Head => Self::HEAD,
20            Method::Post => Self::POST,
21            Method::Put => Self::PUT,
22            Method::Delete => Self::DELETE,
23            Method::Connect => Self::CONNECT,
24            Method::Options => Self::OPTIONS,
25            Method::Trace => Self::TRACE,
26            Method::Patch => Self::PATCH,
27            Method::Other(other) => other.parse()?,
28        })
29    }
30}
31
32impl From<&http1::Method> for Method {
33    fn from(method: &http1::Method) -> Self {
34        match method {
35            &http1::Method::GET => Self::Get,
36            &http1::Method::HEAD => Self::Head,
37            &http1::Method::POST => Self::Post,
38            &http1::Method::PUT => Self::Put,
39            &http1::Method::DELETE => Self::Delete,
40            &http1::Method::CONNECT => Self::Connect,
41            &http1::Method::OPTIONS => Self::Options,
42            &http1::Method::TRACE => Self::Trace,
43            &http1::Method::PATCH => Self::Patch,
44            other => Self::Other(other.to_string()),
45        }
46    }
47}
48
49impl TryFrom<Scheme> for http1::uri::Scheme {
50    type Error = http1::Error;
51
52    fn try_from(scheme: Scheme) -> Result<Self, Self::Error> {
53        Ok(match scheme {
54            Scheme::Http => Self::HTTP,
55            Scheme::Https => Self::HTTPS,
56            Scheme::Other(other) => other.parse()?,
57        })
58    }
59}
60
61impl From<&http1::uri::Scheme> for Scheme {
62    fn from(scheme: &http1::uri::Scheme) -> Self {
63        if scheme == &http1::uri::Scheme::HTTP {
64            Self::Http
65        } else if scheme == &http1::uri::Scheme::HTTPS {
66            Self::Https
67        } else {
68            Self::Other(scheme.to_string())
69        }
70    }
71}
72
73impl TryFrom<FieldEntries> for http1::HeaderMap {
74    type Error = http1::Error;
75
76    fn try_from(entries: FieldEntries) -> Result<Self, Self::Error> {
77        entries
78            .into_iter()
79            .map(|(name, val)| Ok((name.try_into()?, val.try_into()?)))
80            .collect()
81    }
82}
83
84impl From<http1::HeaderMap> for FieldEntries {
85    fn from(map: http1::HeaderMap) -> Self {
86        (&map).into()
87    }
88}
89
90impl From<&http1::HeaderMap> for FieldEntries {
91    fn from(map: &http1::HeaderMap) -> Self {
92        map.iter()
93            .map(|(name, val)| (name.to_string(), val.as_bytes().to_vec()))
94            .collect::<Vec<_>>()
95            .into()
96    }
97}