Skip to content

Commit 7b3d75b

Browse files
authored
Merge pull request #84 from pchickey/pch/wstd_0.6
update to use wstd 0.6
2 parents 343b716 + 3fafb7e commit 7b3d75b

3 files changed

Lines changed: 149 additions & 66 deletions

File tree

Cargo.lock

Lines changed: 109 additions & 21 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@ version = "0.0.0"
44
edition = "2021"
55
publish = false
66

7-
[lib]
8-
crate-type = ["cdylib"]
9-
107
[dependencies]
11-
wit-bindgen-rt = { version = "0.41.0", features = ["bitflags"] }
12-
wstd = "0.5.4"
8+
http-body-util = "0.1.3"
9+
wstd = "0.6"

src/lib.rs

Lines changed: 38 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,24 @@
1-
use wstd::http::body::{BodyForthcoming, IncomingBody};
2-
use wstd::http::server::{Finished, Responder};
3-
use wstd::http::{IntoBody, Request, Response, StatusCode};
4-
use wstd::io::{copy, empty, AsyncWrite};
1+
use wstd::http::{body::Bytes, Body, Request, Response, Result, StatusCode};
52
use wstd::time::{Duration, Instant};
63

74
#[wstd::http_server]
8-
async fn main(req: Request<IncomingBody>, res: Responder) -> Finished {
5+
async fn main(req: Request<Body>) -> Result<Response<Body>> {
96
match req.uri().path_and_query().unwrap().as_str() {
10-
"/wait" => wait(req, res).await,
11-
"/echo" => echo(req, res).await,
12-
"/echo-headers" => echo_headers(req, res).await,
13-
"/echo-trailers" => echo_trailers(req, res).await,
14-
"/" => home(req, res).await,
15-
_ => not_found(req, res).await,
7+
"/wait" => wait(req).await,
8+
"/echo" => echo(req).await,
9+
"/echo-headers" => echo_headers(req).await,
10+
"/echo-trailers" => echo_trailers(req).await,
11+
"/" => home(req).await,
12+
_ => not_found(req).await,
1613
}
1714
}
1815

19-
async fn home(_req: Request<IncomingBody>, res: Responder) -> Finished {
20-
// To send a single string as the response body, use `res::respond`.
21-
res.respond(Response::new("Hello, wasi:http/proxy world!\n".into_body()))
22-
.await
16+
async fn home(_req: Request<Body>) -> Result<Response<Body>> {
17+
// To send a single string as the response body:
18+
Ok(Response::new("Hello, wasi:http/proxy world!\n".into()))
2319
}
2420

25-
async fn wait(_req: Request<IncomingBody>, res: Responder) -> Finished {
21+
async fn wait(_req: Request<Body>) -> Result<Response<Body>> {
2622
// Get the time now
2723
let now = Instant::now();
2824

@@ -32,41 +28,43 @@ async fn wait(_req: Request<IncomingBody>, res: Responder) -> Finished {
3228
// Compute how long we slept for.
3329
let elapsed = Instant::now().duration_since(now).as_millis();
3430

35-
// To stream data to the response body, use `res::start_response`.
36-
let mut body = res.start_response(Response::new(BodyForthcoming));
37-
let result = body
38-
.write_all(format!("slept for {elapsed} millis\n").as_bytes())
39-
.await;
40-
Finished::finish(body, result, None)
31+
Ok(Response::new(
32+
format!("slept for {elapsed} millis\n").into(),
33+
))
4134
}
4235

43-
async fn echo(mut req: Request<IncomingBody>, res: Responder) -> Finished {
36+
async fn echo(req: Request<Body>) -> Result<Response<Body>> {
4437
// Stream data from the req body to the response body.
45-
let mut body = res.start_response(Response::new(BodyForthcoming));
46-
let result = copy(req.body_mut(), &mut body).await;
47-
Finished::finish(body, result, None)
38+
let req_body = req.into_body();
39+
Ok(Response::new(req_body))
4840
}
4941

50-
async fn echo_headers(req: Request<IncomingBody>, responder: Responder) -> Finished {
42+
async fn echo_headers(req: Request<Body>) -> Result<Response<Body>> {
5143
let mut res = Response::builder();
5244
*res.headers_mut().unwrap() = req.into_parts().0.headers;
53-
let res = res.body(empty()).unwrap();
54-
responder.respond(res).await
45+
Ok(res.body(().into()).expect("builder success"))
5546
}
5647

57-
async fn echo_trailers(req: Request<IncomingBody>, res: Responder) -> Finished {
58-
let body = res.start_response(Response::new(BodyForthcoming));
59-
let (trailers, result) = match req.into_body().finish().await {
60-
Ok(trailers) => (trailers, Ok(())),
61-
Err(err) => (Default::default(), Err(std::io::Error::other(err))),
48+
async fn echo_trailers(req: Request<Body>) -> Result<Response<Body>> {
49+
use http_body_util::{BodyExt, Full};
50+
let collected = req.into_body().into_boxed_body().collect().await?;
51+
let (trailers, report) = if let Some(trailers) = collected.trailers() {
52+
(
53+
Some(Ok(trailers.clone())),
54+
format!("recieved trailers: {trailers:?}"),
55+
)
56+
} else {
57+
(None, "request had no trailers".to_owned())
6258
};
63-
Finished::finish(body, result, trailers)
59+
60+
Ok(Response::new(Body::from_http_body(
61+
Full::new(Bytes::from(report)).with_trailers(async { trailers }),
62+
)))
6463
}
6564

66-
async fn not_found(_req: Request<IncomingBody>, responder: Responder) -> Finished {
67-
let res = Response::builder()
65+
async fn not_found(_req: Request<Body>) -> Result<Response<Body>> {
66+
Ok(Response::builder()
6867
.status(StatusCode::NOT_FOUND)
69-
.body(empty())
70-
.unwrap();
71-
responder.respond(res).await
68+
.body(().into())
69+
.expect("builder succeeds"))
7270
}

0 commit comments

Comments
 (0)