diff options
| author | Steven Fackler <[email protected]> | 2014-11-29 11:06:16 -0800 |
|---|---|---|
| committer | Steven Fackler <[email protected]> | 2014-11-29 11:06:16 -0800 |
| commit | c3603b0db00d044c8332d39dd9d49e3c76a2a978 (patch) | |
| tree | 2bf5f0f319d5034eab340670fef9e5d1cbcc2d8c | |
| parent | Release 0.2.1 (diff) | |
| download | rust-openssl-c3603b0db00d044c8332d39dd9d49e3c76a2a978.tar.xz rust-openssl-c3603b0db00d044c8332d39dd9d49e3c76a2a978.zip | |
Make SslStream Cloneable
Closes #6
| -rw-r--r-- | src/ssl/mod.rs | 9 | ||||
| -rw-r--r-- | src/ssl/tests.rs | 16 |
2 files changed, 18 insertions, 7 deletions
diff --git a/src/ssl/mod.rs b/src/ssl/mod.rs index 778b50c7..d29d633e 100644 --- a/src/ssl/mod.rs +++ b/src/ssl/mod.rs @@ -2,7 +2,7 @@ use libc::{c_int, c_void, c_long}; use std::io::{IoResult, IoError, EndOfFile, Stream, Reader, Writer}; use std::mem; use std::ptr; -use std::sync::{Once, ONCE_INIT}; +use std::sync::{Once, ONCE_INIT, Arc}; use bio::{MemBio}; use ffi; @@ -397,9 +397,10 @@ enum LibSslError { } /// A stream wrapper which handles SSL encryption for an underlying stream. +#[deriving(Clone)] pub struct SslStream<S> { stream: S, - ssl: Ssl, + ssl: Arc<Ssl>, buf: Vec<u8> } @@ -407,7 +408,7 @@ impl<S: Stream> SslStream<S> { fn new_base(ssl:Ssl, stream: S) -> SslStream<S> { SslStream { stream: stream, - ssl: ssl, + ssl: Arc::new(ssl), // Maximum TLS record size is 16k buf: Vec::from_elem(16 * 1024, 0u8) } @@ -465,7 +466,7 @@ impl<S: Stream> SslStream<S> { fn in_retry_wrapper(&mut self, blk: |&Ssl| -> c_int) -> Result<c_int, SslError> { loop { - let ret = blk(&self.ssl); + let ret = blk(&*self.ssl); if ret > 0 { return Ok(ret); } diff --git a/src/ssl/tests.rs b/src/ssl/tests.rs index 99ef2386..e4414f84 100644 --- a/src/ssl/tests.rs +++ b/src/ssl/tests.rs @@ -1,7 +1,6 @@ use serialize::hex::FromHex; use std::io::{Writer}; use std::io::net::tcp::TcpStream; -use std::str; use crypto::hash::HashType::{SHA256}; use ssl::SslMethod::Sslv23; @@ -191,6 +190,17 @@ fn test_read() { let mut stream = SslStream::new(&SslContext::new(Sslv23).unwrap(), stream).unwrap(); stream.write("GET /\r\n\r\n".as_bytes()).unwrap(); stream.flush().unwrap(); - let buf = stream.read_to_end().ok().expect("read error"); - print!("{}", str::from_utf8(buf.as_slice())); + stream.read_to_end().ok().expect("read error"); +} + +#[test] +fn test_clone() { + let stream = TcpStream::connect("127.0.0.1:15418").unwrap(); + let mut stream = SslStream::new(&SslContext::new(Sslv23).unwrap(), stream).unwrap(); + let mut stream2 = stream.clone(); + spawn(proc() { + stream2.write("GET /\r\n\r\n".as_bytes()).unwrap(); + stream2.flush().unwrap(); + }); + stream.read_to_end().ok().expect("read error"); } |