From a6084ab3e2ef9b5d26188ee01816316e0805c938 Mon Sep 17 00:00:00 2001 From: Dru Sellers Date: Mon, 9 Jan 2017 12:15:33 -0600 Subject: [PATCH] Change the type for port to u16 in config. --- lib/src/config/config.rs | 13 ++++++++----- lib/src/config/mod.rs | 17 +++++++++++++++++ lib/src/rocket.rs | 2 +- 3 files changed, 26 insertions(+), 6 deletions(-) diff --git a/lib/src/config/config.rs b/lib/src/config/config.rs index 5aea1b5e..2c120432 100644 --- a/lib/src/config/config.rs +++ b/lib/src/config/config.rs @@ -15,7 +15,7 @@ pub struct Config { /// The address to serve on. pub address: String, /// The port to serve on. - pub port: usize, + pub port: u16, /// How much information to log. pub log_level: LoggingLevel, /// The environment that this configuration corresponds to. @@ -99,7 +99,7 @@ impl Config { /// returned: /// /// * **address**: String - /// * **port**: Integer + /// * **port**: Integer (16-bit unsigned) /// * **session_key**: String (192-bit base64) /// * **log**: String /// @@ -119,7 +119,11 @@ impl Config { return Err(self.bad_type(name, val, "an unsigned integer")); } - self.port = port as usize; + if port > (u16::max_value() as i64) { + return Err(self.bad_type(name, val, "a 16-bit unsigned integer")) + } + + self.port = port as u16; } else if name == "session_key" { let key = parse!(self, name, val, as_str, "a string")?; if key.len() != 32 { @@ -231,7 +235,7 @@ impl Config { /// Sets the `port` in `self` to `var` and returns the structure. #[inline(always)] - pub fn port(mut self, var: usize) -> Self { + pub fn port(mut self, var: u16) -> Self { self.port = var; self } @@ -284,4 +288,3 @@ impl PartialEq for Config { && self.filepath == other.filepath } } - diff --git a/lib/src/config/mod.rs b/lib/src/config/mod.rs index 365c6fd5..0acf280f 100644 --- a/lib/src/config/mod.rs +++ b/lib/src/config/mod.rs @@ -567,6 +567,13 @@ mod test { "#.to_string(), TEST_CONFIG_FILENAME), { default_config(Staging).port(6000) }); + + check_config!(RocketConfig::parse(r#" + [stage] + port = 65535 + "#.to_string(), TEST_CONFIG_FILENAME), { + default_config(Staging).port(65535) + }); } #[test] @@ -589,6 +596,16 @@ mod test { [staging] port = -1 "#.to_string(), TEST_CONFIG_FILENAME).is_err()); + + assert!(RocketConfig::parse(r#" + [staging] + port = 65536 + "#.to_string(), TEST_CONFIG_FILENAME).is_err()); + + assert!(RocketConfig::parse(r#" + [staging] + port = 105836 + "#.to_string(), TEST_CONFIG_FILENAME).is_err()); } #[test] diff --git a/lib/src/rocket.rs b/lib/src/rocket.rs index 082f9c58..3f210bcd 100644 --- a/lib/src/rocket.rs +++ b/lib/src/rocket.rs @@ -25,7 +25,7 @@ use http::uri::URI; /// application. pub struct Rocket { address: String, - port: usize, + port: u16, router: Router, default_catchers: HashMap, catchers: HashMap,