53
53
54
54
// If we have reference to the stream, unwrap it. Otherwise,
55
55
// get the underlying stream from the request
56
- let to_decode = decoded. to_stream ( ) ;
56
+ let to_decode = decoded. into_stream ( ) ;
57
57
58
58
// Copy the response into the writer
59
59
io:: copy ( & mut res, & mut writer) . await ?;
@@ -166,6 +166,9 @@ pub async fn encode(res: Response) -> io::Result<Encoder> {
166
166
Ok ( Encoder :: new ( buf, res) )
167
167
}
168
168
169
+ /// The number returned from httparse when the request is HTTP 1.1
170
+ const HTTP_1_1_VERSION : u8 = 1 ;
171
+
169
172
/// Decode an HTTP request on the server.
170
173
pub async fn decode < R > ( reader : R ) -> Result < Option < DecodedRequest > , Exception >
171
174
where
@@ -203,7 +206,7 @@ where
203
206
let uri = httparse_req. path . ok_or_else ( || "No uri found" ) ?;
204
207
let uri = url:: Url :: parse ( uri) ?;
205
208
let version = httparse_req. version . ok_or_else ( || "No version found" ) ?;
206
- if version != 1 {
209
+ if version != HTTP_1_1_VERSION {
207
210
return Err ( "Unsupported HTTP version" . into ( ) ) ;
208
211
}
209
212
let mut req = Request :: new ( Method :: from_str ( method) ?, uri) ;
@@ -222,10 +225,9 @@ where
222
225
. and_then ( |s| s. parse :: < usize > ( ) . ok ( ) ) ;
223
226
224
227
if let Some ( len) = length {
225
- req = req. set_body ( reader) ;
228
+ req = req. set_body_reader ( reader) ;
226
229
req = req. set_len ( len) ;
227
230
228
- // Return the request.
229
231
Ok ( Some ( DecodedRequest :: WithBody ( req) ) )
230
232
} else {
231
233
return Err ( "Invalid value for Content-Length" . into ( ) ) ;
@@ -263,9 +265,11 @@ impl DecodedRequest {
263
265
}
264
266
265
267
/// Consume self and get access to the underlying stream
266
- fn to_stream ( self ) -> Box < dyn BufRead + Unpin + Send + ' static > {
268
+ ///
269
+ /// If the request has a body, the underlying stream is t
270
+ fn into_stream ( self ) -> Box < dyn BufRead + Unpin + Send + ' static > {
267
271
match self {
268
- DecodedRequest :: WithBody ( r) => r. into_body ( ) ,
272
+ DecodedRequest :: WithBody ( r) => r. into_body_reader ( ) ,
269
273
DecodedRequest :: WithoutBody ( _, s) => s,
270
274
}
271
275
}
0 commit comments