@@ -16,39 +16,34 @@ pub struct Response<B: Body> {
16
16
body : B ,
17
17
}
18
18
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
+ }
46
40
47
41
impl Response < IncomingBody > {
48
42
pub ( crate ) fn try_from_incoming_response ( incoming : IncomingResponse ) -> super :: Result < Self > {
49
43
let headers: HeaderMap = header_map_from_wasi ( incoming. headers ( ) ) ?;
50
44
let status = incoming. status ( ) . into ( ) ;
51
45
46
+ let kind = BodyKind :: from_headers ( & headers) ;
52
47
// `body_stream` is a child of `incoming_body` which means we cannot
53
48
// drop the parent before we drop the child
54
49
let incoming_body = incoming
@@ -59,6 +54,7 @@ impl Response<IncomingBody> {
59
54
. expect ( "cannot call `stream` twice on an incoming body" ) ;
60
55
61
56
let body = IncomingBody {
57
+ kind,
62
58
buf_offset : 0 ,
63
59
buf : None ,
64
60
body_stream,
@@ -97,6 +93,7 @@ impl<B: Body> Response<B> {
97
93
/// An incoming HTTP body
98
94
#[ derive( Debug ) ]
99
95
pub struct IncomingBody {
96
+ kind : BodyKind ,
100
97
buf : Option < Vec < u8 > > ,
101
98
// How many bytes have we already read from the buf?
102
99
buf_offset : usize ,
@@ -147,3 +144,18 @@ impl AsyncRead for IncomingBody {
147
144
Ok ( len)
148
145
}
149
146
}
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