Skip to content

Commit 02807a3

Browse files
committed
impl Body for IncomingBody. uncomment the BodyKind stuff and use it.
1 parent 42a3829 commit 02807a3

File tree

1 file changed

+39
-27
lines changed

1 file changed

+39
-27
lines changed

src/http/response.rs

Lines changed: 39 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -16,39 +16,34 @@ pub struct Response<B: Body> {
1616
body: B,
1717
}
1818

19-
// #[derive(Debug)]
20-
// enum BodyKind {
21-
// Fixed(u64),
22-
// Chunked,
23-
// }
24-
25-
// impl BodyKind {
26-
// fn from_headers(headers: &Fields) -> BodyKind {
27-
// dbg!(&headers);
28-
// if let Some(values) = headers.0.get("content-length") {
29-
// let value = values
30-
// .get(0)
31-
// .expect("no value found for content-length; violates HTTP/1.1");
32-
// let content_length = String::from_utf8(value.to_owned())
33-
// .unwrap()
34-
// .parse::<u64>()
35-
// .expect("content-length should be a u64; violates HTTP/1.1");
36-
// BodyKind::Fixed(content_length)
37-
// } else if let Some(values) = headers.0.get("transfer-encoding") {
38-
// dbg!(values);
39-
// BodyKind::Chunked
40-
// } else {
41-
// dbg!("Encoding neither has a content-length nor transfer-encoding");
42-
// BodyKind::Chunked
43-
// }
44-
// }
45-
// }
19+
#[derive(Debug)]
20+
enum BodyKind {
21+
Fixed(u64),
22+
Chunked,
23+
}
24+
25+
impl BodyKind {
26+
fn from_headers(headers: &HeaderMap) -> BodyKind {
27+
if let Some(value) = headers.get("content-length") {
28+
let content_length = std::str::from_utf8(value.as_ref())
29+
.unwrap()
30+
.parse::<u64>()
31+
.expect("content-length should be a u64; violates HTTP/1.1");
32+
BodyKind::Fixed(content_length)
33+
} else if headers.contains_key("transfer-encoding") {
34+
BodyKind::Chunked
35+
} else {
36+
BodyKind::Chunked
37+
}
38+
}
39+
}
4640

4741
impl Response<IncomingBody> {
4842
pub(crate) fn try_from_incoming_response(incoming: IncomingResponse) -> super::Result<Self> {
4943
let headers: HeaderMap = header_map_from_wasi(incoming.headers())?;
5044
let status = incoming.status().into();
5145

46+
let kind = BodyKind::from_headers(&headers);
5247
// `body_stream` is a child of `incoming_body` which means we cannot
5348
// drop the parent before we drop the child
5449
let incoming_body = incoming
@@ -59,6 +54,7 @@ impl Response<IncomingBody> {
5954
.expect("cannot call `stream` twice on an incoming body");
6055

6156
let body = IncomingBody {
57+
kind,
6258
buf_offset: 0,
6359
buf: None,
6460
body_stream,
@@ -97,6 +93,7 @@ impl<B: Body> Response<B> {
9793
/// An incoming HTTP body
9894
#[derive(Debug)]
9995
pub struct IncomingBody {
96+
kind: BodyKind,
10097
buf: Option<Vec<u8>>,
10198
// How many bytes have we already read from the buf?
10299
buf_offset: usize,
@@ -147,3 +144,18 @@ impl AsyncRead for IncomingBody {
147144
Ok(len)
148145
}
149146
}
147+
148+
impl Body for IncomingBody {
149+
fn len(&self) -> Option<usize> {
150+
match self.kind {
151+
BodyKind::Fixed(l) => {
152+
if l > (usize::MAX as u64) {
153+
None
154+
} else {
155+
Some(l as usize)
156+
}
157+
}
158+
BodyKind::Chunked => None,
159+
}
160+
}
161+
}

0 commit comments

Comments
 (0)