spacegate_kernel/utils/
parse_host.rs

1#[derive(Debug)]
2pub struct HostAndPort<'a> {
3    pub host: &'a [u8],
4    pub port: Option<&'a [u8]>,
5}
6
7impl<'a> HostAndPort<'a> {
8    #[inline]
9    pub fn host_end_with(&self, suffix: &[u8]) -> bool {
10        self.host.ends_with(suffix)
11    }
12    #[allow(clippy::indexing_slicing)]
13    pub fn from_header(host: &'a hyper::http::HeaderValue) -> Self {
14        Self::from_bytes(host.as_bytes())
15    }
16    #[allow(clippy::indexing_slicing)]
17    pub fn from_bytes(host: &'a [u8]) -> Self {
18        let bytes = host;
19        let mut comma_token_pos = None;
20
21        for (idx, byte) in bytes.iter().enumerate().rev() {
22            if *byte == b':' {
23                comma_token_pos = Some(idx);
24                break;
25            } else if !byte.is_ascii_digit() {
26                break;
27            }
28        }
29        if let Some(comma_token_pos) = comma_token_pos {
30            let host = &bytes[..comma_token_pos];
31            let port = if comma_token_pos == bytes.len() - 1 {
32                None
33            } else {
34                Some(&bytes[comma_token_pos + 1..])
35            };
36            HostAndPort { host, port }
37        } else {
38            HostAndPort { host: bytes, port: None }
39        }
40    }
41}