1
1
use wasi:: http:: types:: { IncomingBody as WasiIncomingBody , IncomingResponse } ;
2
2
use wasi:: io:: streams:: { InputStream , StreamError } ;
3
3
4
- use super :: { fields:: header_map_from_wasi, Body , HeaderMap , StatusCode } ;
4
+ use super :: { fields:: header_map_from_wasi, Body , Error , HeaderMap , Result , StatusCode } ;
5
5
use crate :: io:: AsyncRead ;
6
6
use crate :: runtime:: Reactor ;
7
7
@@ -23,27 +23,29 @@ enum BodyKind {
23
23
}
24
24
25
25
impl BodyKind {
26
- fn from_headers ( headers : & HeaderMap ) -> BodyKind {
26
+ fn from_headers ( headers : & HeaderMap ) -> Result < BodyKind > {
27
27
if let Some ( value) = headers. get ( "content-length" ) {
28
28
let content_length = std:: str:: from_utf8 ( value. as_ref ( ) )
29
29
. unwrap ( )
30
30
. parse :: < u64 > ( )
31
- . expect ( "content-length should be a u64; violates HTTP/1.1" ) ;
32
- BodyKind :: Fixed ( content_length)
31
+ . map_err ( |_| {
32
+ Error :: other ( "incoming content-length should be a u64; violates HTTP/1.1" )
33
+ } ) ?;
34
+ Ok ( BodyKind :: Fixed ( content_length) )
33
35
} else if headers. contains_key ( "transfer-encoding" ) {
34
- BodyKind :: Chunked
36
+ Ok ( BodyKind :: Chunked )
35
37
} else {
36
- BodyKind :: Chunked
38
+ Ok ( BodyKind :: Chunked )
37
39
}
38
40
}
39
41
}
40
42
41
43
impl Response < IncomingBody > {
42
- pub ( crate ) fn try_from_incoming_response ( incoming : IncomingResponse ) -> super :: Result < Self > {
44
+ pub ( crate ) fn try_from_incoming_response ( incoming : IncomingResponse ) -> Result < Self > {
43
45
let headers: HeaderMap = header_map_from_wasi ( incoming. headers ( ) ) ?;
44
46
let status = incoming. status ( ) . into ( ) ;
45
47
46
- let kind = BodyKind :: from_headers ( & headers) ;
48
+ let kind = BodyKind :: from_headers ( & headers) ? ;
47
49
// `body_stream` is a child of `incoming_body` which means we cannot
48
50
// drop the parent before we drop the child
49
51
let incoming_body = incoming
0 commit comments