Skip to content

Commit dccd481

Browse files
author
Stjepan Glavina
committed
Handle EOF in decode
1 parent aab364d commit dccd481

File tree

2 files changed

+28
-13
lines changed

2 files changed

+28
-13
lines changed

examples/server.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use async_h1::{server, Body};
22
use async_std::prelude::*;
3-
use async_std::{net, task, io};
3+
use async_std::{io, net, task};
44

55
fn main() -> Result<(), async_h1::Exception> {
66
task::block_on(async {
@@ -13,10 +13,12 @@ fn main() -> Result<(), async_h1::Exception> {
1313
let stream = stream?;
1414
let (reader, writer) = &mut (&stream, &stream);
1515
let req = server::decode(reader).await?;
16-
// dbg!(req);
17-
let body = Body::from_string("hello chashu".to_owned());
18-
let mut res = server::encode(http::Response::new(body)).await?;
19-
io::copy(&mut res, writer).await?;
16+
if req.is_some() {
17+
// dbg!(req);
18+
let body = Body::from_string("hello chashu".to_owned());
19+
let mut res = server::encode(http::Response::new(body)).await?;
20+
io::copy(&mut res, writer).await?;
21+
}
2022
Ok::<(), async_h1::Exception>(())
2123
});
2224
}

src/server.rs

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
33
use async_std::io::{self, BufRead, BufReader};
44
use async_std::task::{Context, Poll};
5-
use futures_io::AsyncRead;
65
use futures_core::ready;
6+
use futures_io::AsyncRead;
77
use http::{Request, Response, Version};
88

99
use std::pin::Pin;
@@ -78,7 +78,10 @@ impl<R: AsyncRead + Unpin> AsyncRead for Encoder<R> {
7878

7979
/// Encode an HTTP request on the server.
8080
// TODO: return a reader in the response
81-
pub async fn encode<R>(res: Response<Body<R>>) -> Result<Encoder<R>, std::io::Error> where R: AsyncRead {
81+
pub async fn encode<R>(res: Response<Body<R>>) -> io::Result<Encoder<R>>
82+
where
83+
R: AsyncRead,
84+
{
8285
use std::io::Write;
8386
let mut buf: Vec<u8> = vec![];
8487

@@ -98,16 +101,22 @@ pub async fn encode<R>(res: Response<Body<R>>) -> Result<Encoder<R>, std::io::Er
98101
}
99102

100103
for (header, value) in res.headers() {
101-
write!(&mut buf, "{}: {}\r\n", header.as_str(), value.to_str().unwrap())?;
104+
write!(
105+
&mut buf,
106+
"{}: {}\r\n",
107+
header.as_str(),
108+
value.to_str().unwrap()
109+
)?;
102110
}
103111

104112
write!(&mut buf, "\r\n")?;
105113
Ok(Encoder::new(buf, res.into_body()))
106114
}
107115

108116
/// Decode an HTTP request on the server.
109-
pub async fn decode<R>(reader: R) -> Result<Request<Body<BufReader<R>>>, Exception>
110-
where R: AsyncRead + Unpin + Send,
117+
pub async fn decode<R>(reader: R) -> Result<Option<Request<Body<BufReader<R>>>>, Exception>
118+
where
119+
R: AsyncRead + Unpin + Send,
111120
{
112121
let mut reader = BufReader::new(reader);
113122
let mut buf = Vec::new();
@@ -119,7 +128,7 @@ pub async fn decode<R>(reader: R) -> Result<Request<Body<BufReader<R>>>, Excepti
119128
let bytes_read = reader.read_until(b'\n', &mut buf).await?;
120129
// No more bytes are yielded from the stream.
121130
if bytes_read == 0 {
122-
break;
131+
return Ok(None);
123132
}
124133

125134
// We've hit the end delimiter of the stream.
@@ -155,11 +164,15 @@ pub async fn decode<R>(reader: R) -> Result<Request<Body<BufReader<R>>>, Excepti
155164
}
156165

157166
// Process the body if `Content-Length` was passed.
158-
let body = match httparse_req.headers.iter().find(|h| h.name == "Content-Length") {
167+
let body = match httparse_req
168+
.headers
169+
.iter()
170+
.find(|h| h.name == "Content-Length")
171+
{
159172
Some(_header) => Body::new(reader), // TODO: use the header value
160173
None => Body::empty(),
161174
};
162175

163176
// Return the request.
164-
Ok(req.body(body)?)
177+
Ok(Some(req.body(body)?))
165178
}

0 commit comments

Comments
 (0)