@@ -214,35 +214,22 @@ where
214
214
req = req. set_header ( header. name , std:: str:: from_utf8 ( header. value ) ?) ?;
215
215
}
216
216
217
- // Process the body if `Content-Length` was passed.
218
- if let Some ( content_length) = httparse_req
219
- . headers
220
- . iter ( )
221
- . find ( |h| h. name . eq_ignore_ascii_case ( "Content-Length" ) )
222
- {
223
- let length = std:: str:: from_utf8 ( content_length. value )
224
- . ok ( )
225
- . and_then ( |s| s. parse :: < usize > ( ) . ok ( ) ) ;
226
-
227
- if let Some ( len) = length {
228
- req = req. set_body_reader ( reader) ;
229
- req = req. set_len ( len) ;
230
-
231
- Ok ( Some ( DecodedRequest :: WithBody ( req) ) )
232
- } else {
233
- return Err ( "Invalid value for Content-Length" . into ( ) ) ;
234
- }
235
- } else {
236
- Ok ( Some ( DecodedRequest :: WithoutBody ( req, Box :: new ( reader) ) ) )
237
- }
217
+ // Check for content-length, that determines determines whether we can parse
218
+ // it with a known length, or need to use chunked encoding.
219
+ let len = match req. header ( "Content-Length" ) {
220
+ Some ( len) => len. parse :: < usize > ( ) ?,
221
+ None => return Ok ( Some ( DecodedRequest :: WithoutBody ( req, Box :: new ( reader) ) ) ) ,
222
+ } ;
223
+ req = req. set_body_reader ( reader) . set_len ( len) ;
224
+ Ok ( Some ( DecodedRequest :: WithBody ( req) ) )
238
225
}
239
226
240
- /// A decoded response
241
- ///
242
- /// Either a request with body stream OR a request without a
243
- /// a body stream paired with the underlying stream
227
+ /// A decoded request
244
228
pub enum DecodedRequest {
229
+ /// The TCP connection is inside the request already, so the lifetimes match up.
245
230
WithBody ( Request ) ,
231
+ /// The TCP connection is *not* inside the request body, so we need to pass
232
+ /// it along with it to make the lifetimes match up.
246
233
WithoutBody ( Request , Box < dyn BufRead + Unpin + Send + ' static > ) ,
247
234
}
248
235
0 commit comments