From 6ff2f3c194d1ecdf69e583f9b8ea103026b352df Mon Sep 17 00:00:00 2001 From: Christoph Herzog Date: Thu, 29 Feb 2024 13:19:08 +0100 Subject: [PATCH] chore: Completely remove wasix_http_client Followup to a previous removal commit. This now completely removes all usage of wasix_http_client. (exported definitions, implementations, etc) --- Cargo.lock | 1 - Makefile | 3 - lib/wasi-types/src/lib.rs | 1 - lib/wasi-types/src/wasix/mod.rs | 1 - lib/wasix/Cargo.toml | 1 - lib/wasix/src/bindings/mod.rs | 3 - .../src/bindings/wasix_http_client_v1.rs | 675 ------------------ lib/wasix/src/http/client_impl.rs | 165 ----- lib/wasix/src/http/mod.rs | 1 - lib/wasix/src/lib.rs | 31 +- 10 files changed, 2 insertions(+), 880 deletions(-) delete mode 100644 lib/wasi-types/src/wasix/mod.rs delete mode 100644 lib/wasix/src/bindings/mod.rs delete mode 100644 lib/wasix/src/bindings/wasix_http_client_v1.rs delete mode 100644 lib/wasix/src/http/client_impl.rs diff --git a/Cargo.lock b/Cargo.lock index f17d71c8cfb..51b942230e1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6688,7 +6688,6 @@ dependencies = [ "virtual-fs", "virtual-mio", "virtual-net", - "wai-bindgen-wasmer", "waker-fn", "wasm-bindgen", "wasm-bindgen-futures", diff --git a/Makefile b/Makefile index 76273197225..90508225fd6 100644 --- a/Makefile +++ b/Makefile @@ -168,9 +168,6 @@ exclude_tests := --exclude wasmer-c-api --exclude wasmer-cli --exclude wasmer-co # We run integration tests separately (it requires building the c-api) exclude_tests += --exclude wasmer-integration-tests-cli exclude_tests += --exclude wasmer-integration-tests-ios -# wasix_http_client is only for the WASM target, must be tested separately -# FIXME: add separate test step! -exclude_tests += --exclude wasix_http_client ifneq (, $(findstring llvm,$(compilers))) ENABLE_LLVM := 1 diff --git a/lib/wasi-types/src/lib.rs b/lib/wasi-types/src/lib.rs index f1c745d1fe3..7e725ccc5d9 100644 --- a/lib/wasi-types/src/lib.rs +++ b/lib/wasi-types/src/lib.rs @@ -5,4 +5,3 @@ pub mod asyncify; pub mod types; pub mod wasi; -pub mod wasix; diff --git a/lib/wasi-types/src/wasix/mod.rs b/lib/wasi-types/src/wasix/mod.rs deleted file mode 100644 index 4f664f54f58..00000000000 --- a/lib/wasi-types/src/wasix/mod.rs +++ /dev/null @@ -1 +0,0 @@ -// pub mod wasix_http_client_v1; diff --git a/lib/wasix/Cargo.toml b/lib/wasix/Cargo.toml index b1507f3184e..6de6c19fb4b 100644 --- a/lib/wasix/Cargo.toml +++ b/lib/wasix/Cargo.toml @@ -59,7 +59,6 @@ hex = { version = "^0.4" } term_size = { version = "0.3" } linked_hash_set = { version = "0.1" } http = "0.2.8" -wai-bindgen-wasmer = { path = "../wai-bindgen-wasmer", version = "0.18.0", features = ["tracing"] } heapless = "0.7.16" once_cell = "1.17.0" pin-project = "1.0.12" diff --git a/lib/wasix/src/bindings/mod.rs b/lib/wasix/src/bindings/mod.rs deleted file mode 100644 index 9f9054aa43f..00000000000 --- a/lib/wasix/src/bindings/mod.rs +++ /dev/null @@ -1,3 +0,0 @@ -// TODO: remove allow() -#[allow(dead_code, clippy::all)] -pub mod wasix_http_client_v1; diff --git a/lib/wasix/src/bindings/wasix_http_client_v1.rs b/lib/wasix/src/bindings/wasix_http_client_v1.rs deleted file mode 100644 index db958f7fc10..00000000000 --- a/lib/wasix/src/bindings/wasix_http_client_v1.rs +++ /dev/null @@ -1,675 +0,0 @@ -#[derive(Clone)] -pub enum Method<'a> { - Get, - Head, - Post, - Put, - Delete, - Connect, - Options, - Trace, - Patch, - Other(&'a str), -} -impl<'a> core::fmt::Debug for Method<'a> { - fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { - match self { - Method::Get => f.debug_tuple("Method::Get").finish(), - Method::Head => f.debug_tuple("Method::Head").finish(), - Method::Post => f.debug_tuple("Method::Post").finish(), - Method::Put => f.debug_tuple("Method::Put").finish(), - Method::Delete => f.debug_tuple("Method::Delete").finish(), - Method::Connect => f.debug_tuple("Method::Connect").finish(), - Method::Options => f.debug_tuple("Method::Options").finish(), - Method::Trace => f.debug_tuple("Method::Trace").finish(), - Method::Patch => f.debug_tuple("Method::Patch").finish(), - Method::Other(e) => f.debug_tuple("Method::Other").field(e).finish(), - } - } -} -#[derive(Clone)] -pub struct HeaderParam<'a> { - pub key: &'a str, - pub value: &'a [u8], -} -impl<'a> core::fmt::Debug for HeaderParam<'a> { - fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { - f.debug_struct("HeaderParam") - .field("key", &self.key) - .field("value", &self.value) - .finish() - } -} -#[derive(Clone)] -pub struct HeaderResult { - pub key: String, - pub value: Vec, -} -impl core::fmt::Debug for HeaderResult { - fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { - f.debug_struct("HeaderResult") - .field("key", &self.key) - .field("value", &self.value) - .finish() - } -} -pub type HeaderListParam<'a> = Vec>; -pub type HeaderListResult = Vec; -pub type Fd = u32; -pub type TimeoutMs = u32; -#[repr(C)] -#[derive(Copy, Clone)] -pub struct RedirectFollow { - pub max: u32, -} -impl core::fmt::Debug for RedirectFollow { - fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { - f.debug_struct("RedirectFollow") - .field("max", &self.max) - .finish() - } -} -impl wai_bindgen_wasmer::Endian for RedirectFollow { - fn into_le(self) -> Self { - Self { - max: self.max.into_le(), - } - } - fn from_le(self) -> Self { - Self { - max: self.max.from_le(), - } - } -} -unsafe impl wai_bindgen_wasmer::AllBytesValid for RedirectFollow {} -#[derive(Clone, Copy)] -pub enum RedirectPolicy { - NoFollow, - Follow(RedirectFollow), -} -impl core::fmt::Debug for RedirectPolicy { - fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { - match self { - RedirectPolicy::NoFollow => f.debug_tuple("RedirectPolicy::NoFollow").finish(), - RedirectPolicy::Follow(e) => f.debug_tuple("RedirectPolicy::Follow").field(e).finish(), - } - } -} -#[derive(Clone)] -pub enum BodyParam<'a> { - Data(&'a [u8]), - Fd(Fd), -} -impl<'a> core::fmt::Debug for BodyParam<'a> { - fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { - match self { - BodyParam::Data(e) => f.debug_tuple("BodyParam::Data").field(e).finish(), - BodyParam::Fd(e) => f.debug_tuple("BodyParam::Fd").field(e).finish(), - } - } -} -#[derive(Clone)] -pub enum BodyResult { - Data(Vec), - Fd(Fd), -} -impl core::fmt::Debug for BodyResult { - fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { - match self { - BodyResult::Data(e) => f.debug_tuple("BodyResult::Data").field(e).finish(), - BodyResult::Fd(e) => f.debug_tuple("BodyResult::Fd").field(e).finish(), - } - } -} -#[derive(Clone)] -pub struct Request<'a> { - pub url: &'a str, - pub method: Method<'a>, - pub headers: HeaderListParam<'a>, - pub body: Option>, - pub timeout: Option, - pub redirect_policy: Option, -} -impl<'a> core::fmt::Debug for Request<'a> { - fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { - f.debug_struct("Request") - .field("url", &self.url) - .field("method", &self.method) - .field("headers", &self.headers) - .field("body", &self.body) - .field("timeout", &self.timeout) - .field("redirect-policy", &self.redirect_policy) - .finish() - } -} -#[derive(Clone)] -pub struct Response { - pub status: u16, - pub headers: HeaderListResult, - pub body: BodyResult, - pub redirect_urls: Option>, -} -impl core::fmt::Debug for Response { - fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { - f.debug_struct("Response") - .field("status", &self.status) - .field("headers", &self.headers) - .field("body", &self.body) - .field("redirect-urls", &self.redirect_urls) - .finish() - } -} -pub trait WasixHttpClientV1: Sized + Send + Sync + 'static { - type Client: std::fmt::Debug; - fn client_new(&mut self) -> Result; - fn client_send( - &mut self, - self_: &Self::Client, - request: Request<'_>, - ) -> Result; - fn drop_client(&mut self, state: Self::Client) { - drop(state); - } -} -pub struct WasixHttpClientV1Tables { - pub(crate) client_table: wai_bindgen_wasmer::Table, -} -impl Default for WasixHttpClientV1Tables { - fn default() -> Self { - Self { - client_table: Default::default(), - } - } -} -impl Clone for WasixHttpClientV1Tables { - fn clone(&self) -> Self { - Self::default() - } -} -pub struct LazyInitialized { - memory: wasmer::Memory, - func_canonical_abi_realloc: wasmer::TypedFunction<(i32, i32, i32, i32), i32>, -} -#[must_use = "The returned initializer function must be called - with the instance and the store before starting the runtime"] -pub fn add_to_imports( - store: &mut impl wasmer::AsStoreMut, - imports: &mut wasmer::Imports, - data: T, -) -> Box Result<(), anyhow::Error>> -where - T: WasixHttpClientV1, -{ - #[derive(Clone)] - struct EnvWrapper { - data: T, - tables: std::rc::Rc>>, - lazy: std::rc::Rc>, - } - unsafe impl Send for EnvWrapper {} - unsafe impl Sync for EnvWrapper {} - let lazy = std::rc::Rc::new(OnceCell::new()); - let env = EnvWrapper { - data, - tables: std::rc::Rc::default(), - lazy: std::rc::Rc::clone(&lazy), - }; - let env = wasmer::FunctionEnv::new(&mut *store, env); - let mut exports = wasmer::Exports::new(); - let mut store = store.as_store_mut(); - exports.insert( - "client::new", - wasmer::Function::new_typed_with_env( - &mut store, - &env, - move |mut store: wasmer::FunctionEnvMut>, - arg0: i32| - -> Result<(), wasmer::RuntimeError> { - let span = wai_bindgen_wasmer::tracing::span!( - wai_bindgen_wasmer::tracing::Level::TRACE, - "wai-bindgen abi", - module = "wasix_http_client_v1", - function = "client::new", - ); - let _enter = span.enter(); - let func_canonical_abi_realloc = store - .data() - .lazy - .get() - .unwrap() - .func_canonical_abi_realloc - .clone(); - let _memory: wasmer::Memory = store.data().lazy.get().unwrap().memory.clone(); - let data_mut = store.data_mut(); - let tables = data_mut.tables.borrow_mut(); - let host = &mut data_mut.data; - let result = host.client_new(); - drop(tables); - wai_bindgen_wasmer::tracing::event!( - wai_bindgen_wasmer::tracing::Level::TRACE, - result = wai_bindgen_wasmer::tracing::field::debug(&result), - ); - match result { - Ok(e) => { - let val = wai_bindgen_wasmer::rt::as_i32({ - let data_mut = store.data_mut(); - let mut tables = data_mut.tables.borrow_mut(); - tables.client_table.insert(e) as i32 - }); - let _memory_view = _memory.view(&store); - let caller_memory = unsafe { _memory_view.data_unchecked_mut() }; - caller_memory - .store(arg0 + 0, wai_bindgen_wasmer::rt::as_i32(0i32) as u8)?; - caller_memory.store(arg0 + 4, val)?; - } - Err(e) => { - let _memory_view = _memory.view(&store); - let caller_memory = unsafe { _memory_view.data_unchecked_mut() }; - caller_memory - .store(arg0 + 0, wai_bindgen_wasmer::rt::as_i32(1i32) as u8)?; - let vec0 = e; - let ptr0 = func_canonical_abi_realloc.call( - &mut store.as_store_mut(), - 0, - 0, - 1, - vec0.len() as i32, - )?; - let _memory_view = _memory.view(&store); - let caller_memory = unsafe { _memory_view.data_unchecked_mut() }; - caller_memory.store_many(ptr0, vec0.as_bytes())?; - caller_memory - .store(arg0 + 8, wai_bindgen_wasmer::rt::as_i32(vec0.len() as i32))?; - caller_memory.store(arg0 + 4, wai_bindgen_wasmer::rt::as_i32(ptr0))?; - } - }; - Ok(()) - }, - ), - ); - exports.insert( - "client::send", - wasmer::Function::new_typed_with_env( - &mut store, - &env, - move |mut store: wasmer::FunctionEnvMut>, - arg0: i32, - arg1: i32| - -> Result<(), wasmer::RuntimeError> { - let span = wai_bindgen_wasmer::tracing::span!( - wai_bindgen_wasmer::tracing::Level::TRACE, - "wai-bindgen abi", - module = "wasix_http_client_v1", - function = "client::send", - ); - let _enter = span.enter(); - let func_canonical_abi_realloc = store - .data() - .lazy - .get() - .unwrap() - .func_canonical_abi_realloc - .clone(); - let (data_mut, alt_store) = store.data_and_store_mut(); - let _memory: wasmer::Memory = data_mut.lazy.get().unwrap().memory.clone(); - let _memory_view = _memory.view(&alt_store); - let mut _bc = wai_bindgen_wasmer::BorrowChecker::new(unsafe { - _memory_view.data_unchecked_mut() - }); - let tables = data_mut.tables.borrow_mut(); - let load0 = _bc.load::(arg0 + 0)?; - let load1 = _bc.load::(arg0 + 4)?; - let load2 = _bc.load::(arg0 + 8)?; - let ptr3 = load1; - let len3 = load2; - let load4 = _bc.load::(arg0 + 12)?; - let load8 = _bc.load::(arg0 + 24)?; - let load9 = _bc.load::(arg0 + 28)?; - let len16 = load9; - let base16 = load8; - let mut result16 = Vec::with_capacity(len16 as usize); - for i in 0..len16 { - let base = base16 + i * 16; - result16.push({ - let load10 = _bc.load::(base + 0)?; - let load11 = _bc.load::(base + 4)?; - let ptr12 = load10; - let len12 = load11; - let load13 = _bc.load::(base + 8)?; - let load14 = _bc.load::(base + 12)?; - let ptr15 = load13; - let len15 = load14; - HeaderParam { - key: _bc.slice_str(ptr12, len12)?, - value: _bc.slice(ptr15, len15)?, - } - }); - } - let load17 = _bc.load::(arg0 + 32)?; - let load23 = _bc.load::(arg0 + 48)?; - let load25 = _bc.load::(arg0 + 56)?; - let param0 = tables - .client_table - .get((load0) as u32) - .ok_or_else(|| wasmer::RuntimeError::new("invalid handle index"))?; - let param1 = Request { - url: _bc.slice_str(ptr3, len3)?, - method: match i32::from(load4) { - 0 => Method::Get, - 1 => Method::Head, - 2 => Method::Post, - 3 => Method::Put, - 4 => Method::Delete, - 5 => Method::Connect, - 6 => Method::Options, - 7 => Method::Trace, - 8 => Method::Patch, - 9 => Method::Other({ - let load5 = _bc.load::(arg0 + 16)?; - let load6 = _bc.load::(arg0 + 20)?; - let ptr7 = load5; - let len7 = load6; - _bc.slice_str(ptr7, len7)? - }), - _ => return Err(invalid_variant("Method")), - }, - headers: result16, - body: match i32::from(load17) { - 0 => None, - 1 => Some({ - let load18 = _bc.load::(arg0 + 36)?; - match i32::from(load18) { - 0 => BodyParam::Data({ - let load19 = _bc.load::(arg0 + 40)?; - let load20 = _bc.load::(arg0 + 44)?; - let ptr21 = load19; - let len21 = load20; - _bc.slice(ptr21, len21)? - }), - 1 => BodyParam::Fd({ - let load22 = _bc.load::(arg0 + 40)?; - load22 as u32 - }), - _ => return Err(invalid_variant("BodyParam")), - } - }), - _ => return Err(invalid_variant("option")), - }, - timeout: match i32::from(load23) { - 0 => None, - 1 => Some({ - let load24 = _bc.load::(arg0 + 52)?; - load24 as u32 - }), - _ => return Err(invalid_variant("option")), - }, - redirect_policy: match i32::from(load25) { - 0 => None, - 1 => Some({ - let load26 = _bc.load::(arg0 + 60)?; - match i32::from(load26) { - 0 => RedirectPolicy::NoFollow, - 1 => RedirectPolicy::Follow({ - let load27 = _bc.load::(arg0 + 64)?; - RedirectFollow { max: load27 as u32 } - }), - _ => return Err(invalid_variant("RedirectPolicy")), - } - }), - _ => return Err(invalid_variant("option")), - }, - }; - wai_bindgen_wasmer::tracing::event!( - wai_bindgen_wasmer::tracing::Level::TRACE, - self_ = wai_bindgen_wasmer::tracing::field::debug(¶m0), - request = wai_bindgen_wasmer::tracing::field::debug(¶m1), - ); - let host = &mut data_mut.data; - let result = host.client_send(param0, param1); - drop(tables); - wai_bindgen_wasmer::tracing::event!( - wai_bindgen_wasmer::tracing::Level::TRACE, - result = wai_bindgen_wasmer::tracing::field::debug(&result), - ); - match result { - Ok(e) => { - let _memory_view = _memory.view(&store); - let caller_memory = unsafe { _memory_view.data_unchecked_mut() }; - caller_memory - .store(arg1 + 0, wai_bindgen_wasmer::rt::as_i32(0i32) as u8)?; - let Response { - status: status28, - headers: headers28, - body: body28, - redirect_urls: redirect_urls28, - } = e; - caller_memory.store( - arg1 + 4, - wai_bindgen_wasmer::rt::as_i32(wai_bindgen_wasmer::rt::as_i32(status28)) - as u16, - )?; - let vec32 = headers28; - let len32 = vec32.len() as i32; - let result32 = func_canonical_abi_realloc.call( - &mut store.as_store_mut(), - 0, - 0, - 4, - len32 * 16, - )?; - for (i, e) in vec32.into_iter().enumerate() { - let base = result32 + (i as i32) * 16; - { - let HeaderResult { - key: key29, - value: value29, - } = e; - let vec30 = key29; - let ptr30 = func_canonical_abi_realloc.call( - &mut store.as_store_mut(), - 0, - 0, - 1, - vec30.len() as i32, - )?; - let _memory_view = _memory.view(&store); - let caller_memory = unsafe { _memory_view.data_unchecked_mut() }; - caller_memory.store_many(ptr30, vec30.as_bytes())?; - caller_memory.store( - base + 4, - wai_bindgen_wasmer::rt::as_i32(vec30.len() as i32), - )?; - caller_memory - .store(base + 0, wai_bindgen_wasmer::rt::as_i32(ptr30))?; - let vec31 = value29; - let ptr31 = func_canonical_abi_realloc.call( - &mut store.as_store_mut(), - 0, - 0, - 1, - (vec31.len() as i32) * 1, - )?; - let _memory_view = _memory.view(&store); - let caller_memory = unsafe { _memory_view.data_unchecked_mut() }; - caller_memory.store_many(ptr31, &vec31)?; - caller_memory.store( - base + 12, - wai_bindgen_wasmer::rt::as_i32(vec31.len() as i32), - )?; - caller_memory - .store(base + 8, wai_bindgen_wasmer::rt::as_i32(ptr31))?; - } - } - let _memory_view = _memory.view(&store); - let caller_memory = unsafe { _memory_view.data_unchecked_mut() }; - caller_memory.store(arg1 + 12, wai_bindgen_wasmer::rt::as_i32(len32))?; - caller_memory.store(arg1 + 8, wai_bindgen_wasmer::rt::as_i32(result32))?; - match body28 { - BodyResult::Data(e) => { - caller_memory - .store(arg1 + 16, wai_bindgen_wasmer::rt::as_i32(0i32) as u8)?; - let vec33 = e; - let ptr33 = func_canonical_abi_realloc.call( - &mut store.as_store_mut(), - 0, - 0, - 1, - (vec33.len() as i32) * 1, - )?; - let _memory_view = _memory.view(&store); - let caller_memory = unsafe { _memory_view.data_unchecked_mut() }; - caller_memory.store_many(ptr33, &vec33)?; - caller_memory.store( - arg1 + 24, - wai_bindgen_wasmer::rt::as_i32(vec33.len() as i32), - )?; - caller_memory - .store(arg1 + 20, wai_bindgen_wasmer::rt::as_i32(ptr33))?; - } - BodyResult::Fd(e) => { - let _memory_view = _memory.view(&store); - let caller_memory = unsafe { _memory_view.data_unchecked_mut() }; - caller_memory - .store(arg1 + 16, wai_bindgen_wasmer::rt::as_i32(1i32) as u8)?; - caller_memory.store( - arg1 + 20, - wai_bindgen_wasmer::rt::as_i32(wai_bindgen_wasmer::rt::as_i32( - e, - )), - )?; - } - }; - match redirect_urls28 { - Some(e) => { - let _memory_view = _memory.view(&store); - let caller_memory = unsafe { _memory_view.data_unchecked_mut() }; - caller_memory - .store(arg1 + 28, wai_bindgen_wasmer::rt::as_i32(1i32) as u8)?; - let vec35 = e; - let len35 = vec35.len() as i32; - let result35 = func_canonical_abi_realloc.call( - &mut store.as_store_mut(), - 0, - 0, - 4, - len35 * 8, - )?; - for (i, e) in vec35.into_iter().enumerate() { - let base = result35 + (i as i32) * 8; - { - let vec34 = e; - let ptr34 = func_canonical_abi_realloc.call( - &mut store.as_store_mut(), - 0, - 0, - 1, - vec34.len() as i32, - )?; - let _memory_view = _memory.view(&store); - let caller_memory = - unsafe { _memory_view.data_unchecked_mut() }; - caller_memory.store_many(ptr34, vec34.as_bytes())?; - caller_memory.store( - base + 4, - wai_bindgen_wasmer::rt::as_i32(vec34.len() as i32), - )?; - caller_memory.store( - base + 0, - wai_bindgen_wasmer::rt::as_i32(ptr34), - )?; - } - } - let _memory_view = _memory.view(&store); - let caller_memory = unsafe { _memory_view.data_unchecked_mut() }; - caller_memory - .store(arg1 + 36, wai_bindgen_wasmer::rt::as_i32(len35))?; - caller_memory - .store(arg1 + 32, wai_bindgen_wasmer::rt::as_i32(result35))?; - } - None => { - let e = (); - { - let _memory_view = _memory.view(&store); - let caller_memory = - unsafe { _memory_view.data_unchecked_mut() }; - caller_memory.store( - arg1 + 28, - wai_bindgen_wasmer::rt::as_i32(0i32) as u8, - )?; - let () = e; - } - } - }; - } - Err(e) => { - let _memory_view = _memory.view(&store); - let caller_memory = unsafe { _memory_view.data_unchecked_mut() }; - caller_memory - .store(arg1 + 0, wai_bindgen_wasmer::rt::as_i32(1i32) as u8)?; - let vec36 = e; - let ptr36 = func_canonical_abi_realloc.call( - &mut store.as_store_mut(), - 0, - 0, - 1, - vec36.len() as i32, - )?; - let _memory_view = _memory.view(&store); - let caller_memory = unsafe { _memory_view.data_unchecked_mut() }; - caller_memory.store_many(ptr36, vec36.as_bytes())?; - caller_memory - .store(arg1 + 8, wai_bindgen_wasmer::rt::as_i32(vec36.len() as i32))?; - caller_memory.store(arg1 + 4, wai_bindgen_wasmer::rt::as_i32(ptr36))?; - } - }; - Ok(()) - }, - ), - ); - imports.register_namespace("wasix_http_client_v1", exports); - let mut canonical_abi = imports - .get_namespace_exports("canonical_abi") - .unwrap_or_else(wasmer::Exports::new); - canonical_abi.insert( - "resource_drop_client", - wasmer::Function::new_typed_with_env( - &mut store, - &env, - move |mut store: wasmer::FunctionEnvMut>, - handle: u32| - -> Result<(), wasmer::RuntimeError> { - let data_mut = store.data_mut(); - let mut tables = data_mut.tables.borrow_mut(); - let handle = tables.client_table.remove(handle).map_err(|e| { - wasmer::RuntimeError::new(format!("failed to remove handle: {}", e)) - })?; - let host = &mut data_mut.data; - host.drop_client(handle); - Ok(()) - }, - ), - ); - imports.register_namespace("canonical_abi", canonical_abi); - let f = move |_instance: &wasmer::Instance, _store: &dyn wasmer::AsStoreRef| { - let memory = _instance.exports.get_memory("memory")?.clone(); - let func_canonical_abi_realloc = _instance - .exports - .get_typed_function(&_store.as_store_ref(), "canonical_abi_realloc") - .unwrap() - .clone(); - lazy.set(LazyInitialized { - memory, - func_canonical_abi_realloc, - }) - .map_err(|_e| anyhow::anyhow!("Couldn't set lazy initialized data"))?; - Ok(()) - }; - Box::new(f) -} -use wai_bindgen_wasmer::once_cell::unsync::OnceCell; -use wai_bindgen_wasmer::rt::invalid_variant; -use wai_bindgen_wasmer::rt::RawMem; -#[allow(unused_imports)] -use wasmer::AsStoreMut as _; -#[allow(unused_imports)] -use wasmer::AsStoreRef as _; diff --git a/lib/wasix/src/http/client_impl.rs b/lib/wasix/src/http/client_impl.rs deleted file mode 100644 index 97d23dbd8e4..00000000000 --- a/lib/wasix/src/http/client_impl.rs +++ /dev/null @@ -1,165 +0,0 @@ -use std::sync::Arc; - -use http::{HeaderMap, HeaderValue}; -use url::Url; - -use crate::{ - bindings::wasix_http_client_v1 as sys, - capabilities::Capabilities, - http::{DynHttpClient, HttpClientCapabilityV1}, - runtime::task_manager::InlineWaker, - Runtime, WasiEnv, -}; - -impl std::fmt::Display for sys::Method<'_> { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - let v = match self { - sys::Method::Get => "GET", - sys::Method::Head => "HEAD", - sys::Method::Post => "POST", - sys::Method::Put => "PUT", - sys::Method::Delete => "DELETE", - sys::Method::Connect => "CONNECT", - sys::Method::Options => "OPTIONS", - sys::Method::Trace => "TRACE", - sys::Method::Patch => "PATCH", - sys::Method::Other(other) => *other, - }; - write!(f, "{v}") - } -} - -pub struct WasixHttpClientImpl { - cap: Capabilities, - runtime: Arc, -} - -impl WasixHttpClientImpl { - pub fn new(env: &WasiEnv) -> Self { - Self { - // TODO: Should be a shared reference - // Currently this client would not adapt to changes in the capabilities. - cap: env.capabilities.clone(), - runtime: env.runtime.clone(), - } - } -} - -#[derive(Debug)] -pub struct ClientImpl { - client: DynHttpClient, - capabilities: HttpClientCapabilityV1, -} - -impl sys::WasixHttpClientV1 for WasixHttpClientImpl { - type Client = ClientImpl; - - fn client_new(&mut self) -> Result { - let capabilities = if self.cap.insecure_allow_all { - HttpClientCapabilityV1::new_allow_all() - } else if !self.cap.http_client.is_deny_all() { - self.cap.http_client.clone() - } else { - return Err("Permission denied - http client not enabled".to_string()); - }; - - let client = self - .runtime - .http_client() - .ok_or_else(|| "No http client available".to_string())? - .clone(); - Ok(ClientImpl { - client, - capabilities, - }) - } - - fn client_send( - &mut self, - self_: &Self::Client, - request: sys::Request<'_>, - ) -> Result { - let uri: http::Uri = request - .url - .parse() - .map_err(|err| format!("Invalid request url: {err}"))?; - let host = uri.host().unwrap_or_default(); - if !self_.capabilities.can_access_domain(host) { - return Err(format!( - "Permission denied: http capability not enabled for host '{host}'" - )); - } - - let headers = request - .headers - .into_iter() - .map(|h| { - let value = HeaderValue::from_bytes(h.value).map_err(|e| e.to_string())?; - let key = - http::HeaderName::from_bytes(h.key.as_bytes()).map_err(|e| e.to_string())?; - Ok((key, value)) - }) - .collect::>()?; - - // FIXME: stream body... - - let body = match request.body { - Some(sys::BodyParam::Fd(_)) => { - return Err("File descriptor bodies not supported yet".to_string()); - } - Some(sys::BodyParam::Data(data)) => Some(data.to_vec()), - None => None, - }; - - let method = match request.method { - sys::Method::Get => http::Method::GET, - sys::Method::Head => http::Method::HEAD, - sys::Method::Post => http::Method::POST, - sys::Method::Put => http::Method::PUT, - sys::Method::Delete => http::Method::DELETE, - sys::Method::Connect => http::Method::CONNECT, - sys::Method::Options => http::Method::OPTIONS, - sys::Method::Trace => http::Method::TRACE, - sys::Method::Patch => http::Method::PATCH, - sys::Method::Other(other) => return Err(format!("Unknown method: {other}")), - }; - - let req = crate::http::HttpRequest { - url: Url::parse(request.url).map_err(|e| e.to_string())?, - method, - headers, - body, - options: crate::http::HttpRequestOptions { - gzip: false, - cors_proxy: None, - }, - }; - let f = self_.client.request(req); - let res = InlineWaker::block_on(f).map_err(|e| e.to_string())?; - - let res_headers = res - .headers - .iter() - .map(|(name, value)| sys::HeaderResult { - key: name.to_string(), - value: value.as_bytes().to_vec(), - }) - .collect(); - - let res_body = if let Some(b) = res.body { - sys::BodyResult::Data(b) - } else { - sys::BodyResult::Data(Vec::new()) - }; - - Ok({ - sys::Response { - status: res.status.as_u16(), - headers: res_headers, - body: res_body, - // TODO: provide redirect urls? - redirect_urls: None, - } - }) - } -} diff --git a/lib/wasix/src/http/mod.rs b/lib/wasix/src/http/mod.rs index b2f6aebce56..97ca4954496 100644 --- a/lib/wasix/src/http/mod.rs +++ b/lib/wasix/src/http/mod.rs @@ -1,5 +1,4 @@ mod client; -pub mod client_impl; #[cfg(feature = "host-reqwest")] pub mod reqwest; diff --git a/lib/wasix/src/lib.rs b/lib/wasix/src/lib.rs index 7d2678d1535..c7d559f1f2b 100644 --- a/lib/wasix/src/lib.rs +++ b/lib/wasix/src/lib.rs @@ -55,9 +55,6 @@ mod state; mod syscalls; mod utils; -/// WAI based bindings. -mod bindings; - use std::sync::Arc; #[allow(unused_imports)] @@ -685,7 +682,7 @@ fn stub_initializer( // TODO: split function into two variants, one for JS and one for sys. // (this will make code less messy) fn import_object_for_all_wasi_versions( - module: &wasmer::Module, + _module: &wasmer::Module, store: &mut impl AsStoreMut, env: &FunctionEnv, ) -> (Imports, ModuleInitializer) { @@ -705,31 +702,7 @@ fn import_object_for_all_wasi_versions( "wasix_64v1" => exports_wasix_64v1, }; - // TODO: clean this up! - cfg_if::cfg_if! { - if #[cfg(feature = "sys")] { - // Check if the module needs http. - - let has_canonical_realloc = module.exports().any(|t| t.name() == "canonical_abi_realloc"); - let has_wasix_http_import = module.imports().any(|t| t.module() == "wasix_http_client_v1"); - - let init = if has_canonical_realloc && has_wasix_http_import { - let wenv = env.as_ref(store); - let http = crate::http::client_impl::WasixHttpClientImpl::new(wenv); - crate::bindings::wasix_http_client_v1::add_to_imports( - store, - &mut imports, - http, - ) - } else { - Box::new(stub_initializer) as ModuleInitializer - }; - } else { - // Prevents unused warning. - let _ = module; - let init = Box::new(stub_initializer) as ModuleInitializer; - } - } + let init = Box::new(stub_initializer) as ModuleInitializer; (imports, init) }