Update for IO API update
The error handling needs to be redone still.
This commit is contained in:
parent
89e79afaf9
commit
1a5e625b4f
|
|
@ -1,12 +1,13 @@
|
||||||
use std::libc::c_ulong;
|
use std::libc::c_ulong;
|
||||||
|
use std::io::IoError;
|
||||||
|
|
||||||
use super::ffi;
|
use ssl::ffi;
|
||||||
|
|
||||||
/// An SSL error
|
/// An SSL error
|
||||||
#[deriving(ToStr)]
|
#[deriving(ToStr)]
|
||||||
pub enum SslError {
|
pub enum SslError {
|
||||||
/// The underlying stream has reported an EOF
|
/// The underlying stream has reported an error
|
||||||
StreamEof,
|
StreamError(IoError),
|
||||||
/// The SSL session has been closed by the other end
|
/// The SSL session has been closed by the other end
|
||||||
SslSessionClosed,
|
SslSessionClosed,
|
||||||
/// An error in the OpenSSL library
|
/// An error in the OpenSSL library
|
||||||
|
|
|
||||||
33
ssl/mod.rs
33
ssl/mod.rs
|
|
@ -1,11 +1,12 @@
|
||||||
|
use extra::sync::one::{Once, ONCE_INIT};
|
||||||
use std::cast;
|
use std::cast;
|
||||||
use std::libc::{c_int, c_void, c_char};
|
use std::libc::{c_int, c_void, c_char};
|
||||||
use std::ptr;
|
use std::ptr;
|
||||||
use std::unstable::mutex::{Mutex, Once, ONCE_INIT};
|
use std::io::{IoResult, IoError, OtherIoError, Stream, Reader, Writer};
|
||||||
use std::io::{Stream, Reader, Writer};
|
use std::unstable::mutex::Mutex;
|
||||||
use std::vec;
|
use std::vec;
|
||||||
|
|
||||||
use ssl::error::{SslError, SslSessionClosed, StreamEof};
|
use ssl::error::{SslError, SslSessionClosed, StreamError};
|
||||||
|
|
||||||
pub mod error;
|
pub mod error;
|
||||||
mod ffi;
|
mod ffi;
|
||||||
|
|
@ -467,12 +468,12 @@ impl<S: Stream> SslStream<S> {
|
||||||
ErrorWantRead => {
|
ErrorWantRead => {
|
||||||
self.flush();
|
self.flush();
|
||||||
match self.stream.read(self.buf) {
|
match self.stream.read(self.buf) {
|
||||||
Some(len) =>
|
Ok(len) =>
|
||||||
self.ssl.get_rbio().write(self.buf.slice_to(len)),
|
self.ssl.get_rbio().write(self.buf.slice_to(len)),
|
||||||
None => return Err(StreamEof)
|
Err(err) => return Err(StreamError(err))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
ErrorWantWrite => self.flush(),
|
ErrorWantWrite => { self.flush(); }
|
||||||
ErrorZeroReturn => return Err(SslSessionClosed),
|
ErrorZeroReturn => return Err(SslSessionClosed),
|
||||||
ErrorSsl => return Err(SslError::get()),
|
ErrorSsl => return Err(SslError::get()),
|
||||||
_ => unreachable!()
|
_ => unreachable!()
|
||||||
|
|
@ -482,26 +483,33 @@ impl<S: Stream> SslStream<S> {
|
||||||
|
|
||||||
fn write_through(&mut self) {
|
fn write_through(&mut self) {
|
||||||
loop {
|
loop {
|
||||||
|
// TODO propogate errors
|
||||||
match self.ssl.get_wbio().read(self.buf) {
|
match self.ssl.get_wbio().read(self.buf) {
|
||||||
Some(len) => self.stream.write(self.buf.slice_to(len)),
|
Some(len) => self.stream.write(self.buf.slice_to(len)),
|
||||||
None => break
|
None => break
|
||||||
}
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<S: Stream> Reader for SslStream<S> {
|
impl<S: Stream> Reader for SslStream<S> {
|
||||||
fn read(&mut self, buf: &mut [u8]) -> Option<uint> {
|
fn read(&mut self, buf: &mut [u8]) -> IoResult<uint> {
|
||||||
match self.in_retry_wrapper(|ssl| { ssl.read(buf) }) {
|
match self.in_retry_wrapper(|ssl| { ssl.read(buf) }) {
|
||||||
Ok(len) => Some(len as uint),
|
Ok(len) => Ok(len as uint),
|
||||||
Err(StreamEof) | Err(SslSessionClosed) => None,
|
Err(SslSessionClosed) =>
|
||||||
|
Err(IoError {
|
||||||
|
kind: OtherIoError,
|
||||||
|
desc: "SSL session closed",
|
||||||
|
detail: None
|
||||||
|
}),
|
||||||
|
Err(StreamError(e)) => Err(e),
|
||||||
_ => unreachable!()
|
_ => unreachable!()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<S: Stream> Writer for SslStream<S> {
|
impl<S: Stream> Writer for SslStream<S> {
|
||||||
fn write(&mut self, buf: &[u8]) {
|
fn write(&mut self, buf: &[u8]) -> IoResult<()> {
|
||||||
let mut start = 0;
|
let mut start = 0;
|
||||||
while start < buf.len() {
|
while start < buf.len() {
|
||||||
let ret = self.in_retry_wrapper(|ssl| {
|
let ret = self.in_retry_wrapper(|ssl| {
|
||||||
|
|
@ -513,9 +521,10 @@ impl<S: Stream> Writer for SslStream<S> {
|
||||||
}
|
}
|
||||||
self.write_through();
|
self.write_through();
|
||||||
}
|
}
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn flush(&mut self) {
|
fn flush(&mut self) -> IoResult<()> {
|
||||||
self.write_through();
|
self.write_through();
|
||||||
self.stream.flush()
|
self.stream.flush()
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -155,6 +155,6 @@ fn test_read() {
|
||||||
let mut stream = SslStream::new(&SslContext::new(Sslv23), stream);
|
let mut stream = SslStream::new(&SslContext::new(Sslv23), stream);
|
||||||
stream.write("GET /\r\n\r\n".as_bytes());
|
stream.write("GET /\r\n\r\n".as_bytes());
|
||||||
stream.flush();
|
stream.flush();
|
||||||
let buf = stream.read_to_end();
|
let buf = stream.read_to_end().ok().expect("read error");
|
||||||
print!("{}", str::from_utf8(buf));
|
print!("{}", str::from_utf8(buf));
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue