Skip to content

Commit 3c83d46

Browse files
committed
Move types around and add docs
1 parent 2a6f075 commit 3c83d46

3 files changed

Lines changed: 85 additions & 62 deletions

File tree

src/io.rs

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
//! Types and traits for SSL adaptors.
2+
3+
pub use priv_io::Stream;
4+
5+
use openssl::ssl::{SslStream, SslContext};
6+
use std::error::Error;
7+
use std::io::prelude::*;
8+
9+
/// A trait implemented by SSL adaptors.
10+
pub trait StreamWrapper: Read+Write+Send {
11+
/// Returns a reference to the underlying `Stream`.
12+
fn get_ref(&self) -> &Stream;
13+
14+
/// Returns a mutable reference to the underlying `Stream`.
15+
fn get_mut(&mut self) -> &mut Stream;
16+
}
17+
18+
impl StreamWrapper for SslStream<Stream> {
19+
fn get_ref(&self) -> &Stream {
20+
self.get_ref()
21+
}
22+
23+
fn get_mut(&mut self) -> &mut Stream {
24+
self.get_mut()
25+
}
26+
}
27+
28+
/// A trait implemented by types that can negotiate SSL over a Postgres stream.
29+
pub trait NegotiateSsl {
30+
/// Negotiates an SSL session, returning a wrapper around the provided
31+
/// stream.
32+
///
33+
/// The host portion of the connection parameters is provided for hostname
34+
/// verification.
35+
fn negotiate_ssl(&mut self, host: &str, stream: Stream)
36+
-> Result<Box<StreamWrapper>, Box<Error>>;
37+
}
38+
39+
impl NegotiateSsl for SslContext {
40+
fn negotiate_ssl(&mut self, _: &str, stream: Stream)
41+
-> Result<Box<StreamWrapper>, Box<Error>> {
42+
let stream = try!(SslStream::new(self, stream));
43+
Ok(Box::new(stream))
44+
}
45+
}
46+
47+
/// An uninhabited type implementing `NegotiateSsl`.
48+
///
49+
/// `NoSsl` cannot be instantiated, so the only `SslMode<NoSslMode>` value that
50+
/// can exist is `SslMode::None`. `NoSsl` is the default value of `SslMode`'s
51+
/// parameter so `&mut SslMode::None` can always be passed into
52+
/// `Connection::connect` even if no SSL implementation is available.
53+
pub enum NoSsl {}
54+
55+
impl NegotiateSsl for NoSsl {
56+
fn negotiate_ssl(&mut self, _: &str, _: Stream) -> Result<Box<StreamWrapper>, Box<Error>> {
57+
match *self {}
58+
}
59+
}

src/lib.rs

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ use std::cell::{Cell, RefCell};
6666
use std::collections::{VecDeque, HashMap};
6767
use std::fmt;
6868
use std::iter::IntoIterator;
69-
use std::io;
69+
use std::io as std_io;
7070
use std::io::prelude::*;
7171
use std::mem;
7272
use std::slice;
@@ -79,7 +79,7 @@ use std::path::PathBuf;
7979
pub use error::{Error, ConnectError, SqlState, DbError, ErrorPosition};
8080
#[doc(inline)]
8181
pub use types::{Oid, Type, Kind, ToSql, FromSql};
82-
pub use io_util::{SslMode, NegotiateSsl, StreamWrapper, NoSsl, Stream};
82+
use io::{NoSsl, StreamWrapper, NegotiateSsl};
8383
use types::IsNull;
8484
#[doc(inline)]
8585
pub use types::Slice;
@@ -93,9 +93,10 @@ use url::Url;
9393
mod macros;
9494

