diff --git a/Cargo.lock b/Cargo.lock index e94e0ba6..99fc7606 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -74,8 +74,8 @@ dependencies = [ "axum-core", "bytes", "futures-util", - "http 1.1.0", - "http-body 1.0.0", + "http", + "http-body", "http-body-util", "itoa", "matchit", @@ -100,8 +100,8 @@ dependencies = [ "async-trait", "bytes", "futures-util", - "http 1.1.0", - "http-body 1.0.0", + "http", + "http-body", "http-body-util", "mime", "pin-project-lite", @@ -583,16 +583,16 @@ checksum = "d2fabcfbdc87f4758337ca535fb41a6d701b65693ce38287d856d1674551ec9b" [[package]] name = "h2" -version = "0.3.25" +version = "0.4.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4fbd2820c5e49886948654ab546d0688ff24530286bdcf8fca3cefb16d4618eb" +checksum = "51ee2dd2e4f378392eeff5d51618cd9a63166a2513846bbc55f21cfacd9199d4" dependencies = [ "bytes", "fnv", "futures-core", "futures-sink", "futures-util", - "http 0.2.12", + "http", "indexmap 2.2.6", "slab", "tokio", @@ -648,17 +648,6 @@ dependencies = [ "digest", ] -[[package]] -name = "http" -version = "0.2.12" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "601cbb57e577e2f5ef5be8e7b83f0f63994f25aa94d673e54a92d5c516d101f1" -dependencies = [ - "bytes", - "fnv", - "itoa", -] - [[package]] name = "http" version = "1.1.0" @@ -670,17 +659,6 @@ dependencies = [ "itoa", ] -[[package]] -name = "http-body" -version = "0.4.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7ceab25649e9960c0311ea418d17bee82c0dcec1bd053b5f9a66e265a693bed2" -dependencies = [ - "bytes", - "http 0.2.12", - "pin-project-lite", -] - [[package]] name = "http-body" version = "1.0.0" @@ -688,7 +666,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1cac85db508abc24a2e48553ba12a996e87244a0395ce011e62b37158745d643" dependencies = [ "bytes", - "http 1.1.0", + "http", ] [[package]] @@ -699,8 +677,8 @@ checksum = "0475f8b2ac86659c21b64320d5d653f9efe42acd2a4e560073ec61a155a34f1d" dependencies = [ "bytes", "futures-core", - "http 1.1.0", - "http-body 1.0.0", + "http", + "http-body", "pin-project-lite", ] @@ -710,47 +688,60 @@ version = "1.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d897f394bad6a705d5f4104762e116a75639e470d80901eed05a860a95cb1904" -[[package]] -name = "httpdate" -version = "1.0.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "df3b46402a9d5adb4c86a0cf463f42e19994e3ee891101b1841f30a545cb49a9" - [[package]] name = "hyper" -version = "0.14.28" +version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bf96e135eb83a2a8ddf766e426a841d8ddd7449d5f00d34ea02b41d2f19eef80" +checksum = "186548d73ac615b32a73aafe38fb4f56c0d340e110e5a200bcadbaf2e199263a" dependencies = [ "bytes", "futures-channel", - "futures-core", "futures-util", "h2", - "http 0.2.12", - "http-body 0.4.6", + "http", + "http-body", "httparse", - "httpdate", "itoa", "pin-project-lite", - "socket2", + "smallvec", "tokio", - "tower-service", - "tracing", "want", ] [[package]] name = "hyper-tls" -version = "0.5.0" +version = "0.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d6183ddfa99b85da61a140bea0efc93fdf56ceaa041b37d553518030827f9905" +checksum = "70206fc6890eaca9fde8a0bf71caa2ddfc9fe045ac9e5c70df101a7dbde866e0" dependencies = [ "bytes", + "http-body-util", "hyper", + "hyper-util", "native-tls", "tokio", "tokio-native-tls", + "tower-service", +] + +[[package]] +name = "hyper-util" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ca38ef113da30126bbff9cd1705f9273e15d45498615d138b0c20279ac7a76aa" +dependencies = [ + "bytes", + "futures-channel", + "futures-util", + "http", + "http-body", + "hyper", + "pin-project-lite", + "socket2", + "tokio", + "tower", + "tower-service", + "tracing", ] [[package]] @@ -1377,9 +1368,9 @@ checksum = "adad44e29e4c806119491a7f06f03de4d1af22c3a680dd47f1e6e179439d1f56" [[package]] name = "reqwest" -version = "0.11.27" +version = "0.12.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dd67538700a17451e7cba03ac727fb961abb7607553461627b97de0b89cf4a62" +checksum = "2d66674f2b6fb864665eea7a3c1ac4e3dfacd2fda83cf6f935a612e01b0e3338" dependencies = [ "base64", "bytes", @@ -1387,10 +1378,12 @@ dependencies = [ "futures-core", "futures-util", "h2", - "http 0.2.12", - "http-body 0.4.6", + "http", + "http-body", + "http-body-util", "hyper", "hyper-tls", + "hyper-util", "ipnet", "js-sys", "log", @@ -1992,8 +1985,10 @@ dependencies = [ "futures-util", "pin-project", "pin-project-lite", + "tokio", "tower-layer", "tower-service", + "tracing", ] [[package]] @@ -2014,6 +2009,7 @@ version = "0.1.40" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c3523ab5a71916ccf420eebdf5521fcef02141234bbc0b8a49f2fdc4544364ef" dependencies = [ + "log", "pin-project-lite", "tracing-attributes", "tracing-core", @@ -2118,7 +2114,7 @@ dependencies = [ "byteorder", "bytes", "data-encoding", - "http 1.1.0", + "http", "httparse", "log", "rand", @@ -2692,11 +2688,12 @@ dependencies = [ "chrono-tz", "futures-channel", "futures-util", - "http 1.1.0", - "http-body 1.0.0", + "http", + "http-body", "js-sys", "matchit", "pin-project", + "reqwest", "serde", "serde-wasm-bindgen 0.6.5", "serde_json", @@ -2785,7 +2782,7 @@ dependencies = [ "futures-util", "getrandom", "hex", - "http 1.1.0", + "http", "md5", "rand", "regex", diff --git a/worker-kv/Cargo.toml b/worker-kv/Cargo.toml index 65ebda94..be31f131 100644 --- a/worker-kv/Cargo.toml +++ b/worker-kv/Cargo.toml @@ -21,7 +21,7 @@ serde-wasm-bindgen = "0.5.0" [dev-dependencies] fs_extra = "1.2.0" psutil = { git = "https://github.com/mygnu/rust-psutil", branch = "update-dependencies" } -reqwest = { version = "0.11.8", features = ["json"] } +reqwest = { version = "0.12.0", features = ["json"] } tokio = { version = "1.5.0", features = [ "rt", "macros", diff --git a/worker/Cargo.toml b/worker/Cargo.toml index 87d053ab..98f3ef23 100644 --- a/worker/Cargo.toml +++ b/worker/Cargo.toml @@ -28,6 +28,7 @@ http.workspace = true http-body = "1" matchit = "0.7" pin-project = "1.1.0" +reqwest = { version = "0.12.0" } serde = { version = "1.0.164", features = ["derive"] } serde_json = "1.0.96" tokio = { version = "1.28", default-features = false } diff --git a/worker/src/global.rs b/worker/src/global.rs index d9d7ea32..3f245b22 100644 --- a/worker/src/global.rs +++ b/worker/src/global.rs @@ -1,22 +1,25 @@ use std::ops::Deref; +use std::time::Duration; +use reqwest::{Body, Client, Error, Response}; +use tokio::time; use wasm_bindgen::JsCast; use wasm_bindgen_futures::JsFuture; use crate::{ - request::Request as WorkerRequest, response::Response as WorkerResponse, AbortSignal, Result, + AbortSignal, Result, }; /// Construct a Fetch call from a URL string or a Request object. Call its `send` method to execute /// the request. pub enum Fetch { Url(url::Url), - Request(WorkerRequest), + Request(Body), } impl Fetch { /// Execute a Fetch call and receive a Response. - pub async fn send(&self) -> Result { + pub async fn send(&self) -> Result { match self { Fetch::Url(url) => fetch_with_str(url.as_ref(), None).await, Fetch::Request(req) => fetch_with_request(req, None).await, @@ -24,7 +27,7 @@ impl Fetch { } /// Execute a Fetch call and receive a Response. - pub async fn send_with_signal(&self, signal: &AbortSignal) -> Result { + pub async fn send_with_signal(&self, signal: &AbortSignal) -> Result { match self { Fetch::Url(url) => fetch_with_str(url.as_ref(), Some(signal)).await, Fetch::Request(req) => fetch_with_request(req, Some(signal)).await, @@ -32,6 +35,41 @@ impl Fetch { } } +//#[cfg(feature = "http")] +async fn fetch_with_str(url: &str, signal: Option<&AbortSignal>) -> Result { + let client = Client::new(); + let request_future = client.get(url).send(); + let timeout_future = time::sleep(Duration::from_secs(10)); // TODO: Remove 10 seconds magic value. + tokio::select! { + result = request_future => { + // The request completed successfully + result.map_err(|err| err.into()) + }, + _ = timeout_future => { + // The timeout occurred before the request completed + Err(reqwest::Error::new(reqwest::StatusCode::REQUEST_TIMEOUT, "Request timed out")) + } + } +} + +//#[cfg(feature = "http")] +async fn fetch_with_request( + request: &Response, + signal: Option<&AbortSignal>, +) -> Result { + let mut init = web_sys::RequestInit::new(); + init.signal(signal.map(|x| x.deref())); + + let worker: web_sys::WorkerGlobalScope = js_sys::global().unchecked_into(); + let req = request.inner(); + let promise = worker.fetch_with_request_and_init(req, &init); + let resp = JsFuture::from(promise).await?; + let edge_response: web_sys::Response = resp.dyn_into()?; + Ok(edge_response.into()) +} + +/* +#[cfg(not(feature = "http"))] async fn fetch_with_str(url: &str, signal: Option<&AbortSignal>) -> Result { let mut init = web_sys::RequestInit::new(); init.signal(signal.map(|x| x.deref())); @@ -43,6 +81,7 @@ async fn fetch_with_str(url: &str, signal: Option<&AbortSignal>) -> Result, @@ -56,4 +95,4 @@ async fn fetch_with_request( let resp = JsFuture::from(promise).await?; let edge_response: web_sys::Response = resp.dyn_into()?; Ok(edge_response.into()) -} +}*/