Skip to content

Commit 3517bd1

Browse files
yangzong18huiguangjun
authored andcommitted
add force path style
1 parent f1336c9 commit 3517bd1

File tree

6 files changed

+98
-12
lines changed

6 files changed

+98
-12
lines changed

oss/bucket_test.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2280,6 +2280,51 @@ func (s *OssBucketSuite) TestGetConfig(c *C) {
22802280
c.Assert(bucket.GetConfig().IsEnableMD5, Equals, false)
22812281
}
22822282

2283+
func (s *OssBucketSuite) TestForcePathStyle(c *C) {
2284+
url, err := url.ParseRequestURI(endpoint)
2285+
client, err := New(endpoint, accessID, accessKey, ForcePathStyle(true))
2286+
c.Assert(err, IsNil)
2287+
2288+
_, err = client.GetBucketInfo(bucketName)
2289+
c.Assert(err, NotNil)
2290+
c.Assert(err.(ServiceError).Code, Equals, "SecondLevelDomainForbidden")
2291+
c.Assert(err.(ServiceError).HostID, Equals, url.Host)
2292+
2293+
bucket, err := client.Bucket(bucketName)
2294+
c.Assert(err, IsNil)
2295+
2296+
c.Assert(bucket.GetConfig().IsPathStyle, Equals, true)
2297+
2298+
objectName := "demo.txt"
2299+
2300+
err = bucket.PutObject(objectName, strings.NewReader("hi oss"))
2301+
c.Assert(err, NotNil)
2302+
c.Assert(err.(ServiceError).Code, Equals, "SecondLevelDomainForbidden")
2303+
2304+
str, err := bucket.SignURL(objectName, HTTPPut, 3600)
2305+
c.Assert(err, IsNil)
2306+
strUrl := endpoint + "/" + bucketName + "/" + objectName
2307+
c.Assert(strings.Contains(str, strUrl), Equals, true)
2308+
}
2309+
2310+
func (s *OssBucketSuite) TestUseCname(c *C) {
2311+
url, err := url.ParseRequestURI(endpoint)
2312+
c.Assert(err, IsNil)
2313+
cnameEndpoint := bucketName + "." + url.Host
2314+
client, err := New(cnameEndpoint, accessID, accessKey, UseCname(true))
2315+
c.Assert(err, IsNil)
2316+
2317+
info, err := client.GetBucketInfo(bucketName)
2318+
2319+
c.Assert(err, IsNil)
2320+
c.Assert(info.BucketInfo.Name, Equals, bucketName)
2321+
2322+
client, err = New(cnameEndpoint, accessID, accessKey)
2323+
_, err = client.GetBucketInfo(bucketName)
2324+
c.Assert(err, NotNil)
2325+
c.Assert(err.(ServiceError).HostID, Equals, bucketName+"."+cnameEndpoint)
2326+
}
2327+
22832328
func (s *OssBucketSuite) TestContextTimeout(c *C) {
22842329
client, err := New(endpoint, accessID, accessKey)
22852330
c.Assert(err, IsNil)

oss/client.go

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,6 @@ func New(endpoint, accessKeyID, accessKeySecret string, options ...ClientOption)
4949

5050
// URL parse
5151
url := &urlMaker{}
52-
err := url.Init(config.Endpoint, config.IsCname, config.IsUseProxy)
53-
if err != nil {
54-
return nil, err
55-
}
5652

5753
// HTTP connect
5854
conn := &Conn{config: config, url: url}
@@ -68,6 +64,11 @@ func New(endpoint, accessKeyID, accessKeySecret string, options ...ClientOption)
6864
option(client)
6965
}
7066

67+
err := url.InitExt(config.Endpoint, config.IsCname, config.IsUseProxy, config.IsPathStyle)
68+
if err != nil {
69+
return nil, err
70+
}
71+
7172
if config.AuthVersion != AuthV1 && config.AuthVersion != AuthV2 && config.AuthVersion != AuthV4 {
7273
return nil, fmt.Errorf("Init client Error, invalid Auth version: %v", config.AuthVersion)
7374
}
@@ -2597,7 +2598,16 @@ func (client Client) LimitDownloadSpeed(downSpeed int) error {
25972598
func UseCname(isUseCname bool) ClientOption {
25982599
return func(client *Client) {
25992600
client.Config.IsCname = isUseCname
2600-
client.Conn.url.Init(client.Config.Endpoint, client.Config.IsCname, client.Config.IsUseProxy)
2601+
}
2602+
}
2603+
2604+
// ForcePathStyle sets the flag of using Path Style. By default it's false.
2605+
//
2606+
// isPathStyle true: the endpoint has the Path Style, false: the endpoint does not have Path Style. Default is false.
2607+
//
2608+
func ForcePathStyle(isPathStyle bool) ClientOption {
2609+
return func(client *Client) {
2610+
client.Config.IsPathStyle = isPathStyle
26012611
}
26022612
}
26032613

@@ -2694,7 +2704,6 @@ func Proxy(proxyHost string) ClientOption {
26942704
return func(client *Client) {
26952705
client.Config.IsUseProxy = true
26962706
client.Config.ProxyHost = proxyHost
2697-
client.Conn.url.Init(client.Config.Endpoint, client.Config.IsCname, client.Config.IsUseProxy)
26982707
}
26992708
}
27002709

@@ -2711,7 +2720,6 @@ func AuthProxy(proxyHost, proxyUser, proxyPassword string) ClientOption {
27112720
client.Config.IsAuthProxy = true
27122721
client.Config.ProxyUser = proxyUser
27132722
client.Config.ProxyPassword = proxyPassword
2714-
client.Conn.url.Init(client.Config.Endpoint, client.Config.IsCname, client.Config.IsUseProxy)
27152723
}
27162724
}
27172725

