-
Notifications
You must be signed in to change notification settings - Fork 16
Add a http server proxy example #66
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
+101
−6
Merged
Changes from 7 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
4f49910
Add a http server proxy example
tomasol 1af8d33
Propagate the `Client.finish` error to the proxy server's response
tomasol 884df59
Add a simplified reverse proxy example
tomasol 4e1b606
Fix a typo
tomasol 0c2b8ab
wip: Partially the proxy example to `wstd`
tomasol e653a1f
Finish the streaming proxy example
tomasol 04395ab
Simplify the response creation
tomasol b0db315
Skip copying request/response body contents through guest memory
tomasol 0be8bd6
Add `http_server_proxy` test
tomasol 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
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,65 @@ | ||
| // Run the example with: | ||
| // cargo build --example http_server_proxy --target=wasm32-wasip2 | ||
| // wasmtime serve -Scli -Shttp --env TARGET_URL=https://example.com/ target/wasm32-wasip2/debug/examples/http_server_proxy.wasm | ||
| // curl --no-buffer -v 127.0.0.1:8080/proxy/ | ||
|
|
||
| use wstd::http::body::Body; | ||
| use wstd::http::{Client, Error, Request, Response, StatusCode, Uri}; | ||
|
|
||
| const PROXY_PREFIX: &str = "/proxy/"; | ||
|
|
||
| #[wstd::http_server] | ||
| async fn main(server_req: Request<Body>) -> Result<Response<Body>, Error> { | ||
| match server_req.uri().path_and_query().unwrap().as_str() { | ||
| api_prefixed_path if api_prefixed_path.starts_with(PROXY_PREFIX) => { | ||
| // Remove PROXY_PREFIX | ||
| let target_url = | ||
| std::env::var("TARGET_URL").expect("missing environment variable TARGET_URL"); | ||
| let target_url: Uri = format!( | ||
| "{target_url}{}", | ||
| api_prefixed_path | ||
| .strip_prefix(PROXY_PREFIX) | ||
| .expect("checked above") | ||
| ) | ||
| .parse() | ||
| .expect("final target url should be parseable"); | ||
| println!("Proxying to {target_url}"); | ||
| proxy(server_req, target_url).await | ||
| } | ||
| _ => Ok(http_not_found(server_req)), | ||
| } | ||
| } | ||
|
|
||
| async fn proxy(server_req: Request<Body>, target_url: Uri) -> Result<Response<Body>, Error> { | ||
| let client = Client::new(); | ||
| let mut client_req = Request::builder(); | ||
| client_req = client_req.uri(target_url).method(server_req.method()); | ||
|
|
||
| // Copy headers from `server_req` to the `client_req`. | ||
| for (key, value) in server_req.headers() { | ||
| client_req = client_req.header(key, value); | ||
| } | ||
|
|
||
| // Stream the request body. | ||
| let client_body = Body::from_http_body(server_req.into_body().into_boxed_body()); | ||
| let client_req = client_req.body(client_body)?; | ||
| // Send the request. | ||
| let client_resp = client.send(client_req).await?; | ||
| // Copy headers from `client_resp` to `server_resp`. | ||
| let mut server_resp = Response::builder(); | ||
| for (key, value) in client_resp.headers() { | ||
| server_resp | ||
| .headers_mut() | ||
| .expect("no errors could be in ResponseBuilder") | ||
| .append(key, value.clone()); | ||
| } | ||
| let resp_body = Body::from_http_body(client_resp.into_body().into_boxed_body()); | ||
| Ok(server_resp.body(resp_body)?) | ||
| } | ||
|
|
||
| fn http_not_found(_request: Request<Body>) -> Response<Body> { | ||
| Response::builder() | ||
| .status(StatusCode::NOT_FOUND) | ||
| .body(Body::empty()) | ||
| .unwrap() | ||
| } | ||
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.