Skip to content

Commit cffe5a3

Browse files
committed
Update response.rs
1 parent 1c7956e commit cffe5a3

File tree

1 file changed

+29
-30
lines changed

1 file changed

+29
-30
lines changed

src/http/response.rs

Lines changed: 29 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -114,38 +114,37 @@ pub struct IncomingBody {
114114

115115
impl AsyncRead for IncomingBody {
116116
async fn read(&mut self, out_buf: &mut [u8]) -> crate::io::Result<usize> {
117-
loop {
118-
let buf = match &mut self.buf {
119-
Some(ref mut buf) => buf,
120-
None => {
121-
// Wait for an event to be ready
122-
let pollable = self.body_stream.subscribe();
123-
self.reactor.wait_for(pollable).await;
124-
125-
// Read the bytes from the body stream
126-
let buf = self.body_stream.read(CHUNK_SIZE).map_err(|err| match err {
127-
StreamError::LastOperationFailed(err) => {
128-
std::io::Error::other(format!("{}", err.to_debug_string()))
129-
}
130-
StreamError::Closed => std::io::Error::other("Connection closed"),
131-
})?;
132-
self.buf.insert(buf)
133-
}
134-
};
135-
136-
// copy bytes
137-
let max = (buf.len() - self.buf_offset).min(out_buf.len());
138-
let slice = &buf[self.buf_offset..max];
139-
out_buf[0..max].copy_from_slice(slice);
140-
self.buf_offset += max;
141-
142-
// reset the local slice if necessary
143-
if self.buf_offset == buf.len() {
144-
self.buf = None;
145-
self.buf_offset = 0;
117+
let buf = match &mut self.buf {
118+
Some(ref mut buf) => buf,
119+
None => {
120+
// Wait for an event to be ready
121+
let pollable = self.body_stream.subscribe();
122+
self.reactor.wait_for(pollable).await;
123+
124+
// Read the bytes from the body stream
125+
let buf = self.body_stream.read(CHUNK_SIZE).map_err(|err| match err {
126+
StreamError::LastOperationFailed(err) => {
127+
std::io::Error::other(format!("{}", err.to_debug_string()))
128+
}
129+
StreamError::Closed => std::io::Error::other("Connection closed"),
130+
})?;
131+
self.buf.insert(buf)
146132
}
133+
};
147134

148-
break Ok(dbg!(max));
135+
// copy bytes
136+
let len = (buf.len() - self.buf_offset).min(out_buf.len());
137+
let max = self.buf_offset + len;
138+
let slice = &buf[self.buf_offset..max];
139+
out_buf[0..len].copy_from_slice(slice);
140+
self.buf_offset += len;
141+
142+
// reset the local slice if necessary
143+
if self.buf_offset == buf.len() {
144+
self.buf = None;
145+
self.buf_offset = 0;
149146
}
147+
148+
Ok(len)
150149
}
151150
}

0 commit comments

Comments
 (0)