|
| 1 | +//! Interfaces for HTTP interactions of the guest. |
| 2 | +
|
| 3 | +use std::sync::Arc; |
| 4 | + |
| 5 | +use http::HeaderName; |
| 6 | +use tokio::runtime::Handle; |
| 7 | +use wasmtime_wasi_http::{ |
| 8 | + DEFAULT_FORBIDDEN_HEADERS, |
| 9 | + p2::{ |
| 10 | + HttpResult, WasiHttpCtxView, WasiHttpHooks, WasiHttpView, |
| 11 | + bindings::http::types::ErrorCode as HttpErrorCode, |
| 12 | + body::HyperOutgoingBody, |
| 13 | + default_send_request_handler, |
| 14 | + types::{HostFutureIncomingResponse, OutgoingRequestConfig}, |
| 15 | + }, |
| 16 | +}; |
| 17 | + |
| 18 | +pub use types::{HttpConnectionMode, HttpMethod, HttpPort}; |
| 19 | +pub use validator::{ |
| 20 | + AllowCertainHttpRequests, AllowHttpEndpoint, AllowHttpHost, HttpRequestRejected, |
| 21 | + HttpRequestValidator, RejectAllHttpRequests, |
| 22 | +}; |
| 23 | + |
| 24 | +use crate::state::WasmStateImpl; |
| 25 | + |
| 26 | +mod types; |
| 27 | +mod validator; |
| 28 | + |
| 29 | +impl WasiHttpView for WasmStateImpl { |
| 30 | + fn http(&mut self) -> WasiHttpCtxView<'_> { |
| 31 | + WasiHttpCtxView { |
| 32 | + ctx: &mut self.wasi_http_ctx, |
| 33 | + table: &mut self.resource_table, |
| 34 | + hooks: &mut self.wasi_http_hooks, |
| 35 | + } |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +/// Implements [`WasiHttpHooks`]. |
| 40 | +#[derive(Debug)] |
| 41 | +pub(crate) struct WasiHttpHooksImpl { |
| 42 | + /// HTTP request validator. |
| 43 | + pub(crate) http_validator: Arc<dyn HttpRequestValidator>, |
| 44 | + |
| 45 | + /// Handle to tokio I/O runtime. |
| 46 | + pub(crate) io_rt: Handle, |
| 47 | +} |
| 48 | + |
| 49 | +impl WasiHttpHooks for WasiHttpHooksImpl { |
| 50 | + fn send_request( |
| 51 | + &mut self, |
| 52 | + mut request: hyper::Request<HyperOutgoingBody>, |
| 53 | + config: OutgoingRequestConfig, |
| 54 | + ) -> HttpResult<HostFutureIncomingResponse> { |
| 55 | + let _guard = self.io_rt.enter(); |
| 56 | + |
| 57 | + // Python `requests` sends this so we allow it but later drop it from the actual request. |
| 58 | + request.headers_mut().remove(hyper::header::CONNECTION); |
| 59 | + |
| 60 | + // technically we could return an error straight away, but `urllib3` doesn't handle that super well, so we |
| 61 | + // create a future and validate the error in there (before actually starting the request of course) |
| 62 | + |
| 63 | + let validator = Arc::clone(&self.http_validator); |
| 64 | + let handle = wasmtime_wasi::runtime::spawn(async move { |
| 65 | + // yes, that's another layer of futures. The WASI interface is somewhat nested. |
| 66 | + let fut = async { |
| 67 | + let mode = HttpConnectionMode::from_use_tls(config.use_tls); |
| 68 | + validator |
| 69 | + .validate(&request, mode) |
| 70 | + .map_err(|_| HttpErrorCode::HttpRequestDenied)?; |
| 71 | + |
| 72 | + log::debug!( |
| 73 | + "UDF HTTP request: {} {} ({mode:?})", |
| 74 | + request.method().as_str(), |
| 75 | + request.uri(), |
| 76 | + ); |
| 77 | + default_send_request_handler(request, config).await |
| 78 | + }; |
| 79 | + |
| 80 | + Ok(fut.await) |
| 81 | + }); |
| 82 | + |
| 83 | + Ok(HostFutureIncomingResponse::pending(handle)) |
| 84 | + } |
| 85 | + |
| 86 | + fn is_forbidden_header(&mut self, name: &HeaderName) -> bool { |
| 87 | + // Python `requests` sends this so we allow it but later drop it from the actual request. |
| 88 | + if name == hyper::header::CONNECTION { |
| 89 | + return false; |
| 90 | + } |
| 91 | + |
| 92 | + DEFAULT_FORBIDDEN_HEADERS.contains(name) |
| 93 | + } |
| 94 | +} |
0 commit comments