Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion include/cos_defines.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

namespace qcloud_cos {

#define COS_CPP_SDK_VERSON "v5.5.17"
#define COS_CPP_SDK_VERSON "v5.5.18"

/// 路径分隔符
const char kPathDelimiter[] = "/";
Expand Down
2 changes: 2 additions & 0 deletions include/util/string_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,8 @@ class StringUtil {
* @brief 从字符串中获取uint16整型
*/
static uint16_t GetUint16FromStrWithBigEndian(const char* str);

static size_t GetLengthFromIStream(std::istream& is);
};
} // namespace qcloud_cos

Expand Down
2 changes: 2 additions & 0 deletions src/op/base_op.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ CosResult BaseOp::NormalAction(
if (!req.SignHeaderHost()) {
not_sign_headers.insert("Host");
}
req_headers[kHttpHeaderContentLength] = std::to_string(req_body.length());

// 2. 计算签名
std::string auth_str =
Expand Down Expand Up @@ -339,6 +340,7 @@ CosResult BaseOp::UploadAction(
if (!req.SignHeaderHost()) {
not_sign_headers.insert("Host");
}
req_headers[kHttpHeaderContentLength] = std::to_string(StringUtil::GetLengthFromIStream(is));
// 2. 计算签名
std::string auth_str =
AuthTool::Sign(GetAccessKey(), GetSecretKey(), req.GetMethod(),
Expand Down
14 changes: 9 additions & 5 deletions src/op/object_op.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2090,6 +2090,7 @@ void ObjectOp::FillUploadTask(const std::string& upload_id,
if (!sign_header_host) {
not_sign_headers.insert("Host");
}
req_headers[kHttpHeaderContentLength] = std::to_string(len);
std::string auth_str = AuthTool::Sign(GetAccessKey(), GetSecretKey(), "PUT",
path, req_headers, req_params, not_sign_headers);
req_headers["Authorization"] = auth_str;
Expand Down Expand Up @@ -2130,6 +2131,7 @@ void ObjectOp::FillCopyTask(const std::string& upload_id,
if (!sign_header_host) {
not_sign_headers.insert("Host");
}
req_headers[kHttpHeaderContentLength] = "0";
std::string auth_str = AuthTool::Sign(GetAccessKey(), GetSecretKey(), "PUT",
path, req_headers, req_params, not_sign_headers);
req_headers["Authorization"] = auth_str;
Expand Down Expand Up @@ -2161,17 +2163,19 @@ std::string ObjectOp::GeneratePresignedUrl(const GeneratePresignedUrlReq& req) {
}

std::map<std::string, std::string> headers;
headers["Host"] = host;
BaseReq req_header;
req_header.AddHeaders(req.GetHeaders());
req_header.AddHeader("Host", host);
std::unordered_set<std::string> not_sign_headers;
if (!req.SignHeaderHost()) {
not_sign_headers.insert("Host");
}
if (req.GetStartTimeInSec() == 0 || req.GetExpiredTimeInSec() == 0) {
auth_str = AuthTool::Sign(GetAccessKey(), GetSecretKey(), req.GetMethod(),
req.GetPath(), headers, req.GetParams(), not_sign_headers);
req.GetPath(), req_header.GetHeaders(), req.GetParams(), not_sign_headers);
} else {
auth_str = AuthTool::Sign(
GetAccessKey(), GetSecretKey(), req.GetMethod(), req.GetPath(), headers,
GetAccessKey(), GetSecretKey(), req.GetMethod(), req.GetPath(), req_header.GetHeaders(),
req.GetParams(), req.GetStartTimeInSec(),
req.GetStartTimeInSec() + req.GetExpiredTimeInSec(), not_sign_headers);
}
Expand All @@ -2190,9 +2194,9 @@ std::string ObjectOp::GeneratePresignedUrl(const GeneratePresignedUrlReq& req) {
c_itr != req_params.end(); ++c_itr) {
std::string part = "";
if (c_itr->second.empty()) {
part = c_itr->first + "&";
part = "&" + c_itr->first;
} else {
part = c_itr->first + "=" + c_itr->second + "&";
part = "&" + c_itr->first + "=" + c_itr->second;
}
query_str += part;
}
Expand Down
5 changes: 0 additions & 5 deletions src/util/http_sender.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,10 +169,6 @@ int HttpSender::SendRequest(
}

// 3. 计算长度
std::streampos pos = is.tellg();
is.seekg(0, std::ios::end);
req.setContentLength(is.tellg());
is.seekg(pos);

std::ostringstream debug_os;
req.write(debug_os);
Expand Down Expand Up @@ -398,7 +394,6 @@ int HttpSender::SendRequest(
// req.add(c_itr->first, (c_itr->second).c_str());
req.add(c_itr->first, c_itr->second);
}
req.add("Content-Length", StringUtil::Uint64ToString(req_body.size()));

std::ostringstream debug_os;
req.write(debug_os);
Expand Down
12 changes: 12 additions & 0 deletions src/util/string_util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -273,4 +273,16 @@ uint16_t StringUtil::GetUint16FromStrWithBigEndian(const char* str) {
num |= tmp;
return num;
}

size_t StringUtil::GetLengthFromIStream(std::istream& is) {
std::streampos pos = is.tellg();
is.seekg(0, std::ios::end);
std::streampos size = is.tellg();
is.seekg(pos);
if (size == std::streampos(-1)) {
return 0;
}

return static_cast<size_t>(size);
}
} // namespace qcloud_cos
2 changes: 1 addition & 1 deletion unittest/src/async_op_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class AsyncOpTest : public testing::Test {
}
m_client = new CosAPI(*m_config);

m_bucket_name = "coscppsdkv5ut" + GetEnvVar("COS_CPP_V5_TAG") + "-" +
m_bucket_name = "coscppsdkv5ut-async" + GetEnvVar("COS_CPP_V5_TAG") + "-" +
GetEnvVar("CPP_SDK_V5_APPID");
{
PutBucketReq req(m_bucket_name);
Expand Down
14 changes: 7 additions & 7 deletions unittest/src/bucket_op_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -198,13 +198,13 @@ TEST_F(BucketOpTest, GetServiceTest) {

TEST_F(BucketOpTest, SetCredentailTest) {
{
qcloud_cos::CosConfig config(00000,"secretId","secretKey",GetEnvVar("CPP_SDK_V5_REGION"));
qcloud_cos::CosConfig config(00000,"secretId","secretKey",GetEnvVar("CPP_SDK_V5_OTHER_REGION"));
qcloud_cos::CosAPI cos(config);
cos.SetCredentail(GetEnvVar("CPP_SDK_V5_ACCESS_KEY"),GetEnvVar("CPP_SDK_V5_SECRET_KEY"),"");
EXPECT_EQ(config.GetAccessKey(), "secretId");
EXPECT_EQ(config.GetAppId(), 00000);
EXPECT_EQ(config.GetSecretKey(), "secretKey");
EXPECT_EQ(config.GetRegion(), GetEnvVar("CPP_SDK_V5_REGION"));
EXPECT_EQ(config.GetRegion(), GetEnvVar("CPP_SDK_V5_OTHER_REGION"));
bool result = cos.IsBucketExist(m_bucket_name2);
EXPECT_TRUE(result);
}
Expand Down Expand Up @@ -312,13 +312,13 @@ TEST_F(BucketOpTest, GetBucketLocationTest) {
// normal CPP_SDK_V5_REGION
{
std::string location = m_client2->GetBucketLocation(m_bucket_name2);
EXPECT_EQ(location,GetEnvVar("CPP_SDK_V5_REGION"));
EXPECT_EQ(location, GetEnvVar("CPP_SDK_V5_OTHER_REGION"));
}

// wrong ""
{
std::string location = m_client2->GetBucketLocation(m_bucket_name_wrong);
EXPECT_EQ(location,"");
EXPECT_EQ(location, "");
}
}

Expand Down Expand Up @@ -1447,10 +1447,10 @@ TEST_F(BucketOpTest, BucketReferer) {
}

TEST_F(BucketOpTest, InvalidConfig) {
qcloud_cos::CosConfig config(123, "ak", "sk", "");
ASSERT_TRUE(config.GetRegion().empty());
qcloud_cos::CosConfig config(123, "ak", "", "ap-guangzhou");
ASSERT_FALSE(config.GetRegion().empty());
qcloud_cos::CosAPI cos(config);
HeadBucketReq req("test_bucket");
HeadBucketReq req("test-bucket-1253960454");
HeadBucketResp resp;
CosResult result = cos.HeadBucket(req, &resp);
ASSERT_TRUE(!result.IsSucc());
Expand Down
Loading