9595
mod error;
96-
mod io_util;
96+
pub mod io;
9797
mod message;
9898
mod ugh_privacy;
99+
mod priv_io;
99100
mod url;
100101
mod util;
101102
pub mod types;
@@ -390,7 +391,7 @@ pub fn cancel_query<T, N>(params: T, ssl: &mut SslMode<N>, data: CancelData)
390391
-> result::Result<(), ConnectError>
391392
where T: IntoConnectParams, N: NegotiateSsl {
392393
let params = try!(params.into_connect_params());
393-
let mut socket = try!(io_util::initialize_stream(&params, ssl));
394+
let mut socket = try!(priv_io::initialize_stream(&params, ssl));
394395

395396
try!(socket.write_message(&CancelRequest {
396397
code: message::CANCEL_CODE,
@@ -456,6 +457,16 @@ impl IsolationLevel {
456457
}
457458
}
458459

460+
/// Specifies the SSL support requested for a new connection.
461+
pub enum SslMode<N = NoSsl> {
462+
/// The connection will not use SSL.
463+
None,
464+
/// The connection will use SSL if the backend supports it.
465+
Prefer(N),
466+
/// The connection must use SSL.
467+
Require(N),
468+
}
469+
459470
#[derive(Clone)]
460471
struct CachedStatement {
461472
name: String,
@@ -490,7 +501,7 @@ impl InnerConnection {
490501
-> result::Result<InnerConnection, ConnectError>
491502
where T: IntoConnectParams, N: NegotiateSsl {
492503
let params = try!(params.into_connect_params());
493-
let stream = try!(io_util::initialize_stream(&params, ssl));
504+
let stream = try!(priv_io::initialize_stream(&params, ssl));
494505

495506
let ConnectParams { user, database, mut options, .. } = params;
496507

@@ -570,15 +581,15 @@ impl InnerConnection {
570581
}
571582
}
572583

573-
fn write_messages(&mut self, messages: &[FrontendMessage]) -> io::Result<()> {
584+
fn write_messages(&mut self, messages: &[FrontendMessage]) -> std_io::Result<()> {
574585
debug_assert!(!self.desynchronized);
575586
for message in messages {
576587
try_desync!(self, self.stream.write_message(message));
577588
}
578589
Ok(try_desync!(self, self.stream.flush()))
579590
}
580591

581-
fn read_one_message(&mut self) -> io::Result<Option<BackendMessage>> {
592+
fn read_one_message(&mut self) -> std_io::Result<Option<BackendMessage>> {
582593
debug_assert!(!self.desynchronized);
583594
match try_desync!(self, self.stream.read_message()) {
584595
NoticeResponse { fields } => {
@@ -595,15 +606,15 @@ impl InnerConnection {
595606
}
596607
}
597608

598-
fn read_message_with_notification(&mut self) -> io::Result<BackendMessage> {
609+
fn read_message_with_notification(&mut self) -> std_io::Result<BackendMessage> {
599610
loop {
600611
if let Some(msg) = try!(self.read_one_message()) {
601612
return Ok(msg);
602613
}
603614
}
604615
}
605616

606-
fn read_message(&mut self) -> io::Result<BackendMessage> {
617+
fn read_message(&mut self) -> std_io::Result<BackendMessage> {
607618
loop {
608619
match try!(self.read_message_with_notification()) {
609620
NotificationResponse { pid, channel, payload } => {

src/io_util.rs renamed to src/priv_io.rs

Lines changed: 6 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
1-
use openssl::ssl::{SslStream, SslContext};
2-
use std::error::Error;
1+
use byteorder::ReadBytesExt;
32
use std::io;
43
use std::io::prelude::*;
54
use std::net::TcpStream;
65
#[cfg(feature = "unix_socket")]
76
use unix_socket::UnixStream;
8-
use byteorder::ReadBytesExt;
97

10-
use {ConnectParams, ConnectTarget, ConnectError};
11-
use message;
12-
use message::WriteMessage;
8+
use {SslMode, ConnectError, ConnectParams, ConnectTarget};
9+
use io::{NegotiateSsl, StreamWrapper};
10+
use message::{self, WriteMessage};
1311
use message::FrontendMessage::SslRequest;
1412

1513
const DEFAULT_PORT: u16 = 5432;
1614

15+
/// A connection to the Postgres server.
1716
pub struct Stream(InternalStream);
1817

1918
impl Read for Stream {
@@ -42,53 +41,7 @@ impl StreamWrapper for Stream {
4241
}
4342
}
4443

45-
pub trait StreamWrapper: Read+Write+Send {
46-
fn get_ref(&self) -> &Stream;
47-
fn get_mut(&mut self) -> &mut Stream;
48-
}
49-
50-
impl StreamWrapper for SslStream<Stream> {
51-
fn get_ref(&self) -> &Stream {
52-
self.get_ref()
53-
}
54-
55-
fn get_mut(&mut self) -> &mut Stream {
56-
self.get_mut()
57-
}
58-
}
59-
60-
pub trait NegotiateSsl {
61-
fn negotiate_ssl(&mut self, host: &str, stream: Stream)
62-
-> Result<Box<StreamWrapper>, Box<Error>>;
63-
}
64-
65-
impl NegotiateSsl for SslContext {
66-
fn negotiate_ssl(&mut self, _: &str, stream: Stream)
67-
-> Result<Box<StreamWrapper>, Box<Error>> {
68-
let stream = try!(SslStream::new(self, stream));
69-
Ok(Box::new(stream))
70-
}
71-
}
72-
73-
/// Specifies the SSL support requested for a new connection.
74-
pub enum SslMode<N = NoSsl> {
75-
/// The connection will not use SSL.
76-
None,
77-
/// The connection will use SSL if the backend supports it.
78-
Prefer(N),
79-
/// The connection must use SSL.
80-
Require(N),
81-
}
82-
83-
pub enum NoSsl {}
84-
85-
impl NegotiateSsl for NoSsl {
86-
fn negotiate_ssl(&mut self, _: &str, _: Stream) -> Result<Box<StreamWrapper>, Box<Error>> {
87-
match *self {}
88-
}
89-
}
90-
91-
pub enum InternalStream {
44+
enum InternalStream {
9245
Tcp(TcpStream),
9346
#[cfg(feature = "unix_socket")]
9447
Unix(UnixStream),

0 commit comments

Comments
 (0)