move normalize_uri_path and extract_cookie to proxmox-rest-server crate
Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
This commit is contained in:
committed by
Thomas Lamprecht
parent
605fe2e7e7
commit
778c7d954b
@ -49,27 +49,6 @@ pub fn assert_if_modified(digest1: &str, digest2: &str) -> Result<(), Error> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Extract a specific cookie from cookie header.
|
||||
/// We assume cookie_name is already url encoded.
|
||||
pub fn extract_cookie(cookie: &str, cookie_name: &str) -> Option<String> {
|
||||
for pair in cookie.split(';') {
|
||||
let (name, value) = match pair.find('=') {
|
||||
Some(i) => (pair[..i].trim(), pair[(i + 1)..].trim()),
|
||||
None => return None, // Cookie format error
|
||||
};
|
||||
|
||||
if name == cookie_name {
|
||||
use percent_encoding::percent_decode;
|
||||
if let Ok(value) = percent_decode(value.as_bytes()).decode_utf8() {
|
||||
return Some(value.into());
|
||||
} else {
|
||||
return None; // Cookie format error
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
None
|
||||
}
|
||||
|
||||
/// Detect modified configuration files
|
||||
///
|
||||
@ -81,31 +60,6 @@ pub fn detect_modified_configuration_file(digest1: &[u8;32], digest2: &[u8;32])
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// normalize uri path
|
||||
///
|
||||
/// Do not allow ".", "..", or hidden files ".XXXX"
|
||||
/// Also remove empty path components
|
||||
pub fn normalize_uri_path(path: &str) -> Result<(String, Vec<&str>), Error> {
|
||||
let items = path.split('/');
|
||||
|
||||
let mut path = String::new();
|
||||
let mut components = vec![];
|
||||
|
||||
for name in items {
|
||||
if name.is_empty() {
|
||||
continue;
|
||||
}
|
||||
if name.starts_with('.') {
|
||||
bail!("Path contains illegal components.");
|
||||
}
|
||||
path.push('/');
|
||||
path.push_str(name);
|
||||
components.push(name);
|
||||
}
|
||||
|
||||
Ok((path, components))
|
||||
}
|
||||
|
||||
/// An easy way to convert types to Any
|
||||
///
|
||||
/// Mostly useful to downcast trait objects (see RpcEnvironment).
|
||||
|
Reference in New Issue
Block a user