Skip to content

Commit f208fd7

Browse files
committed
Propagate the Client.finish error to the proxy server's response
1 parent 2142719 commit f208fd7

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

examples/http_server_proxy.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
// Run the example with:
2-
// wasmtime serve -Scli -Shttp --env TARGET_URL=https://example.com http_server_proxy.wasm
2+
// cargo build --example http_server_proxy --target=wasm32-wasip2
3+
// wasmtime serve -Scli -Shttp --env TARGET_URL=https://example.com/ target/wasm32-wasip2/debug/examples/http_server_proxy.wasm
4+
// Test with `curl --no-buffer -v 127.0.0.1:8080/proxy/`
35
use futures_concurrency::prelude::*;
46
use wstd::http::body::{BodyForthcoming, IncomingBody};
57
use wstd::http::server::{Finished, Responder};
68
use wstd::http::{Client, Request, Response, StatusCode, Uri};
79
use wstd::io::{copy, empty};
810

9-
const PROXY_PREFIX: &str = "/proxy";
11+
const PROXY_PREFIX: &str = "/proxy/";
1012

1113
#[wstd::http_server]
1214
async fn main(mut server_req: Request<IncomingBody>, responder: Responder) -> Finished {
@@ -23,6 +25,7 @@ async fn main(mut server_req: Request<IncomingBody>, responder: Responder) -> Fi
2325
)
2426
.parse()
2527
.expect("final target url should be parseable");
28+
println!("Proxying to {target_url}");
2629

2730
let client = Client::new();
2831
let mut client_req = Request::builder();
@@ -45,9 +48,14 @@ async fn main(mut server_req: Request<IncomingBody>, responder: Responder) -> Fi
4548
// Copy the server request body to client's request body.
4649
let server_req_to_client_req = async {
4750
let res = copy(server_req.body_mut(), &mut client_request_body).await;
48-
// TODO: Convert to io error if necessary
49-
let _ = Client::finish(client_request_body, None);
50-
res
51+
Client::finish(client_request_body, None)
52+
.map_err(|_http_err| {
53+
std::io::Error::new(
54+
std::io::ErrorKind::InvalidData,
55+
"Failed to read HTTP request body",
56+
)
57+
})
58+
.and(res)
5159
};
5260

5361
// Copy the client response headers to server response.

0 commit comments

Comments
 (0)