Skip to content

Commit

Permalink
code cleanup per PR feedback
Browse files Browse the repository at this point in the history
Signed-off-by: Joel Dice <joel.dice@fermyon.com>
  • Loading branch information
dicej committed Oct 31, 2023
1 parent 4f4e25c commit 0219a5d
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions examples/wasi-http-rust-streaming-outgoing-body/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,32 +99,33 @@ async fn handle_request(request: IncomingRequest, response_out: ResponseOutparam
.and_then(|v| Url::parse(v).ok())
}) {
match double_echo(request, &url).await {
Ok((request_copy, response)) => {
let mut stream = response.take_body_stream();
Ok((request_copy, incoming_response)) => {
let mut incoming_response_body = incoming_response.take_body_stream();

let response = OutgoingResponse::new(
let outgoing_response = OutgoingResponse::new(
200,
&Headers::new(
&headers
.into_iter()
.filter_map(|(k, v)| (k == "content-type").then_some((k, v)))
.filter(|(k, _)| k == "content-type")
.collect::<Vec<_>>(),
),
);

let mut body = response.take_body();
let mut outgoing_response_body = outgoing_response.take_body();

response_out.set(response);
response_out.set(outgoing_response);

let response_copy = async move {
while let Some(chunk) = stream.next().await {
body.send(chunk?).await?;
while let Some(chunk) = incoming_response_body.next().await {
outgoing_response_body.send(chunk?).await?;
}
Ok::<_, anyhow::Error>(())
};

let (request_copy, response_copy) =
future::join(request_copy, response_copy).await;

if let Err(e) = request_copy.and(response_copy) {
eprintln!("error piping to and from {url}: {e}");
}
Expand Down

0 comments on commit 0219a5d

Please # to comment.