-
Notifications
You must be signed in to change notification settings - Fork 185
RUST-229 Parse IPv6 addresses in the connection string #1242
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,7 @@ use std::{ | |
convert::TryFrom, | ||
fmt::{self, Display, Formatter, Write}, | ||
hash::{Hash, Hasher}, | ||
net::Ipv6Addr, | ||
path::PathBuf, | ||
str::FromStr, | ||
time::Duration, | ||
|
@@ -128,9 +129,29 @@ impl<'de> Deserialize<'de> for ServerAddress { | |
where | ||
D: Deserializer<'de>, | ||
{ | ||
let s: String = Deserialize::deserialize(deserializer)?; | ||
Self::parse(s.as_str()) | ||
.map_err(|e| <D::Error as serde::de::Error>::custom(format!("{}", e))) | ||
#[derive(Deserialize)] | ||
#[serde(untagged)] | ||
enum ServerAddressHelper { | ||
String(String), | ||
Object { host: String, port: Option<u16> }, | ||
} | ||
|
||
let helper = ServerAddressHelper::deserialize(deserializer)?; | ||
match helper { | ||
ServerAddressHelper::String(string) => { | ||
Self::parse(string).map_err(serde::de::Error::custom) | ||
} | ||
ServerAddressHelper::Object { host, port } => { | ||
#[cfg(unix)] | ||
if host.ends_with("sock") { | ||
return Ok(Self::Unix { | ||
path: PathBuf::from(host), | ||
}); | ||
} | ||
|
||
Ok(Self::Tcp { host, port }) | ||
} | ||
} | ||
} | ||
} | ||
|
||
|
@@ -185,74 +206,99 @@ impl FromStr for ServerAddress { | |
} | ||
|
||
impl ServerAddress { | ||
/// Parses an address string into a `ServerAddress`. | ||
/// Parses an address string into a [`ServerAddress`]. | ||
pub fn parse(address: impl AsRef<str>) -> Result<Self> { | ||
let address = address.as_ref(); | ||
// checks if the address is a unix domain socket | ||
|
||
#[cfg(unix)] | ||
{ | ||
if address.ends_with(".sock") { | ||
return Ok(ServerAddress::Unix { | ||
path: PathBuf::from(address), | ||
}); | ||
#[cfg(unix)] | ||
{ | ||
let address = | ||
percent_decode(address, "unix domain sockets must be URL-encoded")?; | ||
return Ok(Self::Unix { | ||
path: PathBuf::from(address), | ||
}); | ||
} | ||
#[cfg(not(unix))] | ||
|
||
return Err(ErrorKind::InvalidArgument { | ||
message: "unix domain sockets are not supported on this platform", | ||
} | ||
.into()); | ||
} | ||
} | ||
let mut parts = address.split(':'); | ||
let hostname = match parts.next() { | ||
Some(part) => { | ||
if part.is_empty() { | ||
return Err(ErrorKind::InvalidArgument { | ||
message: format!( | ||
"invalid server address: \"{}\"; hostname cannot be empty", | ||
address | ||
), | ||
} | ||
.into()); | ||
|
||
let (hostname, port) = if let Some(ip_literal) = address.strip_prefix("[") { | ||
let Some((hostname, port)) = ip_literal.split_once("]") else { | ||
return Err(ErrorKind::InvalidArgument { | ||
message: format!( | ||
"invalid server address {}: missing closing ']' in IP literal hostname", | ||
address | ||
), | ||
} | ||
part | ||
} | ||
None => { | ||
.into()); | ||
}; | ||
|
||
if let Err(parse_error) = Ipv6Addr::from_str(hostname) { | ||
isabelatkinson marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return Err(ErrorKind::InvalidArgument { | ||
message: format!("invalid server address: \"{}\"", address), | ||
message: format!("invalid server address {}: {}", address, parse_error), | ||
} | ||
.into()) | ||
.into()); | ||
} | ||
}; | ||
|
||
let port = match parts.next() { | ||
Some(part) => { | ||
let port = u16::from_str(part).map_err(|_| ErrorKind::InvalidArgument { | ||
let port = if port.is_empty() { | ||
None | ||
} else if let Some(port) = port.strip_prefix(":") { | ||
Some(port) | ||
} else { | ||
return Err(ErrorKind::InvalidArgument { | ||
message: format!( | ||
"port must be valid 16-bit unsigned integer, instead got: {}", | ||
part | ||
"invalid server address {}: the hostname can only be followed by a port \ | ||
prefixed with ':', got {}", | ||
address, port | ||
), | ||
})?; | ||
|
||
if port == 0 { | ||
return Err(ErrorKind::InvalidArgument { | ||
message: format!( | ||
"invalid server address: \"{}\"; port must be non-zero", | ||
address | ||
), | ||
} | ||
.into()); | ||
} | ||
if parts.next().is_some() { | ||
.into()); | ||
}; | ||
|
||
(hostname, port) | ||
} else { | ||
match address.split_once(":") { | ||
Some((hostname, port)) => (hostname, Some(port)), | ||
None => (address, None), | ||
} | ||
}; | ||
|
||
if hostname.is_empty() { | ||
return Err(ErrorKind::InvalidArgument { | ||
message: format!( | ||
"invalid server address {}: the hostname cannot be empty", | ||
address | ||
), | ||
} | ||
.into()); | ||
} | ||
|
||
let port = if let Some(port) = port { | ||
match u16::from_str(port) { | ||
Ok(0) | Err(_) => { | ||
return Err(ErrorKind::InvalidArgument { | ||
message: format!( | ||
"address \"{}\" contains more than one unescaped ':'", | ||
address | ||
"invalid server address {}: the port must be an integer between 1 and \ | ||
65535, got {}", | ||
address, port | ||
), | ||
} | ||
.into()); | ||
.into()) | ||
} | ||
|
||
Some(port) | ||
Ok(port) => Some(port), | ||
} | ||
None => None, | ||
} else { | ||
None | ||
}; | ||
|
||
Ok(ServerAddress::Tcp { | ||
Ok(Self::Tcp { | ||
host: hostname.to_lowercase(), | ||
port, | ||
}) | ||
|
@@ -1440,31 +1486,15 @@ impl ConnectionString { | |
None => (None, None), | ||
}; | ||
|
||
let mut host_list = Vec::with_capacity(hosts_section.len()); | ||
for host in hosts_section.split(',') { | ||
let address = if host.ends_with(".sock") { | ||
#[cfg(unix)] | ||
{ | ||
ServerAddress::parse(percent_decode( | ||
host, | ||
"Unix domain sockets must be URL-encoded", | ||
)?) | ||
} | ||
#[cfg(not(unix))] | ||
return Err(ErrorKind::InvalidArgument { | ||
message: "Unix domain sockets are not supported on this platform".to_string(), | ||
} | ||
.into()); | ||
} else { | ||
ServerAddress::parse(host) | ||
}?; | ||
host_list.push(address); | ||
} | ||
let hosts = hosts_section | ||
.split(',') | ||
.map(ServerAddress::parse) | ||
.collect::<Result<Vec<ServerAddress>>>()?; | ||
|
||
let host_info = if !srv { | ||
HostInfo::HostIdentifiers(host_list) | ||
HostInfo::HostIdentifiers(hosts) | ||
} else { | ||
match &host_list[..] { | ||
match &hosts[..] { | ||
[ServerAddress::Tcp { host, port: None }] => HostInfo::DnsRecord(host.clone()), | ||
[ServerAddress::Tcp { | ||
host: _, | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is already inside the
#[cfg(unix)]
on line 213, so I don't think this is needed.