Skip to content

Commit

Permalink
Merge pull request #169 from tencentyun/feature_huberyxxiao_42e1ad74
Browse files Browse the repository at this point in the history
Feature huberyxxiao 42e1ad74
  • Loading branch information
Huberyxiao authored Nov 13, 2024
2 parents b8266fa + 41c2746 commit cf7f18b
Show file tree
Hide file tree
Showing 17 changed files with 254 additions and 50 deletions.
26 changes: 13 additions & 13 deletions demo/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,19 @@ add_executable(select_objec_demo ${select_objec_demo_src})

set(EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR}/bin)

target_link_libraries(cos_demo cossdk ${POCO_LIBS} ${OPENSSL_LIBS} ${SYSTEM_LIBS})
target_link_libraries(put_object_demo cossdk ${POCO_LIBS} ${OPENSSL_LIBS} ${SYSTEM_LIBS})
target_link_libraries(get_object_url_demo cossdk ${POCO_LIBS} ${OPENSSL_LIBS} ${SYSTEM_LIBS})
target_link_libraries(head_object_demo cossdk ${POCO_LIBS} ${OPENSSL_LIBS} ${SYSTEM_LIBS})
target_link_libraries(get_object_demo cossdk ${POCO_LIBS} ${OPENSSL_LIBS} ${SYSTEM_LIBS})
target_link_libraries(delete_object_demo cossdk ${POCO_LIBS} ${OPENSSL_LIBS} ${SYSTEM_LIBS})
target_link_libraries(put_get_object_acl_demo cossdk ${POCO_LIBS} ${OPENSSL_LIBS} ${SYSTEM_LIBS})
target_link_libraries(put_get_delete_object_tagging_demo cossdk ${POCO_LIBS} ${OPENSSL_LIBS} ${SYSTEM_LIBS})
target_link_libraries(restore_object_demo cossdk ${POCO_LIBS} ${OPENSSL_LIBS} ${SYSTEM_LIBS})
target_link_libraries(copy_move_object_demo cossdk ${POCO_LIBS} ${OPENSSL_LIBS} ${SYSTEM_LIBS})
target_link_libraries(multi_put_object_demo cossdk ${POCO_LIBS} ${OPENSSL_LIBS} ${SYSTEM_LIBS})
target_link_libraries(multi_get_object_demo cossdk ${POCO_LIBS} ${OPENSSL_LIBS} ${SYSTEM_LIBS})
target_link_libraries(select_objec_demo cossdk ${POCO_LIBS} ${OPENSSL_LIBS} ${SYSTEM_LIBS})
target_link_libraries(cos_demo cossdk ssl crypto ${POCO_LIBS} ${OPENSSL_LIBS} ${SYSTEM_LIBS})
target_link_libraries(put_object_demo ssl crypto cossdk ${POCO_LIBS} ${OPENSSL_LIBS} ${SYSTEM_LIBS})
target_link_libraries(get_object_url_demo ssl crypto cossdk ${POCO_LIBS} ${OPENSSL_LIBS} ${SYSTEM_LIBS})
target_link_libraries(head_object_demo ssl crypto cossdk ${POCO_LIBS} ${OPENSSL_LIBS} ${SYSTEM_LIBS})
target_link_libraries(get_object_demo ssl crypto cossdk ${POCO_LIBS} ${OPENSSL_LIBS} ${SYSTEM_LIBS})
target_link_libraries(delete_object_demo ssl crypto cossdk ${POCO_LIBS} ${OPENSSL_LIBS} ${SYSTEM_LIBS})
target_link_libraries(put_get_object_acl_demo ssl crypto cossdk ${POCO_LIBS} ${OPENSSL_LIBS} ${SYSTEM_LIBS})
target_link_libraries(put_get_delete_object_tagging_demo ssl crypto cossdk ${POCO_LIBS} ${OPENSSL_LIBS} ${SYSTEM_LIBS})
target_link_libraries(restore_object_demo cossdk ssl crypto ${POCO_LIBS} ${OPENSSL_LIBS} ${SYSTEM_LIBS})
target_link_libraries(copy_move_object_demo cossdk ssl crypto ${POCO_LIBS} ${OPENSSL_LIBS} ${SYSTEM_LIBS})
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})

