Skip to content

Commit

Permalink
feat(types): Custom endpoint deny oss prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
tu6ge committed Mar 10, 2023
1 parent 848f149 commit 4aa7bbc
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
15 changes: 14 additions & 1 deletion src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,19 @@ impl FromStr for BucketBase {
/// ```
/// # use aliyun_oss_client::config::BucketBase;
/// # use aliyun_oss_client::types::EndPoint;
/// # use std::borrow::Cow;
/// let bucket: BucketBase = "abc.oss-cn-shanghai.aliyuncs.com".parse().unwrap();
/// assert_eq!(bucket.name(), "abc");
/// assert_eq!(bucket.endpoint(), EndPoint::CnShanghai);
///
/// let bucket: BucketBase = "abc.oss-cn-jinan.aliyuncs.com".parse().unwrap();
/// assert_eq!(bucket.name(), "abc");
/// assert_eq!(bucket.endpoint(), EndPoint::Other(Cow::Borrowed("cn-jinan")));
///
/// let bucket: BucketBase = "abc.oss-cn-jinan".parse().unwrap();
/// assert_eq!(bucket.name(), "abc");
/// assert_eq!(bucket.endpoint(), EndPoint::Other(Cow::Borrowed("cn-jinan")));
///
/// assert!("abc*#!".parse::<BucketBase>().is_err());
/// assert!("abc".parse::<BucketBase>().is_err());
/// ```
Expand All @@ -147,10 +156,14 @@ impl FromStr for BucketBase {
}

let (bucket, endpoint) = domain.split_once('.').ok_or(InvalidBucketBase::Tacitly)?;
let endpoint = match endpoint.find('.') {
Some(s) => &endpoint[0..s],
None => endpoint,
};

Ok(Self {
name: BucketName::from_static(bucket)?,
endpoint: EndPoint::new(endpoint)?,
endpoint: EndPoint::new(endpoint.trim_start_matches("oss-"))?,
})
}
}
Expand Down
12 changes: 11 additions & 1 deletion src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,8 @@ impl<'a> EndPoint {
/// assert!(EndPoint::new("-abc").is_err());
/// assert!(EndPoint::new("abc-def234ab").is_ok());
/// assert!(EndPoint::new("abc-def*#$%^ab").is_err());
/// assert!(EndPoint::new("cn-jinan").is_ok());
/// assert!(EndPoint::new("oss-cn-jinan").is_err());
/// ```
pub fn new(url: &'a str) -> Result<Self, InvalidEndPoint> {
use EndPoint::*;
Expand Down Expand Up @@ -355,6 +357,11 @@ impl<'a> EndPoint {
if url.starts_with('-') || url.ends_with('-') {
return Err(InvalidEndPoint);
}

if url.starts_with("oss") {
return Err(InvalidEndPoint);
}

fn valid_character(c: char) -> bool {
match c {
_ if c.is_ascii_lowercase() => true,
Expand Down Expand Up @@ -462,7 +469,10 @@ impl Error for InvalidEndPoint {}
#[cfg(feature = "core")]
impl fmt::Display for InvalidEndPoint {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "endpoint must like with https://xxx.aliyuncs.com")
write!(
f,
"endpoint must not with `-` prefix or `-` suffix or `oss-` prefix"
)
}
}

Expand Down

0 comments on commit 4aa7bbc

Please sign in to comment.