2017-04-17 07:34:28 +00:00
|
|
|
use std::fmt;
|
2018-06-04 16:06:08 +00:00
|
|
|
use std::result::Result as StdResult;
|
2017-01-14 00:45:46 +00:00
|
|
|
|
2019-06-13 01:48:02 +00:00
|
|
|
use crate::config::Value;
|
2017-01-14 00:45:46 +00:00
|
|
|
|
2018-06-04 16:06:08 +00:00
|
|
|
use pear::{Result, parser, switch};
|
2018-04-05 08:14:56 +00:00
|
|
|
use pear::parsers::*;
|
|
|
|
use pear::combinators::*;
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
pub fn is_whitespace(byte: char) -> bool {
|
|
|
|
byte == ' ' || byte == '\t'
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
2018-04-05 16:48:24 +00:00
|
|
|
fn is_not_separator(byte: char) -> bool {
|
2018-04-05 08:14:56 +00:00
|
|
|
match byte {
|
2018-04-05 16:48:24 +00:00
|
|
|
',' | '{' | '}' | '[' | ']' => false,
|
|
|
|
_ => true
|
2017-01-14 00:45:46 +00:00
|
|
|
}
|
2018-04-05 08:14:56 +00:00
|
|
|
}
|
2017-01-14 00:45:46 +00:00
|
|
|
|
2018-04-05 16:48:24 +00:00
|
|
|
// FIXME: Be more permissive here?
|
|
|
|
#[inline(always)]
|
|
|
|
fn is_ident_char(byte: char) -> bool {
|
|
|
|
match byte {
|
2019-06-13 01:48:02 +00:00
|
|
|
'0'..='9' | 'A'..='Z' | 'a'..='z' | '_' | '-' => true,
|
2018-04-05 16:48:24 +00:00
|
|
|
_ => false
|
2018-04-05 08:14:56 +00:00
|
|
|
}
|
|
|
|
}
|
2017-01-14 00:45:46 +00:00
|
|
|
|
2018-04-05 08:14:56 +00:00
|
|
|
#[parser]
|
2018-06-04 16:06:08 +00:00
|
|
|
fn array<'a>(input: &mut &'a str) -> Result<Value, &'a str> {
|
|
|
|
Value::Array(collection('[', value, ',', ']')?)
|
2018-04-05 08:14:56 +00:00
|
|
|
}
|
2017-04-13 07:18:31 +00:00
|
|
|
|
2018-04-05 08:14:56 +00:00
|
|
|
#[parser]
|
2018-06-04 16:06:08 +00:00
|
|
|
fn key<'a>(input: &mut &'a str) -> Result<String, &'a str> {
|
|
|
|
take_some_while(is_ident_char)?.to_string()
|
2018-04-05 08:14:56 +00:00
|
|
|
}
|
2017-04-13 07:18:31 +00:00
|
|
|
|
2018-04-05 08:14:56 +00:00
|
|
|
#[parser]
|
2018-06-04 16:06:08 +00:00
|
|
|
fn key_value<'a>(input: &mut &'a str) -> Result<(String, Value), &'a str> {
|
|
|
|
let key = (surrounded(key, is_whitespace)?, eat('=')?).0.to_string();
|
|
|
|
(key, surrounded(value, is_whitespace)?)
|
|
|
|
}
|
2017-04-13 07:18:31 +00:00
|
|
|
|
2018-06-04 16:06:08 +00:00
|
|
|
#[parser]
|
|
|
|
fn table<'a>(input: &mut &'a str) -> Result<Value, &'a str> {
|
|
|
|
Value::Table(collection('{', key_value, ',', '}')?)
|
2018-04-05 08:14:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[parser]
|
2018-06-04 16:06:08 +00:00
|
|
|
fn value<'a>(input: &mut &'a str) -> Result<Value, &'a str> {
|
|
|
|
skip_while(is_whitespace)?;
|
2018-04-05 08:14:56 +00:00
|
|
|
let val = switch! {
|
|
|
|
eat_slice("true") => Value::Boolean(true),
|
|
|
|
eat_slice("false") => Value::Boolean(false),
|
2018-06-04 16:06:08 +00:00
|
|
|
peek('{') => table()?,
|
|
|
|
peek('[') => array()?,
|
|
|
|
peek('"') => Value::String(delimited('"', |_| true, '"')?.to_string()),
|
2018-04-05 16:48:24 +00:00
|
|
|
_ => {
|
2018-06-04 16:06:08 +00:00
|
|
|
let value_str = take_some_while(is_not_separator)?;
|
2018-04-05 16:48:24 +00:00
|
|
|
if let Ok(int) = value_str.parse::<i64>() {
|
|
|
|
Value::Integer(int)
|
|
|
|
} else if let Ok(float) = value_str.parse::<f64>() {
|
|
|
|
Value::Float(float)
|
|
|
|
} else {
|
|
|
|
Value::String(value_str.into())
|
|
|
|
}
|
|
|
|
}
|
2017-04-13 07:18:31 +00:00
|
|
|
};
|
2017-01-14 00:45:46 +00:00
|
|
|
|
2018-06-04 16:06:08 +00:00
|
|
|
skip_while(is_whitespace)?;
|
2018-04-05 08:14:56 +00:00
|
|
|
val
|
|
|
|
}
|
|
|
|
|
2018-06-04 16:06:08 +00:00
|
|
|
pub fn parse_simple_toml_value(mut input: &str) -> StdResult<Value, String> {
|
|
|
|
parse!(value: &mut input).map_err(|e| e.to_string())
|
2017-01-14 00:45:46 +00:00
|
|
|
}
|
|
|
|
|
2017-04-17 07:34:28 +00:00
|
|
|
/// A simple wrapper over a `Value` reference with a custom implementation of
|
|
|
|
/// `Display`. This is used to log config values at initialization.
|
Overhaul URI types.
This is fairly large commit with several entangled logical changes.
The primary change in this commit is to completely overhaul how URI
handling in Rocket works. Prior to this commit, the `Uri` type acted as
an origin API. Its parser was minimal and lenient, allowing URIs that
were invalid according to RFC 7230. By contrast, the new `Uri` type
brings with it a strict RFC 7230 compliant parser. The `Uri` type now
represents any kind of valid URI, not simply `Origin` types. Three new
URI types were introduced:
* `Origin` - represents valid origin URIs
* `Absolute` - represents valid absolute URIs
* `Authority` - represents valid authority URIs
The `Origin` type replaces `Uri` in many cases:
* As fields and method inputs of `Route`
* The `&Uri` request guard is now `&Origin`
* The `uri!` macro produces an `Origin` instead of a `Uri`
The strict nature of URI parsing cascaded into the following changes:
* Several `Route` methods now `panic!` on invalid URIs
* The `Rocket::mount()` method is (correctly) stricter with URIs
* The `Redirect` constructors take a `TryInto<Uri>` type
* Dispatching of a `LocalRequest` correctly validates URIs
Overall, URIs are now properly and uniformly handled throughout Rocket's
codebase, resulting in a more reliable and correct system.
In addition to these URI changes, the following changes are also part of
this commit:
* The `LocalRequest::cloned_dispatch()` method was removed in favor of
chaining `.clone().dispatch()`.
* The entire Rocket codebase uses `crate` instead of `pub(crate)` as a
visibility modifier.
* Rocket uses the `crate_visibility_modifier` and `try_from` features.
A note on unsafety: this commit introduces many uses of `unsafe` in the
URI parser. All of these uses are a result of unsafely transforming byte
slices (`&[u8]` or similar) into strings (`&str`). The parser ensures
that these casts are safe, but of course, we must label their use
`unsafe`. The parser was written to be as generic and efficient as
possible and thus can parse directly from byte sources. Rocket, however,
does not make use of this fact and so would be able to remove all uses
of `unsafe` by parsing from an existing `&str`. This should be
considered in the future.
Fixes #443.
Resolves #263.
2018-07-29 01:26:15 +00:00
|
|
|
crate struct LoggedValue<'a>(pub &'a Value);
|
2017-04-17 07:34:28 +00:00
|
|
|
|
2019-06-13 01:48:02 +00:00
|
|
|
impl fmt::Display for LoggedValue<'_> {
|
2017-04-17 07:34:28 +00:00
|
|
|
#[inline]
|
2019-06-13 01:48:02 +00:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
use crate::config::Value::*;
|
2017-04-17 07:34:28 +00:00
|
|
|
match *self.0 {
|
|
|
|
String(_) | Integer(_) | Float(_) | Boolean(_) | Datetime(_) | Array(_) => {
|
|
|
|
self.0.fmt(f)
|
|
|
|
}
|
|
|
|
Table(ref map) => {
|
|
|
|
write!(f, "{{ ")?;
|
|
|
|
for (i, (key, val)) in map.iter().enumerate() {
|
|
|
|
write!(f, "{} = {}", key, LoggedValue(val))?;
|
|
|
|
if i != map.len() - 1 { write!(f, ", ")?; }
|
|
|
|
}
|
|
|
|
|
|
|
|
write!(f, " }}")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-13 07:18:31 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
use std::collections::BTreeMap;
|
|
|
|
use super::parse_simple_toml_value;
|
2018-04-05 08:14:56 +00:00
|
|
|
use super::Value::{self, *};
|
2017-04-13 07:18:31 +00:00
|
|
|
|
|
|
|
macro_rules! assert_parse {
|
|
|
|
($string:expr, $value:expr) => (
|
|
|
|
match parse_simple_toml_value($string) {
|
|
|
|
Ok(value) => assert_eq!(value, $value),
|
|
|
|
Err(e) => panic!("{:?} failed to parse: {:?}", $string, e)
|
|
|
|
};
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn parse_toml_values() {
|
|
|
|
assert_parse!("1", Integer(1));
|
|
|
|
assert_parse!("1.32", Float(1.32));
|
|
|
|
assert_parse!("true", Boolean(true));
|
|
|
|
assert_parse!("false", Boolean(false));
|
2018-04-05 08:14:56 +00:00
|
|
|
assert_parse!("\"hello, WORLD!\"", String("hello, WORLD!".into()));
|
2017-04-13 07:18:31 +00:00
|
|
|
assert_parse!("hi", String("hi".into()));
|
|
|
|
assert_parse!("\"hi\"", String("hi".into()));
|
|
|
|
|
|
|
|
assert_parse!("[]", Array(Vec::new()));
|
2017-06-29 21:04:54 +00:00
|
|
|
assert_parse!("[1]", vec![1].into());
|
|
|
|
assert_parse!("[1, 2, 3]", vec![1, 2, 3].into());
|
|
|
|
assert_parse!("[1.32, 2]", Array(vec![1.32.into(), 2.into()]));
|
2017-04-13 07:18:31 +00:00
|
|
|
|
|
|
|
assert_parse!("{}", Table(BTreeMap::new()));
|
2018-04-05 08:14:56 +00:00
|
|
|
|
2017-04-13 07:18:31 +00:00
|
|
|
assert_parse!("{a=b}", Table({
|
|
|
|
let mut map = BTreeMap::new();
|
2017-06-29 21:04:54 +00:00
|
|
|
map.insert("a".into(), "b".into());
|
2017-04-13 07:18:31 +00:00
|
|
|
map
|
|
|
|
}));
|
2018-04-05 08:14:56 +00:00
|
|
|
|
2017-04-13 07:18:31 +00:00
|
|
|
assert_parse!("{v=1, on=true,pi=3.14}", Table({
|
|
|
|
let mut map = BTreeMap::new();
|
2017-06-29 21:04:54 +00:00
|
|
|
map.insert("v".into(), 1.into());
|
|
|
|
map.insert("on".into(), true.into());
|
|
|
|
map.insert("pi".into(), 3.14.into());
|
2017-04-13 07:18:31 +00:00
|
|
|
map
|
|
|
|
}));
|
2018-04-05 08:14:56 +00:00
|
|
|
|
|
|
|
assert_parse!("{v=[1, 2, 3], v2=[a, \"b\"], on=true,pi=3.14}", Table({
|
|
|
|
let mut map = BTreeMap::new();
|
|
|
|
map.insert("v".into(), vec![1, 2, 3].into());
|
|
|
|
map.insert("v2".into(), vec!["a", "b"].into());
|
|
|
|
map.insert("on".into(), true.into());
|
|
|
|
map.insert("pi".into(), 3.14.into());
|
|
|
|
map
|
|
|
|
}));
|
|
|
|
|
|
|
|
assert_parse!("{v=[[1], [2, 3], [4,5]]}", Table({
|
|
|
|
let mut map = BTreeMap::new();
|
|
|
|
let first: Value = vec![1].into();
|
|
|
|
let second: Value = vec![2, 3].into();
|
|
|
|
let third: Value = vec![4, 5].into();
|
|
|
|
map.insert("v".into(), vec![first, second, third].into());
|
|
|
|
map
|
|
|
|
}));
|
2017-04-13 07:18:31 +00:00
|
|
|
}
|
|
|
|
}
|