Skip to content

Commit 59e1273

Browse files
committed
Start switching over to postgres-protocol
1 parent 1c27224 commit 59e1273

3 files changed

Lines changed: 21 additions & 26 deletions

File tree

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ byteorder = "0.5"
3737
hex = "0.2"
3838
log = "0.3"
3939
phf = "=0.7.15"
40+
postgres-protocol = { git = "https://github.com/sfackler/rust-postgres-protocol" }
4041
bit-vec = { version = "0.4", optional = true }
4142
chrono = { version = "0.2.14", optional = true }
4243
eui48 = { version = "0.1", optional = true }

src/lib.rs

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ extern crate hex;
4848
#[macro_use]
4949
extern crate log;
5050
extern crate phf;
51+
extern crate postgres_protocol;
5152

5253
use bufstream::BufStream;
5354
use md5::Md5;
@@ -60,6 +61,7 @@ use std::mem;
6061
use std::result;
6162
use std::sync::Arc;
6263
use std::time::Duration;
64+
use postgres_protocol::message::frontend;
6365

6466
use error::{Error, ConnectError, SqlState, DbError};
6567
use io::{TlsStream, TlsHandshake};
@@ -205,6 +207,7 @@ struct StatementInfo {
205207

206208
struct InnerConnection {
207209
stream: BufStream<Box<TlsStream>>,
210+
io_buf: Vec<u8>,
208211
notice_handler: Box<HandleNotice>,
209212
notifications: VecDeque<Notification>,
210213
cancel_data: CancelData,
@@ -246,6 +249,7 @@ impl InnerConnection {
246249

247250
let mut conn = InnerConnection {
248251
stream: BufStream::new(stream),
252+
io_buf: vec![],
249253
next_stmt_id: 0,
250254
notice_handler: Box::new(LoggingNoticeHandler),
251255
notifications: VecDeque::new(),
@@ -274,10 +278,10 @@ impl InnerConnection {
274278
options.push(("database".to_owned(), database));
275279
}
276280

277-
try!(conn.write_messages(&[Frontend::StartupMessage {
278-
version: message::PROTOCOL_VERSION,
279-
parameters: &options,
280-
}]));
281+
try!(conn.write_message(&frontend::StartupMessage {
282+
parameters: &options,
283+
}));
284+
try!(conn.stream.flush());
281285

282286
try!(conn.handle_auth(user));
283287

@@ -296,6 +300,14 @@ impl InnerConnection {
296300
Ok(conn)
297301
}
298302

303+
fn write_message<M>(&mut self, message: &M) -> std_io::Result<()>
304+
where M: frontend::Message
305+
{
306+
self.io_buf.clear();
307+
try!(message.write(&mut self.io_buf));
308+
self.stream.write_all(&self.io_buf)
309+
}
310+
299311
fn write_messages(&mut self, messages: &[Frontend]) -> std_io::Result<()> {
300312
debug_assert!(!self.desynchronized);
301313
for message in messages {
@@ -380,7 +392,8 @@ impl InnerConnection {
380392
let pass = try!(user.password.ok_or_else(|| {
381393
ConnectError::ConnectParams("a password was requested but not provided".into())
382394
}));
383-
try!(self.write_messages(&[Frontend::PasswordMessage { password: &pass }]));
395+
try!(self.write_message(&frontend::PasswordMessage { password: &pass }));
396+
try!(self.stream.flush());
384397
}
385398
Backend::AuthenticationMD5Password { salt } => {
386399
let pass = try!(user.password.ok_or_else(|| {
@@ -394,7 +407,8 @@ impl InnerConnection {
394407
hasher.input(output.as_bytes());
395408
hasher.input(&salt);
396409
let output = format!("md5{}", hasher.result_str());
397-
try!(self.write_messages(&[Frontend::PasswordMessage { password: &output }]));
410+
try!(self.write_message(&frontend::PasswordMessage { password: &output }));
411+
try!(self.stream.flush());
398412
}
399413
Backend::AuthenticationKerberosV5 |
400414
Backend::AuthenticationSCMCredential |

src/message.rs

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ use byteorder::{BigEndian, ReadBytesExt, WriteBytesExt};
77
use types::Oid;
88
use priv_io::StreamOptions;
99

10-
pub const PROTOCOL_VERSION: u32 = 0x0003_0000;
1110
pub const CANCEL_CODE: u32 = 80877102;
1211
pub const SSL_CODE: u32 = 80877103;
1312

@@ -122,19 +121,12 @@ pub enum Frontend<'a> {
122121
query: &'a str,
123122
param_types: &'a [Oid],
124123
},
125-
PasswordMessage {
126-
password: &'a str,
127-
},
128124
Query {
129125
query: &'a str,
130126
},
131127
SslRequest {
132128
code: u32,
133129
},
134-
StartupMessage {
135-
version: u32,
136-
parameters: &'a [(String, String)],
137-
},
138130
Sync,
139131
Terminate,
140132
}
@@ -227,22 +219,10 @@ impl<W: Write> WriteMessage for W {
227219
try!(buf.write_u32::<BigEndian>(ty));
228220
}
229221
}
230-
Frontend::PasswordMessage { password } => {
231-
ident = Some(b'p');
232-
try!(buf.write_cstr(password));
233-
}
234222
Frontend::Query { query } => {
235223
ident = Some(b'Q');
236224
try!(buf.write_cstr(query));
237225
}
238-
Frontend::StartupMessage { version, parameters } => {
239-
try!(buf.write_u32::<BigEndian>(version));
240-
for &(ref k, ref v) in parameters {
241-
try!(buf.write_cstr(&**k));
242-
try!(buf.write_cstr(&**v));
243-
}
244-
try!(buf.write_u8(0));
245-
}
246226
Frontend::SslRequest { code } => try!(buf.write_u32::<BigEndian>(code)),
247227
Frontend::Sync => ident = Some(b'S'),
248228
Frontend::Terminate => ident = Some(b'X'),

0 commit comments

Comments
 (0)