|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | +// Copyright 2024 Keylime Authors |
| 3 | + |
| 4 | +use pest::Parser; |
| 5 | +use pest_derive::Parser; |
| 6 | +use thiserror::Error; |
| 7 | + |
| 8 | +#[derive(Parser)] |
| 9 | +#[grammar = "hostname.pest"] |
| 10 | +pub struct HostnameParser; |
| 11 | + |
| 12 | +#[derive(Error, Debug)] |
| 13 | +pub enum HostnameParsingError { |
| 14 | + #[error("Invalid input {0}")] |
| 15 | + InvalidInput(String), |
| 16 | + |
| 17 | + #[error("failed to parse the input {input}")] |
| 18 | + ParseError { |
| 19 | + input: String, |
| 20 | + source: Box<pest::error::Error<Rule>>, |
| 21 | + }, |
| 22 | +} |
| 23 | + |
| 24 | +/// Parses a hostname from a string slice following RFC-1123 |
| 25 | +/// |
| 26 | +/// Valid hostnames are formed by labels separated by dots ('.'). |
| 27 | +/// |
| 28 | +/// The labels can only contain alphanumeric characters ('a'..'z' | 'A'..'Z' | '0'..'9') and the |
| 29 | +/// hyphen ('-'). The labels cannot begin or end with an hyphen. |
| 30 | +/// |
| 31 | +/// # Arguments |
| 32 | +/// |
| 33 | +/// * `hostname` the string to be parsed |
| 34 | +/// |
| 35 | +/// # Returns |
| 36 | +/// |
| 37 | +/// The obtained hostname as a &str if it is a valid hostname |
| 38 | +/// |
| 39 | +/// # Examples |
| 40 | +/// |
| 41 | +/// Valid hostnames: |
| 42 | +/// |
| 43 | +/// * `hostname` |
| 44 | +/// * `host-name` |
| 45 | +/// * `a.b.c` |
| 46 | +/// * `a-b.c-d.e-f` |
| 47 | +/// |
| 48 | +/// Invalid hostnames: |
| 49 | +/// |
| 50 | +/// * `a_b.c` |
| 51 | +/// * `a.b-.c` |
| 52 | +/// * `a.-b.c` |
| 53 | +pub fn parse_hostname(hostname: &str) -> Result<&str, HostnameParsingError> { |
| 54 | + let Some(pair) = HostnameParser::parse(Rule::hostname, hostname) |
| 55 | + .map_err(|e| HostnameParsingError::ParseError { |
| 56 | + input: hostname.to_string(), |
| 57 | + source: Box::new(e), |
| 58 | + })? |
| 59 | + .next() |
| 60 | + else { |
| 61 | + return Err(HostnameParsingError::InvalidInput(hostname.to_string())); |
| 62 | + }; |
| 63 | + return Ok(pair.as_str()); |
| 64 | +} |
| 65 | + |
| 66 | +// Unit Testing |
| 67 | +#[cfg(test)] |
| 68 | +mod tests { |
| 69 | + use super::*; |
| 70 | + |
| 71 | + #[test] |
| 72 | + fn test_parse_hostname() { |
| 73 | + // Sanity: most common case |
| 74 | + assert_eq!(parse_hostname("hostname").unwrap(), "hostname"); //#[allow_ci] |
| 75 | + assert_eq!(parse_hostname("ab.cd.ef").unwrap(), "ab.cd.ef"); //#[allow_ci] |
| 76 | + assert_eq!(parse_hostname("ab-cd-ef").unwrap(), "ab-cd-ef"); //#[allow_ci] |
| 77 | + |
| 78 | + // More advanced cases |
| 79 | + assert_eq!( |
| 80 | + parse_hostname("hostname-123.test").unwrap(), //#[allow_ci] |
| 81 | + "hostname-123.test" |
| 82 | + ); |
| 83 | + assert_eq!(parse_hostname("123-456.789").unwrap(), "123-456.789"); //#[allow_ci] |
| 84 | + assert_eq!(parse_hostname("1----9").unwrap(), "1----9"); //#[allow_ci] |
| 85 | + |
| 86 | + // Invalid input |
| 87 | + assert!(parse_hostname("-host-na.me").is_err()); |
| 88 | + assert!(parse_hostname("host-na.me-").is_err()); |
| 89 | + assert!(parse_hostname(".host-na.me").is_err()); |
| 90 | + assert!(parse_hostname("host-na.me.").is_err()); |
| 91 | + assert!(parse_hostname("host_name").is_err()); |
| 92 | + assert!(parse_hostname("host..name").is_err()); |
| 93 | + } |
| 94 | +} |
0 commit comments