mirror of https://github.com/rwf2/Rocket.git
Panic on illegal, dynamic mount points.
This commit is contained in:
parent
1304857aee
commit
dd7e95b3c5
|
@ -16,5 +16,5 @@ fn hi<'r>(name: &'r str) -> &'r str {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
rocket::ignite().mount("/", routes![hello, hi]).launch();
|
rocket::ignite().mount("/<test>", routes![hello, hi]).launch();
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::str::from_utf8_unchecked;
|
use std::str::from_utf8_unchecked;
|
||||||
use std::cmp::min;
|
use std::cmp::min;
|
||||||
use std::process;
|
|
||||||
use std::io::{self, Write};
|
use std::io::{self, Write};
|
||||||
|
|
||||||
use term_painter::Color::*;
|
use term_painter::Color::*;
|
||||||
|
@ -330,6 +329,11 @@ impl Rocket {
|
||||||
/// generation facilities. Requests to the `/hello/world` URI will be
|
/// generation facilities. Requests to the `/hello/world` URI will be
|
||||||
/// dispatched to the `hi` route.
|
/// 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: `<param>`.
|
||||||
|
///
|
||||||
/// ```rust
|
/// ```rust
|
||||||
/// # #![feature(plugin)]
|
/// # #![feature(plugin)]
|
||||||
/// # #![plugin(rocket_codegen)]
|
/// # #![plugin(rocket_codegen)]
|
||||||
|
@ -368,6 +372,13 @@ impl Rocket {
|
||||||
/// ```
|
/// ```
|
||||||
pub fn mount(mut self, base: &str, routes: Vec<Route>) -> Self {
|
pub fn mount(mut self, base: &str, routes: Vec<Route>) -> Self {
|
||||||
info!("🛰 {} '{}':", Magenta.paint("Mounting"), base);
|
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 {
|
for mut route in routes {
|
||||||
let path = format!("{}/{}", base, route.path.as_uri());
|
let path = format!("{}/{}", base, route.path.as_uri());
|
||||||
route.set_path(path);
|
route.set_path(path);
|
||||||
|
@ -448,9 +459,8 @@ impl Rocket {
|
||||||
let server = match hyper::Server::http(full_addr.as_str()) {
|
let server = match hyper::Server::http(full_addr.as_str()) {
|
||||||
Ok(hyper_server) => hyper_server,
|
Ok(hyper_server) => hyper_server,
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
error!("failed to start server.");
|
error!("Failed to start server.");
|
||||||
error_!("{}", e);
|
panic!("{}", e);
|
||||||
process::exit(1);
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,12 @@
|
||||||
|
extern crate rocket;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
#[should_panic]
|
||||||
|
fn bad_dynamic_mount() {
|
||||||
|
rocket::ignite().mount("<name>", vec![]);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn good_static_mount() {
|
||||||
|
rocket::ignite().mount("abcdefghijkl_mno", vec![]);
|
||||||
|
}
|
Loading…
Reference in New Issue