include_directories(${CMAKE_SOURCE_DIR}/include/ ${POCO_INCLUDE_DIR})

Expand Down
36 changes: 36 additions & 0 deletions demo/object_op_demo/get_object_demo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "cos_api.h"
#include "cos_sys_config.h"
#include "util/auth_tool.h"
#include <openssl/ssl.h>

/**
* 本样例演示了如何使用 COS C++ SDK 进行简单下载和列出
Expand All @@ -22,6 +23,22 @@ std::string region = "ap-guangzhou";
std::string bucket_name = "examplebucket-12500000000";
std::string tmp_token = "token";

/**
* 本方法为 SSL_CTX 的回调方法,用户可以在此方法中配置 SSL_CTX 信息
*/
int SslCtxCallback(void *ssl_ctx, void *data) {
std::cout << "ssl_ctx: " << ssl_ctx << " data: " << data << std::endl;

SSL_CTX *ctx = (SSL_CTX *)ssl_ctx;
std::cout << "ssl_ctx in" << std::endl;
SSL_CTX_use_PrivateKey_file(ctx, "/data/cert/client.key", SSL_FILETYPE_PEM);
SSL_CTX_use_certificate_chain_file(ctx, "/data/cert/client.crt");
std::cout << "ssl_ctx out" << std::endl;

return 0;
}


/*
* 本方法包含调用是否正常的判断,和请求结果的输出
* 可通过本方法判断是否请求成功,并输出结果信息
Expand Down Expand Up @@ -82,6 +99,24 @@ void GetObjectByStreamDemo(qcloud_cos::CosAPI& cos) {
std::cout << os.str() << std::endl;
}

/**
* 使用 https 双向认证
*/
void GetObjectByStreamDemoWithMutualAuthentication(qcloud_cos::CosAPI& cos) {
std::string object_name = "index.html";
std::ostringstream os;
qcloud_cos::GetObjectByStreamReq req(bucket_name, object_name, os);
req.SetHttps();
req.SetSSLCtxCallback(SslCtxCallback, nullptr);
qcloud_cos::GetObjectByStreamResp resp;

qcloud_cos::CosResult result = cos.GetObject(req, &resp);
std::cout << "===================GetObjectResponse=====================" << std::endl;
PrintResult(result, resp);
std::cout << "=========================================================" << std::endl;
std::cout << os.str() << std::endl;
}

