Skip to content

Commit

Permalink
Merge pull request #115 from chen-honggang/dev/uri-exception
Browse files Browse the repository at this point in the history
DestDomain支持按实例单独配置
  • Loading branch information
chen-honggang authored Jul 28, 2022
2 parents 1aab4b6 + a48f0f4 commit 7d76677
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 12 deletions.
10 changes: 10 additions & 0 deletions include/cos_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class CosConfig {
m_set_intranet_once(false),
m_is_use_intranet(false),
m_intranet_addr(""),
m_dest_domain(""),
m_config_parsed(false) {}

/// \brief CosConfig构造函数
Expand All @@ -46,6 +47,7 @@ class CosConfig {
m_set_intranet_once(false),
m_is_use_intranet(false),
m_intranet_addr(""),
m_dest_domain(""),
m_config_parsed(false) {}

/// \brief CosConfig构造函数
Expand All @@ -65,6 +67,7 @@ class CosConfig {
m_set_intranet_once(false),
m_is_use_intranet(false),
m_intranet_addr(""),
m_dest_domain(""),
m_config_parsed(false) {}

/// \brief CosConfig复制构造函数
Expand All @@ -79,6 +82,7 @@ class CosConfig {
m_set_intranet_once = config.m_set_intranet_once;
m_is_use_intranet = config.m_is_use_intranet;
m_intranet_addr = config.m_intranet_addr;
m_dest_domain = config.m_dest_domain;
m_config_parsed = config.m_config_parsed;
}

Expand All @@ -94,6 +98,7 @@ class CosConfig {
m_set_intranet_once = config.m_set_intranet_once;
m_is_use_intranet = config.m_is_use_intranet;
m_intranet_addr = config.m_intranet_addr;
m_dest_domain = config.m_dest_domain;
m_config_parsed = config.m_config_parsed;
return *this;
}
Expand Down Expand Up @@ -166,6 +171,10 @@ class CosConfig {

bool GetSetIntranetOnce() const {return m_set_intranet_once;}

void SetDestDomain(const std::string& domain);

const std::string& GetDestDomain() const;

/// \brief 设置日志回调
void SetLogCallback(const LogCallback log_callback);

Expand All @@ -189,6 +198,7 @@ class CosConfig {
bool m_set_intranet_once;
bool m_is_use_intranet;
std::string m_intranet_addr;
std::string m_dest_domain;
bool m_config_parsed;
};

Expand Down
2 changes: 1 addition & 1 deletion include/cos_defines.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

namespace qcloud_cos {

#define COS_CPP_SDK_VERSON "v5.5.7"
#define COS_CPP_SDK_VERSON "v5.5.8"

/// 路径分隔符
const std::string kPathDelimiter = "/";
Expand Down
2 changes: 2 additions & 0 deletions include/op/base_op.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ class BaseOp {
/// \brief 获取Token
std::string GetTmpToken() const;

std::string GetDestDomain() const;

/// \brief 封装了cos Service/Bucket/Object 相关接口的通用操作,
/// 包括签名计算、请求发送、返回内容解析等
///
Expand Down
3 changes: 2 additions & 1 deletion src/cos_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,8 @@ std::string CosAPI::GetObjectUrl(const std::string& bucket,
} else {
object_url = "http://";
}
std::string destdomain = CosSysConfig::GetDestDomain();
std::string destdomain = m_config->GetDestDomain().empty() ?
CosSysConfig::GetDestDomain() : m_config->GetDestDomain();
if (!destdomain.empty()) {
object_url += destdomain;
} else {
Expand Down
13 changes: 13 additions & 0 deletions src/cos_config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ CosConfig::CosConfig(const std::string& config_file)
m_set_intranet_once(false),
m_is_use_intranet(false),
m_intranet_addr(""),
m_dest_domain(""),
m_config_parsed(false) {
if (InitConf(config_file)) {
m_config_parsed = true;
Expand Down Expand Up @@ -183,6 +184,7 @@ bool CosConfig::InitConf(const std::string& config_file) {
std::string str_value;
if (JsonObjectGetStringValue(object, "DestDomain", &str_value)) {
CosSysConfig::SetDestDomain(str_value);
m_dest_domain = str_value;
}

if (JsonObjectGetBoolValue(object, "IsDomainSameToHost", &bool_value)) {
Expand Down Expand Up @@ -258,6 +260,17 @@ std::string CosConfig::GetIntranetAddr() {
return m_intranet_addr;
}

void CosConfig::SetDestDomain(const std::string& domain)
{
CosSysConfig::SetDestDomain(domain);
m_dest_domain = domain;
}

const std::string& CosConfig::GetDestDomain() const
{
return m_dest_domain;
}

void CosConfig::SetLogCallback(const LogCallback log_callback) {
CosSysConfig::SetLogCallback(log_callback);
}
Expand Down
7 changes: 6 additions & 1 deletion src/cos_sys_config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ unsigned CosSysConfig::m_dns_cache_expire_seconds = 600;
unsigned CosSysConfig::m_dns_cache_size = 1000;

std::mutex m_intranet_addr_lock;
std::mutex m_dest_domain_lock;

void CosSysConfig::PrintValue() {
std::cout << "upload_part_size:" << m_upload_part_size << std::endl;
Expand Down Expand Up @@ -118,6 +119,7 @@ void CosSysConfig::SetDownSliceSize(unsigned slice_size) {
}

void CosSysConfig::SetDestDomain(const std::string& dest_domain) {
std::lock_guard<std::mutex> lock(m_dest_domain_lock);
m_dest_domain = dest_domain;
}

Expand Down Expand Up @@ -249,7 +251,10 @@ std::string CosSysConfig::GetCIHost(const std::string& bucket_name,
return host;
}

std::string CosSysConfig::GetDestDomain() { return m_dest_domain; }
std::string CosSysConfig::GetDestDomain() {
std::lock_guard<std::mutex> lock(m_dest_domain_lock);
return m_dest_domain;
}

std::string CosSysConfig::GetIntranetAddr() {
std::lock_guard<std::mutex> lock(m_intranet_addr_lock);
Expand Down
13 changes: 10 additions & 3 deletions src/op/base_op.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ std::string BaseOp::GetSecretKey() const { return m_config->GetSecretKey(); }

std::string BaseOp::GetTmpToken() const { return m_config->GetTmpToken(); }

std::string BaseOp::GetDestDomain() const {
return m_config->GetDestDomain().empty() ?
CosSysConfig::GetDestDomain() : m_config->GetDestDomain();
}

CosResult BaseOp::NormalAction(const std::string& host, const std::string& path,
const BaseReq& req, const std::string& req_body,
bool check_body, BaseResp* resp) {
Expand Down Expand Up @@ -76,7 +81,7 @@ CosResult BaseOp::NormalAction(
if (!CosSysConfig::IsDomainSameToHost()) {
req_headers["Host"] = host;
} else {
req_headers["Host"] = CosSysConfig::GetDestDomain();
req_headers["Host"] = GetDestDomain();
}

// 2. 计算签名
Expand Down Expand Up @@ -156,7 +161,7 @@ CosResult BaseOp::DownloadAction(const std::string& host,
if (!CosSysConfig::IsDomainSameToHost()) {
req_headers["Host"] = host;
} else {
req_headers["Host"] = CosSysConfig::GetDestDomain();
req_headers["Host"] = GetDestDomain();
}

// 2. 计算签名
Expand Down Expand Up @@ -242,7 +247,7 @@ CosResult BaseOp::UploadAction(
if (!CosSysConfig::IsDomainSameToHost()) {
req_headers["Host"] = host;
} else {
req_headers["Host"] = CosSysConfig::GetDestDomain();
req_headers["Host"] = GetDestDomain();
}

// 2. 计算签名
Expand Down Expand Up @@ -312,6 +317,8 @@ std::string BaseOp::GetRealUrl(const std::string& host, const std::string& path,
} else if (CosSysConfig::IsUseIntranet() &&
!CosSysConfig::GetIntranetAddr().empty()) {
dest_host = CosSysConfig::GetIntranetAddr();
} else if (!m_config->GetDestDomain().empty()) {
dest_host = m_config->GetDestDomain();
} else if (!CosSysConfig::GetDestDomain().empty()) {
dest_host = CosSysConfig::GetDestDomain();
} else if (CosSysConfig::GetUseDnsCache()) {
Expand Down
12 changes: 6 additions & 6 deletions src/op/object_op.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1151,7 +1151,7 @@ ObjectOp::MultiThreadDownload(const GetObjectByFileReq& req,
if (!CosSysConfig::IsDomainSameToHost()) {
headers["Host"] = host;
} else {
headers["Host"] = CosSysConfig::GetDestDomain();
headers["Host"] = GetDestDomain();
}

const std::string& tmp_token = m_config->GetTmpToken();
Expand Down Expand Up @@ -1599,7 +1599,7 @@ void ObjectOp::FillUploadTask(const std::string& upload_id,
if (!CosSysConfig::IsDomainSameToHost()) {
req_headers["Host"] = host;
} else {
req_headers["Host"] = CosSysConfig::GetDestDomain();
req_headers["Host"] = GetDestDomain();
}
std::string auth_str = AuthTool::Sign(GetAccessKey(), GetSecretKey(), "PUT",
path, req_headers, req_params);
Expand Down Expand Up @@ -1630,7 +1630,7 @@ void ObjectOp::FillCopyTask(const std::string& upload_id,
if (!CosSysConfig::IsDomainSameToHost()) {
req_headers["Host"] = host;
} else {
req_headers["Host"] = CosSysConfig::GetDestDomain();
req_headers["Host"] = GetDestDomain();
}

req_headers["x-cos-copy-source-range"] = range;
Expand All @@ -1651,8 +1651,8 @@ std::string ObjectOp::GeneratePresignedUrl(const GeneratePresignedUrlReq& req) {
std::string auth_str = "";

std::string host;
if (!CosSysConfig::GetDestDomain().empty()) {
host = CosSysConfig::GetDestDomain();
if (!m_config->GetDestDomain().empty() || !CosSysConfig::GetDestDomain().empty()) {
host = GetDestDomain();
} else {
host = CosSysConfig::GetHost(GetAppId(), m_config->GetRegion(),
req.GetBucketName());
Expand Down Expand Up @@ -1885,7 +1885,7 @@ CosResult ObjectOp::ResumableGetObject(const GetObjectByFileReq& req,
if (!CosSysConfig::IsDomainSameToHost()) {
headers["Host"] = host;
} else {
headers["Host"] = CosSysConfig::GetDestDomain();
headers["Host"] = GetDestDomain();
}

const std::string& tmp_token = m_config->GetTmpToken();
Expand Down

0 comments on commit 7d76677

Please sign in to comment.