-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add the HTTP bindings to perform HTTP requests from workers (#168)
* feat: add the HTTP bindings to perform HTTP requests from workers * fix: exclude wit-bindgen-backport packages from cargo deny * fix: remove unnecesary channel when calling reqwest * fix: remove unnecesary intermediate variable * improve: implement From<reqwest::Error> to build the final error for the binding
- Loading branch information
1 parent
59a6d6f
commit 103f1cf
Showing
10 changed files
with
326 additions
and
11 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
// Copyright 2023 VMware, Inc. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
use reqwest::Method; | ||
use tokio::runtime::Builder; | ||
|
||
// Implement the HTTP bindings for the workers. | ||
wit_bindgen_wasmtime::export!({paths: ["../../wit/core/http.wit"]}); | ||
use http::{Http, HttpError, HttpMethod, HttpRequest, HttpRequestError, HttpResponse}; | ||
|
||
pub use http::add_to_linker; | ||
|
||
pub struct HttpBindings {} | ||
|
||
/// Map the reqwest error to a known http-error | ||
/// HttpError comes from the HTTP bindings | ||
impl From<reqwest::Error> for HttpError { | ||
fn from(value: reqwest::Error) -> Self { | ||
if value.is_timeout() { | ||
HttpError::Timeout | ||
} else if value.is_redirect() { | ||
HttpError::RedirectLoop | ||
} else if value.is_request() { | ||
HttpError::InvalidRequest | ||
} else if value.is_body() { | ||
HttpError::InvalidRequestBody | ||
} else if value.is_decode() { | ||
HttpError::InvalidResponseBody | ||
} else { | ||
HttpError::InternalError | ||
} | ||
} | ||
} | ||
|
||
impl Http for HttpBindings { | ||
fn send_http_request( | ||
&mut self, | ||
req: HttpRequest<'_>, | ||
) -> Result<HttpResponse, HttpRequestError> { | ||
// Create local variables from the request | ||
let mut headers = Vec::new(); | ||
let uri = req.uri.to_string(); | ||
let body = req.body.unwrap_or(&[]).to_vec(); | ||
|
||
for (key, value) in req.headers { | ||
headers.push((key.to_string(), value.to_string())); | ||
} | ||
|
||
// Run the request in an async thread | ||
let thread_result = std::thread::spawn(move || { | ||
Builder::new_current_thread() | ||
.enable_all() | ||
.build() | ||
.unwrap() | ||
.block_on(async { | ||
let client = reqwest::Client::new(); | ||
|
||
let method = match req.method { | ||
HttpMethod::Get => Method::GET, | ||
HttpMethod::Post => Method::POST, | ||
HttpMethod::Put => Method::PUT, | ||
HttpMethod::Patch => Method::PATCH, | ||
HttpMethod::Delete => Method::DELETE, | ||
HttpMethod::Options => Method::OPTIONS, | ||
HttpMethod::Head => Method::HEAD, | ||
}; | ||
|
||
let mut builder = client.request(method, uri); | ||
|
||
for (key, value) in headers { | ||
builder = builder.header(key, value); | ||
} | ||
|
||
builder = builder.body(body); | ||
|
||
match builder.send().await { | ||
Ok(res) => { | ||
let mut headers = Vec::new(); | ||
let status = res.status().as_u16(); | ||
|
||
for (name, value) in res.headers().iter() { | ||
headers | ||
.push((name.to_string(), value.to_str().unwrap().to_string())); | ||
} | ||
|
||
let body = res.bytes().await; | ||
|
||
Ok(HttpResponse { | ||
headers, | ||
status, | ||
body: Some(body.unwrap().to_vec()), | ||
}) | ||
} | ||
Err(e) => { | ||
let message = e.to_string(); | ||
|
||
// Manage the different possible errors from Reqwest | ||
Err(HttpRequestError { | ||
error: e.into(), | ||
message, | ||
}) | ||
} | ||
} | ||
}) | ||
}) | ||
.join(); | ||
|
||
match thread_result { | ||
Ok(res) => match res { | ||
Ok(res) => Ok(res), | ||
Err(err) => Err(err), | ||
}, | ||
Err(_) => Err(HttpRequestError { | ||
error: HttpError::InternalError, | ||
message: "There was an error processing the request on the host side.".to_string(), | ||
}), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
// Copyright 2023 VMware, Inc. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
pub mod http; |
Oops, something went wrong.