11use serde:: de:: DeserializeOwned ;
22
33use crate :: {
4- body_data_reader:: BodyDataReader , FormDataReader , HttpFailResult , JsonEncodedData ,
5- UrlEncodedData , WebContentType ,
4+ body_data_reader:: BodyDataReader , HttpFailResult , JsonEncodedData , UrlEncodedData ,
5+ WebContentType ,
66} ;
77
88pub enum BodyContentType {
@@ -29,7 +29,7 @@ impl BodyContentType {
2929}
3030
3131pub struct HttpRequestBody {
32- content_type : Option < String > ,
32+ pub content_type : Option < String > ,
3333 raw_body : Vec < u8 > ,
3434 body_content_type : BodyContentType ,
3535}
@@ -75,6 +75,12 @@ impl HttpRequestBody {
7575 }
7676
7777 pub fn get_body_data_reader ( & self ) -> Result < BodyDataReader , HttpFailResult > {
78+ if let Some ( content_type) = self . content_type . as_ref ( ) {
79+ if let Some ( boundary) = extract_boundary ( content_type. as_bytes ( ) ) {
80+ let reader = BodyDataReader :: create_as_form_data ( boundary, & self . raw_body ) ;
81+ return Ok ( reader) ;
82+ }
83+ }
7884 match self . body_content_type {
7985 BodyContentType :: Json => get_body_data_reader_as_json_encoded ( self . raw_body . as_slice ( ) ) ,
8086 BodyContentType :: UrlEncoded => {
@@ -88,18 +94,6 @@ impl HttpRequestBody {
8894 }
8995 }
9096 }
91-
92- pub fn get_body_form_data ( & self ) -> Result < FormDataReader , HttpFailResult > {
93- if self . content_type . is_none ( ) {
94- panic ! ( "Content type is not set" ) ;
95- }
96-
97- println ! ( "Content type: {:?}" , self . content_type) ;
98- Ok ( FormDataReader :: new (
99- extract_boundary ( self . content_type . as_ref ( ) . unwrap ( ) . as_bytes ( ) ) ,
100- & self . raw_body ,
101- ) )
102- }
10397}
10498
10599fn get_body_data_reader_as_url_encoded (
@@ -138,23 +132,16 @@ fn get_body_data_reader_as_json_encoded(body: &[u8]) -> Result<BodyDataReader, H
138132 }
139133}
140134
141- fn extract_boundary ( src : & [ u8 ] ) -> & [ u8 ] {
142- let pos = rust_extensions:: slice_of_u8_utils:: find_sequence_pos ( src, "boundary" . as_bytes ( ) , 0 ) ;
143-
144- if pos. is_none ( ) {
145- panic ! ( "Can not find boundary tag" ) ;
146- }
135+ fn extract_boundary ( src : & [ u8 ] ) -> Option < & [ u8 ] > {
136+ let pos = rust_extensions:: slice_of_u8_utils:: find_sequence_pos ( src, "boundary" . as_bytes ( ) , 0 ) ?;
147137
148- let pos = rust_extensions:: slice_of_u8_utils:: find_byte_pos ( src, '=' as u8 , pos. unwrap ( ) ) ;
149- if pos. is_none ( ) {
150- panic ! ( "Boundary tag does not have eq mark after it" ) ;
151- }
138+ let pos = rust_extensions:: slice_of_u8_utils:: find_byte_pos ( src, '=' as u8 , pos) ?;
152139
153- let end_pos = rust_extensions:: slice_of_u8_utils:: find_byte_pos ( src, ';' as u8 , pos. unwrap ( ) ) ;
140+ let end_pos = rust_extensions:: slice_of_u8_utils:: find_byte_pos ( src, ';' as u8 , pos) ;
154141
155142 match end_pos {
156- Some ( end_pos) => & src[ pos. unwrap ( ) + 1 ..end_pos] ,
157- None => & src[ pos. unwrap ( ) + 1 ..] ,
143+ Some ( end_pos) => Some ( & src[ pos + 1 ..end_pos] ) ,
144+ None => Some ( & src[ pos + 1 ..] ) ,
158145 }
159146}
160147
@@ -197,7 +184,7 @@ mod test {
197184 let content_type_header =
198185 "multipart/form-data; boundary=----WebKitFormBoundaryXayIfSQWkEtJ6k10" ;
199186
200- let boundary = extract_boundary ( content_type_header. as_bytes ( ) ) ;
187+ let boundary = extract_boundary ( content_type_header. as_bytes ( ) ) . unwrap ( ) ;
201188
202189 assert_eq ! (
203190 "----WebKitFormBoundaryXayIfSQWkEtJ6k10" ,
0 commit comments