Use rust-openssl's MaybeSslStream

This commit is contained in:
Steven Fackler 2014-11-28 15:54:37 -08:00
parent 6334f045cb
commit e7f66f89ac
3 changed files with 8 additions and 49 deletions

View File

@ -24,7 +24,7 @@ default = ["uuid", "time"]
[dependencies]
phf = "0.1"
phf_mac = "0.1"
openssl = "0.2"
openssl = "0.2.1"
[dependencies.uuid]
optional = true

View File

@ -1,8 +1,8 @@
use openssl::ssl;
use openssl::ssl::{SslStream, MaybeSslStream};
use std::io::net::ip::Port;
use std::io::net::tcp::TcpStream;
use std::io::net::pipe::UnixStream;
use std::io::{Stream, IoResult};
use std::io::IoResult;
use {ConnectParams, SslMode, ConnectTarget, ConnectError};
use message;
@ -11,46 +11,6 @@ use message::FrontendMessage::SslRequest;
const DEFAULT_PORT: Port = 5432;
pub enum MaybeSslStream<S> {
Ssl(ssl::SslStream<S>),
Normal(S),
}
impl<S: Stream> Reader for MaybeSslStream<S> {
fn read(&mut self, buf: &mut [u8]) -> IoResult<uint> {
match *self {
MaybeSslStream::Ssl(ref mut s) => s.read(buf),
MaybeSslStream::Normal(ref mut s) => s.read(buf),
}
}
}
impl<S: Stream> Writer for MaybeSslStream<S> {
fn write(&mut self, buf: &[u8]) -> IoResult<()> {
match *self {
MaybeSslStream::Ssl(ref mut s) => s.write(buf),
MaybeSslStream::Normal(ref mut s) => s.write(buf),
}
}
fn flush(&mut self) -> IoResult<()> {
match *self {
MaybeSslStream::Ssl(ref mut s) => s.flush(),
MaybeSslStream::Normal(ref mut s) => s.flush(),
}
}
}
impl MaybeSslStream<InternalStream> {
#[allow(dead_code)]
pub fn set_read_timeout(&mut self, timeout_ms: Option<u64>) {
match *self {
MaybeSslStream::Ssl(ref mut s) => s.get_inner().set_read_timeout(timeout_ms),
MaybeSslStream::Normal(ref mut s) => s.set_read_timeout(timeout_ms),
}
}
}
pub enum InternalStream {
Tcp(TcpStream),
Unix(UnixStream),
@ -82,6 +42,7 @@ impl Writer for InternalStream {
}
impl InternalStream {
#[allow(dead_code)]
pub fn set_read_timeout(&mut self, timeout_ms: Option<u64>) {
match *self {
InternalStream::Tcp(ref mut s) => s.set_read_timeout(timeout_ms),
@ -124,7 +85,7 @@ pub fn initialize_stream(params: &ConnectParams, ssl: &SslMode)
}
}
match ssl::SslStream::new(ctx, socket) {
match SslStream::new(ctx, socket) {
Ok(stream) => Ok(MaybeSslStream::Ssl(stream)),
Err(err) => Err(ConnectError::SslError(err))
}

View File

@ -68,7 +68,7 @@ extern crate log;
use url::Url;
use openssl::crypto::hash::{HashType, Hasher};
use openssl::ssl::SslContext;
use openssl::ssl::{SslContext, MaybeSslStream};
use serialize::hex::ToHex;
use std::cell::{Cell, RefCell};
use std::collections::{RingBuf, HashMap};
@ -78,7 +78,7 @@ use std::mem;
use std::fmt;
use std::result;
use io::{MaybeSslStream, InternalStream};
use io::InternalStream;
use message::{FrontendMessage, BackendMessage, RowDescriptionEntry};
use message::FrontendMessage::*;
use message::BackendMessage::*;
@ -583,9 +583,7 @@ impl InnerConnection {
Sync]));
let resp = match try!(self.read_message()) {
CloseComplete => Ok(()),
ErrorResponse { fields } => {
DbError::new(fields)
}
ErrorResponse { fields } => DbError::new(fields),
_ => {
self.desynchronized = true;
return Err(Error::BadResponse);