From cadc80c24ffbcf518c55fc2d7923ef84400f1f1f Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Tue, 2 Jun 2020 13:12:54 +0200 Subject: [PATCH] Can get POST virtual directories --- .../virtual_directory_controller.rs | 3 +++ src/data/http_request_handler.rs | 12 +++++++++ src/utils/mod.rs | 3 ++- src/utils/virtual_directories_utils.rs | 27 +++++++++++++++++++ 4 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 src/utils/virtual_directories_utils.rs diff --git a/src/controllers/virtual_directory_controller.rs b/src/controllers/virtual_directory_controller.rs index 816e786..890919b 100644 --- a/src/controllers/virtual_directory_controller.rs +++ b/src/controllers/virtual_directory_controller.rs @@ -7,5 +7,8 @@ use crate::controllers::routes::RequestResult; /// Find a user by its virtual directory pub fn find_user(r: &mut HttpRequestHandler) -> RequestResult { + + let directory = r.post_virtual_directory("subfolder")?; + r.success("implement it") } \ No newline at end of file diff --git a/src/data/http_request_handler.rs b/src/data/http_request_handler.rs index 787acca..01f0262 100644 --- a/src/data/http_request_handler.rs +++ b/src/data/http_request_handler.rs @@ -13,6 +13,7 @@ use crate::data::config::conf; use crate::data::error::{ExecError, ResultBoxError}; use crate::data::user::UserID; use crate::helpers::{account_helper, api_helper, user_helper}; +use crate::utils::virtual_directories_utils::check_virtual_directory; /// Http request handler /// @@ -321,4 +322,15 @@ impl HttpRequestHandler { Ok(user_id) } + + /// Get a virtual directory included in a POST request + pub fn post_virtual_directory(&mut self, name: &str) -> ResultBoxError { + let dir = self.post_string(name)?; + + if !check_virtual_directory(&dir) { + self.bad_request(format!("Invalid virtual directory specified in '{}' !", name))?; + } + + Ok(dir) + } } \ No newline at end of file diff --git a/src/utils/mod.rs b/src/utils/mod.rs index ab27a0d..3156a82 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -3,4 +3,5 @@ //! This module contains utilities that can be used anywhere in the code pub mod crypt_utils; -pub mod user_data_utils; \ No newline at end of file +pub mod user_data_utils; +pub mod virtual_directories_utils; \ No newline at end of file diff --git a/src/utils/virtual_directories_utils.rs b/src/utils/virtual_directories_utils.rs new file mode 100644 index 0000000..6f64113 --- /dev/null +++ b/src/utils/virtual_directories_utils.rs @@ -0,0 +1,27 @@ +//! # Virtual directory utilities +//! +//! @author Pierre Hubert + +/// Check out whether a virtual directory is valid or not +/// +/// ``` +/// use comunic_server::utils::virtual_directories_utils::check_virtual_directory; +/// +/// assert_eq!(check_virtual_directory("ab"), false); +/// assert_eq!(check_virtual_directory("john"), true); +/// assert_eq!(check_virtual_directory("secret123"), true); +/// assert_eq!(check_virtual_directory("ab.php"), false); +/// ``` +pub fn check_virtual_directory(dir: &str) -> bool { + if dir.len() < 4 || dir.len() > 30 { + return false; + } + + for d in vec![".html", ".txt", ".php", "à", "â", "é", "ê", "@", "/", "\"", "'", "<", ">", "?", "&", "#"] { + if dir.contains(d) { + return false; + } + } + + true +} \ No newline at end of file