Fix plaintext write logic for limited rustls buffer.

This commit is contained in:
PZ Read 2017-05-26 15:54:47 +08:00
parent d224148327
commit 72c9c1d59e

View File

@ -209,17 +209,29 @@ impl<S, C> io::Write for TlsStream<S, C>
where S: AsyncRead + AsyncWrite, C: Session where S: AsyncRead + AsyncWrite, C: Session
{ {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> { fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
let output = self.session.write(buf)?; loop {
let output = self.session.write(buf)?;
while self.session.wants_write() { while self.session.wants_write() {
match self.session.write_tls(&mut self.io) { match self.session.write_tls(&mut self.io) {
Ok(_) => (), Ok(_) => (),
Err(ref e) if e.kind() == io::ErrorKind::WouldBlock => break, Err(ref e) if e.kind() == io::ErrorKind::WouldBlock => {
Err(e) => return Err(e) if output == 0 {
// Both rustls buffer and IO buffer are blocking.
return Err(io::Error::from(io::ErrorKind::WouldBlock));
} else {
break;
}
}
Err(e) => return Err(e)
}
}
if output > 0 {
// Already wrote something out.
return Ok(output);
} }
} }
Ok(output)
} }
fn flush(&mut self) -> io::Result<()> { fn flush(&mut self) -> io::Result<()> {