MCPcopy Create free account
hub / github.com/bytecodealliance/wstd / try_from_incoming

Function try_from_incoming

src/http/response.rs:10–38  ·  view source on GitHub ↗
(incoming: IncomingResponse)

Source from the content-addressed store, hash-verified

8pub use http::response::{Builder, Response};
9
10pub(crate) fn try_from_incoming(incoming: IncomingResponse) -> Result<Response<Body>, Error> {
11 let headers: HeaderMap = header_map_from_wasi(incoming.headers())?;
12 // TODO: Does WASI guarantee that the incoming status is valid?
13 let status = StatusCode::from_u16(incoming.status())
14 .map_err(|err| anyhow::anyhow!("wasi provided invalid status code ({err})"))?;
15
16 let hint = BodyHint::from_headers(&headers)?;
17 // `body_stream` is a child of `incoming_body` which means we cannot
18 // drop the parent before we drop the child
19 let incoming_body = incoming
20 .consume()
21 .expect("cannot call `consume` twice on incoming response");
22 let body = Body::from_incoming(incoming_body, hint);
23
24 let mut builder = Response::builder().status(status);
25 // The [`http::response::Builder`] keeps internal state of whether the
26 // builder has errored, which is only reachable by passing
27 // [`Builder::header`] an erroring `TryInto<HeaderName>` or
28 // `TryInto<HeaderValue>`. Since the `Builder::header` method is never
29 // used, we know `Builder::headers_mut` will never give the None case, nor
30 // will `Builder::body` give the error case. So, rather than treat those
31 // as control flow, we unwrap if this invariant is ever broken because
32 // that would only be possible due to some unrecoverable bug in wstd,
33 // rather than incorrect use or invalid input.
34 *builder.headers_mut().expect("builder has not errored") = headers;
35 Ok(builder
36 .body(body)
37 .expect("response builder should not error"))
38}

Callers

nothing calls this directly

Calls 1

header_map_from_wasiFunction · 0.85

Tested by

no test coverage detected