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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
use crate::bindings::http::types::{
    FutureIncomingResponse, OutgoingRequest, RequestOptions, Scheme,
};
use crate::types::{ActiveFields, ActiveFuture, ActiveResponse, HttpResponse, TableHttpExt};
use crate::WasiHttpView;
use anyhow::Context;
use bytes::{Bytes, BytesMut};
use http_body_util::{BodyExt, Empty, Full};
use hyper::{Method, Request};
#[cfg(not(any(target_arch = "riscv64", target_arch = "s390x")))]
use std::sync::Arc;
use std::time::Duration;
use tokio::net::TcpStream;
use tokio::time::timeout;
#[cfg(not(any(target_arch = "riscv64", target_arch = "s390x")))]
use tokio_rustls::rustls::{self, OwnedTrustAnchor};
use wasmtime_wasi::preview2::{StreamState, TableStreamExt};

#[async_trait::async_trait]
impl<T: WasiHttpView> crate::bindings::http::outgoing_handler::Host for T {
    async fn handle(
        &mut self,
        request_id: OutgoingRequest,
        options: Option<RequestOptions>,
    ) -> wasmtime::Result<FutureIncomingResponse> {
        let future = ActiveFuture::new(request_id, options);
        let future_id = self
            .table_mut()
            .push_future(Box::new(future))
            .context("[handle] pushing future")?;
        Ok(future_id)
    }
}

#[cfg(feature = "sync")]
pub mod sync {
    use crate::bindings::http::outgoing_handler::{
        Host as AsyncHost, RequestOptions as AsyncRequestOptions,
    };
    use crate::bindings::sync::http::types::{
        FutureIncomingResponse, OutgoingRequest, RequestOptions,
    };
    use crate::WasiHttpView;
    use wasmtime_wasi::preview2::in_tokio;

    // same boilerplate everywhere, converting between two identical types with different
    // definition sites. one day wasmtime-wit-bindgen will make all this unnecessary
    impl From<RequestOptions> for AsyncRequestOptions {
        fn from(other: RequestOptions) -> Self {
            Self {
                connect_timeout_ms: other.connect_timeout_ms,
                first_byte_timeout_ms: other.first_byte_timeout_ms,
                between_bytes_timeout_ms: other.between_bytes_timeout_ms,
            }
        }
    }

    impl<T: WasiHttpView> crate::bindings::sync::http::outgoing_handler::Host for T {
        fn handle(
            &mut self,
            request_id: OutgoingRequest,
            options: Option<RequestOptions>,
        ) -> wasmtime::Result<FutureIncomingResponse> {
            in_tokio(async { AsyncHost::handle(self, request_id, options.map(|v| v.into())).await })
        }
    }
}

fn port_for_scheme(scheme: &Option<Scheme>) -> &str {
    match scheme {
        Some(s) => match s {
            Scheme::Http => ":80",
            Scheme::Https => ":443",
            // This should never happen.
            _ => panic!("unsupported scheme!"),
        },
        None => ":443",
    }
}

#[async_trait::async_trait]
pub trait WasiHttpViewExt {
    async fn handle_async(
        &mut self,
        request_id: OutgoingRequest,
        options: Option<RequestOptions>,
    ) -> wasmtime::Result<FutureIncomingResponse, crate::bindings::http::types::Error>;
}

