Skip to content

Commit 5d0c49b

Browse files
committed
small improvements
1 parent a9e711a commit 5d0c49b

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

src/server.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ where
5353

5454
// If we have reference to the stream, unwrap it. Otherwise,
5555
// get the underlying stream from the request
56-
let to_decode = decoded.to_stream();
56+
let to_decode = decoded.into_stream();
5757

5858
// Copy the response into the writer
5959
io::copy(&mut res, &mut writer).await?;
@@ -166,6 +166,9 @@ pub async fn encode(res: Response) -> io::Result<Encoder> {
166166
Ok(Encoder::new(buf, res))
167167
}
168168

169+
/// The number returned from httparse when the request is HTTP 1.1
170+
const HTTP_1_1_VERSION: u8 = 1;
171+
169172
/// Decode an HTTP request on the server.
170173
pub async fn decode<R>(reader: R) -> Result<Option<DecodedRequest>, Exception>
171174
where
@@ -203,7 +206,7 @@ where
203206
let uri = httparse_req.path.ok_or_else(|| "No uri found")?;
204207
let uri = url::Url::parse(uri)?;
205208
let version = httparse_req.version.ok_or_else(|| "No version found")?;
206-
if version != 1 {
209+
if version != HTTP_1_1_VERSION {
207210
return Err("Unsupported HTTP version".into());
208211
}
209212
let mut req = Request::new(Method::from_str(method)?, uri);
@@ -222,10 +225,9 @@ where
222225
.and_then(|s| s.parse::<usize>().ok());
223226

224227
if let Some(len) = length {
225-
req = req.set_body(reader);
228+
req = req.set_body_reader(reader);
226229
req = req.set_len(len);
227230

228-
// Return the request.
229231
Ok(Some(DecodedRequest::WithBody(req)))
230232
} else {
231233
return Err("Invalid value for Content-Length".into());
@@ -263,9 +265,11 @@ impl DecodedRequest {
263265
}
264266

265267
/// 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> {
267271
match self {
268-
DecodedRequest::WithBody(r) => r.into_body(),
272+
DecodedRequest::WithBody(r) => r.into_body_reader(),
269273
DecodedRequest::WithoutBody(_, s) => s,
270274
}
271275
}

0 commit comments

Comments
 (0)