aws_sdk_s3/endpoint_lib/
s3.rs
1use crate::endpoint_lib::diagnostic::DiagnosticCollector;
8use crate::endpoint_lib::host::is_valid_host_label;
9use once_cell::sync::Lazy;
10use regex_lite::Regex;
11
12static VIRTUAL_HOSTABLE_SEGMENT: Lazy<Regex> = Lazy::new(|| Regex::new("^[a-z\\d][a-z\\d\\-.]{1,61}[a-z\\d]$").unwrap());
13
14static IPV4: Lazy<Regex> = Lazy::new(|| Regex::new("^(\\d+\\.){3}\\d+$").unwrap());
15
16static DOTS_AND_DASHES: Lazy<Regex> = Lazy::new(|| Regex::new(r"^.*((\.-)|(-\.)).*$").unwrap());
17
18pub(crate) fn is_virtual_hostable_s3_bucket(host_label: &str, allow_subdomains: bool, e: &mut DiagnosticCollector) -> bool {
20 if !is_valid_host_label(host_label, allow_subdomains, e) {
21 false
22 } else if !allow_subdomains {
23 is_virtual_hostable_segment(host_label)
24 } else {
25 host_label.split('.').all(is_virtual_hostable_segment)
26 }
27}
28
29fn is_virtual_hostable_segment(host_label: &str) -> bool {
30 VIRTUAL_HOSTABLE_SEGMENT.is_match(host_label)
31 && !IPV4.is_match(host_label) && !DOTS_AND_DASHES.is_match(host_label) }
34
35#[test]
36fn check_s3_bucket() {
37 let bucket = "a--b--x-s3";
39 assert!(is_virtual_hostable_s3_bucket(bucket, false, &mut DiagnosticCollector::new()));
40
41 assert!(!is_virtual_hostable_s3_bucket("a-.b-.c", true, &mut DiagnosticCollector::new()))
42}