Make SslStream Cloneable

Closes #6
This commit is contained in:
Steven Fackler 2014-11-29 11:06:16 -08:00
parent ad109a25be
commit c3603b0db0
2 changed files with 18 additions and 7 deletions

View File

@ -2,7 +2,7 @@ use libc::{c_int, c_void, c_long};
use std::io::{IoResult, IoError, EndOfFile, Stream, Reader, Writer}; use std::io::{IoResult, IoError, EndOfFile, Stream, Reader, Writer};
use std::mem; use std::mem;
use std::ptr; use std::ptr;
use std::sync::{Once, ONCE_INIT}; use std::sync::{Once, ONCE_INIT, Arc};
use bio::{MemBio}; use bio::{MemBio};
use ffi; use ffi;
@ -397,9 +397,10 @@ enum LibSslError {
} }
/// A stream wrapper which handles SSL encryption for an underlying stream. /// A stream wrapper which handles SSL encryption for an underlying stream.
#[deriving(Clone)]
pub struct SslStream<S> { pub struct SslStream<S> {
stream: S, stream: S,
ssl: Ssl, ssl: Arc<Ssl>,
buf: Vec<u8> buf: Vec<u8>
} }
@ -407,7 +408,7 @@ impl<S: Stream> SslStream<S> {
fn new_base(ssl:Ssl, stream: S) -> SslStream<S> { fn new_base(ssl:Ssl, stream: S) -> SslStream<S> {
SslStream { SslStream {
stream: stream, stream: stream,
ssl: ssl, ssl: Arc::new(ssl),
// Maximum TLS record size is 16k // Maximum TLS record size is 16k
buf: Vec::from_elem(16 * 1024, 0u8) 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) fn in_retry_wrapper(&mut self, blk: |&Ssl| -> c_int)
-> Result<c_int, SslError> { -> Result<c_int, SslError> {
loop { loop {
let ret = blk(&self.ssl); let ret = blk(&*self.ssl);
if ret > 0 { if ret > 0 {
return Ok(ret); return Ok(ret);
} }

View File

@ -1,7 +1,6 @@
use serialize::hex::FromHex; use serialize::hex::FromHex;
use std::io::{Writer}; use std::io::{Writer};
use std::io::net::tcp::TcpStream; use std::io::net::tcp::TcpStream;
use std::str;
use crypto::hash::HashType::{SHA256}; use crypto::hash::HashType::{SHA256};
use ssl::SslMethod::Sslv23; use ssl::SslMethod::Sslv23;
@ -191,6 +190,17 @@ fn test_read() {
let mut stream = SslStream::new(&SslContext::new(Sslv23).unwrap(), stream).unwrap(); let mut stream = SslStream::new(&SslContext::new(Sslv23).unwrap(), stream).unwrap();
stream.write("GET /\r\n\r\n".as_bytes()).unwrap(); stream.write("GET /\r\n\r\n".as_bytes()).unwrap();
stream.flush().unwrap(); stream.flush().unwrap();
let buf = stream.read_to_end().ok().expect("read error"); stream.read_to_end().ok().expect("read error");
print!("{}", str::from_utf8(buf.as_slice())); }
#[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");
} }