|
| 1 | +use super::rewriter::RequestRewriterError; |
| 2 | +use crate::types::ClusterName; |
| 3 | + |
| 4 | +// If a cluster name does not specify a port, :443 is implied. |
| 5 | +// Most browsers will not specify it, but some (e.g. the `ws` websocket client in Node.js) |
| 6 | +// will, so we strip it. |
| 7 | +const HTTPS_PORT_SUFFIX: &str = ":443"; |
| 8 | + |
| 9 | +/// Returns Ok(Some(subdomain)) if a subdomain is found. |
| 10 | +/// Returns Ok(None) if no subdomain is found, but the host header matches the cluster name. |
| 11 | +/// Returns Err(RequestRewriterError::InvalidHostHeader) if the host header does not |
| 12 | +/// match the cluster name. |
| 13 | +pub fn subdomain_from_host<'a>( |
| 14 | + host: &'a str, |
| 15 | + cluster: &ClusterName, |
| 16 | +) -> Result<Option<&'a str>, RequestRewriterError> { |
| 17 | + let host = if let Some(host) = host.strip_suffix(HTTPS_PORT_SUFFIX) { |
| 18 | + host |
| 19 | + } else { |
| 20 | + host |
| 21 | + }; |
| 22 | + |
| 23 | + if let Some(subdomain) = host.strip_suffix(cluster.as_str()) { |
| 24 | + if subdomain.is_empty() { |
| 25 | + // Subdomain exactly matches cluster name. |
| 26 | + Ok(None) |
| 27 | + } else if let Some(subdomain) = subdomain.strip_suffix('.') { |
| 28 | + Ok(Some(subdomain)) |
| 29 | + } else { |
| 30 | + Err(RequestRewriterError::InvalidHostHeader) |
| 31 | + } |
| 32 | + } else { |
| 33 | + tracing::warn!(host, "Host header does not end in cluster name."); |
| 34 | + Err(RequestRewriterError::InvalidHostHeader) |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +#[cfg(test)] |
| 39 | +mod tests { |
| 40 | + use super::*; |
| 41 | + use std::str::FromStr; |
| 42 | + |
| 43 | + #[test] |
| 44 | + fn no_subdomains() { |
| 45 | + let host = "foo.bar.baz"; |
| 46 | + let cluster = ClusterName::from_str("foo.bar.baz").unwrap(); |
| 47 | + assert_eq!(subdomain_from_host(host, &cluster), Ok(None)); |
| 48 | + } |
| 49 | + |
| 50 | + #[test] |
| 51 | + fn valid_subdomain() { |
| 52 | + let host = "foobar.example.com"; |
| 53 | + let cluster = ClusterName::from_str("example.com").unwrap(); |
| 54 | + assert_eq!(subdomain_from_host(host, &cluster), Ok(Some("foobar"))); |
| 55 | + } |
| 56 | + |
| 57 | + #[test] |
| 58 | + fn valid_suffix_no_dot() { |
| 59 | + let host = "foobarexample.com"; |
| 60 | + let cluster = ClusterName::from_str("example.com").unwrap(); |
| 61 | + assert_eq!( |
| 62 | + subdomain_from_host(host, &cluster), |
| 63 | + Err(RequestRewriterError::InvalidHostHeader) |
| 64 | + ); |
| 65 | + } |
| 66 | + |
| 67 | + #[test] |
| 68 | + fn invalid_suffix() { |
| 69 | + let host = "abc.abc.com"; |
| 70 | + let cluster = ClusterName::from_str("example.com").unwrap(); |
| 71 | + assert_eq!( |
| 72 | + subdomain_from_host(host, &cluster), |
| 73 | + Err(RequestRewriterError::InvalidHostHeader) |
| 74 | + ); |
| 75 | + } |
| 76 | + |
| 77 | + #[test] |
| 78 | + fn allowed_port() { |
| 79 | + let host = "foobar.myhost:8080"; |
| 80 | + let cluster = ClusterName::from_str("myhost:8080").unwrap(); |
| 81 | + assert_eq!(subdomain_from_host(host, &cluster), Ok(Some("foobar"))); |
| 82 | + } |
| 83 | + |
| 84 | + #[test] |
| 85 | + fn port_required() { |
| 86 | + let host = "foobar.myhost"; |
| 87 | + let cluster = ClusterName::from_str("myhost:8080").unwrap(); |
| 88 | + assert_eq!( |
| 89 | + subdomain_from_host(host, &cluster), |
| 90 | + Err(RequestRewriterError::InvalidHostHeader) |
| 91 | + ); |
| 92 | + } |
| 93 | + |
| 94 | + #[test] |
| 95 | + fn port_must_match() { |
| 96 | + let host = "foobar.myhost:8080"; |
| 97 | + let cluster = ClusterName::from_str("myhost").unwrap(); |
| 98 | + assert_eq!( |
| 99 | + subdomain_from_host(host, &cluster), |
| 100 | + Err(RequestRewriterError::InvalidHostHeader) |
| 101 | + ); |
| 102 | + } |
| 103 | + |
| 104 | + #[test] |
| 105 | + fn port_443_optional() { |
| 106 | + let host = "foobar.myhost:443"; |
| 107 | + let cluster = ClusterName::from_str("myhost").unwrap(); |
| 108 | + assert_eq!(subdomain_from_host(host, &cluster), Ok(Some("foobar"))); |
| 109 | + } |
| 110 | +} |
0 commit comments