oss/client_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2745,6 +2745,13 @@ func (s *OssClientSuite) TestHttpsEndpointProxy(c *C) {
27452745
c.Assert(err, IsNil)
27462746
}
27472747

2748+
func (s *OssBucketSuite) TestProxyNavigate(c *C) {
2749+
client, err := New(endpoint, accessID, accessKey, AuthProxy("http://127.0.0.1:8120", "user", "passwd"))
2750+
c.Assert(err, IsNil)
2751+
_, err = client.GetBucketInfo(bucketName)
2752+
c.Assert(strings.Contains(err.Error(), "proxyconnect tcp: dial tcp 127.0.0.1:8120"), Equals, true)
2753+
}
2754+
27482755
// Private
27492756
func (s *OssClientSuite) checkBucket(buckets []BucketProperties, bucket string) bool {
27502757
for _, v := range buckets {

oss/conf.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ type Config struct {
146146
Timeout uint // Timeout in seconds. By default it's 60.
147147
SecurityToken string // STS Token
148148
IsCname bool // If cname is in the endpoint.
149+
IsPathStyle bool // If Path Style is in the endpoint.
149150
HTTPTimeout HTTPTimeout // HTTP timeout
150151
HTTPMaxConns HTTPMaxConns // Http max connections
151152
IsUseProxy bool // Flag of using proxy.
@@ -256,6 +257,7 @@ func getDefaultOssConfig() *Config {
256257
config.Timeout = 60 // Seconds
257258
config.SecurityToken = ""
258259
config.IsCname = false
260+
config.IsPathStyle = false
259261

260262
config.HTTPTimeout.ConnectTimeout = time.Second * 30 // 30s
261263
config.HTTPTimeout.ReadWriteTimeout = time.Second * 60 // 60s

oss/conn.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -808,9 +808,10 @@ func (c *timeoutConn) SetWriteDeadline(t time.Time) error {
808808

809809
// UrlMaker builds URL and resource
810810
const (
811-
urlTypeCname = 1
812-
urlTypeIP = 2
813-
urlTypeAliyun = 3
811+
urlTypeCname = 1
812+
urlTypeIP = 2
813+
urlTypeAliyun = 3
814+
urlTypePathStyle = 4
814815
)
815816

816817
type urlMaker struct {
@@ -822,6 +823,11 @@ type urlMaker struct {
822823

823824
// Init parses endpoint
824825
func (um *urlMaker) Init(endpoint string, isCname bool, isProxy bool) error {
826+
return um.InitExt(endpoint, isCname, isProxy, false)
827+
}
828+
829+
// InitExt parses endpoint
830+
func (um *urlMaker) InitExt(endpoint string, isCname bool, isProxy bool, isPathStyle bool) error {
825831
if strings.HasPrefix(endpoint, "http://") {
826832
um.Scheme = "http"
827833
um.NetLoc = endpoint[len("http://"):]
@@ -854,6 +860,8 @@ func (um *urlMaker) Init(endpoint string, isCname bool, isProxy bool) error {
854860
um.Type = urlTypeIP
855861
} else if isCname {
856862
um.Type = urlTypeCname
863+
} else if isPathStyle {
864+
um.Type = urlTypePathStyle
857865
} else {
858866
um.Type = urlTypeAliyun
859867
}
@@ -902,7 +910,7 @@ func (um urlMaker) buildURL(bucket, object string) (string, string) {
902910
if um.Type == urlTypeCname {
903911
host = um.NetLoc
904912
path = "/" + object
905-
} else if um.Type == urlTypeIP {
913+
} else if um.Type == urlTypeIP || um.Type == urlTypePathStyle {
906914
if bucket == "" {
907915
host = um.NetLoc
908916
path = "/"
@@ -937,7 +945,7 @@ func (um urlMaker) buildURLV4(bucket, object string) (string, string) {
937945
if um.Type == urlTypeCname {
938946
host = um.NetLoc
939947
path = "/" + object
940-
} else if um.Type == urlTypeIP {
948+
} else if um.Type == urlTypeIP || um.Type == urlTypePathStyle {
941949
if bucket == "" {
942950
host = um.NetLoc
943951
path = "/"

oss/conn_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,22 @@ func (s *OssConnSuite) TestURLMarker(c *C) {
8484
c.Assert(um.Type, Equals, urlTypeIP)
8585
c.Assert(um.Scheme, Equals, "https")
8686
c.Assert(um.NetLoc, Equals, "[2401:b180::dc]:8080")
87+
88+
um.InitExt("https://docs.github.com:8080", false, false, true)
89+
c.Assert(um.Type, Equals, urlTypePathStyle)
90+
c.Assert(um.Scheme, Equals, "https")
91+
c.Assert(um.NetLoc, Equals, "docs.github.com:8080")
92+
c.Assert(um.getURL("bucket", "object", "params").String(), Equals, "https://docs.github.com:8080/bucket/object?params")
93+
c.Assert(um.getURL("", "object", "params").String(), Equals, "https://docs.github.com:8080/?params")
94+
95+
um.InitExt("docs.github.com", false, false, true)
96+
c.Assert(um.Type, Equals, urlTypePathStyle)
97+
c.Assert(um.Scheme, Equals, "http")
98+
c.Assert(um.NetLoc, Equals, "docs.github.com")
99+
100+
c.Assert(um.getURL("bucket", "object", "params").String(), Equals, "http://docs.github.com/bucket/object?params")
101+
c.Assert(um.getURL("bucket", "object", "").String(), Equals, "http://docs.github.com/bucket/object")
102+
c.Assert(um.getURL("", "object", "").String(), Equals, "http://docs.github.com/")
87103
}
88104

89105
func (s *OssConnSuite) TestAuth(c *C) {

0 commit comments

Comments
 (0)