Fix IPv6 address parsing and validation.

This commit is contained in:
Sergio Benitez 2017-02-14 23:10:36 -08:00
parent a496d1dbc4
commit 0c963da1fd
3 changed files with 17 additions and 9 deletions

View File

@ -1,5 +1,5 @@
use std::collections::HashMap;
use std::net::ToSocketAddrs;
use std::net::{IpAddr, lookup_host};
use std::path::{Path, PathBuf};
use std::sync::RwLock;
use std::convert::AsRef;
@ -268,19 +268,18 @@ impl Config {
/// # fn config_test() -> Result<(), ConfigError> {
/// let mut config = Config::new(Environment::Staging)?;
/// assert!(config.set_address("localhost").is_ok());
/// assert!(config.set_address("::").is_ok());
/// assert!(config.set_address("?").is_err());
/// # Ok(())
/// # }
/// ```
pub fn set_address<A: Into<String>>(&mut self, address: A) -> config::Result<()> {
let address = address.into();
if address.contains(':') {
return Err(self.bad_type("address", "string", "a hostname or IP with no port"));
} else if format!("{}:{}", address, 80).to_socket_addrs().is_err() {
if address.parse::<IpAddr>().is_err() && lookup_host(&address).is_err() {
return Err(self.bad_type("address", "string", "a valid hostname or IP"));
}
self.address = address.into();
self.address = address;
Ok(())
}

View File

@ -638,11 +638,19 @@ mod test {
default_config(Development).address("localhost")
});
check_config!(RocketConfig::parse(r#"
[development]
address = "::"
"#.to_string(), TEST_CONFIG_FILENAME), {
default_config(Development).address("::")
});
check_config!(RocketConfig::parse(r#"
[dev]
address = "127.0.0.1"
address = "2001:db8::370:7334"
"#.to_string(), TEST_CONFIG_FILENAME), {
default_config(Development).address("127.0.0.1")
default_config(Development).address("2001:db8::370:7334")
});
check_config!(RocketConfig::parse(r#"
@ -930,9 +938,9 @@ mod test {
check_config!(RocketConfig::parse(format!(r#"
[{}]
address = "7.6.5.4"
address = "::1"
"#, GLOBAL_ENV_NAME), TEST_CONFIG_FILENAME), {
default_config(*env).address("7.6.5.4")
default_config(*env).address("::1")
});
check_config!(RocketConfig::parse(format!(r#"

View File

@ -5,6 +5,7 @@
#![feature(const_fn)]
#![feature(type_ascription)]
#![feature(pub_restricted)]
#![feature(lookup_host)]
//! # Rocket - Core API Documentation
//!