-
Notifications
You must be signed in to change notification settings - Fork 2
initial commit of http-client #1
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or 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 |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| # Maintainers | ||
|
|
||
| * Scott Andrews, [scothis](https://github.com/scothis) | ||
| * Mark Fisher, [markfisher](https://github.com/markfisher) | ||
This file contains hidden or 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 hidden or 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,12 @@ | ||
| [package] | ||
| name = "http-client" | ||
| version = "0.1.0" | ||
| edition = "2024" | ||
| license = "Apache-2.0" | ||
|
|
||
| [lib] | ||
| crate-type = ["cdylib"] | ||
|
|
||
| [dependencies] | ||
| url = "2.5" | ||
| wit-bindgen = { workspace = true, features = ["async-spawn"] } |
This file contains hidden or 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,23 @@ | ||
| # `http-client` | ||
|
|
||
| A higher-level HTTP client that delegates to wasi:http/client. | ||
|
|
||
| ## Request Functions | ||
|
|
||
| (each returns `result<http-response, string>`) | ||
|
|
||
| - `request(method, url, headers, body, options)` | ||
| - `get(url, headers, options)` | ||
| - `post(url, headers, body, options)` | ||
| - `put(url, headers, body, options)` | ||
| - `delete(url, headers, options)` | ||
| - `patch(url, headers, body, options)` | ||
| - `head(url, headers, options)` | ||
| - `options(url, headers, options)` | ||
| - `trace(url, headers, options)` | ||
| - `query(url, headers, body, options)` | ||
|
|
||
| ## The `http-client` World | ||
|
|
||
| - exports `componentized:http/client` | ||
| - imports `wasi:http/client@0.3.1` |
This file contains hidden or 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,248 @@ | ||
| use url::Url; | ||
|
|
||
| wit_bindgen::generate!({ | ||
| path: "../wit", | ||
| world: "http-client", | ||
| generate_all, | ||
| }); | ||
|
|
||
| use exports::componentized::http::client::{ | ||
| ErrorCode, Guest, HttpResponse, Method, RequestOptions, | ||
| }; | ||
|
|
||
| use wasi::http::types::{ | ||
| ErrorCode as WasiErrorCode, Fields, Method as WasiMethod, Request as WasiRequest, | ||
| RequestOptions as WasiRequestOptions, Response as WasiResponse, Scheme, Trailers, | ||
| }; | ||
| use wit_bindgen::rt::async_support::{FutureReader, StreamReader}; | ||
|
|
||
| struct HttpClient; | ||
|
|
||
| impl HttpClient { | ||
| async fn request( | ||
| method: WasiMethod, | ||
| url: String, | ||
| headers: Vec<(String, String)>, | ||
| body: Option<StreamReader<u8>>, | ||
| options: Option<RequestOptions>, | ||
| ) -> Result<HttpResponse, ErrorCode> { | ||
| let request_headers = Fields::new(); | ||
| for (name, value) in &headers { | ||
| request_headers | ||
| .append(name, value.as_bytes()) | ||
| .map_err(|e| err(format!("Invalid request header {name:?}: {e:?}")))?; | ||
| } | ||
|
|
||
| let parsed = Url::parse(&url).map_err(|e| err(format!("Invalid URL: {e}")))?; | ||
| let scheme = match parsed.scheme() { | ||
| "http" => Scheme::Http, | ||
| "https" => Scheme::Https, | ||
| other => return Err(err(format!("Unsupported URL scheme: {other}"))), | ||
| }; | ||
| let host = parsed | ||
| .host_str() | ||
| .ok_or_else(|| err("URL is missing a host".to_string()))?; | ||
| let authority = match parsed.port() { | ||
| Some(port) => format!("{host}:{port}"), | ||
| None => host.to_string(), | ||
| }; | ||
| let path_with_query = match parsed.query() { | ||
| Some(q) => format!("{}?{q}", parsed.path()), | ||
| None => parsed.path().to_string(), | ||
| }; | ||
|
|
||
| let (trailers_tx, trailers_rx) = wit_future::new(|| Ok(None)); | ||
| trailers_tx.write(Ok(None)); | ||
|
|
||
| let wasi_options = options.map(wasi_request_options).transpose()?; | ||
| let (request, send_result) = | ||
| WasiRequest::new(request_headers, body, trailers_rx, wasi_options); | ||
| request | ||
| .set_method(&method) | ||
| .map_err(|()| err("Failed to set request method".to_string()))?; | ||
| request | ||
| .set_scheme(Some(&scheme)) | ||
| .map_err(|()| err("Failed to set request scheme".to_string()))?; | ||
| request | ||
| .set_authority(Some(&authority)) | ||
| .map_err(|()| err("Failed to set request authority".to_string()))?; | ||
| request | ||
| .set_path_with_query(Some(&path_with_query)) | ||
| .map_err(|()| err("Failed to set request path".to_string()))?; | ||
|
|
||
| let response = wasi::http::client::send(request) | ||
| .await | ||
| .map_err(|e| err(format!("HTTP request failed: {e:?}")))?; | ||
|
|
||
| let status = response.get_status_code(); | ||
| let headers = read_fields(&response.get_headers()); | ||
|
|
||
| let (body_stream, wasi_trailers) = WasiResponse::consume_body(response, send_result); | ||
|
|
||
| Ok(HttpResponse { | ||
| status, | ||
| headers, | ||
| body: body_stream, | ||
| trailers: map_trailers(wasi_trailers), | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| fn err(message: String) -> ErrorCode { | ||
| ErrorCode::Other(Some(message)) | ||
| } | ||
|
|
||
| fn read_fields(fields: &Fields) -> Vec<(String, String)> { | ||
| fields | ||
| .copy_all() | ||
| .into_iter() | ||
| .map(|(k, v)| (k, v.into_iter().map(|b| b as char).collect())) | ||
| .collect() | ||
| } | ||
|
|
||
| fn map_trailers( | ||
| wasi: FutureReader<Result<Option<Trailers>, WasiErrorCode>>, | ||
| ) -> FutureReader<Result<Vec<(String, String)>, ErrorCode>> { | ||
| let (tx, rx) = wit_future::new(|| Ok(Vec::new())); | ||
| wit_bindgen::rt::async_support::spawn_local(async move { | ||
| let resolved = match wasi.await { | ||
| Ok(Some(t)) => Ok(read_fields(&t)), | ||
| Ok(None) => Ok(Vec::new()), | ||
| Err(e) => Err(err(format!("wasi:http error: {e:?}"))), | ||
| }; | ||
| tx.write(resolved); | ||
| }); | ||
| rx | ||
| } | ||
|
|
||
| fn wasi_request_options(opts: RequestOptions) -> Result<WasiRequestOptions, ErrorCode> { | ||
| let r = WasiRequestOptions::new(); | ||
| if let Some(ms) = opts.connect_timeout_ms { | ||
| r.set_connect_timeout(Some(ms_to_ns(ms))) | ||
| .map_err(|e| err(format!("connect-timeout: {e:?}")))?; | ||
| } | ||
| if let Some(ms) = opts.first_byte_timeout_ms { | ||
| r.set_first_byte_timeout(Some(ms_to_ns(ms))) | ||
| .map_err(|e| err(format!("first-byte-timeout: {e:?}")))?; | ||
| } | ||
| if let Some(ms) = opts.between_bytes_timeout_ms { | ||
| r.set_between_bytes_timeout(Some(ms_to_ns(ms))) | ||
| .map_err(|e| err(format!("between-bytes-timeout: {e:?}")))?; | ||
| } | ||
| Ok(r) | ||
| } | ||
|
|
||
| fn ms_to_ns(ms: u32) -> u64 { | ||
| u64::from(ms) * 1_000_000 | ||
| } | ||
|
|
||
| fn to_wasi_method(method: Method) -> WasiMethod { | ||
| match method { | ||
| Method::Get => WasiMethod::Get, | ||
| Method::Post => WasiMethod::Post, | ||
| Method::Put => WasiMethod::Put, | ||
| Method::Delete => WasiMethod::Delete, | ||
| Method::Patch => WasiMethod::Patch, | ||
| Method::Head => WasiMethod::Head, | ||
| Method::Options => WasiMethod::Options, | ||
| Method::Trace => WasiMethod::Trace, | ||
| Method::Query => WasiMethod::Other("QUERY".to_string()), | ||
| } | ||
| } | ||
|
|
||
| impl Guest for HttpClient { | ||
| async fn request( | ||
| method: Method, | ||
| url: String, | ||
| headers: Vec<(String, String)>, | ||
| body: Option<StreamReader<u8>>, | ||
| options: Option<RequestOptions>, | ||
| ) -> Result<HttpResponse, ErrorCode> { | ||
| Self::request(to_wasi_method(method), url, headers, body, options).await | ||
| } | ||
|
|
||
| async fn get( | ||
| url: String, | ||
| headers: Vec<(String, String)>, | ||
| options: Option<RequestOptions>, | ||
| ) -> Result<HttpResponse, ErrorCode> { | ||
| Self::request(WasiMethod::Get, url, headers, None, options).await | ||
| } | ||
|
|
||
| async fn post( | ||
| url: String, | ||
| headers: Vec<(String, String)>, | ||
| body: StreamReader<u8>, | ||
| options: Option<RequestOptions>, | ||
| ) -> Result<HttpResponse, ErrorCode> { | ||
| Self::request(WasiMethod::Post, url, headers, Some(body), options).await | ||
| } | ||
|
|
||
| async fn put( | ||
| url: String, | ||
| headers: Vec<(String, String)>, | ||
| body: StreamReader<u8>, | ||
| options: Option<RequestOptions>, | ||
| ) -> Result<HttpResponse, ErrorCode> { | ||
| Self::request(WasiMethod::Put, url, headers, Some(body), options).await | ||
| } | ||
|
|
||
| async fn delete( | ||
| url: String, | ||
| headers: Vec<(String, String)>, | ||
| options: Option<RequestOptions>, | ||
| ) -> Result<HttpResponse, ErrorCode> { | ||
| Self::request(WasiMethod::Delete, url, headers, None, options).await | ||
| } | ||
|
|
||
| async fn patch( | ||
| url: String, | ||
| headers: Vec<(String, String)>, | ||
| body: StreamReader<u8>, | ||
| options: Option<RequestOptions>, | ||
| ) -> Result<HttpResponse, ErrorCode> { | ||
| Self::request(WasiMethod::Patch, url, headers, Some(body), options).await | ||
| } | ||
|
|
||
| async fn head( | ||
| url: String, | ||
| headers: Vec<(String, String)>, | ||
| options: Option<RequestOptions>, | ||
| ) -> Result<HttpResponse, ErrorCode> { | ||
| Self::request(WasiMethod::Head, url, headers, None, options).await | ||
| } | ||
|
|
||
| async fn options( | ||
| url: String, | ||
| headers: Vec<(String, String)>, | ||
| options: Option<RequestOptions>, | ||
| ) -> Result<HttpResponse, ErrorCode> { | ||
| Self::request(WasiMethod::Options, url, headers, None, options).await | ||
| } | ||
|
|
||
| async fn trace( | ||
| url: String, | ||
| headers: Vec<(String, String)>, | ||
| options: Option<RequestOptions>, | ||
| ) -> Result<HttpResponse, ErrorCode> { | ||
| Self::request(WasiMethod::Trace, url, headers, None, options).await | ||
| } | ||
|
|
||
| async fn query( | ||
| url: String, | ||
| headers: Vec<(String, String)>, | ||
| body: StreamReader<u8>, | ||
| options: Option<RequestOptions>, | ||
| ) -> Result<HttpResponse, ErrorCode> { | ||
| Self::request( | ||
| to_wasi_method(Method::Query), | ||
| url, | ||
| headers, | ||
| Some(body), | ||
| options, | ||
| ) | ||
| .await | ||
| } | ||
| } | ||
|
|
||
| export!(HttpClient); |
69 changes: 67 additions & 2 deletions
69
components/wit/deps/componentized-http-0.0.0-0/package.wit
This file contains hidden or 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 |
|---|---|---|
| @@ -1,6 +1,71 @@ | ||
| package componentized:http@0.0.0-0; | ||
|
|
||
| interface client { | ||
| enum method { | ||
| get, | ||
| post, | ||
| put, | ||
| delete, | ||
| patch, | ||
| head, | ||
| options, | ||
| trace, | ||
| query, | ||
| } | ||
|
|
||
| variant error-code { | ||
| other(option<string>), | ||
| } | ||
|
|
||
| /// Per-request options. None fields fall through to host defaults. | ||
| record request-options { | ||
| connect-timeout-ms: option<u32>, | ||
| first-byte-timeout-ms: option<u32>, | ||
| between-bytes-timeout-ms: option<u32>, | ||
| } | ||
|
|
||
| /// A streaming HTTP response. The status and headers are available | ||
| /// immediately; the body streams as `body`, and trailers (if any) resolve | ||
| /// via `trailers` once the body stream is fully consumed. | ||
| record http-response { | ||
| status: u16, | ||
| headers: list<tuple<string, string>>, | ||
| body: stream<u8>, | ||
| trailers: future<result<list<tuple<string, string>>, error-code>>, | ||
| } | ||
|
|
||
| /// Send an HTTP request with an explicit method. Both the request body and | ||
| /// the response body stream. | ||
| request: async func(method: method, url: string, headers: list<tuple<string, string>>, body: option<stream<u8>>, options: option<request-options>) -> result<http-response, error-code>; | ||
|
|
||
| /// HTTP GET request. | ||
| get: async func(url: string, headers: list<tuple<string, string>>, options: option<request-options>) -> result<http-response, error-code>; | ||
|
|
||
| /// HTTP POST request. | ||
| post: async func(url: string, headers: list<tuple<string, string>>, body: stream<u8>, options: option<request-options>) -> result<http-response, error-code>; | ||
|
|
||
| /// HTTP PUT request. | ||
| put: async func(url: string, headers: list<tuple<string, string>>, body: stream<u8>, options: option<request-options>) -> result<http-response, error-code>; | ||
|
|
||
| /// HTTP DELETE request. | ||
| delete: async func(url: string, headers: list<tuple<string, string>>, options: option<request-options>) -> result<http-response, error-code>; | ||
|
|
||
| /// HTTP PATCH request. | ||
| patch: async func(url: string, headers: list<tuple<string, string>>, body: stream<u8>, options: option<request-options>) -> result<http-response, error-code>; | ||
|
|
||
| /// HTTP HEAD request. | ||
| head: async func(url: string, headers: list<tuple<string, string>>, options: option<request-options>) -> result<http-response, error-code>; | ||
|
|
||
| /// HTTP OPTIONS request. | ||
| options: async func(url: string, headers: list<tuple<string, string>>, options: option<request-options>) -> result<http-response, error-code>; | ||
|
|
||
| /// HTTP TRACE request. | ||
| trace: async func(url: string, headers: list<tuple<string, string>>, options: option<request-options>) -> result<http-response, error-code>; | ||
|
|
||
| /// HTTP QUERY request. | ||
| query: async func(url: string, headers: list<tuple<string, string>>, body: stream<u8>, options: option<request-options>) -> result<http-response, error-code>; | ||
| } | ||
|
|
||
| world imports { | ||
| import wasi:clocks/types@0.3.1; | ||
| import wasi:http/types@0.3.1; | ||
| import client; | ||
| } |
This file contains hidden or 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 |
|---|---|---|
| @@ -1,2 +1,6 @@ | ||
| package componentized:components; | ||
|
|
||
| world http-client { | ||
| import wasi:http/client@0.3.1; | ||
| export componentized:http/client@0.0.0-0; | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.