Skip to content

Commit d96c7d5

Browse files
committed
support path style.
1 parent 5b75922 commit d96c7d5

File tree

7 files changed

+223
-25
lines changed

7 files changed

+223
-25
lines changed

sdk/include/alibabacloud/oss/client/ClientConfiguration.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ namespace OSS
9090
*/
9191
std::string caFile;
9292
/**
93-
* your certificate file.
93+
* enable or disable cname, default is false.
9494
*/
9595
bool isCname;
9696
/**
@@ -121,6 +121,10 @@ namespace OSS
121121
* Your http client' implement
122122
*/
123123
std::shared_ptr<HttpClient> httpClient;
124+
/**
125+
* enable or disable path style, default is false.
126+
*/
127+
bool isPathStyle;
124128
};
125129
}
126130
}

sdk/src/OssClientImpl.cc

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -212,8 +212,8 @@ void OssClientImpl::addUrl(const std::shared_ptr<HttpRequest> &httpRequest, cons
212212
{
213213
const OssRequest& ossRequest = static_cast<const OssRequest&>(request);
214214

215-
auto host = CombineHostString(endpoint, ossRequest.bucket(), configuration().isCname);
216-
auto path = CombinePathString(endpoint, ossRequest.bucket(), ossRequest.key());
215+
auto host = CombineHostString(endpoint, ossRequest.bucket(), configuration().isCname, configuration().isPathStyle);
216+
auto path = CombinePathString(endpoint, ossRequest.bucket(), ossRequest.key(), configuration().isPathStyle);
217217

218218
Url url(host);
219219
url.setPath(path);
@@ -1315,9 +1315,9 @@ StringOutcome OssClientImpl::GeneratePresignedUrl(const GeneratePresignedUrlRequ
13151315

13161316
//host
13171317
std::stringstream ss;
1318-
ss << CombineHostString(endpoint_, request.bucket_, configuration().isCname);
1318+
ss << CombineHostString(endpoint_, request.bucket_, configuration().isCname, configuration().isPathStyle);
13191319
//path
1320-
auto path = CombinePathString(endpoint_, request.bucket_, request.key_);
1320+
auto path = CombinePathString(endpoint_, request.bucket_, request.key_, configuration().isPathStyle);
13211321
if (request.unencodedSlash_) {
13221322
StringReplace(path, "%2F", "/");
13231323
}
@@ -1749,9 +1749,9 @@ StringOutcome OssClientImpl::GenerateRTMPSignedUrl(const GenerateRTMPSignedUrlRe
17491749
parameters["Signature"] = signature;
17501750

17511751
ss.str("");
1752-
ss << CombineRTMPString(endpoint_, request.bucket_, configuration().isCname);
1752+
ss << CombineRTMPString(endpoint_, request.bucket_, configuration().isCname, configuration().isPathStyle);
17531753
ss << "/live";
1754-
ss << CombinePathString(endpoint_, request.bucket_, request.key_);
1754+
ss << CombinePathString(endpoint_, request.bucket_, request.key_, configuration().isPathStyle);
17551755
ss << "?";
17561756
ss << CombineQueryString(parameters);
17571757

sdk/src/client/ClientConfiguration.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,8 @@ ClientConfiguration::ClientConfiguration() :
115115
sendRateLimiter(nullptr),
116116
recvRateLimiter(nullptr),
117117
executor(nullptr),
118-
httpClient(nullptr)
118+
httpClient(nullptr),
119+
isPathStyle(false)
119120
{
120121

121122
}

sdk/src/utils/Utils.cc

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -852,14 +852,15 @@ const std::string& AlibabaCloud::OSS::LookupMimeType(const std::string &name)
852852
std::string AlibabaCloud::OSS::CombineHostString(
853853
const std::string &endpoint,
854854
const std::string &bucket,
855-
bool isCname)
855+
bool isCname,
856+
bool isPathStyle)
856857
{
857858
Url url(endpoint);
858859
if (url.scheme().empty()) {
859860
url.setScheme(Http::SchemeToString(Http::HTTP));
860861
}
861862

862-
if (!bucket.empty() && !isCname && !IsIp(url.host())) {
863+
if (!bucket.empty() && !isCname && !IsIp(url.host()) && !isPathStyle) {
863864
std::string host(bucket);
864865
host.append(".").append(url.host());
865866
url.setHost(host);
@@ -873,12 +874,13 @@ std::string AlibabaCloud::OSS::CombineHostString(
873874
std::string AlibabaCloud::OSS::CombinePathString(
874875
const std::string &endpoint,
875876
const std::string &bucket,
876-
const std::string &key)
877+
const std::string &key,
878+
bool isPathStyle)
877879
{
878880
Url url(endpoint);
879881
std::string path;
880882
path = "/";
881-
if (IsIp(url.host())) {
883+
if (IsIp(url.host()) || isPathStyle) {
882884
path.append(bucket).append("/");
883885
}
884886
path.append(UrlEncode(key));
@@ -888,10 +890,11 @@ std::string AlibabaCloud::OSS::CombinePathString(
888890
std::string AlibabaCloud::OSS::CombineRTMPString(
889891
const std::string &endpoint,
890892
const std::string &bucket,
891-
bool isCname)
893+
bool isCname,
894+
bool isPathStyle)
892895
{
893896
Url url(endpoint);
894-
if (!bucket.empty() && !isCname && !IsIp(url.host())) {
897+
if (!bucket.empty() && !isCname && !IsIp(url.host()) && !isPathStyle) {
895898
std::string host(bucket);
896899
host.append(".").append(url.host());
897900
url.setHost(host);

sdk/src/utils/Utils.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,10 @@ namespace OSS
7474
bool IsValidEndpoint(const std::string &value);
7575

7676
const std::string &LookupMimeType(const std::string& name);
77-
std::string CombineHostString(const std::string &endpoint, const std::string &bucket, bool isCname);
78-
std::string CombinePathString(const std::string &endpoint, const std::string &bucket, const std::string &key);
77+
std::string CombineHostString(const std::string &endpoint, const std::string &bucket, bool isCname, bool isPathStyle);
78+
std::string CombinePathString(const std::string &endpoint, const std::string &bucket, const std::string &key, bool isPathStyle);
7979
std::string CombineQueryString(const ParameterCollection &parameters);
80-
std::string CombineRTMPString(const std::string &endpoint, const std::string &bucket, bool isCname);
80+
std::string CombineRTMPString(const std::string &endpoint, const std::string &bucket, bool isCname, bool isPathStyle);
8181

8282

8383
std::streampos GetIOStreamLength(std::iostream &stream);

test/src/Other/EndpointTest.cc

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
/*
2+
* Copyright 2009-2017 Alibaba Cloud All rights reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#include <gtest/gtest.h>
18+
#include <alibabacloud/oss/OssClient.h>
19+
#include "../Config.h"
20+
#include "../Utils.h"
21+
#include "src/utils/Utils.h"
22+
23+
namespace AlibabaCloud {
24+
namespace OSS {
25+
26+
class EndpointTest : public ::testing::Test {
27+
protected:
28+
EndpointTest()
29+
{
30+
}
31+
32+
~EndpointTest() override
33+
{
34+
}
35+
36+
// Sets up the stuff shared by all tests in this test case.
37+
static void SetUpTestCase()
38+
{
39+
Client = std::make_shared<OssClient>(Config::Endpoint, Config::AccessKeyId, Config::AccessKeySecret, ClientConfiguration());
40+
BucketName = TestUtils::GetBucketName("cpp-sdk-endpoint");
41+
Client->CreateBucket(CreateBucketRequest(BucketName));
42+
}
43+
44+
// Tears down the stuff shared by all tests in this test case.
45+
static void TearDownTestCase()
46+
{
47+
OssClient client(Config::Endpoint, Config::AccessKeyId, Config::AccessKeySecret, ClientConfiguration());
48+
TestUtils::CleanBucket(client, BucketName);
49+
Client = nullptr;
50+
}
51+
52+
// Sets up the test fixture.
53+
void SetUp() override
54+
{
55+
}
56+
57+
// Tears down the test fixture.
58+
void TearDown() override
59+
{
60+
}
61+
public:
62+
static std::shared_ptr<OssClient> Client;
63+
static std::string BucketName;
64+
};
65+
66+
std::shared_ptr<OssClient> EndpointTest::Client = nullptr;
67+
std::string EndpointTest::BucketName = "";
68+
69+
70+
TEST_F(EndpointTest, PathStyleTest)
71+
{
72+
auto conf = ClientConfiguration();
73+
conf.isPathStyle = true;
74+
auto client = std::make_shared<OssClient>(Config::Endpoint, Config::AccessKeyId, Config::AccessKeySecret, conf);
75+
76+
auto gboutcome = client->GetBucketAcl(BucketName);
77+
EXPECT_EQ(gboutcome.isSuccess(), false);
78+
EXPECT_EQ(gboutcome.error().Code(), "SecondLevelDomainForbidden");
79+
80+
auto hooutcome = client->GetObject(BucketName, "no-exist-key");
81+
EXPECT_EQ(hooutcome.isSuccess(), false);
82+
EXPECT_EQ(hooutcome.error().Code(), "SecondLevelDomainForbidden");
83+
84+
auto looutcome = client->ListObjects(BucketName);
85+
EXPECT_EQ(looutcome.isSuccess(), false);
86+
EXPECT_EQ(looutcome.error().Code(), "SecondLevelDomainForbidden");
87+
88+
ListBucketsRequest lbrequest;
89+
lbrequest.setPrefix(BucketName);
90+
lbrequest.setMaxKeys(100);
91+
auto lboutcome = client->ListBuckets(lbrequest);
92+
EXPECT_EQ(lboutcome.isSuccess(), true);
93+
EXPECT_EQ(lboutcome.result().Buckets().size(), 1UL);
94+
95+
GeneratePresignedUrlRequest request(BucketName, "no-exist-key", Http::Get);
96+
auto urlOutcome = client->GeneratePresignedUrl(request);
97+
EXPECT_EQ(urlOutcome.isSuccess(), true);
98+
99+
std::string path = "/" + BucketName + "/no-exist-key";
100+
EXPECT_EQ(urlOutcome.result().find(path.c_str()) != std::string::npos, true);
101+
}
102+
103+
TEST_F(EndpointTest, CnameTest)
104+
{
105+
Url url(Config::Endpoint);
106+
107+
auto cnameEndpoint = BucketName + "." + url.authority();
108+
109+
auto conf = ClientConfiguration();
110+
conf.isCname = true;
111+
auto client = std::make_shared<OssClient>(cnameEndpoint, Config::AccessKeyId, Config::AccessKeySecret, conf);
112+
113+
auto gboutcome = client->GetBucketAcl(BucketName);
114+
EXPECT_EQ(gboutcome.isSuccess(), true);
115+
EXPECT_EQ(gboutcome.result().Acl(), CannedAccessControlList::Private);
116+
117+
auto hooutcome = client->GetObject(BucketName, "no-exist-key");
118+
EXPECT_EQ(hooutcome.isSuccess(), false);
119+
EXPECT_EQ(hooutcome.error().Code(), "NoSuchKey");
120+
121+
auto looutcome = client->ListObjects(BucketName);
122+
EXPECT_EQ(looutcome.isSuccess(), true);
123+
124+
ListBucketsRequest lbrequest;
125+
lbrequest.setPrefix(BucketName);
126+
lbrequest.setMaxKeys(100);
127+
auto lboutcome = client->ListBuckets(lbrequest);
128+
EXPECT_EQ(lboutcome.isSuccess(), false);
129+
EXPECT_EQ(lboutcome.error().Code(), "SignatureDoesNotMatch");
130+
EXPECT_EQ(lboutcome.error().Host(), cnameEndpoint);
131+
132+
GeneratePresignedUrlRequest request(BucketName, "no-exist-key", Http::Get);
133+
auto urlOutcome = client->GeneratePresignedUrl(request);
134+
EXPECT_EQ(urlOutcome.isSuccess(), true);
135+
136+
auto gOutcome = Client->GetObjectByUrl(urlOutcome.result());
137+
EXPECT_EQ(gOutcome.isSuccess(), false);
138+
EXPECT_EQ(gOutcome.error().Code(), "NoSuchKey");
139+
140+
}
141+
142+
}
143+
}

test/src/Other/UtilsFunctionTest.cc

Lines changed: 55 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -535,27 +535,74 @@ TEST_F(UtilsFunctionTest, GetIOStreamLengthResetContentPositionTest)
535535

536536
TEST_F(UtilsFunctionTest, CombineHostStringTest)
537537
{
538-
EXPECT_STREQ(CombineHostString("http://oss-cn-hangzhou.aliyuncs.com", "test-bucket", false).c_str(),
538+
EXPECT_STREQ(CombineHostString("http://oss-cn-hangzhou.aliyuncs.com", "test-bucket", false, false).c_str(),
539539
"http://test-bucket.oss-cn-hangzhou.aliyuncs.com");
540-
EXPECT_STREQ(CombineHostString("oss-cn-hangzhou.aliyuncs.com", "test-bucket", false).c_str(),
540+
EXPECT_STREQ(CombineHostString("oss-cn-hangzhou.aliyuncs.com", "test-bucket", false, false).c_str(),
541541
"http://test-bucket.oss-cn-hangzhou.aliyuncs.com");
542-
EXPECT_STREQ(CombineHostString("http://192.168.1.1", "test-bucket", false).c_str(),
542+
EXPECT_STREQ(CombineHostString("http://192.168.1.1", "test-bucket", false, false).c_str(),
543543
"http://192.168.1.1");
544544

545-
EXPECT_STREQ(CombineHostString("http://cname.com", "test-bucket", true).c_str(),
545+
EXPECT_STREQ(CombineHostString("http://cname.com", "test-bucket", true, false).c_str(),
546546
"http://cname.com");
547-
EXPECT_STREQ(CombineHostString("cname.com", "test-bucket", true).c_str(),
547+
EXPECT_STREQ(CombineHostString("cname.com", "test-bucket", true, false).c_str(),
548548
"http://cname.com");
549-
EXPECT_STREQ(CombineHostString("http://192.168.1.1", "test-bucket", true).c_str(),
549+
EXPECT_STREQ(CombineHostString("http://192.168.1.1", "test-bucket", true, false).c_str(),
550+
"http://192.168.1.1");
551+
552+
//path style
553+
EXPECT_STREQ(CombineHostString("http://oss-cn-hangzhou.aliyuncs.com", "test-bucket", false, true).c_str(),
554+
"http://oss-cn-hangzhou.aliyuncs.com");
555+
EXPECT_STREQ(CombineHostString("oss-cn-hangzhou.aliyuncs.com", "test-bucket", false, true).c_str(),
556+
"http://oss-cn-hangzhou.aliyuncs.com");
557+
EXPECT_STREQ(CombineHostString("http://cname.com", "test-bucket", true, true).c_str(),
558+
"http://cname.com");
559+
EXPECT_STREQ(CombineHostString("cname.com", "test-bucket", true, true).c_str(),
560+
"http://cname.com");
561+
EXPECT_STREQ(CombineHostString("http://192.168.1.1", "test-bucket", true, true).c_str(),
550562
"http://192.168.1.1");
551563
}
552564

553565
TEST_F(UtilsFunctionTest, CombinePathStringTest)
554566
{
555-
EXPECT_STREQ(CombinePathString("http://oss-cn-hangzhou.aliyuncs.com", "test-bucket", "test-key").c_str(),
567+
EXPECT_STREQ(CombinePathString("http://oss-cn-hangzhou.aliyuncs.com", "test-bucket", "test-key", false).c_str(),
556568
"/test-key");
557-
EXPECT_STREQ(CombinePathString("http://192.168.1.1", "test-bucket", "test-key").c_str(),
569+
EXPECT_STREQ(CombinePathString("http://192.168.1.1", "test-bucket", "test-key", false).c_str(),
558570
"/test-bucket/test-key");
571+
572+
//path style
573+
EXPECT_STREQ(CombinePathString("http://oss-cn-hangzhou.aliyuncs.com", "test-bucket", "test-key", true).c_str(),
574+
"/test-bucket/test-key");
575+
EXPECT_STREQ(CombinePathString("http://192.168.1.1", "test-bucket", "test-key", true).c_str(),
576+
"/test-bucket/test-key");
577+
}
578+
579+
TEST_F(UtilsFunctionTest, CombineRTMPStringTest)
580+
{
581+
EXPECT_STREQ(CombineRTMPString("http://oss-cn-hangzhou.aliyuncs.com", "test-bucket", false, false).c_str(),
582+
"rtmp://test-bucket.oss-cn-hangzhou.aliyuncs.com");
583+
EXPECT_STREQ(CombineRTMPString("oss-cn-hangzhou.aliyuncs.com", "test-bucket", false, false).c_str(),
584+
"rtmp://test-bucket.oss-cn-hangzhou.aliyuncs.com");
585+
EXPECT_STREQ(CombineRTMPString("http://192.168.1.1", "test-bucket", false, false).c_str(),
586+
"rtmp://192.168.1.1");
587+
588+
EXPECT_STREQ(CombineRTMPString("http://cname.com", "test-bucket", true, false).c_str(),
589+
"rtmp://cname.com");
590+
EXPECT_STREQ(CombineRTMPString("cname.com", "test-bucket", true, false).c_str(),
591+
"rtmp://cname.com");
592+
EXPECT_STREQ(CombineRTMPString("http://192.168.1.1", "test-bucket", true, false).c_str(),
593+
"rtmp://192.168.1.1");
594+
595+
//path style
596+
EXPECT_STREQ(CombineRTMPString("http://oss-cn-hangzhou.aliyuncs.com", "test-bucket", false, true).c_str(),
597+
"rtmp://oss-cn-hangzhou.aliyuncs.com");
598+
EXPECT_STREQ(CombineRTMPString("oss-cn-hangzhou.aliyuncs.com", "test-bucket", false, true).c_str(),
599+
"rtmp://oss-cn-hangzhou.aliyuncs.com");
600+
EXPECT_STREQ(CombineRTMPString("http://cname.com", "test-bucket", true, true).c_str(),
601+
"rtmp://cname.com");
602+
EXPECT_STREQ(CombineRTMPString("cname.com", "test-bucket", true, true).c_str(),
603+
"rtmp://cname.com");
604+
EXPECT_STREQ(CombineRTMPString("http://192.168.1.1", "test-bucket", true, true).c_str(),
605+
"rtmp://192.168.1.1");
559606
}
560607

561608
TEST_F(UtilsFunctionTest, CombineQueryStringTest)

0 commit comments

Comments
 (0)