#[async_trait::async_trait]
impl<T: WasiHttpView> WasiHttpViewExt for T {
    async fn handle_async(
        &mut self,
        request_id: OutgoingRequest,
        options: Option<RequestOptions>,
    ) -> wasmtime::Result<FutureIncomingResponse, crate::bindings::http::types::Error> {
        tracing::debug!("preparing outgoing request");
        let opts = options.unwrap_or(
            // TODO: Configurable defaults here?
            RequestOptions {
                connect_timeout_ms: Some(600 * 1000),
                first_byte_timeout_ms: Some(600 * 1000),
                between_bytes_timeout_ms: Some(600 * 1000),
            },
        );
        let connect_timeout =
            Duration::from_millis(opts.connect_timeout_ms.unwrap_or(600 * 1000).into());
        let first_bytes_timeout =
            Duration::from_millis(opts.first_byte_timeout_ms.unwrap_or(600 * 1000).into());
        let between_bytes_timeout =
            Duration::from_millis(opts.between_bytes_timeout_ms.unwrap_or(600 * 1000).into());

        let request = self
            .table()
            .get_request(request_id)
            .context("[handle_async] getting request")?;
        tracing::debug!("http request retrieved from table");

        let method = match request.method() {
            crate::bindings::http::types::Method::Get => Method::GET,
            crate::bindings::http::types::Method::Head => Method::HEAD,
            crate::bindings::http::types::Method::Post => Method::POST,
            crate::bindings::http::types::Method::Put => Method::PUT,
            crate::bindings::http::types::Method::Delete => Method::DELETE,
            crate::bindings::http::types::Method::Connect => Method::CONNECT,
            crate::bindings::http::types::Method::Options => Method::OPTIONS,
            crate::bindings::http::types::Method::Trace => Method::TRACE,
            crate::bindings::http::types::Method::Patch => Method::PATCH,
            crate::bindings::http::types::Method::Other(s) => {
                return Err(crate::bindings::http::types::Error::InvalidUrl(format!(
                    "unknown method {}",
                    s
                ))
                .into());
            }
        };

        let scheme = match request.scheme().as_ref().unwrap_or(&Scheme::Https) {
            Scheme::Http => "http://",
            Scheme::Https => "https://",
            Scheme::Other(s) => {
                return Err(crate::bindings::http::types::Error::InvalidUrl(format!(
                    "unsupported scheme {}",
                    s
                ))
                .into());
            }
        };

        // Largely adapted from https://hyper.rs/guides/1/client/basic/
        let authority = match request.authority().find(":") {
            Some(_) => request.authority().to_owned(),
            None => request.authority().to_owned() + port_for_scheme(request.scheme()),
        };
        let tcp_stream = TcpStream::connect(authority.clone()).await?;
        let mut sender = if scheme == "https://" {
            tracing::debug!("initiating client connection client with TLS");
            #[cfg(not(any(target_arch = "riscv64", target_arch = "s390x")))]
            {
                //TODO: uncomment this code and make the tls implementation a feature decision.
                //let connector = tokio_native_tls::native_tls::TlsConnector::builder().build()?;
                //let connector = tokio_native_tls::TlsConnector::from(connector);
                //let host = authority.split(":").next().unwrap_or(&authority);
                //let stream = connector.connect(&host, stream).await?;

                // derived from https://github.com/tokio-rs/tls/blob/master/tokio-rustls/examples/client/src/main.rs
                let mut root_cert_store = rustls::RootCertStore::empty();
                root_cert_store.add_trust_anchors(webpki_roots::TLS_SERVER_ROOTS.iter().map(
                    |ta| {
                        OwnedTrustAnchor::from_subject_spki_name_constraints(
                            ta.subject,
                            ta.spki,
                            ta.name_constraints,
                        )
                    },
                ));
                let config = rustls::ClientConfig::builder()
                    .with_safe_defaults()
                    .with_root_certificates(root_cert_store)
                    .with_no_client_auth();
                let connector = tokio_rustls::TlsConnector::from(Arc::new(config));
                let mut parts = authority.split(":");
                let host = parts.next().unwrap_or(&authority);
                let domain = rustls::ServerName::try_from(host)?;
                let stream = connector.connect(domain, tcp_stream).await.map_err(|e| {
                    crate::bindings::http::types::Error::ProtocolError(e.to_string())
                })?;

                let t = timeout(
                    connect_timeout,
                    hyper::client::conn::http1::handshake(stream),
                )
                .await?;
                let (s, conn) = t?;
                tokio::task::spawn(async move {
                    if let Err(err) = conn.await {
                        tracing::debug!("[host/client] Connection failed: {:?}", err);
                    }
                });
                s
            }
            #[cfg(any(target_arch = "riscv64", target_arch = "s390x"))]
            return Err(crate::bindings::http::types::Error::UnexpectedError(
                "unsupported architecture for SSL".to_string(),
            ));
        } else {
            tracing::debug!("initiating client connection without TLS");
            let t = timeout(
                connect_timeout,
                hyper::client::conn::http1::handshake(tcp_stream),
            )
            .await?;
            let (s, conn) = t?;
            tokio::task::spawn(async move {
                if let Err(err) = conn.await {
                    tracing::debug!("[host/client] Connection failed: {:?}", err);
                }
            });
            s
        };

        let url = scheme.to_owned() + &request.authority() + &request.path_with_query();

        tracing::debug!("request to url {:?}", &url);
        let mut call = Request::builder()
            .method(method)
            .uri(url)
            .header(hyper::header::HOST, request.authority());

        if let Some(headers) = request.headers() {
            for (key, val) in self
                .table()
                .get_fields(headers)
                .context("[handle_async] getting request headers")?
                .iter()
            {
                for item in val {
                    call = call.header(key, item.clone());
                }
            }
        }

        let mut response = ActiveResponse::new();
        let body = match request.body() {
            Some(id) => {
                let table = self.table_mut();
                let stream = table
                    .get_stream(id)
                    .context("[handle_async] getting stream")?;
                let input_stream = table
                    .get_input_stream_mut(stream.incoming())
                    .context("[handle_async] getting mutable input stream")?;
                let mut bytes = BytesMut::new();
                let mut eof = StreamState::Open;
                while eof != StreamState::Closed {
                    let (chunk, state) = input_stream.read(4096)?;
                    eof = if chunk.is_empty() {
                        StreamState::Closed
                    } else {
                        state
                    };
                    bytes.extend_from_slice(&chunk[..]);
                }
                Full::<Bytes>::new(bytes.freeze()).boxed()
            }
            None => Empty::<Bytes>::new().boxed(),
        };
        let request = call.body(body)?;
        tracing::trace!("hyper request {:?}", request);
        let t = timeout(first_bytes_timeout, sender.send_request(request)).await?;
        let mut res = t?;
        tracing::trace!("hyper response {:?}", res);
        response.status = res.status().as_u16();

        let mut map = ActiveFields::new();
        for (key, value) in res.headers().iter() {
            let mut vec = Vec::new();
            vec.push(value.as_bytes().to_vec());
            map.insert(key.as_str().to_string(), vec);
        }
        let headers = self
            .table_mut()
            .push_fields(Box::new(map))
            .context("[handle_async] pushing response headers")?;
        response.set_headers(headers);

        let mut buf: Vec<u8> = Vec::new();
        while let Some(next) = timeout(between_bytes_timeout, res.frame()).await? {
            let frame = next?;
            tracing::debug!("response body next frame");
            if let Some(chunk) = frame.data_ref() {
                tracing::trace!("response body chunk size {:?}", chunk.len());
                buf.extend_from_slice(chunk);
            }
            if let Some(trailers) = frame.trailers_ref() {
                tracing::debug!("response trailers present");
                let mut map = ActiveFields::new();
                for (name, value) in trailers.iter() {
                    let key = name.to_string();
                    match map.get_mut(&key) {
                        Some(vec) => vec.push(value.as_bytes().to_vec()),
                        None => {
                            let mut vec = Vec::new();
                            vec.push(value.as_bytes().to_vec());
                            map.insert(key, vec);
                        }
                    };
                }
                let trailers = self
                    .table_mut()
                    .push_fields(Box::new(map))
                    .context("[handle_async] pushing response trailers")?;
                response.set_trailers(trailers);
                tracing::debug!("http trailers saved to table");
            }
        }

        let response_id = self
            .table_mut()
            .push_response(Box::new(response))
            .context("[handle_async] pushing response")?;
        tracing::trace!("response body {:?}", std::str::from_utf8(&buf[..]).unwrap());
        let (stream_id, stream) = self
            .table_mut()
            .push_stream(Bytes::from(buf), response_id)
            .await
            .context("[handle_async] pushing stream")?;
        let response = self
            .table_mut()
            .get_response_mut(response_id)
            .context("[handle_async] getting mutable response")?;
        response.set_body(stream_id);
        tracing::debug!("http response saved to table with id {:?}", response_id);

        self.http_ctx_mut().streams.insert(stream_id, stream);

        Ok(response_id)
    }
}