Skip to content

Commit 7f23b8f

Browse files
committed
Add a way to consume trailers from an IncomingBody.
1 parent 083dc21 commit 7f23b8f

File tree

1 file changed

+25
-3
lines changed

1 file changed

+25
-3
lines changed

src/http/body.rs

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
//! HTTP body types
22
3+
use crate::http::fields::header_map_from_wasi;
34
use crate::io::{AsyncInputStream, AsyncOutputStream, AsyncRead, AsyncWrite, Cursor, Empty};
5+
use crate::runtime::AsyncPollable;
46
use core::fmt;
57
use http::header::{CONTENT_LENGTH, TRANSFER_ENCODING};
68
use wasi::http::types::IncomingBody as WasiIncomingBody;
@@ -116,9 +118,9 @@ impl Body for Empty {
116118
pub struct IncomingBody {
117119
kind: BodyKind,
118120
// IMPORTANT: the order of these fields here matters. `body_stream` must
119-
// be dropped before `_incoming_body`.
121+
// be dropped before `incoming_body`.
120122
body_stream: AsyncInputStream,
121-
_incoming_body: WasiIncomingBody,
123+
incoming_body: WasiIncomingBody,
122124
}
123125

124126
impl IncomingBody {
@@ -130,9 +132,29 @@ impl IncomingBody {
130132
Self {
131133
kind,
132134
body_stream,
133-
_incoming_body: incoming_body,
135+
incoming_body,
134136
}
135137
}
138+
139+
/// Consume this `IncomingBody` and return the trailers, if present.
140+
pub async fn finish(self) -> Result<Option<HeaderMap>, Error> {
141+
// The stream is a child resource of the `IncomingBody`, so ensure that
142+
// it's dropped first.
143+
drop(self.body_stream);
144+
145+
let trailers = WasiIncomingBody::finish(self.incoming_body);
146+
147+
AsyncPollable::new(trailers.subscribe()).wait_for().await;
148+
149+
let trailers = trailers.get().unwrap().unwrap()?;
150+
151+
let trailers = match trailers {
152+
None => None,
153+
Some(trailers) => Some(header_map_from_wasi(trailers)?),
154+
};
155+
156+
Ok(trailers)
157+
}
136158
}
137159

138160
impl AsyncRead for IncomingBody {

0 commit comments

Comments
 (0)