Skip to content

Commit 3c182fc

Browse files
pchickeycalvinrp
andcommitted
fix impls of AsyncRead to not error when stream closed
Closes #12 this is also from cbaf050 Co-authored-by: Calvin Prewitt <[email protected]>
1 parent a1a6e1c commit 3c182fc

File tree

2 files changed

+19
-7
lines changed

2 files changed

+19
-7
lines changed

src/http/response.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -117,12 +117,16 @@ impl AsyncRead for IncomingBody {
117117
Reactor::current().wait_for(pollable).await;
118118

119119
// Read the bytes from the body stream
120-
let buf = self.body_stream.read(CHUNK_SIZE).map_err(|err| match err {
121-
StreamError::LastOperationFailed(err) => {
122-
std::io::Error::other(format!("{}", err.to_debug_string()))
120+
let buf = match self.body_stream.read(CHUNK_SIZE) {
121+
Ok(buf) => buf,
122+
Err(StreamError::Closed) => return Ok(0),
123+
Err(StreamError::LastOperationFailed(err)) => {
124+
return Err(std::io::Error::other(format!(
125+
"last operation failed: {}",
126+
err.to_debug_string()
127+
)))
123128
}
124-
StreamError::Closed => std::io::Error::other("Connection closed"),
125-
})?;
129+
};
126130
self.buf.insert(buf)
127131
}
128132
};

src/net/tcp_stream.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,11 @@ impl TcpStream {
3131
impl AsyncRead for TcpStream {
3232
async fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
3333
Reactor::current().wait_for(self.input.subscribe()).await;
34-
let slice = self.input.read(buf.len() as u64).map_err(to_io_err)?;
34+
let slice = match self.input.read(buf.len() as u64) {
35+
Ok(slice) => slice,
36+
Err(StreamError::Closed) => return Ok(0),
37+
Err(e) => return Err(to_io_err(e)),
38+
};
3539
let bytes_read = slice.len();
3640
buf[..bytes_read].clone_from_slice(&slice);
3741
Ok(bytes_read)
@@ -41,7 +45,11 @@ impl AsyncRead for TcpStream {
4145
impl AsyncRead for &TcpStream {
4246
async fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
4347
Reactor::current().wait_for(self.input.subscribe()).await;
44-
let slice = self.input.read(buf.len() as u64).map_err(to_io_err)?;
48+
let slice = match self.input.read(buf.len() as u64) {
49+
Ok(slice) => slice,
50+
Err(StreamError::Closed) => return Ok(0),
51+
Err(e) => return Err(to_io_err(e)),
52+
};
4553
let bytes_read = slice.len();
4654
buf[..bytes_read].clone_from_slice(&slice);
4755
Ok(bytes_read)

0 commit comments

Comments
 (0)