Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Web http client #4141

Merged
merged 3 commits into from
Aug 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions lib/wasi-web/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions lib/wasix/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ url = "2.3.1"
petgraph = "0.6.3"
rayon = { version = "1.7.0", optional = true }
wasm-bindgen = { version = "0.2.87", optional = true }
js-sys = { version = "0.3.64", optional = true }
wasm-bindgen-futures = { version = "0.4.37", optional = true }
web-sys = { version = "0.3.64", features = ["Request", "RequestInit", "Window", "WorkerGlobalScope", "RequestMode", "Response", "Headers"], optional = true }

[target.'cfg(not(target_arch = "riscv64"))'.dependencies.reqwest]
version = "0.11"
Expand All @@ -94,6 +97,7 @@ winapi = "0.3"
wasmer = { path = "../api", version = "=4.1.1", default-features = false, features = ["wat", "js-serializable-module"] }
tokio = { version = "1", features = [ "sync", "macros", "rt" ], default_features = false }
pretty_assertions = "1.3.0"
wasm-bindgen-test = "0.3.0"

[target.'cfg(target_arch = "wasm32")'.dev-dependencies]
wasm-bindgen-test = "0.3.0"
Expand All @@ -111,15 +115,15 @@ time = ["tokio/time"]
webc_runner_rt_wcgi = ["hyper", "wcgi", "wcgi-host", "tower", "tower-http"]
webc_runner_rt_emscripten = ["wasmer-emscripten"]

sys = ["webc/mmap", "time"]
sys = ["webc/mmap", "time", "virtual-mio/sys"]
sys-default = ["sys", "logging", "host-fs", "sys-poll", "sys-thread", "host-vnet", "host-threads", "host-reqwest"]
sys-poll = []
sys-thread = ["tokio/rt", "tokio/time", "tokio/rt-multi-thread", "rayon"]

# Deprecated. Kept it for compatibility
compiler = []

js = ["virtual-fs/no-time", "getrandom/js", "chrono"]
js = ["virtual-fs/no-time", "getrandom/js", "chrono", "js-sys", "wasm-bindgen", "wasm-bindgen-futures", "web-sys"]
js-default = ["js"]
test-js = ["js", "wasmer/wat"]

Expand Down
53 changes: 53 additions & 0 deletions lib/wasix/src/http/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,25 @@ pub struct HttpRequest {
pub options: HttpRequestOptions,
}

impl HttpRequest {
fn from_http_parts(parts: http::request::Parts, body: impl Into<Option<Vec<u8>>>) -> Self {
let http::request::Parts {
method,
uri,
headers,
..
} = parts;

HttpRequest {
url: uri.to_string().parse().unwrap(),
method,
headers,
body: body.into(),
options: HttpRequestOptions::default(),
}
}
}

impl std::fmt::Debug for HttpRequest {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let HttpRequest {
Expand All @@ -85,6 +104,40 @@ impl std::fmt::Debug for HttpRequest {
}
}

impl From<http::Request<Option<Vec<u8>>>> for HttpRequest {
fn from(value: http::Request<Option<Vec<u8>>>) -> Self {
let (parts, body) = value.into_parts();
HttpRequest::from_http_parts(parts, body)
}
}

impl From<http::Request<Vec<u8>>> for HttpRequest {
fn from(value: http::Request<Vec<u8>>) -> Self {
let (parts, body) = value.into_parts();
HttpRequest::from_http_parts(parts, body)
}
}

impl From<http::Request<&str>> for HttpRequest {
fn from(value: http::Request<&str>) -> Self {
value.map(|body| body.to_string()).into()
}
}

impl From<http::Request<String>> for HttpRequest {
fn from(value: http::Request<String>) -> Self {
let (parts, body) = value.into_parts();
HttpRequest::from_http_parts(parts, body.into_bytes())
}
}

impl From<http::Request<()>> for HttpRequest {
fn from(value: http::Request<()>) -> Self {
let (parts, _) = value.into_parts();
HttpRequest::from_http_parts(parts, None)
}
}

// TODO: use types from http crate?
pub struct HttpResponse {
pub body: Option<Vec<u8>>,
Expand Down
8 changes: 8 additions & 0 deletions lib/wasix/src/http/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ pub mod client_impl;
#[cfg(feature = "host-reqwest")]
pub mod reqwest;

#[cfg(feature = "js")]
mod web_http_client;
Michael-F-Bryan marked this conversation as resolved.
Show resolved Hide resolved

#[cfg(feature = "js")]
pub use self::web_http_client::WebHttpClient;

pub use self::client::*;

pub(crate) const USER_AGENT: &str = concat!(env!("CARGO_PKG_NAME"), "-", env!("CARGO_PKG_VERSION"));
Expand All @@ -13,6 +19,8 @@ pub fn default_http_client() -> Option<impl HttpClient + Send + Sync + 'static>
cfg_if::cfg_if! {
if #[cfg(feature = "host-reqwest")] {
Some(self::reqwest::ReqwestHttpClient::default())
} else if #[cfg(feature = "js")] {
Some(web_http_client::WebHttpClient::default())
} else {
// Note: We need something to use with turbofish otherwise returning
// a plain None will complain about not being able to infer the "T"
Expand Down
Loading