diff --git a/demo/CMakeLists.txt b/demo/CMakeLists.txt index f82b52e..ceb424c 100644 --- a/demo/CMakeLists.txt +++ b/demo/CMakeLists.txt @@ -16,6 +16,10 @@ file(GLOB copy_move_object_demo_src "${CMAKE_SOURCE_DIR}/demo/object_op_demo/cop file(GLOB multi_put_object_demo_src "${CMAKE_SOURCE_DIR}/demo/object_op_demo/multi_put_object_demo.cpp") file(GLOB multi_get_object_demo_src "${CMAKE_SOURCE_DIR}/demo/object_op_demo/multi_get_object_demo.cpp") file(GLOB select_objec_demo_src "${CMAKE_SOURCE_DIR}/demo/object_op_demo/select_objec_demo.cpp") +file(GLOB get_bucket_list_demo_src "${CMAKE_SOURCE_DIR}/demo/bucket_op_demo/get_bucket_list_demo.cpp") +file(GLOB delete_bucket_demo_src "${CMAKE_SOURCE_DIR}/demo/bucket_op_demo/delete_bucket_demo.cpp") +file(GLOB head_bucket_demo_src "${CMAKE_SOURCE_DIR}/demo/bucket_op_demo/head_bucket_demo.cpp") +file(GLOB put_bucket_demo_src "${CMAKE_SOURCE_DIR}/demo/bucket_op_demo/put_bucket_demo.cpp") link_directories(${POCO_LINK_DIR} ${OPENSSL_LINK_DIR}) #这一行要放到add_executable前面 @@ -32,6 +36,10 @@ add_executable(copy_move_object_demo ${copy_move_object_demo_src}) add_executable(multi_put_object_demo ${multi_put_object_demo_src}) add_executable(multi_get_object_demo ${multi_get_object_demo_src}) add_executable(select_objec_demo ${select_objec_demo_src}) +add_executable(get_bucket_list_demo ${get_bucket_list_demo_src}) +add_executable(delete_bucket_demo ${delete_bucket_demo_src}) +add_executable(head_bucket_demo ${head_bucket_demo_src}) +add_executable(put_bucket_demo ${put_bucket_demo_src}) set(EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR}/bin) @@ -48,6 +56,10 @@ target_link_libraries(copy_move_object_demo cossdk ssl crypto ${POCO_LIBS} ${OPE target_link_libraries(multi_put_object_demo cossdk ssl crypto ${POCO_LIBS} ${OPENSSL_LIBS} ${SYSTEM_LIBS}) target_link_libraries(multi_get_object_demo cossdk ssl crypto ${POCO_LIBS} ${OPENSSL_LIBS} ${SYSTEM_LIBS}) target_link_libraries(select_objec_demo ssl crypto cossdk ${POCO_LIBS} ${OPENSSL_LIBS} ${SYSTEM_LIBS}) +target_link_libraries(get_bucket_list_demo ssl crypto cossdk ${POCO_LIBS} ${OPENSSL_LIBS} ${SYSTEM_LIBS}) +target_link_libraries(delete_bucket_demo ssl crypto cossdk ${POCO_LIBS} ${OPENSSL_LIBS} ${SYSTEM_LIBS}) +target_link_libraries(head_bucket_demo ssl crypto cossdk ${POCO_LIBS} ${OPENSSL_LIBS} ${SYSTEM_LIBS}) +target_link_libraries(put_bucket_demo ssl crypto cossdk ${POCO_LIBS} ${OPENSSL_LIBS} ${SYSTEM_LIBS}) include_directories(${CMAKE_SOURCE_DIR}/include/ ${POCO_INCLUDE_DIR}) diff --git a/demo/bucket_op_demo/delete_bucket_demo.cpp b/demo/bucket_op_demo/delete_bucket_demo.cpp new file mode 100644 index 0000000..0d2bbfe --- /dev/null +++ b/demo/bucket_op_demo/delete_bucket_demo.cpp @@ -0,0 +1,73 @@ + +#include +#include + +#include +#include +#include +#include +#include + +#include "cos_api.h" +#include "cos_sys_config.h" +#include "util/auth_tool.h" + +/** + * 本样例演示了如何使用 COS C++ SDK 进行存储桶的删除 + */ +using namespace qcloud_cos; + +uint64_t appid = 12500000000; +std::string tmp_secret_id = "AKIDXXXXXXXX"; +std::string tmp_secret_key = "1A2Z3YYYYYYYYYY"; +std::string region = "ap-guangzhou"; +std::string bucket_name = "examplebucket-12500000000"; +std::string tmp_token = "token"; + +/* + * 本方法包含调用是否正常的判断,和请求结果的输出 + * 可通过本方法判断是否请求成功,并输出结果信息 + */ +void PrintResult(const qcloud_cos::CosResult& result, const qcloud_cos::BaseResp& resp) { + if (result.IsSucc()) { + std::cout << "Request Succ." << std::endl; + std::cout << resp.DebugString() << std::endl; + } else { + std::cout << "ErrorMsg=" << result.GetErrorMsg() << std::endl; + std::cout << "HttpStatus=" << result.GetHttpStatus() << std::endl; + std::cout << "ErrorCode=" << result.GetErrorCode() << std::endl; + std::cout << "ErrorMsg=" << result.GetErrorMsg() << std::endl; + std::cout << "ResourceAddr=" << result.GetResourceAddr() << std::endl; + std::cout << "XCosRequestId=" << result.GetXCosRequestId() << std::endl; + std::cout << "XCosTraceId=" << result.GetXCosTraceId() << std::endl; + } +} + +/* + * 通过参数形式初始化 CosAPI 对象 + */ +qcloud_cos::CosAPI InitCosAPI() { + qcloud_cos::CosConfig config(appid, tmp_secret_id, tmp_secret_key, region); + config.SetTmpToken(tmp_token); // 推荐使用临时密钥初始化 CosAPI 对象, 如果您使用永久密钥初始化 CosAPI 对象,请注释 + qcloud_cos::CosAPI cos_tmp(config); + return cos_tmp; +} + +void DeleteBucket(qcloud_cos::CosAPI& cos) { + qcloud_cos::DeleteBucketReq req(bucket_name); + qcloud_cos::DeleteBucketResp resp; + qcloud_cos::CosResult result = cos.DeleteBucket(req, &resp); + + std::cout << "===================DeleteBucketResponse=====================" + << std::endl; + PrintResult(result, resp); + std::cout << "=========================================================" + << std::endl; +} + + +int main() { + qcloud_cos::CosAPI cos = InitCosAPI(); + CosSysConfig::SetLogLevel((LOG_LEVEL)COS_LOG_ERR); + DeleteBucket(cos); +} \ No newline at end of file diff --git a/demo/bucket_op_demo/get_bucket_list_demo.cpp b/demo/bucket_op_demo/get_bucket_list_demo.cpp new file mode 100644 index 0000000..7e06fbc --- /dev/null +++ b/demo/bucket_op_demo/get_bucket_list_demo.cpp @@ -0,0 +1,80 @@ + +#include +#include + +#include +#include +#include +#include +#include + +#include "cos_api.h" +#include "cos_sys_config.h" +#include "util/auth_tool.h" + +/** + * 本样例演示了如何使用 COS C++ SDK 进行存储桶列表的获取 + */ +using namespace qcloud_cos; + +uint64_t appid = 12500000000; +std::string tmp_secret_id = "AKIDXXXXXXXX"; +std::string tmp_secret_key = "1A2Z3YYYYYYYYYY"; +std::string region = "ap-guangzhou"; +std::string tmp_token = "token"; + +/* + * 本方法包含调用是否正常的判断,和请求结果的输出 + * 可通过本方法判断是否请求成功,并输出结果信息 + */ +void PrintResult(const qcloud_cos::CosResult& result, const qcloud_cos::BaseResp& resp) { + if (result.IsSucc()) { + std::cout << "Request Succ." << std::endl; + std::cout << resp.DebugString() << std::endl; + } else { + std::cout << "ErrorMsg=" << result.GetErrorMsg() << std::endl; + std::cout << "HttpStatus=" << result.GetHttpStatus() << std::endl; + std::cout << "ErrorCode=" << result.GetErrorCode() << std::endl; + std::cout << "ErrorMsg=" << result.GetErrorMsg() << std::endl; + std::cout << "ResourceAddr=" << result.GetResourceAddr() << std::endl; + std::cout << "XCosRequestId=" << result.GetXCosRequestId() << std::endl; + std::cout << "XCosTraceId=" << result.GetXCosTraceId() << std::endl; + } +} + +/* + * 通过参数形式初始化 CosAPI 对象 + */ +qcloud_cos::CosAPI InitCosAPI() { + qcloud_cos::CosConfig config(appid, tmp_secret_id, tmp_secret_key, region); + config.SetTmpToken(tmp_token); // 推荐使用临时密钥初始化 CosAPI 对象, 如果您使用永久密钥初始化 CosAPI 对象,请注释 + qcloud_cos::CosAPI cos_tmp(config); + return cos_tmp; +} + +void GetService(qcloud_cos::CosAPI& cos) { + qcloud_cos::GetServiceReq req; + qcloud_cos::GetServiceResp resp; + qcloud_cos::CosResult result = cos.GetService(req, &resp); + + std::cout << "===================GetService=====================" + << std::endl; + PrintResult(result, resp); + const qcloud_cos::Owner& owner = resp.GetOwner(); + const std::vector& buckets = resp.GetBuckets(); + std::cout << "owner.m_id=" << owner.m_id << ", owner.display_name=" << owner.m_display_name << std::endl; + for (std::vector::const_iterator itr = buckets.begin(); + itr != buckets.end(); ++itr) { + const qcloud_cos::Bucket& bucket = *itr; + std::cout << "Bucket name=" << bucket.m_name + << ", location=" << bucket.m_location + << ", create_date=" << bucket.m_create_date << std::endl; + } + std::cout << "=========================================================" << std::endl; +} + +int main() { + qcloud_cos::CosAPI cos = InitCosAPI(); + CosSysConfig::SetLogLevel((LOG_LEVEL)COS_LOG_ERR); + GetService(cos); +} \ No newline at end of file diff --git a/demo/bucket_op_demo/head_bucket_demo.cpp b/demo/bucket_op_demo/head_bucket_demo.cpp new file mode 100644 index 0000000..a0db173 --- /dev/null +++ b/demo/bucket_op_demo/head_bucket_demo.cpp @@ -0,0 +1,84 @@ + +#include +#include + +#include +#include +#include +#include +#include + +#include "cos_api.h" +#include "cos_sys_config.h" +#include "util/auth_tool.h" + +/** + * 本样例演示了如何使用 COS C++ SDK 进行存储桶检索和判断是否存在 + */ +using namespace qcloud_cos; + +uint64_t appid = 12500000000; +std::string tmp_secret_id = "AKIDXXXXXXXX"; +std::string tmp_secret_key = "1A2Z3YYYYYYYYYY"; +std::string region = "ap-guangzhou"; +std::string bucket_name = "examplebucket-12500000000"; +std::string tmp_token = "token"; + +/* + * 本方法包含调用是否正常的判断,和请求结果的输出 + * 可通过本方法判断是否请求成功,并输出结果信息 + */ +void PrintResult(const qcloud_cos::CosResult& result, const qcloud_cos::BaseResp& resp) { + if (result.IsSucc()) { + std::cout << "Request Succ." << std::endl; + std::cout << resp.DebugString() << std::endl; + } else { + std::cout << "ErrorMsg=" << result.GetErrorMsg() << std::endl; + std::cout << "HttpStatus=" << result.GetHttpStatus() << std::endl; + std::cout << "ErrorCode=" << result.GetErrorCode() << std::endl; + std::cout << "ErrorMsg=" << result.GetErrorMsg() << std::endl; + std::cout << "ResourceAddr=" << result.GetResourceAddr() << std::endl; + std::cout << "XCosRequestId=" << result.GetXCosRequestId() << std::endl; + std::cout << "XCosTraceId=" << result.GetXCosTraceId() << std::endl; + } +} + +/* + * 通过参数形式初始化 CosAPI 对象 + */ +qcloud_cos::CosAPI InitCosAPI() { + qcloud_cos::CosConfig config(appid, tmp_secret_id, tmp_secret_key, region); + config.SetTmpToken(tmp_token); // 推荐使用临时密钥初始化 CosAPI 对象, 如果您使用永久密钥初始化 CosAPI 对象,请注释 + qcloud_cos::CosAPI cos_tmp(config); + return cos_tmp; +} + +void HeadBucket(qcloud_cos::CosAPI& cos) { + qcloud_cos::HeadBucketReq req(bucket_name); + qcloud_cos::HeadBucketResp resp; + qcloud_cos::CosResult result = cos.HeadBucket(req, &resp); + + std::cout << "===================HeadBucketResponse=====================" + << std::endl; + PrintResult(result, resp); + std::cout << "==========================================================" + << std::endl; +} + +void IsBucketExist(qcloud_cos::CosAPI& cos) { + std::cout << "===================IsBucketExist=====================" + << std::endl; + std::cout << (cos.IsBucketExist("abcdefg") ? "true" : "false") << std::endl; + std::cout << (cos.IsBucketExist(bucket_name) ? "true" : "false") << std::endl; + std::cout + << "====================================================================" + << std::endl; +} + + +int main() { + qcloud_cos::CosAPI cos = InitCosAPI(); + CosSysConfig::SetLogLevel((LOG_LEVEL)COS_LOG_ERR); + HeadBucket(cos); + IsBucketExist(cos); +} \ No newline at end of file diff --git a/demo/bucket_op_demo/put_bucket_demo.cpp b/demo/bucket_op_demo/put_bucket_demo.cpp new file mode 100644 index 0000000..0cfd66e --- /dev/null +++ b/demo/bucket_op_demo/put_bucket_demo.cpp @@ -0,0 +1,85 @@ + +#include +#include + +#include +#include +#include +#include +#include + +#include "cos_api.h" +#include "cos_sys_config.h" +#include "util/auth_tool.h" + +/** + * 本样例演示了如何使用 COS C++ SDK 进行存储桶的创建 + */ +using namespace qcloud_cos; + +uint64_t appid = 12500000000; +std::string tmp_secret_id = "AKIDXXXXXXXX"; +std::string tmp_secret_key = "1A2Z3YYYYYYYYYY"; +std::string region = "ap-guangzhou@xxx"; +std::string bucket_name = "examplebucket-12500000000"; +std::string tmp_token = "token"; + +/* + * 本方法包含调用是否正常的判断,和请求结果的输出 + * 可通过本方法判断是否请求成功,并输出结果信息 + */ +void PrintResult(const qcloud_cos::CosResult& result, const qcloud_cos::BaseResp& resp) { + if (result.IsSucc()) { + std::cout << "Request Succ." << std::endl; + std::cout << resp.DebugString() << std::endl; + } else { + std::cout << "ErrorMsg=" << result.GetErrorMsg() << std::endl; + std::cout << "HttpStatus=" << result.GetHttpStatus() << std::endl; + std::cout << "ErrorCode=" << result.GetErrorCode() << std::endl; + std::cout << "ErrorMsg=" << result.GetErrorMsg() << std::endl; + std::cout << "ResourceAddr=" << result.GetResourceAddr() << std::endl; + std::cout << "XCosRequestId=" << result.GetXCosRequestId() << std::endl; + std::cout << "XCosTraceId=" << result.GetXCosTraceId() << std::endl; + } +} + +/* + * 通过参数形式初始化 CosAPI 对象 + */ +qcloud_cos::CosAPI InitCosAPI() { + qcloud_cos::CosConfig config(appid, tmp_secret_id, tmp_secret_key, region); + config.SetTmpToken(tmp_token); // 推荐使用临时密钥初始化 CosAPI 对象, 如果您使用永久密钥初始化 CosAPI 对象,请注释 + qcloud_cos::CosAPI cos_tmp(config); + return cos_tmp; +} + +void PutBucket(qcloud_cos::CosAPI& cos) { + qcloud_cos::PutBucketReq req(bucket_name); + //创建MAZ存储桶使用下方进行设置 + //req.SetMAZBucket(); + qcloud_cos::PutBucketResp resp; + qcloud_cos::CosResult result = cos.PutBucket(req, &resp); + + std::cout << "===================PutBucketResponse=====================" + << std::endl; + PrintResult(result, resp); + std::cout << "=========================================================" + << std::endl; +} + + +int main() { + try{ + qcloud_cos::CosAPI cos = InitCosAPI(); + CosSysConfig::SetLogLevel((LOG_LEVEL)COS_LOG_ERR); + + PutBucket(cos); + } + catch(const std::exception& e){ + std::cerr << e.what() << "]]" << '\n'; + + std::cout << (strcmp(e.what(),"Invalid region configuration in CosConfig :ap-guangzhou@xxx") == 0) << std::endl; + } + std::cout << "Hello, World!" << std::endl; + +} \ No newline at end of file diff --git a/include/cos_config.h b/include/cos_config.h index 7de1d98..1313b44 100644 --- a/include/cos_config.h +++ b/include/cos_config.h @@ -191,6 +191,8 @@ class CosConfig { bool IsDomainSameToHostEnable() const; + bool CheckRegion(); + // void SetDomainSameToHostEnable(bool is_domain_same_to_host_enable); /// \brief 设置日志回调 diff --git a/include/request/bucket_req.h b/include/request/bucket_req.h index 35a3436..40935aa 100644 --- a/include/request/bucket_req.h +++ b/include/request/bucket_req.h @@ -12,18 +12,26 @@ #include "cos_defines.h" #include "request/base_req.h" #include "util/string_util.h" +#include "util/illegal_intercept.h" namespace qcloud_cos { class BucketReq : public BaseReq { public: BucketReq() : m_bucket_name("") {} - explicit BucketReq(const std::string& bucket_name) : m_bucket_name(bucket_name) {} + explicit BucketReq(const std::string& bucket_name) : m_bucket_name(bucket_name) { + if (!IllegalIntercept::CheckBucket(bucket_name)){ + throw std::invalid_argument("Invalid bucket_name argument :" + bucket_name); + } + } virtual ~BucketReq() {} // getter and setter std::string GetBucketName() const { return m_bucket_name; } void SetBucketName(const std::string& bucket_name) { + if (!IllegalIntercept::CheckBucket(bucket_name)){ + throw std::invalid_argument("Invalid bucket_name argument :" + bucket_name); + } m_bucket_name = bucket_name; } virtual bool GenerateRequestBody(std::string* body) const { UNUSED_PARAM(body); return true; } diff --git a/include/request/object_req.h b/include/request/object_req.h index 4c05894..8904079 100644 --- a/include/request/object_req.h +++ b/include/request/object_req.h @@ -16,6 +16,7 @@ #include "request/base_req.h" #include "trsf/async_context.h" #include "util/file_util.h" +#include "util/illegal_intercept.h" namespace qcloud_cos { @@ -24,6 +25,9 @@ class ObjectReq : public BaseReq { ObjectReq(const std::string& bucket_name, const std::string& object_name) : m_bucket_name(bucket_name), m_progress_cb(NULL), m_done_cb(NULL), m_user_data(NULL) { + if (!IllegalIntercept::CheckBucket(bucket_name)){ + throw std::invalid_argument("Invalid bucket_name argument :" + bucket_name); + } SetObjectName(object_name); } @@ -34,6 +38,9 @@ class ObjectReq : public BaseReq { // getter and setter std::string GetBucketName() const { return m_bucket_name; } void SetBucketName(const std::string& bucket_name) { + if (!IllegalIntercept::CheckBucket(bucket_name)){ + throw std::invalid_argument("Invalid bucket_name argument :" + bucket_name); + } m_bucket_name = bucket_name; } std::string GetObjectName() const { return m_object_name; } @@ -433,6 +440,9 @@ class DeleteObjectsReq : public BaseReq { public: explicit DeleteObjectsReq(const std::string& bucket_name) : m_is_quiet(false), m_bucket_name(bucket_name) { + if (!IllegalIntercept::CheckBucket(bucket_name)){ + throw std::invalid_argument("Invalid bucket_name argument :" + bucket_name); + } SetMethod("POST"); AddParam("delete", ""); } @@ -440,6 +450,9 @@ class DeleteObjectsReq : public BaseReq { DeleteObjectsReq(const std::string& bucket_name, const std::vector& objects) : m_is_quiet(false), m_bucket_name(bucket_name) { + if (!IllegalIntercept::CheckBucket(bucket_name)){ + throw std::invalid_argument("Invalid bucket_name argument :" + bucket_name); + } SetMethod("POST"); AddParam("delete", ""); m_objvers = objects; @@ -455,6 +468,9 @@ class DeleteObjectsReq : public BaseReq { std::string GetBucketName() const { return m_bucket_name; } void SetBucketName(const std::string& bucket_name) { + if (!IllegalIntercept::CheckBucket(bucket_name)){ + throw std::invalid_argument("Invalid bucket_name argument :" + bucket_name); + } m_bucket_name = bucket_name; } @@ -1810,6 +1826,9 @@ class PutObjectsByDirectoryReq : public PutObjectReq { const std::string& directory_name) : m_bucket_name(bucket_name), m_directory_name(directory_name), m_cos_path("") { + if (!IllegalIntercept::CheckBucket(bucket_name)){ + throw std::invalid_argument("Invalid bucket_name argument :" + bucket_name); + } m_need_compute_contentmd5 = true; // 默认打开 } @@ -1818,6 +1837,9 @@ class PutObjectsByDirectoryReq : public PutObjectReq { const std::string& cos_path) : m_bucket_name(bucket_name), m_directory_name(directory_name), m_cos_path(cos_path) { + if (!IllegalIntercept::CheckBucket(bucket_name)){ + throw std::invalid_argument("Invalid bucket_name argument :" + bucket_name); + } m_need_compute_contentmd5 = true; // 默认打开 } @@ -1861,7 +1883,11 @@ class MoveObjectReq : public ObjectReq { const std::string& src_object_name, const std::string& dst_object_name) : m_bucket_name(bucket_name), m_src_object_name(src_object_name), - m_dst_object_name(dst_object_name) {} + m_dst_object_name(dst_object_name) { + if (!IllegalIntercept::CheckBucket(bucket_name)){ + throw std::invalid_argument("Invalid bucket_name argument :" + bucket_name); + } + } virtual ~MoveObjectReq() {} std::string GetBucketName() const { return m_bucket_name; } @@ -1880,7 +1906,11 @@ class DeleteObjectsByPrefixReq { public: DeleteObjectsByPrefixReq(const std::string& bucket_name, const std::string& prefix) - : m_bucket_name(bucket_name), m_prefix(prefix) {} + : m_bucket_name(bucket_name), m_prefix(prefix) { + if (!IllegalIntercept::CheckBucket(bucket_name)){ + throw std::invalid_argument("Invalid bucket_name argument :" + bucket_name); + } + } virtual ~DeleteObjectsByPrefixReq() {} std::string GetBucketName() const { return m_bucket_name; } diff --git a/include/trsf/transfer_handler.h b/include/trsf/transfer_handler.h index d58f473..b9f928c 100644 --- a/include/trsf/transfer_handler.h +++ b/include/trsf/transfer_handler.h @@ -3,6 +3,7 @@ #include "op/cos_result.h" #include "response/object_resp.h" +#include "util/illegal_intercept.h" #include #include #include @@ -89,6 +90,9 @@ class TransferHandler : public std::enable_shared_from_this { ~TransferHandler() {} void SetBucketName(const std::string& bucket_name) { + if (!IllegalIntercept::CheckBucket(bucket_name)){ + throw std::invalid_argument("Invalid bucket_name argument :" + bucket_name); + } m_bucket_name = bucket_name; } std::string GetBucketName() const { return m_bucket_name; } diff --git a/include/util/illegal_intercept.h b/include/util/illegal_intercept.h index b640c27..a3f6eac 100644 --- a/include/util/illegal_intercept.h +++ b/include/util/illegal_intercept.h @@ -6,7 +6,8 @@ namespace qcloud_cos { class IllegalIntercept { public: static bool ObjectKeySimplifyCheck(const std::string& path); - + static bool CheckBucket(const std::string& path); + static bool isAlnum(char c); }; } // namespace qcloud_cos diff --git a/src/cos_api.cpp b/src/cos_api.cpp index a688449..e4bf10b 100644 --- a/src/cos_api.cpp +++ b/src/cos_api.cpp @@ -27,6 +27,9 @@ Poco::TaskManager& GetGlobalTaskManager() { CosAPI::CosAPI(CosConfig& config) : m_config(new CosConfig(config)), m_object_op(m_config), m_bucket_op(m_config), m_service_op(m_config) { + if (!m_config->CheckRegion()){ + throw std::invalid_argument("Invalid region configuration in CosConfig :" + m_config->GetRegion()); + } CosInit(); } diff --git a/src/cos_config.cpp b/src/cos_config.cpp index 1518958..0373b2d 100644 --- a/src/cos_config.cpp +++ b/src/cos_config.cpp @@ -7,6 +7,7 @@ #include "cos_sys_config.h" #include "util/string_util.h" +#include "util/illegal_intercept.h" namespace qcloud_cos { CosConfig::CosConfig(const std::string& config_file) @@ -290,4 +291,18 @@ void CosConfig::SetLogCallback(const LogCallback log_callback) { CosSysConfig::SetLogCallback(log_callback); } +bool CosConfig::CheckRegion(){ + // 检查 region 是否符合规范 + if (m_region.empty() || !IllegalIntercept::isAlnum(m_region.front()) || !IllegalIntercept::isAlnum(m_region.back())) { + return false; + } + for (size_t i = 1; i < m_region.size() - 1; ++i) { + char c = m_region[i]; + if (!IllegalIntercept::isAlnum(c) && c != '.' && c != '-') { + return false; + } + } + return true; +} + } // namespace qcloud_cos diff --git a/src/util/illegal_intercept.cpp b/src/util/illegal_intercept.cpp index 2b4e777..c0153ea 100644 --- a/src/util/illegal_intercept.cpp +++ b/src/util/illegal_intercept.cpp @@ -50,4 +50,51 @@ bool IllegalIntercept::ObjectKeySimplifyCheck(const std::string& path) { } return true; } + +bool IllegalIntercept::isAlnum(char c) { + return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9'); +} + +bool MatchesPattern2(const std::string& str) { + if (str.size() != 1 || !IllegalIntercept::isAlnum(str[0])) { + return false; + } + return true; +} + +bool MatchesPattern1(const std::string& str) { + if (str.empty()) { + return false; + } + + // 检查第一个字符是否为字母或数字 + if (!IllegalIntercept::isAlnum(str.front())) { + return false; + } + + // 检查最后一个字符是否为字母或数字 + if (!IllegalIntercept::isAlnum(str.back())) { + return false; + } + + // 检查中间字符是否为字母、数字或短横线 + for (size_t i = 1; i < str.size() - 1; ++i) { + char c = str[i]; + if (!IllegalIntercept::isAlnum(c) && c != '-') { + return false; + } + } + + return true; +} + +bool IllegalIntercept::CheckBucket(const std::string& bucket){ + if (MatchesPattern1(bucket)) { + return true; + } + if (MatchesPattern2(bucket)) { + return true; + } + return false; +} } // namespace qcloud_cos \ No newline at end of file diff --git a/unittest/src/bucket_op_test.cpp b/unittest/src/bucket_op_test.cpp index 9bb463b..297186f 100644 --- a/unittest/src/bucket_op_test.cpp +++ b/unittest/src/bucket_op_test.cpp @@ -59,6 +59,32 @@ class BucketOpTest : public testing::Test { m_owner2 = GetEnvVar("CPP_SDK_V5_OTHER_UIN"); std::cout << "================SetUpTestCase End====================" << std::endl; + try{ + qcloud_cos::CosConfig * _config = new CosConfig("./config.json"); + _config->SetAccessKey(GetEnvVar("CPP_SDK_V5_ACCESS_KEY")); + _config->SetSecretKey(GetEnvVar("CPP_SDK_V5_SECRET_KEY")); + _config->SetRegion("ap-guangzhou@xxxx"); + qcloud_cos::CosAPI * _client = new CosAPI(*_config); + } + catch(const std::exception& e){ + EXPECT_EQ(e.what(), "Invalid region configuration in CosConfig :ap-guangzhou@xxxx"); + } + try{ + qcloud_cos::CosConfig * _config = new CosConfig("./config.json"); + _config->SetAccessKey(GetEnvVar("CPP_SDK_V5_ACCESS_KEY")); + _config->SetSecretKey(GetEnvVar("CPP_SDK_V5_SECRET_KEY")); + _config->SetRegion("ap-guangzhou"); + qcloud_cos::CosAPI * _client = new CosAPI(*_config); + qcloud_cos::PutBucketReq req("bucket_name-12500000000@xxxx"); + req.SetBucketName("bucket_name-12500000000@xxxx"); + qcloud_cos::PutBucketResp resp; + _client->PutBucket(req,&resp); + } + catch(const std::exception& e) + { + EXPECT_EQ(e.what(), "Invalid bucket_name argument :bucket_name-12500000000@xxxx"); + } + } static void TearDownTestCase() { diff --git a/unittest/src/object_op_test.cpp b/unittest/src/object_op_test.cpp index 0cd273e..e7a41fb 100644 --- a/unittest/src/object_op_test.cpp +++ b/unittest/src/object_op_test.cpp @@ -214,6 +214,10 @@ std::map ObjectOpTest::m_to_be_aborted; #if 1 +int SslCtxCallback(void *ssl_ctx, void *data) { + return 0; +} + TEST_F(ObjectOpTest, PutObjectByFileTest) { // 1. ObjectName为普通字符串 { @@ -550,6 +554,15 @@ TEST_F(ObjectOpTest, MoveObjectTest) { ASSERT_TRUE(result.IsSucc()); MoveObjectReq mv_req(m_bucket_name, "move_object_src", "move_object_dst"); + try{ + // 非法bucket_name + MoveObjectReq test_req("bucket_name-12500000000@xxxx", "move_object_src", "move_object_dst"); + } + catch(const std::exception& e){ + EXPECT_EQ(e.what(), "Invalid bucket_name argument :bucket_name-12500000000@xxxx"); + } + mv_req.SetHttps(); + mv_req.SetSSLCtxCallback(SslCtxCallback, nullptr); CosResult mv_result = m_client->MoveObject(mv_req); ASSERT_TRUE(mv_result.IsSucc()); } @@ -587,6 +600,20 @@ TEST_F(ObjectOpTest, DeleteObjectsTest) { TestUtils::WriteRandomDatatoFile(local_file4, 1024); PutObjectsByDirectoryReq req(m_bucket_name, directory_name); + try{ + // 非法bucket_name + PutObjectsByDirectoryReq test_req("bucket_name-12500000000@xxxx",directory_name); + } + catch(const std::exception& e){ + EXPECT_EQ(e.what(), "Invalid bucket_name argument :bucket_name-12500000000@xxxx"); + } + try{ + // 非法bucket_name + PutObjectsByDirectoryReq test_req("bucket_name-12500000000@xxxx",directory_name,"111"); + } + catch(const std::exception& e){ + EXPECT_EQ(e.what(), "Invalid bucket_name argument :bucket_name-12500000000@xxxx"); + } PutObjectsByDirectoryResp resp; CosResult result = m_client->PutObjects(req, &resp); ASSERT_TRUE(result.IsSucc()); @@ -597,6 +624,13 @@ TEST_F(ObjectOpTest, DeleteObjectsTest) { TestUtils::RemoveDirectory(directory_name); DeleteObjectsByPrefixReq del_req(m_bucket_name, directory_name); + try{ + // 非法bucket_name + DeleteObjectsByPrefixReq test_req("bucket_name-12500000000@xxxx",directory_name); + } + catch(const std::exception& e){ + EXPECT_EQ(e.what(), "Invalid bucket_name argument :bucket_name-12500000000@xxxx"); + } DeleteObjectsByPrefixResp del_resp; CosResult del_result = m_client->DeleteObjects(del_req, &del_resp); ASSERT_TRUE(del_result.IsSucc()); @@ -638,6 +672,28 @@ TEST_F(ObjectOpTest, DeleteObjectsTest) { to_be_deleted.push_back(pair); } qcloud_cos::DeleteObjectsReq req(m_bucket_name, to_be_deleted); + try{ + // 非法bucket_name + DeleteObjectsReq get_req("bucket_name-12500000000@xxxx"); + } + catch(const std::exception& e){ + EXPECT_EQ(e.what(), "Invalid bucket_name argument :bucket_name-12500000000@xxxx"); + } + try{ + // 非法bucket_name + DeleteObjectsReq get_req("bucket_name-12500000000@xxxx",to_be_deleted); + } + catch(const std::exception& e){ + EXPECT_EQ(e.what(), "Invalid bucket_name argument :bucket_name-12500000000@xxxx"); + } + try{ + // 非法bucket_name + req.SetBucketName("bucket_name-12500000000@xxxx"); + } + catch(const std::exception& e){ + EXPECT_EQ(e.what(), "Invalid bucket_name argument :bucket_name-12500000000@xxxx"); + } + req.SetBucketName(m_bucket_name); qcloud_cos::DeleteObjectsResp resp; qcloud_cos::CosResult del_result = m_client->DeleteObjects(req, &resp); ASSERT_TRUE(del_result.IsSucc()); @@ -656,6 +712,21 @@ TEST_F(ObjectOpTest, GetObjectByStreamTest) { std::ostringstream os; GetObjectByStreamReq get_req(m_bucket_name, object_name, os); + try{ + // 非法bucket_name + GetObjectByStreamReq get_req("bucket_name-12500000000@xxxx", object_name, os); + } + catch(const std::exception& e){ + EXPECT_EQ(e.what(), "Invalid bucket_name argument :bucket_name-12500000000@xxxx"); + } + try{ + // 非法bucket_name + get_req.SetBucketName("bucket_name-12500000000@xxxx"); + } + catch(const std::exception& e){ + EXPECT_EQ(e.what(), "Invalid bucket_name argument :bucket_name-12500000000@xxxx"); + } + get_req.SetBucketName(m_bucket_name); GetObjectByStreamResp get_resp; CosResult get_result = m_client->GetObject(get_req, &get_resp); ASSERT_TRUE(get_result.IsSucc()); @@ -1917,6 +1988,8 @@ TEST_F(ObjectOpTest, ResumableGetObjectTest) { std::string file_download = "resumable_get_object_test_file_download"; GetObjectByFileReq get_req(m_bucket_name, object_name, file_download); GetObjectByFileResp get_resp; + get_req.SetHttps(); + get_req.SetSSLCtxCallback(SslCtxCallback, nullptr); CosResult get_result = m_client->ResumableGetObject(get_req, &get_resp); ASSERT_TRUE(get_result.IsSucc()); @@ -2080,6 +2153,8 @@ TEST_F(ObjectOpTest, MultiPutObjectTest_OneStep) { // 2. 上传 MultiPutObjectReq req(m_bucket_name, object_name, filename); + req.SetHttps(); + req.SetSSLCtxCallback(SslCtxCallback, nullptr); req.SetRecvTimeoutInms(1000 * 200); MultiPutObjectResp resp; @@ -2185,6 +2260,8 @@ TEST_F(ObjectOpTest, CopyTest) { std::string local_file = "./object_test_copy_data_source"; TestUtils::WriteRandomDatatoFile(local_file, 1024 * 1024); PutObjectByFileReq req(m_bucket_name, "object_test_copy_data_source", local_file); + req.SetHttps(); + req.SetSSLCtxCallback(SslCtxCallback, nullptr); req.SetXCosStorageClass(kStorageClassStandard); PutObjectByFileResp resp; CosResult result = m_client->PutObject(req, &resp); @@ -2195,6 +2272,8 @@ TEST_F(ObjectOpTest, CopyTest) { std::string host = CosSysConfig::GetHost(m_config->GetAppId(), m_config->GetRegion(), m_bucket_name); CopyReq req(m_bucket_name, "object_test_copy_data_copy"); + req.SetHttps(); + req.SetSSLCtxCallback(SslCtxCallback, nullptr); CopyResp resp; req.SetXCosCopySource(host + "/object_test_copy_data_source"); CosResult result = m_client->Copy(req, &resp); @@ -2235,6 +2314,8 @@ TEST_F(ObjectOpTest, CopyTest2) { } { CopyReq req(m_bucket_name, "object_test_copy_data_copy2"); + req.SetHttps(); + req.SetSSLCtxCallback(SslCtxCallback, nullptr); CopyResp resp; req.SetXCosCopySource(host + "/object_test_copy_data_source2"); CosResult result = m_client->Copy(req, &resp); @@ -2256,6 +2337,8 @@ TEST_F(ObjectOpTest, CopyTest3) { "cppsdkcopysrctest2-"+GetEnvVar("CPP_SDK_V5_APPID")); { CopyReq req(m_bucket_name, "object_test_copy_data_copy3"); + req.SetHttps(); + req.SetSSLCtxCallback(SslCtxCallback, nullptr); CopyResp resp; req.SetXCosCopySource(host + "/object_test_copy_data_copy3"); CosResult result = m_client->Copy(req, &resp); @@ -2315,6 +2398,8 @@ TEST_F(ObjectOpTest, AbortMultiUploadTest) { config1->SetSecretKey(GetEnvVar("CPP_SDK_V5_SECRET_KEY")); config1->SetRegion(GetEnvVar("CPP_SDK_V5_REGION")); ObjectOp m_object_op(config1); + req1.SetHttps(); + req1.SetSSLCtxCallback(SslCtxCallback, nullptr); std::string resume_uploadid = m_object_op.GetResumableUploadID(req1, m_bucket_name, object_name, false); if (!resume_uploadid.empty()) { resume_flag = m_object_op.CheckUploadPart(req1, m_bucket_name, object_name, @@ -2882,6 +2967,8 @@ TEST_F(ObjectOpTest, TestMultiPutObjectWithMeta) { qcloud_cos::MultiGetObjectReq get_req(m_bucket_name, object_name, local_file_download); qcloud_cos::MultiGetObjectResp get_resp; + get_req.SetHttps(); + get_req.SetSSLCtxCallback(SslCtxCallback, nullptr); CosResult get_result = m_client->MultiGetObject(get_req, &get_resp); // checkout common header ASSERT_TRUE(get_result.IsSucc());