Skip to content

Commit 74b6c09

Browse files
committed
change echo_headers to return the headers in the body
1 parent 101be5f commit 74b6c09

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

src/lib.rs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,21 @@ async fn echo(mut req: Request<IncomingBody>, res: Responder) -> Finished {
4747
Finished::finish(body, result, None)
4848
}
4949

50-
async fn echo_headers(req: Request<IncomingBody>, responder: Responder) -> Finished {
51-
let mut res = Response::builder();
52-
*res.headers_mut().unwrap() = req.into_parts().0.headers;
53-
let res = res.body(empty()).unwrap();
54-
responder.respond(res).await
50+
async fn echo_headers(req: Request<IncomingBody>, res: Responder) -> Finished {
51+
let mut body = res.start_response(Response::new(BodyForthcoming));
52+
let mut result = Ok(());
53+
54+
for (name, value) in req.headers().iter() {
55+
if let Err(e) = body
56+
.write_all(format!("{}: {}\n", name, value.to_str().unwrap_or("")).as_bytes())
57+
.await
58+
{
59+
result = Err(e);
60+
break;
61+
}
62+
}
63+
64+
Finished::finish(body, result, None)
5565
}
5666

5767
async fn echo_trailers(req: Request<IncomingBody>, res: Responder) -> Finished {

0 commit comments

Comments
 (0)