diff --git a/include/cos_config.h b/include/cos_config.h index 2679cfa..b52f172 100644 --- a/include/cos_config.h +++ b/include/cos_config.h @@ -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构造函数 @@ -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构造函数 @@ -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复制构造函数 @@ -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; } @@ -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; } @@ -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); @@ -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; }; diff --git a/include/cos_defines.h b/include/cos_defines.h index 9479e78..c3d46ae 100644 --- a/include/cos_defines.h +++ b/include/cos_defines.h @@ -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 = "/"; diff --git a/include/op/base_op.h b/include/op/base_op.h index 125b57f..0da31a8 100644 --- a/include/op/base_op.h +++ b/include/op/base_op.h @@ -45,6 +45,8 @@ class BaseOp { /// \brief 获取Token std::string GetTmpToken() const; + std::string GetDestDomain() const; + /// \brief 封装了cos Service/Bucket/Object 相关接口的通用操作, /// 包括签名计算、请求发送、返回内容解析等 /// diff --git a/src/cos_api.cpp b/src/cos_api.cpp index 0bd4851..2caa9a2 100644 --- a/src/cos_api.cpp +++ b/src/cos_api.cpp @@ -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 { diff --git a/src/cos_config.cpp b/src/cos_config.cpp index fae2a4a..fcfa723 100644 --- a/src/cos_config.cpp +++ b/src/cos_config.cpp @@ -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; @@ -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)) { @@ -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); } diff --git a/src/cos_sys_config.cpp b/src/cos_sys_config.cpp index 6e7fcd7..0729a94 100644 --- a/src/cos_sys_config.cpp +++ b/src/cos_sys_config.cpp @@ -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; @@ -118,6 +119,7 @@ void CosSysConfig::SetDownSliceSize(unsigned slice_size) { } void CosSysConfig::SetDestDomain(const std::string& dest_domain) { + std::lock_guard lock(m_dest_domain_lock); m_dest_domain = dest_domain; } @@ -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 lock(m_dest_domain_lock); + return m_dest_domain; +} std::string CosSysConfig::GetIntranetAddr() { std::lock_guard lock(m_intranet_addr_lock); diff --git a/src/op/base_op.cpp b/src/op/base_op.cpp index 143ec32..058f50f 100644 --- a/src/op/base_op.cpp +++ b/src/op/base_op.cpp @@ -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) { @@ -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. 计算签名 @@ -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. 计算签名 @@ -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. 计算签名 @@ -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()) { diff --git a/src/op/object_op.cpp b/src/op/object_op.cpp index 0ba715e..bf80bfa 100644 --- a/src/op/object_op.cpp +++ b/src/op/object_op.cpp @@ -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(); @@ -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); @@ -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; @@ -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()); @@ -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();