aws_sdk_s3/endpoint_lib/
s3.rs

1// Code generated by software.amazon.smithy.rust.codegen.smithy-rs. DO NOT EDIT.
2/*
3 *  Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
4 *  SPDX-License-Identifier: Apache-2.0
5 */
6
7use 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
18/// Evaluates whether a string is a DNS-compatible bucket name that can be used with virtual hosted-style addressing.
19pub(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) // don't allow ip address
32        && !DOTS_AND_DASHES.is_match(host_label) // don't allow names like bucket-.name or bucket.-name
33}
34
35#[test]
36fn check_s3_bucket() {
37    // check that double dashses are valid
38    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}