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

Method respond

src/http/server.rs:38–70  ·  view source on GitHub ↗
(self, response: Response<B>)

Source from the content-addressed store, hash-verified

36 /// This is used by the `http_server` macro.
37 #[doc(hidden)]
38 pub async fn respond<B: Into<Body>>(self, response: Response<B>) -> Result<(), Error> {
39 let headers = response.headers();
40 let status = response.status().as_u16();
41
42 let wasi_headers = header_map_to_wasi(headers).expect("header error");
43
44 // Consume the `response` and prepare to write the body.
45 let body = response.into_body().into();
46
47 // Automatically add a Content-Length header.
48 if let Some(len) = body.content_length() {
49 let mut buffer = itoa::Buffer::new();
50 wasi_headers
51 .append(CONTENT_LENGTH.as_str(), buffer.format(len).as_bytes())
52 .unwrap();
53 }
54
55 let wasi_response = OutgoingResponse::new(wasi_headers);
56
57 // Unwrap because `StatusCode` has already validated the status.
58 wasi_response.set_status_code(status).unwrap();
59
60 // Unwrap because we can be sure we only call these once.
61 let wasi_body = wasi_response.body().unwrap();
62
63 // Set the outparam to the response, which allows wasi-http to send
64 // the response status and headers.
65 ResponseOutparam::set(self.outparam, Ok(wasi_response));
66
67 // Then send the body. The response will be fully sent once this
68 // future is ready.
69 body.send(wasi_body).await
70 }
71
72 /// This is used by the `http_server` macro.
73 #[doc(hidden)]

Callers

nothing calls this directly

Calls 3

header_map_to_wasiFunction · 0.85
content_lengthMethod · 0.80
sendMethod · 0.45

Tested by

no test coverage detected