From dd7e95b3c580c39fb712c3b3d83f48066736600b Mon Sep 17 00:00:00 2001 From: Sergio Benitez Date: Sat, 17 Dec 2016 10:51:44 -0800 Subject: [PATCH] Panic on illegal, dynamic mount points. --- examples/hello_person/src/main.rs | 2 +- lib/src/rocket.rs | 18 ++++++++++++++---- lib/tests/mount_point.rs | 12 ++++++++++++ 3 files changed, 27 insertions(+), 5 deletions(-) create mode 100644 lib/tests/mount_point.rs diff --git a/examples/hello_person/src/main.rs b/examples/hello_person/src/main.rs index 43f08fc7..0526724a 100644 --- a/examples/hello_person/src/main.rs +++ b/examples/hello_person/src/main.rs @@ -16,5 +16,5 @@ fn hi<'r>(name: &'r str) -> &'r str { } fn main() { - rocket::ignite().mount("/", routes![hello, hi]).launch(); + rocket::ignite().mount("/", routes![hello, hi]).launch(); } diff --git a/lib/src/rocket.rs b/lib/src/rocket.rs index 098baef1..48c6c42c 100644 --- a/lib/src/rocket.rs +++ b/lib/src/rocket.rs @@ -1,7 +1,6 @@ use std::collections::HashMap; use std::str::from_utf8_unchecked; use std::cmp::min; -use std::process; use std::io::{self, Write}; use term_painter::Color::*; @@ -330,6 +329,11 @@ impl Rocket { /// generation facilities. Requests to the `/hello/world` URI will be /// dispatched to the `hi` route. /// + /// # Panics + /// + /// The `base` mount point must be a static path. That is, the mount point + /// must _not_ contain dynamic path parameters: ``. + /// /// ```rust /// # #![feature(plugin)] /// # #![plugin(rocket_codegen)] @@ -368,6 +372,13 @@ impl Rocket { /// ``` pub fn mount(mut self, base: &str, routes: Vec) -> Self { info!("🛰 {} '{}':", Magenta.paint("Mounting"), base); + + if base.contains('<') { + error_!("Bad mount point: '{}'.", base); + error_!("Mount points must be static paths!"); + panic!("Bad mount point.") + } + for mut route in routes { let path = format!("{}/{}", base, route.path.as_uri()); route.set_path(path); @@ -448,9 +459,8 @@ impl Rocket { let server = match hyper::Server::http(full_addr.as_str()) { Ok(hyper_server) => hyper_server, Err(e) => { - error!("failed to start server."); - error_!("{}", e); - process::exit(1); + error!("Failed to start server."); + panic!("{}", e); } }; diff --git a/lib/tests/mount_point.rs b/lib/tests/mount_point.rs new file mode 100644 index 00000000..9973adf6 --- /dev/null +++ b/lib/tests/mount_point.rs @@ -0,0 +1,12 @@ +extern crate rocket; + +#[test] +#[should_panic] +fn bad_dynamic_mount() { + rocket::ignite().mount("", vec![]); +} + +#[test] +fn good_static_mount() { + rocket::ignite().mount("abcdefghijkl_mno", vec![]); +}