1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
pub use crate::http_impl::WasiHttpViewExt;
pub use crate::types::{WasiHttpCtx, WasiHttpView};
use core::fmt::Formatter;
use std::fmt::{self, Display};

pub mod component_impl;
pub mod http_impl;
pub mod incoming_handler;
pub mod proxy;
pub mod types;
pub mod types_impl;

pub mod bindings {
    #[cfg(feature = "sync")]
    pub mod sync {
        pub(crate) mod _internal {
            wasmtime::component::bindgen!({
                path: "wit",
                interfaces: "
                    import wasi:http/incoming-handler
                    import wasi:http/outgoing-handler
                    import wasi:http/types
                ",
                tracing: true,
                with: {
                    "wasi:io/streams": wasmtime_wasi::preview2::bindings::sync_io::io::streams,
                    "wasi:poll/poll": wasmtime_wasi::preview2::bindings::sync_io::poll::poll,
                }
            });
        }
        pub use self::_internal::wasi::http;
    }

    pub(crate) mod _internal_rest {
        wasmtime::component::bindgen!({
            path: "wit",
            interfaces: "
                import wasi:http/incoming-handler
                import wasi:http/outgoing-handler
                import wasi:http/types
            ",
            tracing: true,
            async: true,
            with: {
                "wasi:io/streams": wasmtime_wasi::preview2::bindings::io::streams,
                "wasi:poll/poll": wasmtime_wasi::preview2::bindings::poll::poll,
            }
        });
    }

    pub use self::_internal_rest::wasi::http;
}

pub fn add_to_linker<T: WasiHttpView>(linker: &mut wasmtime::Linker<T>) -> anyhow::Result<()> {
    crate::component_impl::add_component_to_linker::<T>(linker, |t| t)
}

pub mod sync {
    use crate::types::WasiHttpView;

    pub fn add_to_linker<T: WasiHttpView>(linker: &mut wasmtime::Linker<T>) -> anyhow::Result<()> {
        crate::component_impl::sync::add_component_to_linker::<T>(linker, |t| t)
    }
}

impl std::error::Error for crate::bindings::http::types::Error {}

impl Display for crate::bindings::http::types::Error {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        match self {
            crate::bindings::http::types::Error::InvalidUrl(m) => {
                write!(f, "[InvalidUrl] {}", m)
            }
            crate::bindings::http::types::Error::ProtocolError(m) => {
                write!(f, "[ProtocolError] {}", m)
            }
            crate::bindings::http::types::Error::TimeoutError(m) => {
                write!(f, "[TimeoutError] {}", m)
            }
            crate::bindings::http::types::Error::UnexpectedError(m) => {
                write!(f, "[UnexpectedError] {}", m)
            }
        }
    }
}

impl From<wasmtime_wasi::preview2::TableError> for crate::bindings::http::types::Error {
    fn from(err: wasmtime_wasi::preview2::TableError) -> Self {
        Self::UnexpectedError(err.to_string())
    }
}

impl From<anyhow::Error> for crate::bindings::http::types::Error {
    fn from(err: anyhow::Error) -> Self {
        Self::UnexpectedError(err.to_string())
    }
}

impl From<std::io::Error> for crate::bindings::http::types::Error {
    fn from(err: std::io::Error) -> Self {
        let message = err.to_string();
        match err.kind() {
            std::io::ErrorKind::InvalidInput => Self::InvalidUrl(message),
            std::io::ErrorKind::AddrNotAvailable => Self::InvalidUrl(message),
            _ => {
                if message.starts_with("failed to lookup address information") {
                    Self::InvalidUrl("invalid dnsname".to_string())
                } else {
                    Self::ProtocolError(message)
                }
            }
        }
    }
}

impl From<http::Error> for crate::bindings::http::types::Error {
    fn from(err: http::Error) -> Self {
        Self::InvalidUrl(err.to_string())
    }
}

impl From<hyper::Error> for crate::bindings::http::types::Error {
    fn from(err: hyper::Error) -> Self {
        let message = err.message().to_string();
        if err.is_timeout() {
            Self::TimeoutError(message)
        } else if err.is_parse_status() || err.is_user() {
            Self::InvalidUrl(message)
        } else if err.is_body_write_aborted()
            || err.is_canceled()
            || err.is_closed()
            || err.is_incomplete_message()
            || err.is_parse()
        {
            Self::ProtocolError(message)
        } else {
            Self::UnexpectedError(message)
        }
    }
}

impl From<tokio::time::error::Elapsed> for crate::bindings::http::types::Error {
    fn from(err: tokio::time::error::Elapsed) -> Self {
        Self::TimeoutError(err.to_string())
    }
}

#[cfg(not(any(target_arch = "riscv64", target_arch = "s390x")))]
impl From<rustls::client::InvalidDnsNameError> for crate::bindings::http::types::Error {
    fn from(_err: rustls::client::InvalidDnsNameError) -> Self {
        Self::InvalidUrl("invalid dnsname".to_string())
    }
}