mirror of https://github.com/rwf2/Rocket.git
Add 'Config::root_relative()'. Make TLS config paths root-relative.
Prior to this commit, relative paths to TLS PEM files were incorrectly treated as being relative to the CWD as opposed to the Rocket config file, when present. This commit resolves the issue.
This commit is contained in:
parent
237370533c
commit
2f84d4b18a
|
@ -21,6 +21,7 @@ use rocket::request::Request;
|
||||||
use rocket::fairing::{Fairing, AdHoc};
|
use rocket::fairing::{Fairing, AdHoc};
|
||||||
use rocket::response::{self, Content, Responder};
|
use rocket::response::{self, Content, Responder};
|
||||||
use rocket::http::{ContentType, Status};
|
use rocket::http::{ContentType, Status};
|
||||||
|
use rocket::config::ConfigError;
|
||||||
|
|
||||||
const DEFAULT_TEMPLATE_DIR: &'static str = "templates";
|
const DEFAULT_TEMPLATE_DIR: &'static str = "templates";
|
||||||
|
|
||||||
|
@ -158,10 +159,12 @@ impl Template {
|
||||||
/// ```
|
/// ```
|
||||||
pub fn fairing() -> impl Fairing {
|
pub fn fairing() -> impl Fairing {
|
||||||
AdHoc::on_attach(|rocket| {
|
AdHoc::on_attach(|rocket| {
|
||||||
let mut template_root = rocket.config().root().join(DEFAULT_TEMPLATE_DIR);
|
let mut template_root = rocket.config()
|
||||||
|
.root_relative(DEFAULT_TEMPLATE_DIR);
|
||||||
|
|
||||||
match rocket.config().get_str("template_dir") {
|
match rocket.config().get_str("template_dir") {
|
||||||
Ok(dir) => template_root = rocket.config().root().join(dir),
|
Ok(dir) => template_root = rocket.config().root_relative(dir),
|
||||||
Err(ref e) if !e.is_not_found() => {
|
Err(e@ConfigError::NotFound) => {
|
||||||
e.pretty_print();
|
e.pretty_print();
|
||||||
warn_!("Using default directory '{:?}'", template_root);
|
warn_!("Using default directory '{:?}'", template_root);
|
||||||
}
|
}
|
||||||
|
|
|
@ -33,7 +33,7 @@ pear_codegen = "0.0.11"
|
||||||
rustls = { version = "0.11.0", optional = true }
|
rustls = { version = "0.11.0", optional = true }
|
||||||
cookie = { version = "0.10.0", features = ["percent-encode", "secure"] }
|
cookie = { version = "0.10.0", features = ["percent-encode", "secure"] }
|
||||||
hyper = { version = "0.10.13", default-features = false }
|
hyper = { version = "0.10.13", default-features = false }
|
||||||
hyper-sync-rustls = { version = "0.2.1", features = ["server"], optional = true }
|
hyper-sync-rustls = { version = "0.3.0-rc.1", features = ["server"], optional = true }
|
||||||
ordermap = "0.2"
|
ordermap = "0.2"
|
||||||
isatty = "0.1"
|
isatty = "0.1"
|
||||||
|
|
||||||
|
|
|
@ -313,7 +313,12 @@ impl Config {
|
||||||
/// # }
|
/// # }
|
||||||
/// ```
|
/// ```
|
||||||
pub fn set_root<P: AsRef<Path>>(&mut self, path: P) {
|
pub fn set_root<P: AsRef<Path>>(&mut self, path: P) {
|
||||||
self.config_path = path.as_ref().join("Rocket.custom.toml")
|
let new_path = match self.config_path.file_name() {
|
||||||
|
Some(file) => path.as_ref().join(file),
|
||||||
|
None => path.as_ref().join("Rocket.custom.toml")
|
||||||
|
};
|
||||||
|
|
||||||
|
self.config_path = new_path
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Sets the address of `self` to `address`.
|
/// Sets the address of `self` to `address`.
|
||||||
|
@ -495,14 +500,14 @@ impl Config {
|
||||||
let pem_err = "malformed PEM file";
|
let pem_err = "malformed PEM file";
|
||||||
|
|
||||||
// Load the certificates.
|
// Load the certificates.
|
||||||
let certs = tls::load_certs(certs_path)
|
let certs = tls::load_certs(self.root_relative(certs_path))
|
||||||
.map_err(|e| match e {
|
.map_err(|e| match e {
|
||||||
Io(e) => ConfigError::Io(e, "tls.certs"),
|
Io(e) => ConfigError::Io(e, "tls.certs"),
|
||||||
_ => self.bad_type("tls", pem_err, "a valid certificates file")
|
_ => self.bad_type("tls", pem_err, "a valid certificates file")
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
// And now the private key.
|
// And now the private key.
|
||||||
let key = tls::load_private_key(key_path)
|
let key = tls::load_private_key(self.root_relative(key_path))
|
||||||
.map_err(|e| match e {
|
.map_err(|e| match e {
|
||||||
Io(e) => ConfigError::Io(e, "tls.key"),
|
Io(e) => ConfigError::Io(e, "tls.key"),
|
||||||
_ => self.bad_type("tls", pem_err, "a valid private key file")
|
_ => self.bad_type("tls", pem_err, "a valid private key file")
|
||||||
|
@ -787,6 +792,35 @@ impl Config {
|
||||||
None => panic!("root(): path {:?} has no parent", self.config_path)
|
None => panic!("root(): path {:?} has no parent", self.config_path)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// If `path` is a relative path, `path` is appended to the [`root`] at
|
||||||
|
/// which the configuration file for `self` is stored and the new path is
|
||||||
|
/// returned. If `path` is absolute, `path` is returned unaltered.
|
||||||
|
///
|
||||||
|
/// [`root`]: /rocket/struct.Config.html#method.root
|
||||||
|
///
|
||||||
|
/// # Example
|
||||||
|
///
|
||||||
|
/// ```rust
|
||||||
|
/// use std::env::current_dir;
|
||||||
|
/// use std::path::Path;
|
||||||
|
/// use rocket::config::{Config, Environment};
|
||||||
|
///
|
||||||
|
/// let config = Config::new(Environment::Staging)
|
||||||
|
/// .expect("can retrieve current directory");
|
||||||
|
///
|
||||||
|
/// assert_eq!(config.root(), current_dir().unwrap());
|
||||||
|
/// assert_eq!(config.root_relative("abc"), config.root().join("abc"));
|
||||||
|
/// assert_eq!(config.root_relative("/abc"), Path::new("/abc"));
|
||||||
|
/// ```
|
||||||
|
pub fn root_relative<P: AsRef<Path>>(&self, path: P) -> PathBuf {
|
||||||
|
let path = path.as_ref();
|
||||||
|
if path.is_absolute() {
|
||||||
|
path.into()
|
||||||
|
} else {
|
||||||
|
self.root().join(path)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl fmt::Debug for Config {
|
impl fmt::Debug for Config {
|
||||||
|
|
Loading…
Reference in New Issue