Skip to content

Commit 5c0e71f

Browse files
author
Andrew Witten
authored
RUST-1021 use ServerAddress::parse for URI parsing (#471)
RUST-1021 use ServerAddress::parse for URI parsing
1 parent 8d65a70 commit 5c0e71f

File tree

1 file changed

+24
-50
lines changed

1 file changed

+24
-50
lines changed

src/client/options/mod.rs

Lines changed: 24 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -176,13 +176,23 @@ impl FromStr for ServerAddress {
176176
}
177177

178178
impl ServerAddress {
179-
/// Parses an address string into a `StreamAddress`.
179+
/// Parses an address string into a `ServerAddress`.
180180
pub fn parse(address: impl AsRef<str>) -> Result<Self> {
181181
let address = address.as_ref();
182182
let mut parts = address.split(':');
183-
184183
let hostname = match parts.next() {
185-
Some(part) => part,
184+
Some(part) => {
185+
if part.is_empty() {
186+
return Err(ErrorKind::InvalidArgument {
187+
message: format!(
188+
"invalid server address: \"{}\"; hostname cannot be empty",
189+
address
190+
),
191+
}
192+
.into());
193+
}
194+
part
195+
}
186196
None => {
187197
return Err(ErrorKind::InvalidArgument {
188198
message: format!("invalid server address: \"{}\"", address),
@@ -200,6 +210,15 @@ impl ServerAddress {
200210
),
201211
})?;
202212

213+
if port == 0 {
214+
return Err(ErrorKind::InvalidArgument {
215+
message: format!(
216+
"invalid server address: \"{}\"; port must be non-zero",
217+
address
218+
),
219+
}
220+
.into());
221+
}
203222
if parts.next().is_some() {
204223
return Err(ErrorKind::InvalidArgument {
205224
message: format!(
@@ -216,7 +235,7 @@ impl ServerAddress {
216235
};
217236

218237
Ok(ServerAddress::Tcp {
219-
host: hostname.to_string(),
238+
host: hostname.to_lowercase(),
220239
port,
221240
})
222241
}
@@ -1327,52 +1346,7 @@ impl ClientOptionsParser {
13271346
None => (None, None),
13281347
};
13291348

1330-
let hosts: Result<Vec<_>> = hosts_section
1331-
.split(',')
1332-
.map(|host| {
1333-
let (hostname, port) = match host.find(':') {
1334-
Some(index) => host.split_at(index),
1335-
None => (host, ""),
1336-
};
1337-
1338-
if hostname.is_empty() {
1339-
return Err(ErrorKind::InvalidArgument {
1340-
message: "connection string contains no host".to_string(),
1341-
}
1342-
.into());
1343-
}
1344-
let port = if port.is_empty() {
1345-
None
1346-
} else {
1347-
let port_string_without_colon = &port[1..];
1348-
let p: u16 = port_string_without_colon.parse().map_err(|_| {
1349-
ErrorKind::InvalidArgument {
1350-
message: format!(
1351-
"invalid port specified in connection string: {}",
1352-
port
1353-
),
1354-
}
1355-
})?;
1356-
1357-
if p == 0 {
1358-
return Err(ErrorKind::InvalidArgument {
1359-
message: format!(
1360-
"invalid port specified in connection string: {}",
1361-
port
1362-
),
1363-
}
1364-
.into());
1365-
}
1366-
1367-
Some(p)
1368-
};
1369-
1370-
Ok(ServerAddress::Tcp {
1371-
host: hostname.to_lowercase(),
1372-
port,
1373-
})
1374-
})
1375-
.collect();
1349+
let hosts: Result<Vec<_>> = hosts_section.split(',').map(ServerAddress::parse).collect();
13761350

13771351
let hosts = hosts?;
13781352

0 commit comments

Comments
 (0)