Skip to content

Commit

Permalink
Merge pull request fortanix#9 from FauxFaux/close_notify
Browse files Browse the repository at this point in the history
Map CloseNotify to EOF
  • Loading branch information
lolgesten authored Sep 11, 2019
2 parents 2fa21f1 + 580e159 commit afa0cf9
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
27 changes: 26 additions & 1 deletion src/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -58,14 +61,36 @@ 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),
}
}
}

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 {
Expand Down
11 changes: 11 additions & 0 deletions tests/https-agent.rs
Original file line number Diff line number Diff line change
@@ -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();
}

0 comments on commit afa0cf9

Please sign in to comment.