Merge pull request #9 from FauxFaux/close_notify

Map CloseNotify to EOF
This commit is contained in:
Martin Algesten
2019-09-11 14:55:13 +02:00
committed by GitHub
2 changed files with 37 additions and 1 deletions

View File

@@ -4,6 +4,9 @@ use std::net::TcpStream;
use std::net::ToSocketAddrs;
use std::time::Duration;
use rustls::StreamOwned;
use rustls::ClientSession;
use crate::error::Error;
use crate::unit::Unit;
@@ -58,7 +61,7 @@ impl Read for Stream {
match self {
Stream::Http(sock) => sock.read(buf),
#[cfg(feature = "tls")]
Stream::Https(stream) => stream.read(buf),
Stream::Https(stream) => read_https(stream, buf),
Stream::Cursor(read) => read.read(buf),
#[cfg(test)]
Stream::Test(reader, _) => reader.read(buf),
@@ -66,6 +69,28 @@ impl Read for Stream {
}
}
fn read_https(stream: &mut StreamOwned<ClientSession, TcpStream>, buf: &mut [u8]) -> IoResult<usize> {
match stream.read(buf) {
Ok(size) => Ok(size),
Err(ref e) if is_close_notify(e) => Ok(0),
Err(e) => Err(e),
}
}
fn is_close_notify(e: &std::io::Error) -> bool {
if e.kind() != std::io::ErrorKind::ConnectionAborted {
return false;
}
if let Some(msg) = e.get_ref() {
// :(
return msg.description().contains("CloseNotify");
}
false
}
impl Write for Stream {
fn write(&mut self, buf: &[u8]) -> IoResult<usize> {
match self {

11
tests/https-agent.rs Normal file
View File

@@ -0,0 +1,11 @@
use std::io::Read;
#[test]
fn connection_close() {
let agent = ureq::Agent::default().build();
let resp = agent.get("https://example.com/404")
.set("Connection", "close")
.call();
assert_eq!(resp.status(), 404);
resp.into_reader().read_to_end(&mut vec![]).unwrap();
}