Read buffer to avoid byte-by-byte syscalls (#141)

Fixes #140
This commit is contained in:
Martin Algesten
2020-09-13 03:27:15 +02:00
committed by GitHub
parent 960c0ff43b
commit 50c19c5484
3 changed files with 58 additions and 22 deletions

View File

@@ -593,14 +593,16 @@ pub(crate) fn set_stream(resp: &mut Response, url: String, unit: Option<Unit>, s
fn read_next_line<R: Read>(reader: &mut R) -> IoResult<String> {
let mut buf = Vec::new();
let mut prev_byte_was_cr = false;
let mut one = [0_u8];
loop {
let byte = reader.bytes().next();
let amt = reader.read(&mut one[..])?;
let byte = match byte {
Some(b) => b?,
None => return Err(IoError::new(ErrorKind::ConnectionAborted, "Unexpected EOF")),
};
if amt == 0 {
return Err(IoError::new(ErrorKind::ConnectionAborted, "Unexpected EOF"));
}
let byte = one[0];
if byte == b'\n' && prev_byte_was_cr {
buf.pop(); // removing the '\r'