Skip to content

Commit

Permalink
Merge pull request #289 from tencentyun/feature_jojoliang_1402cf3a
Browse files Browse the repository at this point in the history
兼容URL
  • Loading branch information
agin719 authored Nov 20, 2024
2 parents 8f34e29 + 4bf6e48 commit 89bbe7d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 23 deletions.
14 changes: 5 additions & 9 deletions cos.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (

const (
// Version current go sdk version
Version = "0.7.58"
Version = "0.7.59"
UserAgent = "cos-go-sdk-v5/" + Version
contentTypeXML = "application/xml"
defaultServiceBaseURL = "http://service.cos.myqcloud.com"
Expand All @@ -48,11 +48,9 @@ var (
regionChecker = regexp.MustCompile(`^[a-z-1]+$`)

// 校验传入的url
domainSuffix = regexp.MustCompile(`^.*\.(myqcloud\.com(:[0-9]+){0,1}|tencentcos\.cn(:[0-9]+){0,1})$`)
bucketDomainChecker = regexp.MustCompile(`^(http://|https://){0,1}([a-z0-9-]+-[0-9]+\.){0,1}((cos|cos-internal|cos-website|ci)\.[a-z-1]+|file)\.(myqcloud\.com|tencentcos\.cn)(:[0-9]+){0,1}$`)
serviceDomainChecker = regexp.MustCompile(`^(http://|https://){0,1}((service.cos.myqcloud.com|service.cos-internal.tencentcos.cn|service.cos.tencentcos.cn)|(cos|cos-internal)\.[a-z-1]+\.(myqcloud\.com|tencentcos\.cn))(:[0-9]+){0,1}$`)
batchDomainChecker = regexp.MustCompile(`^(http://|https://){0,1}([0-9]+\.){1}cos-control\.[a-z-1]+\.(myqcloud\.com|tencentcos\.cn)(:[0-9]+){0,1}$`)
invalidBucketErr = fmt.Errorf("invalid bucket format, please check your cos.BaseURL")
domainSuffix = regexp.MustCompile(`^.*\.(myqcloud\.com(:[0-9]+){0,1}|tencentcos\.cn(:[0-9]+){0,1})$`)
bucketDomainChecker = regexp.MustCompile(`^(http://|https://){0,1}([a-z0-9-]+\.)+(myqcloud\.com|tencentcos\.cn)(:[0-9]+){0,1}$`)
invalidBucketErr = fmt.Errorf("invalid bucket format, please check your cos.BaseURL")

switchHost = regexp.MustCompile(`([a-z0-9-]+-[0-9]+\.)(cos\.[a-z-1]+)\.(myqcloud\.com)(:[0-9]+){0,1}$`)
accelerateDomainSuffix = "accelerate.myqcloud.com"
Expand Down Expand Up @@ -93,9 +91,7 @@ func (*BaseURL) innerCheck(u *url.URL, reg *regexp.Regexp) bool {
}

func (u *BaseURL) Check() bool {
return u.innerCheck(u.BucketURL, bucketDomainChecker) &&
(u.innerCheck(u.ServiceURL, serviceDomainChecker) || u.innerCheck(u.ServiceURL, bucketDomainChecker)) &&
(u.innerCheck(u.BatchURL, batchDomainChecker) || u.innerCheck(u.BatchURL, bucketDomainChecker))
return u.innerCheck(u.BucketURL, bucketDomainChecker) && u.innerCheck(u.ServiceURL, bucketDomainChecker) && u.innerCheck(u.BatchURL, bucketDomainChecker)
}

// NewBucketURL 生成 BaseURL 所需的 BucketURL
Expand Down
23 changes: 9 additions & 14 deletions cos_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,15 @@ func Test_BaseURL(t *testing.T) {
if !(&BaseURL{BucketURL: u, ServiceURL: u, BatchURL: u}).Check() {
t.Errorf("BaseURL check failed: %v", u)
}
u, _ = url.Parse("https://example-125000000.cos-internal.accelerate.tencentcos.cn:443")
u, _ = url.Parse("https://cluster-1.cos-2.ap-guangzhou.myqcloud.com")
if !(&BaseURL{BucketURL: u, ServiceURL: u, BatchURL: u}).Check() {
t.Errorf("BaseURL check failed: %v", u)
}
u, _ = url.Parse("https://test-1250000.global.tencentcos.cn")
if !(&BaseURL{BucketURL: u, ServiceURL: u, BatchURL: u}).Check() {
t.Errorf("BaseURL check failed: %v", u)
}
u, _ = url.Parse("http://1.cos-c-internal.ap-singapore.tencentcos.cn")
if !(&BaseURL{BucketURL: u, ServiceURL: u, BatchURL: u}).Check() {
t.Errorf("BaseURL check failed: %v", u)
}
Expand Down Expand Up @@ -341,17 +349,4 @@ func Test_BaseURL(t *testing.T) {
if !(&BaseURL{BatchURL: u}).Check() {
t.Errorf("BaseURL check failed: %v", u)
}
u, _ = url.Parse("http://cos-control.ap-guangzhou.tencentcos.cn")
if (&BaseURL{BatchURL: u}).Check() {
t.Errorf("BaseURL check failed: %v", u)
}
u, _ = url.Parse("http://cos-control.ap-guangzhou.myqcloud.com")
if (&BaseURL{BatchURL: u}).Check() {
t.Errorf("BaseURL check failed: %v", u)
}
u, _ = url.Parse("http://cos-control.ap-guangzhou.myqcloud.com")
if (&BaseURL{BatchURL: u}).Check() {
t.Errorf("BaseURL check failed: %v", u)
}

}

0 comments on commit 89bbe7d

Please sign in to comment.