Skip to content

Commit

Permalink
fixed parse failure for subdomains on parse_host function (#369)
Browse files Browse the repository at this point in the history
  • Loading branch information
elertan authored Dec 28, 2023
1 parent 558c868 commit fed18ac
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion rpc/wrpc/client/src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ pub fn parse_host(input: &str) -> Result<ParseHostOutput, ParseHostError> {
let does_not_end_with_hyphen = !host.ends_with('-');
let has_at_least_one_hyphen = host.contains('-');
let hyphens_are_separated_by_valid_chars =
has_at_least_one_hyphen.then(|| host.split('-').all(|part| part.chars().all(|c| c.is_ascii_alphanumeric())));
has_at_least_one_hyphen.then(|| host.split('-').all(|part| part.chars().all(|c| c == '.' || c.is_ascii_alphanumeric())));
let tld = host.split('.').last();
// Prevents e.g. numbers being used as TLDs (which in turn prevents e.g. mistakes in IPv4 addresses as being detected as a domain).
let tld_exists_and_is_not_number = tld.map(|tld| tld.parse::<i32>().is_err()).unwrap_or(false);
Expand Down Expand Up @@ -348,4 +348,13 @@ mod tests {
assert_eq!(output.host, Host::Domain("123.com"));
assert_eq!(output.port, None);
}

#[test]
fn wrpc_parse_mixed_subdomains() {
let input = "alpha-123.beta.gamma.com";
let output = parse_host(input).unwrap();
assert_eq!(output.scheme, None);
assert_eq!(output.host, Host::Domain("alpha-123.beta.gamma.com"));
assert_eq!(output.port, None);
}
}

0 comments on commit fed18ac

Please sign in to comment.