Skip to content

Commit

Permalink
feat: correctly handle percent-encoded socket as host for postgres URI
Browse files Browse the repository at this point in the history
  • Loading branch information
dmarcuse authored and mehcode committed Oct 18, 2020
1 parent 7b1b8c1 commit d97014f
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion sqlx-core/src/postgres/options/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ impl FromStr for PgConnectOptions {
let mut options = Self::default();

if let Some(host) = url.host_str() {
options = options.host(host);
let host_decoded = percent_decode_str(host);
options = match host_decoded.clone().next() {
Some(b'/') => options.socket(&*host_decoded.decode_utf8().map_err(Error::config)?),
_ => options.host(host),
}
}

if let Some(port) = url.port() {
Expand Down Expand Up @@ -174,3 +178,20 @@ fn it_parses_password_with_non_ascii_chars_correctly() {

assert_eq!(Some("p@ssw0rd".into()), opts.password);
}

#[test]
fn it_parses_socket_correctly_percent_encoded() {
let uri = "postgres://%2Fvar%2Flib%2Fpostgres/database";
let opts = PgConnectOptions::from_str(uri).unwrap();

assert_eq!(Some("/var/lib/postgres/".into()), opts.socket);
}
#[test]
fn it_parses_socket_correctly_with_username_percent_encoded() {
let uri = "postgres://some_user@%2Fvar%2Flib%2Fpostgres/database";
let opts = PgConnectOptions::from_str(uri).unwrap();

assert_eq!("some_user", opts.username);
assert_eq!(Some("/var/lib/postgres/".into()), opts.socket);
assert_eq!(Some("database"), opts.database.as_deref());
}

0 comments on commit d97014f

Please sign in to comment.