Skip to content

Commit 5baabc2

Browse files
Merge pull request #180 from tencentyun/feature_gavinhgchen_504e37e0
adapt strict signature
2 parents 60423d4 + af10fc0 commit 5baabc2

File tree

11 files changed

+191
-55
lines changed

11 files changed

+191
-55
lines changed

include/cos_defines.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
namespace qcloud_cos {
1414

15-
#define COS_CPP_SDK_VERSON "v5.5.17"
15+
#define COS_CPP_SDK_VERSON "v5.5.18"
1616

1717
/// 路径分隔符
1818
const char kPathDelimiter[] = "/";

include/util/string_util.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,8 @@ class StringUtil {
264264
* @brief 从字符串中获取uint16整型
265265
*/
266266
static uint16_t GetUint16FromStrWithBigEndian(const char* str);
267+
268+
static size_t GetLengthFromIStream(std::istream& is);
267269
};
268270
} // namespace qcloud_cos
269271

src/op/base_op.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ CosResult BaseOp::NormalAction(
159159
if (!req.SignHeaderHost()) {
160160
not_sign_headers.insert("Host");
161161
}
162+
req_headers[kHttpHeaderContentLength] = std::to_string(req_body.length());
162163

163164
// 2. 计算签名
164165
std::string auth_str =
@@ -339,6 +340,7 @@ CosResult BaseOp::UploadAction(
339340
if (!req.SignHeaderHost()) {
340341
not_sign_headers.insert("Host");
341342
}
343+
req_headers[kHttpHeaderContentLength] = std::to_string(StringUtil::GetLengthFromIStream(is));
342344
// 2. 计算签名
343345
std::string auth_str =
344346
AuthTool::Sign(GetAccessKey(), GetSecretKey(), req.GetMethod(),

src/op/object_op.cpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2090,6 +2090,7 @@ void ObjectOp::FillUploadTask(const std::string& upload_id,
20902090
if (!sign_header_host) {
20912091
not_sign_headers.insert("Host");
20922092
}
2093+
req_headers[kHttpHeaderContentLength] = std::to_string(len);
20932094
std::string auth_str = AuthTool::Sign(GetAccessKey(), GetSecretKey(), "PUT",
20942095
path, req_headers, req_params, not_sign_headers);
20952096
req_headers["Authorization"] = auth_str;
@@ -2130,6 +2131,7 @@ void ObjectOp::FillCopyTask(const std::string& upload_id,
21302131
if (!sign_header_host) {
21312132
not_sign_headers.insert("Host");
21322133
}
2134+
req_headers[kHttpHeaderContentLength] = "0";
21332135
std::string auth_str = AuthTool::Sign(GetAccessKey(), GetSecretKey(), "PUT",
21342136
path, req_headers, req_params, not_sign_headers);
21352137
req_headers["Authorization"] = auth_str;
@@ -2161,17 +2163,19 @@ std::string ObjectOp::GeneratePresignedUrl(const GeneratePresignedUrlReq& req) {
21612163
}
21622164

21632165
std::map<std::string, std::string> headers;
2164-
headers["Host"] = host;
2166+
BaseReq req_header;
2167+
req_header.AddHeaders(req.GetHeaders());
2168+
req_header.AddHeader("Host", host);
21652169
std::unordered_set<std::string> not_sign_headers;
21662170
if (!req.SignHeaderHost()) {
21672171
not_sign_headers.insert("Host");
21682172
}
21692173
if (req.GetStartTimeInSec() == 0 || req.GetExpiredTimeInSec() == 0) {
21702174
auth_str = AuthTool::Sign(GetAccessKey(), GetSecretKey(), req.GetMethod(),
2171-
req.GetPath(), headers, req.GetParams(), not_sign_headers);
2175+
req.GetPath(), req_header.GetHeaders(), req.GetParams(), not_sign_headers);
21722176
} else {
21732177
auth_str = AuthTool::Sign(
2174-
GetAccessKey(), GetSecretKey(), req.GetMethod(), req.GetPath(), headers,
2178+
GetAccessKey(), GetSecretKey(), req.GetMethod(), req.GetPath(), req_header.GetHeaders(),
21752179
req.GetParams(), req.GetStartTimeInSec(),
21762180
req.GetStartTimeInSec() + req.GetExpiredTimeInSec(), not_sign_headers);
21772181
}
@@ -2190,9 +2194,9 @@ std::string ObjectOp::GeneratePresignedUrl(const GeneratePresignedUrlReq& req) {
21902194
c_itr != req_params.end(); ++c_itr) {
21912195
std::string part = "";
21922196
if (c_itr->second.empty()) {
2193-
part = c_itr->first + "&";
2197+
part = "&" + c_itr->first;
21942198
} else {
2195-
part = c_itr->first + "=" + c_itr->second + "&";
2199+
part = "&" + c_itr->first + "=" + c_itr->second;
21962200
}
21972201
query_str += part;
21982202
}

src/util/http_sender.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -169,10 +169,6 @@ int HttpSender::SendRequest(
169169
}
170170

171171
// 3. 计算长度
172-
std::streampos pos = is.tellg();
173-
is.seekg(0, std::ios::end);
174-
req.setContentLength(is.tellg());
175-
is.seekg(pos);
176172

177173
std::ostringstream debug_os;
178174
req.write(debug_os);
@@ -398,7 +394,6 @@ int HttpSender::SendRequest(
398394
// req.add(c_itr->first, (c_itr->second).c_str());
399395
req.add(c_itr->first, c_itr->second);
400396
}
401-
req.add("Content-Length", StringUtil::Uint64ToString(req_body.size()));
402397

403398
std::ostringstream debug_os;
404399
req.write(debug_os);

src/util/string_util.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,4 +273,16 @@ uint16_t StringUtil::GetUint16FromStrWithBigEndian(const char* str) {
273273
num |= tmp;
274274
return num;
275275
}
276+
277+
size_t StringUtil::GetLengthFromIStream(std::istream& is) {
278+
std::streampos pos = is.tellg();
279+
is.seekg(0, std::ios::end);
280+
std::streampos size = is.tellg();
281+
is.seekg(pos);
282+
if (size == std::streampos(-1)) {
283+
return 0;
284+
}
285+
286+
return static_cast<size_t>(size);
287+
}
276288
} // namespace qcloud_cos

unittest/src/async_op_test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class AsyncOpTest : public testing::Test {
3737
}
3838
m_client = new CosAPI(*m_config);
3939

40-
m_bucket_name = "coscppsdkv5ut" + GetEnvVar("COS_CPP_V5_TAG") + "-" +
40+
m_bucket_name = "coscppsdkv5ut-async" + GetEnvVar("COS_CPP_V5_TAG") + "-" +
4141
GetEnvVar("CPP_SDK_V5_APPID");
4242
{
4343
PutBucketReq req(m_bucket_name);

unittest/src/bucket_op_test.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -198,13 +198,13 @@ TEST_F(BucketOpTest, GetServiceTest) {
198198

199199
TEST_F(BucketOpTest, SetCredentailTest) {
200200
{
201-
qcloud_cos::CosConfig config(00000,"secretId","secretKey",GetEnvVar("CPP_SDK_V5_REGION"));
201+
qcloud_cos::CosConfig config(00000,"secretId","secretKey",GetEnvVar("CPP_SDK_V5_OTHER_REGION"));
202202
qcloud_cos::CosAPI cos(config);
203203
cos.SetCredentail(GetEnvVar("CPP_SDK_V5_ACCESS_KEY"),GetEnvVar("CPP_SDK_V5_SECRET_KEY"),"");
204204
EXPECT_EQ(config.GetAccessKey(), "secretId");
205205
EXPECT_EQ(config.GetAppId(), 00000);
206206
EXPECT_EQ(config.GetSecretKey(), "secretKey");
207-
EXPECT_EQ(config.GetRegion(), GetEnvVar("CPP_SDK_V5_REGION"));
207+
EXPECT_EQ(config.GetRegion(), GetEnvVar("CPP_SDK_V5_OTHER_REGION"));
208208
bool result = cos.IsBucketExist(m_bucket_name2);
209209
EXPECT_TRUE(result);
210210
}
@@ -312,13 +312,13 @@ TEST_F(BucketOpTest, GetBucketLocationTest) {
312312
// normal CPP_SDK_V5_REGION
313313
{
314314
std::string location = m_client2->GetBucketLocation(m_bucket_name2);
315-
EXPECT_EQ(location,GetEnvVar("CPP_SDK_V5_REGION"));
315+
EXPECT_EQ(location, GetEnvVar("CPP_SDK_V5_OTHER_REGION"));
316316
}
317317

318318
// wrong ""
319319
{
320320
std::string location = m_client2->GetBucketLocation(m_bucket_name_wrong);
321-
EXPECT_EQ(location,"");
321+
EXPECT_EQ(location, "");
322322
}
323323
}
324324

@@ -1447,10 +1447,10 @@ TEST_F(BucketOpTest, BucketReferer) {
14471447
}
14481448

14491449
TEST_F(BucketOpTest, InvalidConfig) {
1450-
qcloud_cos::CosConfig config(123, "ak", "sk", "");
1451-
ASSERT_TRUE(config.GetRegion().empty());
1450+
qcloud_cos::CosConfig config(123, "ak", "", "ap-guangzhou");
1451+
ASSERT_FALSE(config.GetRegion().empty());
14521452
qcloud_cos::CosAPI cos(config);
1453-
HeadBucketReq req("test_bucket");
1453+
HeadBucketReq req("test-bucket-1253960454");
14541454
HeadBucketResp resp;
14551455
CosResult result = cos.HeadBucket(req, &resp);
14561456
ASSERT_TRUE(!result.IsSucc());

0 commit comments

Comments
 (0)