Skip to content

Commit

Permalink
domain_fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
dzlk17 committed Sep 25, 2024
1 parent e3c0e5f commit 46e28e7
Showing 1 changed file with 48 additions and 4 deletions.
52 changes: 48 additions & 4 deletions server/src/http/cloud/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -301,15 +301,39 @@ pub fn refresh_auth_token(
}

pub fn custom_validate_domain_name(domain_name: &String) -> anyhow::Result<String> {
let mut domain = domain_name.trim();

// Remove 'https://', 'http://', 'www.' prefix or path if present
if let Some(stripped) = domain.strip_prefix("https://") {
domain = stripped;
} else if let Some(stripped) = domain.strip_prefix("http://") {
domain = stripped;
}

if let Some(stripped) = domain.strip_prefix("www.") {
domain = stripped;
}

if let Some((base_domain, _)) = domain.split_once('/') {
domain = base_domain;
}

// Check if the domain name is empty
if domain_name.trim().is_empty() {
if domain.is_empty() {
warn!("Domain name is empty: {:?}", domain_name);
bail!(CloudApiErrors::InvalidDomainName);
}

match parse_domain_name(domain_name) {
Ok(name) => Ok(name.to_string()),
println!("Root: {:?}", domain);
match parse_domain_name(domain) {
Ok(name) => {
println!("Domain name: {:?}", name);
println!("Suffix: {:?}", name.suffix());
println!("name{:?}", name.root());
return Ok(name.to_string());
}
Err(err) => {
println!("Error: {:?}", err);
warn!("Failed to convert domain name to ascii: {:?}", err);
bail!(CloudApiErrors::InvalidDomainName);
}
Expand All @@ -333,7 +357,7 @@ pub fn extract_domain_name(origin: &String) -> Result<String, String> {
Ok(domain_name.to_string())
}

#[cfg(feature = "cloud_integration_tests")]
// #[cfg(feature = "cloud_integration_tests")]
#[cfg(test)]
mod tests {
use super::*;
Expand All @@ -354,6 +378,26 @@ mod tests {
fn test_invalid_domain() {
let domain_name = String::from("this is not a domain");
assert!(custom_validate_domain_name(&domain_name).is_err());
let domain_name = String::from("https://www.figma.com/");
assert_eq!(
custom_validate_domain_name(&domain_name).unwrap(),
"figma.com".to_string()
);
let domain_name = String::from("http://www.figma.com");
assert_eq!(
custom_validate_domain_name(&domain_name).unwrap(),
"figma.com".to_string()
);
let domain_name = String::from("www.figma.com");
assert_eq!(
custom_validate_domain_name(&domain_name).unwrap(),
"figma.com".to_string()
);
let domain_name = String::from("https://www.figma.com/dfgsg");
assert_eq!(
custom_validate_domain_name(&domain_name).unwrap(),
"figma.com".to_string()
);
}

#[test]
Expand Down

0 comments on commit 46e28e7

Please sign in to comment.