From c2bac9401a064229c27000b80df7748b8646528e Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Thu, 25 Jun 2020 18:19:09 +0200 Subject: [PATCH] Can check the validity of an URL --- src/utils/string_utils.rs | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/utils/string_utils.rs b/src/utils/string_utils.rs index 10d3e08..b500f80 100644 --- a/src/utils/string_utils.rs +++ b/src/utils/string_utils.rs @@ -2,6 +2,12 @@ //! //! This module contains utilities that can be used accross all the application +use std::convert::TryFrom; +use std::str::FromStr; + +use actix_web::dev::Url; +use actix_web::http::Uri; + /// Escape an HTML string /// /// Removes the HTML code included inside a string @@ -16,4 +22,19 @@ pub fn remove_html_nodes(input: &str) -> String { input.replace("<", "<") .replace(">", ">") +} + +/// Check out whether a URL is valid or not +/// +/// ``` +/// use comunic_server::utils::string_utils::check_url; +/// +/// let url1 = "http://communniquons.org/?url=some&arg2=myname#content3"; +/// assert_eq!(check_url(url1), true); +/// +/// let url2 = "h@ttp://communniquons.org/?url=some&arg2=myname#content3"; +/// assert_eq!(check_url(url2), false); +/// ``` +pub fn check_url(url: &str) -> bool { + Uri::from_str(url).is_ok() } \ No newline at end of file