test_unix_connection now detects the socket directory.
Change pg_hba.conf to allow connections through the socket. Ignore connect_unix doc test. It requires `extern crate url;` which is not allowed with rustdoc. Also, per comments on PR #35: - Inline open_unix - Centralize common code from connect and connect_unix in connect_finish.
This commit is contained in:
parent
707e7ccfa4
commit
5e85d6b9bd
54
src/lib.rs
54
src/lib.rs
@ -328,13 +328,6 @@ fn open_socket(host: &str, port: Port)
|
||||
Err(SocketError(err.unwrap()))
|
||||
}
|
||||
|
||||
fn open_unix(path: &Path) -> Result<UnixStream, PostgresConnectError> {
|
||||
match UnixStream::connect(path) {
|
||||
Ok(unix) => Ok(unix),
|
||||
Err(err) => Err(SocketError(err))
|
||||
}
|
||||
}
|
||||
|
||||
fn initialize_stream(host: &str, port: Port, ssl: &SslMode)
|
||||
-> Result<InternalStream, PostgresConnectError> {
|
||||
let mut socket = match open_socket(host, port) {
|
||||
@ -367,9 +360,9 @@ fn initialize_stream(host: &str, port: Port, ssl: &SslMode)
|
||||
|
||||
fn initialize_unix(path: &Path)
|
||||
-> Result<InternalStream, PostgresConnectError> {
|
||||
match open_unix(path) {
|
||||
match UnixStream::connect(path) {
|
||||
Ok(unix) => Ok(NormalUnix(unix)),
|
||||
Err(err) => Err(err)
|
||||
Err(err) => Err(SocketError(err))
|
||||
}
|
||||
}
|
||||
|
||||
@ -454,31 +447,13 @@ impl InnerPostgresConnection {
|
||||
|
||||
let stream = try!(initialize_stream(host, port, ssl));
|
||||
|
||||
let conn = InnerPostgresConnection {
|
||||
stream: BufferedStream::new(stream),
|
||||
next_stmt_id: 0,
|
||||
notice_handler: ~DefaultNoticeHandler,
|
||||
notifications: RingBuf::new(),
|
||||
cancel_data: PostgresCancelData { process_id: 0, secret_key: 0 },
|
||||
unknown_types: HashMap::new(),
|
||||
desynchronized: false,
|
||||
finished: false,
|
||||
canary: CANARY,
|
||||
};
|
||||
|
||||
args.push((~"client_encoding", ~"UTF8"));
|
||||
// Postgres uses the value of TimeZone as the time zone for TIMESTAMP
|
||||
// WITH TIME ZONE values. Timespec converts to GMT internally.
|
||||
args.push((~"TimeZone", ~"GMT"));
|
||||
// We have to clone here since we need the user again for auth
|
||||
args.push((~"user", user.user.clone()));
|
||||
if !path.is_empty() {
|
||||
// path contains the leading /
|
||||
let (_, path) = path.slice_shift_char();
|
||||
args.push((~"database", path.to_owned()));
|
||||
}
|
||||
|
||||
InnerPostgresConnection::connect_finish(conn, args, user)
|
||||
InnerPostgresConnection::connect_finish(stream, args, user)
|
||||
}
|
||||
|
||||
fn connect_unix(socket_dir: &Path, port: Port, user: UserInfo, database: ~str)
|
||||
@ -488,7 +463,16 @@ impl InnerPostgresConnection {
|
||||
|
||||
let stream = try!(initialize_unix(&socket));
|
||||
|
||||
let conn = InnerPostgresConnection {
|
||||
let mut args = Vec::new();
|
||||
args.push((~"database", database));
|
||||
|
||||
InnerPostgresConnection::connect_finish(stream, args, user)
|
||||
}
|
||||
|
||||
fn connect_finish(stream: InternalStream, mut args: Vec<(~str, ~str)>, user: UserInfo)
|
||||
-> Result<InnerPostgresConnection, PostgresConnectError> {
|
||||
|
||||
let mut conn = InnerPostgresConnection {
|
||||
stream: BufferedStream::new(stream),
|
||||
next_stmt_id: 0,
|
||||
notice_handler: ~DefaultNoticeHandler,
|
||||
@ -500,21 +484,13 @@ impl InnerPostgresConnection {
|
||||
canary: CANARY,
|
||||
};
|
||||
|
||||
let mut args = Vec::new();
|
||||
|
||||
args.push((~"client_encoding", ~"UTF8"));
|
||||
// Postgres uses the value of TimeZone as the time zone for TIMESTAMP
|
||||
// WITH TIME ZONE values. Timespec converts to GMT internally.
|
||||
args.push((~"TimeZone", ~"GMT"));
|
||||
// We have to clone here since we need the user again for auth
|
||||
args.push((~"user", user.user.clone()));
|
||||
args.push((~"database", database));
|
||||
|
||||
InnerPostgresConnection::connect_finish(conn, args, user)
|
||||
}
|
||||
|
||||
fn connect_finish(mut conn: InnerPostgresConnection, args: Vec<(~str, ~str)>, user: UserInfo)
|
||||
-> Result<InnerPostgresConnection, PostgresConnectError> {
|
||||
try_pg_conn!(conn.write_messages([StartupMessage {
|
||||
version: message::PROTOCOL_VERSION,
|
||||
parameters: args.as_slice()
|
||||
@ -796,7 +772,9 @@ impl PostgresConnection {
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
/// ```rust,no_run
|
||||
/// ```rust,ignore
|
||||
/// # extern crate url;
|
||||
/// # use url::UserInfo;
|
||||
/// # use postgres::PostgresConnection;
|
||||
/// let path = Path::new("/tmp");
|
||||
/// let port = 5432;
|
||||
|
13
src/test.rs
13
src/test.rs
@ -110,7 +110,18 @@ fn test_connection_finish() {
|
||||
|
||||
#[test]
|
||||
fn test_unix_connection() {
|
||||
let conn = or_fail!(PostgresConnection::connect_unix(&Path::new("/tmp"), 5432, UserInfo::new(~"postgres", None), ~"postgres"));
|
||||
let conn = or_fail!(PostgresConnection::connect("postgres://postgres@localhost", &NoSsl));
|
||||
let stmt = or_fail!(conn.prepare("SHOW unix_socket_directories"));
|
||||
let result = or_fail!(stmt.query([]));
|
||||
let unix_socket_directories: ~str = result.map(|row| row[1]).next().unwrap();
|
||||
|
||||
if unix_socket_directories == ~"" {
|
||||
fail!("can't test connect_unix; unix_socket_directories is empty");
|
||||
}
|
||||
|
||||
let unix_socket_directory = unix_socket_directories.splitn(',', 1).next().unwrap();
|
||||
|
||||
let conn = or_fail!(PostgresConnection::connect_unix(&Path::new(unix_socket_directory), 5432, UserInfo::new(~"postgres", None), ~"postgres"));
|
||||
assert!(conn.finish().is_ok());
|
||||
}
|
||||
|
||||
|
@ -8,3 +8,5 @@ host all md5_user ::1/128 md5
|
||||
host all postgres 127.0.0.1/32 trust
|
||||
# IPv6 local connections:
|
||||
host all postgres ::1/128 trust
|
||||
# Unix socket connections:
|
||||
local all postgres trust
|
||||
|
Loading…
Reference in New Issue
Block a user