aws_sdk_s3/endpoint_lib/
host.rs
1use crate::endpoint_lib::diagnostic::DiagnosticCollector;
8
9pub(crate) fn is_valid_host_label(label: &str, allow_dots: bool, e: &mut DiagnosticCollector) -> bool {
10 if allow_dots {
11 for part in label.split('.') {
12 if !is_valid_host_label(part, false, e) {
13 return false;
14 }
15 }
16 true
17 } else {
18 if label.is_empty() || label.len() > 63 {
19 e.report_error("host was too short or too long");
20 return false;
21 }
22 label.chars().enumerate().all(|(idx, ch)| match (ch, idx) {
23 ('-', 0) => {
24 e.report_error("cannot start with `-`");
25 false
26 }
27 _ => ch.is_alphanumeric() || ch == '-',
28 })
29 }
30}
31
32#[cfg(all(test, feature = "gated-tests"))]
33mod test {
34 use proptest::proptest;
35
36 fn is_valid_host_label(label: &str, allow_dots: bool) -> bool {
37 super::is_valid_host_label(label, allow_dots, &mut DiagnosticCollector::new())
38 }
39
40 #[allow(clippy::bool_assert_comparison)]
41 #[test]
42 fn basic_cases() {
43 assert_eq!(is_valid_host_label("", false), false);
44 assert_eq!(is_valid_host_label("", true), false);
45 assert_eq!(is_valid_host_label(".", true), false);
46 assert_eq!(is_valid_host_label("a.b", true), true);
47 assert_eq!(is_valid_host_label("a.b", false), false);
48 assert_eq!(is_valid_host_label("a.b.", true), false);
49 assert_eq!(is_valid_host_label("a.b.c", true), true);
50 assert_eq!(is_valid_host_label("a_b", true), false);
51 assert_eq!(is_valid_host_label(&"a".repeat(64), false), false);
52 assert_eq!(is_valid_host_label(&format!("{}.{}", "a".repeat(63), "a".repeat(63)), true), true);
53 }
54
55 #[allow(clippy::bool_assert_comparison)]
56 #[test]
57 fn start_bounds() {
58 assert_eq!(is_valid_host_label("-foo", false), false);
59 assert_eq!(is_valid_host_label("-foo", true), false);
60 assert_eq!(is_valid_host_label(".foo", true), false);
61 assert_eq!(is_valid_host_label("a-b.foo", true), true);
62 }
63
64 use crate::endpoint_lib::diagnostic::DiagnosticCollector;
65 use proptest::prelude::*;
66 proptest! {
67 #[test]
68 fn no_panics(s in any::<String>(), dots in any::<bool>()) {
69 is_valid_host_label(&s, dots);
70 }
71 }
72}