1- use wasi:: http:: types:: { IncomingBody as WasiIncomingBody , IncomingResponse } ;
2-
3- use super :: { fields:: header_map_from_wasi, Body , Error , HeaderMap , Result } ;
4- use crate :: io:: { AsyncInputStream , AsyncRead } ;
1+ use wasi:: http:: types:: IncomingResponse ;
2+
3+ use super :: {
4+ body:: { BodyKind , IncomingBody } ,
5+ fields:: header_map_from_wasi,
6+ Error , HeaderMap , Result ,
7+ } ;
8+ use crate :: io:: AsyncInputStream ;
59use http:: StatusCode ;
610
711pub use http:: Response ;
812
9- #[ derive( Debug ) ]
10- enum BodyKind {
11- Fixed ( u64 ) ,
12- Chunked ,
13- }
14-
15- impl BodyKind {
16- fn from_headers ( headers : & HeaderMap ) -> Result < BodyKind > {
17- if let Some ( value) = headers. get ( "content-length" ) {
18- let content_length = std:: str:: from_utf8 ( value. as_ref ( ) )
19- . unwrap ( )
20- . parse :: < u64 > ( )
21- . map_err ( |_| {
22- Error :: other ( "incoming content-length should be a u64; violates HTTP/1.1" )
23- } ) ?;
24- Ok ( BodyKind :: Fixed ( content_length) )
25- } else if headers. contains_key ( "transfer-encoding" ) {
26- Ok ( BodyKind :: Chunked )
27- } else {
28- Ok ( BodyKind :: Chunked )
29- }
30- }
31- }
32-
3313pub ( crate ) fn try_from_incoming_response (
3414 incoming : IncomingResponse ,
3515) -> Result < Response < IncomingBody > > {
@@ -48,11 +28,7 @@ pub(crate) fn try_from_incoming_response(
4828 . stream ( )
4929 . expect ( "cannot call `stream` twice on an incoming body" ) ;
5030
51- let body = IncomingBody {
52- kind,
53- body_stream : AsyncInputStream :: new ( body_stream) ,
54- _incoming_body : incoming_body,
55- } ;
31+ let body = IncomingBody :: new ( kind, AsyncInputStream :: new ( body_stream) , incoming_body) ;
5632
5733 let mut builder = Response :: builder ( ) . status ( status) ;
5834
@@ -64,34 +40,3 @@ pub(crate) fn try_from_incoming_response(
6440 . body ( body)
6541 . map_err ( |err| Error :: other ( err. to_string ( ) ) )
6642}
67-
68- /// An incoming HTTP body
69- #[ derive( Debug ) ]
70- pub struct IncomingBody {
71- kind : BodyKind ,
72- // IMPORTANT: the order of these fields here matters. `body_stream` must
73- // be dropped before `_incoming_body`.
74- body_stream : AsyncInputStream ,
75- _incoming_body : WasiIncomingBody ,
76- }
77-
78- impl AsyncRead for IncomingBody {
79- async fn read ( & mut self , out_buf : & mut [ u8 ] ) -> crate :: io:: Result < usize > {
80- self . body_stream . read ( out_buf) . await
81- }
82- }
83-
84- impl Body for IncomingBody {
85- fn len ( & self ) -> Option < usize > {
86- match self . kind {
87- BodyKind :: Fixed ( l) => {
88- if l > ( usize:: MAX as u64 ) {
89- None
90- } else {
91- Some ( l as usize )
92- }
93- }
94- BodyKind :: Chunked => None ,
95- }
96- }
97- }
0 commit comments