Skip to content

Commit

Permalink
Merge pull request #210 from manuels/pending
Browse files Browse the repository at this point in the history
Add SslStream.pending()
  • Loading branch information
sfackler committed May 6, 2015
2 parents fb2822d + c8fae31 commit 8a9aa0c
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
1 change: 1 addition & 0 deletions openssl-sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,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;
Expand Down
12 changes: 12 additions & 0 deletions openssl/src/ssl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -882,6 +889,11 @@ impl<S: Read+Write> SslStream<S> {
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<S: Read+Write> Read for SslStream<S> {
Expand Down
24 changes: 24 additions & 0 deletions openssl/src/ssl/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down

0 comments on commit 8a9aa0c

Please sign in to comment.