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

Function http_stream_body

examples/http_server.rs:66–88  ·  view source on GitHub ↗
(_request: Request<Body>)

Source from the content-addressed store, hash-verified

64}
65
66async fn http_stream_body(_request: Request<Body>) -> Result<Response<Body>> {
67 // Get the time now
68 let start = Instant::now();
69
70 let body = move |iters: usize| async move {
71 if iters == 0 {
72 return None;
73 }
74 // Sleep for 0.1 second.
75 wstd::task::sleep(Duration::from_millis(100)).await;
76
77 // Compute how long we slept for.
78 let elapsed = Instant::now().duration_since(start).as_millis();
79 Some((
80 Ok::<_, Infallible>(Bytes::from(format!(
81 "stream started {elapsed} millis ago\n"
82 ))),
83 iters - 1,
84 ))
85 };
86
87 Ok(Response::new(Body::from_try_stream(unfold(5, body))))
88}
89
90async fn http_echo(request: Request<Body>) -> Result<Response<Body>> {
91 let (_parts, body) = request.into_parts();

Callers 1

mainFunction · 0.85

Calls 3

sleepFunction · 0.85
as_millisMethod · 0.80
duration_sinceMethod · 0.80

Tested by

no test coverage detected