From c8fae312ad9a4350f3aa92b5aa09cc9f8971175e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20Sch=C3=B6lling?= Date: Thu, 30 Apr 2015 20:00:30 +0200 Subject: [PATCH] Add SslStream.pending() --- openssl-sys/src/lib.rs | 1 + openssl/src/ssl/mod.rs | 12 ++++++++++++ openssl/src/ssl/tests.rs | 24 ++++++++++++++++++++++++ 3 files changed, 37 insertions(+) diff --git a/openssl-sys/src/lib.rs b/openssl-sys/src/lib.rs index 53f22558..97d4c0d7 100644 --- a/openssl-sys/src/lib.rs +++ b/openssl-sys/src/lib.rs @@ -496,6 +496,7 @@ extern "C" { pub fn SSLv23_method() -> *const SSL_METHOD; pub fn SSL_new(ctx: *mut SSL_CTX) -> *mut SSL; + pub fn SSL_pending(ssl: *const SSL) -> c_int; pub fn SSL_free(ssl: *mut SSL); pub fn SSL_set_bio(ssl: *mut SSL, rbio: *mut BIO, wbio: *mut BIO); pub fn SSL_get_rbio(ssl: *mut SSL) -> *mut BIO; diff --git a/openssl/src/ssl/mod.rs b/openssl/src/ssl/mod.rs index ab559b46..13d9572b 100644 --- a/openssl/src/ssl/mod.rs +++ b/openssl/src/ssl/mod.rs @@ -707,6 +707,13 @@ impl Ssl { } } } + + /// pending() takes into account only bytes from the TLS/SSL record that is currently being processed (if any). + pub fn pending(&self) -> usize { + unsafe { + ffi::SSL_pending(self.ssl) as usize + } + } } macro_rules! make_LibSslError { @@ -882,6 +889,11 @@ impl SslStream { pub fn get_selected_npn_protocol(&self) -> Option<&[u8]> { self.ssl.get_selected_npn_protocol() } + + /// pending() takes into account only bytes from the TLS/SSL record that is currently being processed (if any). + pub fn pending(&self) -> usize { + self.ssl.pending() + } } impl Read for SslStream { diff --git a/openssl/src/ssl/tests.rs b/openssl/src/ssl/tests.rs index e6af551b..c9a2d73a 100644 --- a/openssl/src/ssl/tests.rs +++ b/openssl/src/ssl/tests.rs @@ -337,6 +337,30 @@ fn test_read() { io::copy(&mut stream, &mut io::sink()).ok().expect("read error"); } + +#[test] +fn test_pending() { + let tcp = TcpStream::connect("127.0.0.1:15418").unwrap(); + let mut stream = SslStream::new(&SslContext::new(Sslv23).unwrap(), tcp).unwrap(); + stream.write_all("GET /\r\n\r\n".as_bytes()).unwrap(); + stream.flush().unwrap(); + + // wait for the response and read first byte... + let mut buf = [0u8; 16*1024]; + stream.read(&mut buf[..1]).unwrap(); + + let pending = stream.pending(); + let len = stream.read(&mut buf[1..]).unwrap(); + + assert_eq!(pending, len); + + stream.read(&mut buf[..1]).unwrap(); + + let pending = stream.pending(); + let len = stream.read(&mut buf[1..]).unwrap(); + assert_eq!(pending, len); +} + /// Tests that connecting with the client using NPN, but the server not does not /// break the existing connection behavior. #[test]