1
- use crate :: world:: bindings:: wasi:: http:: types:: { Fields , OutgoingBody , OutgoingResponse } ;
1
+ use crate :: world:: bindings:: wasi:: http:: types:: {
2
+ Fields , IncomingRequest , OutgoingBody , OutgoingResponse ,
3
+ } ;
4
+
5
+ use crate :: world:: bindings:: wasi:: io:: streams:: StreamError ;
2
6
3
7
use crate :: world:: bindings:: exports:: wasi:: http:: incoming_handler:: ResponseOutparam ;
4
8
use std:: collections:: HashMap ;
@@ -49,9 +53,7 @@ impl ResponseBuilder {
49
53
ResponseOutparam :: set ( resp, Ok ( resp_tx) ) ;
50
54
let stream = body. write ( ) . unwrap ( ) ;
51
55
if let Some ( body_content) = self . body_content {
52
- stream
53
- . blocking_write_and_flush ( body_content. as_bytes ( ) )
54
- . unwrap ( ) ;
56
+ let _ = stream. write ( body_content. as_bytes ( ) ) ;
55
57
}
56
58
drop ( stream) ;
57
59
let _ = OutgoingBody :: finish ( body, None ) ;
@@ -71,3 +73,38 @@ pub fn parse_headers(headers: &Fields) -> HashMap<String, Vec<String>> {
71
73
72
74
output
73
75
}
76
+
77
+ pub fn parse_body ( req : IncomingRequest ) -> Result < Vec < u8 > , String > {
78
+ let mut request_body = Vec :: new ( ) ;
79
+ let stream = match req. consume ( ) {
80
+ Ok ( stream) => stream,
81
+ Err ( _) => {
82
+ return Err ( "Failed to consume request stream" . to_string ( ) ) ;
83
+ }
84
+ } ;
85
+ let stream = match stream. stream ( ) {
86
+ Ok ( stream) => stream,
87
+ Err ( _) => {
88
+ return Err ( "Failed to get request stream" . to_string ( ) ) ;
89
+ }
90
+ } ;
91
+
92
+ loop {
93
+ match stream. read ( 4096 ) {
94
+ Ok ( chunk) => {
95
+ if chunk. is_empty ( ) {
96
+ break ;
97
+ }
98
+ request_body. extend_from_slice ( & chunk) ;
99
+ }
100
+ Err ( StreamError :: Closed ) => {
101
+ // Stream is closed, we can stop reading
102
+ break ;
103
+ }
104
+ Err ( e) => {
105
+ return Err ( format ! ( "Failed to read from request stream: {e}" ) ) ;
106
+ }
107
+ }
108
+ }
109
+ Ok ( request_body)
110
+ }
0 commit comments