void GetBucketDemo(qcloud_cos::CosAPI& cos) {
qcloud_cos::GetBucketReq req(bucket_name);
// 设置列出的对象名以 prefix 为前缀
Expand Down Expand Up @@ -127,5 +162,6 @@ int main() {
CosSysConfig::SetLogLevel((LOG_LEVEL)COS_LOG_ERR);
GetObjectByFileDemo(cos);
GetObjectByStreamDemo(cos);
// GetObjectByStreamDemoWithMutualAuthentication(cos);
GetBucketDemo(cos);
}
5 changes: 4 additions & 1 deletion include/cos_defines.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@

#include <string>
#include <vector>
#include <functional>

#include "util/log_util.h"

namespace qcloud_cos {

#define COS_CPP_SDK_VERSON "v5.5.14"
#define COS_CPP_SDK_VERSON "v5.5.15"

/// 路径分隔符
const char kPathDelimiter[] = "/";
Expand Down Expand Up @@ -39,6 +40,8 @@ const uint64_t kPartSize1M = 1 * 1024 * 1024;
/// 分块大小5G
const uint64_t kPartSize5G = (uint64_t)5 * 1024 * 1024 * 1024;

using SSLCtxCallback = std::function<int(void *ssl_ctx, void* user_data)>;

const bool COS_CHANGE_BACKUP_DOMAIN = true;
typedef enum log_out_type {
COS_LOG_NULL = 0,
Expand Down
4 changes: 4 additions & 0 deletions include/op/file_copy_task.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include "Poco/Foundation.h"
#include "Poco/Runnable.h"
#include "cos_defines.h"

namespace qcloud_cos {

Expand Down Expand Up @@ -35,6 +36,7 @@ class FileCopyTask : public Poco::Runnable {

void SetVerifyCert(bool verify_cert);
void SetCaLocation(const std::string& ca_location);
void SetSslCtxCb(SSLCtxCallback cb, void *data);

std::string GetErrMsg() const { return m_err_msg; }

Expand All @@ -58,6 +60,8 @@ class FileCopyTask : public Poco::Runnable {

bool m_verify_cert;
std::string m_ca_location;
SSLCtxCallback m_ssl_ctx_cb;
void *m_user_data;
};

} // namespace qcloud_cos
7 changes: 6 additions & 1 deletion include/op/file_download_task.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ class FileDownTask : public Poco::Runnable {
uint64_t offset = 0, unsigned char* pbuf = NULL,
const size_t data_len = 0,
bool verify_cert = true,
const std::string& ca_lication = "");
const std::string& ca_lication = "",
SSLCtxCallback ssl_ctx_cb = nullptr,
void *user_data = nullptr);

~FileDownTask() {}

Expand All @@ -40,6 +42,7 @@ class FileDownTask : public Poco::Runnable {

void SetVerifyCert(bool verify_cert);
void SetCaLocation(const std::string& ca_location);
void SetSslCtxCb(SSLCtxCallback cb, void *data);

std::string GetTaskResp();

Expand Down Expand Up @@ -72,6 +75,8 @@ class FileDownTask : public Poco::Runnable {

bool m_verify_cert;
std::string m_ca_location;
SSLCtxCallback m_ssl_ctx_cb;
void *m_user_data;

SharedConfig m_config;
};
Expand Down
15 changes: 12 additions & 3 deletions include/op/file_upload_task.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,29 @@ class FileUploadTask : public Poco::Runnable {
uint64_t recv_timeout_in_ms, unsigned char* pbuf = NULL,
const size_t data_len = 0,
bool verify_cert = true,
const std::string& ca_location = "");
const std::string& ca_location = "",
SSLCtxCallback ssl_ctx_cb = nullptr,
void *user_data = nullptr);

FileUploadTask(const std::string& full_url,
const std::map<std::string, std::string>& headers,
const std::map<std::string, std::string>& params,
uint64_t conn_timeout_in_ms, uint64_t recv_timeout_in_ms,
const SharedTransferHandler& handler,
bool verify_cert = true,
const std::string& ca_location = "");
const std::string& ca_location = "",
SSLCtxCallback ssl_ctx_cb = nullptr,
void *user_data = nullptr);

FileUploadTask(const std::string& full_url,
const std::map<std::string, std::string>& headers,
const std::map<std::string, std::string>& params,
uint64_t conn_timeout_in_ms, uint64_t recv_timeout_in_ms,
unsigned char* pbuf = NULL, const size_t data_len = 0,
bool verify_cert = true,
const std::string& ca_location = "");
const std::string& ca_location = "",
SSLCtxCallback ssl_ctx_cb = nullptr,
void *user_data = nullptr);

~FileUploadTask() {}

Expand Down Expand Up @@ -74,6 +80,7 @@ class FileUploadTask : public Poco::Runnable {

void SetVerifyCert(bool verify_cert);
void SetCaLocation(const std::string& ca_location);
void SetSslCtxCb(SSLCtxCallback cb, void *data);

private:
std::string m_full_url;
Expand All @@ -95,6 +102,8 @@ class FileUploadTask : public Poco::Runnable {

bool m_verify_cert;
std::string m_ca_location;
SSLCtxCallback m_ssl_ctx_cb;
void *m_user_data;
};

} // namespace qcloud_cos
3 changes: 2 additions & 1 deletion include/op/object_op.h
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,8 @@ class ObjectOp : public BaseOp {
const std::string& range,
const std::map<std::string, std::string>& headers,
const std::map<std::string, std::string>& params,
bool verify_cert,const std::string& ca_location,
bool verify_cert,const std::string& ca_location,
SSLCtxCallback ssl_ctx_cb, void *user_data,
FileCopyTask* task, bool sign_header_host);

/// \brief 检查是否可以走断点下载
Expand Down
10 changes: 10 additions & 0 deletions include/request/base_req.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,13 @@ class BaseReq {
void SetHttps() { m_is_https = true; }
bool IsHttps() const { return m_is_https; }

void SetSSLCtxCallback(const SSLCtxCallback& ssl_ctx_cb, void *user_data) {
m_ssl_ctx_cb = ssl_ctx_cb;
m_user_data = user_data;
}
const SSLCtxCallback& GetSSLCtxCallback() const { return m_ssl_ctx_cb; }
void *GetSSLCtxCbData() const { return m_user_data; }

/// \brief set whether check content md5 each request, used for close range
/// download check
void SetCheckMD5(bool check_md5) { mb_check_md5 = check_md5; }
Expand Down Expand Up @@ -128,6 +135,9 @@ class BaseReq {
bool mb_verify_cert; // default is true
bool m_sign_header_host; // 是否对header host进行签名
std::string m_ca_location;

SSLCtxCallback m_ssl_ctx_cb;
void* m_user_data;
};

} // namespace qcloud_cos
2 changes: 1 addition & 1 deletion include/request/object_req.h
Original file line number Diff line number Diff line change
Expand Up @@ -1855,7 +1855,7 @@ class PutDirectoryReq : public PutObjectReq {
virtual ~PutDirectoryReq() {}
};

class MoveObjectReq {
class MoveObjectReq : public ObjectReq {
public:
MoveObjectReq(const std::string& bucket_name,
const std::string& src_object_name,
Expand Down
20 changes: 15 additions & 5 deletions include/util/http_sender.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ class HttpSender {
std::string* resp_body, std::string* err_msg,
bool is_check_md5 = false,
bool is_verify_cert = true,
const std::string& ca_location = "");
const std::string& ca_location = "",
SSLCtxCallback ssl_ctx_cb = nullptr,
void *user_data = nullptr);

static int SendRequest(const SharedTransferHandler& handler,
const std::string& http_method,
Expand All @@ -46,7 +48,9 @@ class HttpSender {
std::ostream& resp_stream, std::string* err_msg,
bool is_check_md5 = false,
bool is_verify_cert = true,
const std::string& ca_location = "");
const std::string& ca_location = "",
SSLCtxCallback ssl_ctx_cb = nullptr,
void *user_data = nullptr);

static int SendRequest(const SharedTransferHandler& handler,
const std::string& http_method,
Expand All @@ -59,7 +63,9 @@ class HttpSender {
std::string* resp_body, std::string* err_msg,
bool is_check_md5 = false,
bool is_verify_cert = true,
const std::string& ca_location = "");
const std::string& ca_location = "",
SSLCtxCallback ssl_ctx_cb = nullptr,
void *user_data = nullptr);

static int SendRequest(const SharedTransferHandler& handler,
const std::string& http_method,
Expand All @@ -72,7 +78,9 @@ class HttpSender {
std::ostream& resp_stream, std::string* err_msg,
bool is_check_md5 = false,
bool is_verify_cert = true,
const std::string& ca_location = "");
const std::string& ca_location = "",
SSLCtxCallback ssl_ctx_cb = nullptr,
void *user_data = nullptr);

static int SendRequest(const SharedTransferHandler& handler,
const std::string& http_method,
Expand All @@ -87,7 +95,9 @@ class HttpSender {
std::string* err_msg, uint64_t* real_byte,
bool is_check_md5 = false,
bool is_verify_cert = true,
const std::string& ca_location = "");
const std::string& ca_location = "",
SSLCtxCallback ssl_ctx_cb = nullptr,
void *user_data = nullptr);
};

} // namespace qcloud_cos
Expand Down
9 changes: 6 additions & 3 deletions src/op/base_op.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,8 @@ CosResult BaseOp::NormalAction(
int http_code = HttpSender::SendRequest(nullptr,
req.GetMethod(), dest_url, req_params, req_headers, req_body,
req.GetConnTimeoutInms(), req.GetRecvTimeoutInms(), &resp_headers,
&resp_body, &err_msg, false, req.GetVerifyCert(), req.GetCaLocation());
&resp_body, &err_msg, false, req.GetVerifyCert(), req.GetCaLocation(),
req.GetSSLCtxCallback(), req.GetSSLCtxCbData());
if (http_code == -1) {
result.SetErrorMsg(err_msg);
return result;
Expand Down Expand Up @@ -267,7 +268,8 @@ CosResult BaseOp::DownloadAction(const std::string& host,
handler, req.GetMethod(), dest_url, req_params, req_headers, "",
req.GetConnTimeoutInms(), req.GetRecvTimeoutInms(), &resp_headers,
&xml_err_str, os, &err_msg, &real_byte, req.CheckMD5(),
req.GetVerifyCert(), req.GetCaLocation());
req.GetVerifyCert(), req.GetCaLocation(),
req.GetSSLCtxCallback(), req.GetSSLCtxCbData());
if (http_code == -1) {
result.SetErrorMsg(err_msg);
resp->ParseFromHeaders(resp_headers);
Expand Down Expand Up @@ -358,7 +360,8 @@ CosResult BaseOp::UploadAction(
int http_code = HttpSender::SendRequest(
handler, req.GetMethod(), dest_url, req_params, req_headers, is,
req.GetConnTimeoutInms(), req.GetRecvTimeoutInms(), &resp_headers,
&resp_body, &err_msg, false, req.GetVerifyCert(), req.GetCaLocation());
&resp_body, &err_msg, false, req.GetVerifyCert(), req.GetCaLocation(),
req.GetSSLCtxCallback(), req.GetSSLCtxCbData());
if (http_code == -1) {
result.SetErrorMsg(err_msg);
return result;
Expand Down
11 changes: 9 additions & 2 deletions src/op/file_copy_task.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ FileCopyTask::FileCopyTask(const std::string& full_url,
m_recv_timeout_in_ms(recv_timeout_in_ms),
m_is_task_success(false),
m_etag(""),
m_verify_cert(true) {}
m_verify_cert(true),
m_ssl_ctx_cb(nullptr),
m_user_data(nullptr) {}

bool FileCopyTask::IsTaskSuccess() const { return m_is_task_success; }

Expand Down Expand Up @@ -46,6 +48,11 @@ void FileCopyTask::SetCaLocation(const std::string& ca_location) {
m_ca_location = ca_location;
}

void FileCopyTask::SetSslCtxCb(SSLCtxCallback cb, void *data) {
m_ssl_ctx_cb = cb;
m_user_data = data;
}

void FileCopyTask::run() {
m_is_task_success = false;
CopyTask();
Expand All @@ -61,7 +68,7 @@ void FileCopyTask::CopyTask() {
m_http_status = HttpSender::SendRequest(nullptr,
"PUT", m_full_url, m_params, m_headers, "", m_conn_timeout_in_ms,
m_recv_timeout_in_ms, &m_resp_headers, &m_resp, &m_err_msg,
false, m_verify_cert, m_ca_location);
false, m_verify_cert, m_ca_location, m_ssl_ctx_cb, m_user_data);

if (m_http_status != 200) {
SDK_LOG_ERR("FileUpload: url(%s) fail, httpcode:%d, resp: %s",
Expand Down
Loading

0 comments on commit cf7f18b

Please sign in to comment.