forked from rust-postgres/rust-postgres
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathio.rs
More file actions
131 lines (113 loc) · 3.83 KB
/
Copy pathio.rs
File metadata and controls
131 lines (113 loc) · 3.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
use openssl::ssl;
use std::io::net::ip::Port;
use std::io::net::tcp::TcpStream;
use std::io::net::pipe::UnixStream;
use std::io::{Stream, IoResult};
use {ConnectParams, SslMode, ConnectTarget, ConnectError};
use message;
use message::WriteMessage;
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),
}
impl Reader for InternalStream {
fn read(&mut self, buf: &mut [u8]) -> IoResult<uint> {
match *self {
InternalStream::Tcp(ref mut s) => s.read(buf),
InternalStream::Unix(ref mut s) => s.read(buf),
}
}
}
impl Writer for InternalStream {
fn write(&mut self, buf: &[u8]) -> IoResult<()> {
match *self {
InternalStream::Tcp(ref mut s) => s.write(buf),
InternalStream::Unix(ref mut s) => s.write(buf),
}
}
fn flush(&mut self) -> IoResult<()> {
match *self {
InternalStream::Tcp(ref mut s) => s.flush(),
InternalStream::Unix(ref mut s) => s.flush(),
}
}
}
impl InternalStream {
pub fn set_read_timeout(&mut self, timeout_ms: Option<u64>) {
match *self {
InternalStream::Tcp(ref mut s) => s.set_read_timeout(timeout_ms),
InternalStream::Unix(ref mut s) => s.set_read_timeout(timeout_ms),
}
}
}
fn open_socket(params: &ConnectParams) -> Result<InternalStream, ConnectError> {
let port = params.port.unwrap_or(DEFAULT_PORT);
match params.target {
ConnectTarget::Tcp(ref host) =>
Ok(try!(TcpStream::connect((host[], port)).map(InternalStream::Tcp))),
ConnectTarget::Unix(ref path) => {
let mut path = path.clone();
path.push(format!(".s.PGSQL.{}", port));
Ok(try!(UnixStream::connect(&path).map(InternalStream::Unix)))
}
}
}
pub fn initialize_stream(params: &ConnectParams, ssl: &SslMode)
-> Result<MaybeSslStream<InternalStream>, ConnectError> {
let mut socket = try!(open_socket(params));
let (ssl_required, ctx) = match *ssl {
SslMode::None => return Ok(MaybeSslStream::Normal(socket)),
SslMode::Prefer(ref ctx) => (false, ctx),
SslMode::Require(ref ctx) => (true, ctx)
};
try!(socket.write_message(&SslRequest { code: message::SSL_CODE }));
try!(socket.flush());
if try!(socket.read_u8()) == 'N' as u8 {
if ssl_required {
return Err(ConnectError::NoSslSupport);
} else {
return Ok(MaybeSslStream::Normal(socket));
}
}
match ssl::SslStream::new(ctx, socket) {
Ok(stream) => Ok(MaybeSslStream::Ssl(stream)),
Err(err) => Err(ConnectError::SslError(err))
}
}