Skip to content

Commit cf7f18b

Browse files
Merge pull request #169 from tencentyun/feature_huberyxxiao_42e1ad74
Feature huberyxxiao 42e1ad74
2 parents b8266fa + 41c2746 commit cf7f18b

17 files changed

+254
-50
lines changed

demo/CMakeLists.txt

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -35,19 +35,19 @@ add_executable(select_objec_demo ${select_objec_demo_src})
3535

3636
set(EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR}/bin)
3737

38-
target_link_libraries(cos_demo cossdk ${POCO_LIBS} ${OPENSSL_LIBS} ${SYSTEM_LIBS})
39-
target_link_libraries(put_object_demo cossdk ${POCO_LIBS} ${OPENSSL_LIBS} ${SYSTEM_LIBS})
40-
target_link_libraries(get_object_url_demo cossdk ${POCO_LIBS} ${OPENSSL_LIBS} ${SYSTEM_LIBS})
41-
target_link_libraries(head_object_demo cossdk ${POCO_LIBS} ${OPENSSL_LIBS} ${SYSTEM_LIBS})
42-
target_link_libraries(get_object_demo cossdk ${POCO_LIBS} ${OPENSSL_LIBS} ${SYSTEM_LIBS})
43-
target_link_libraries(delete_object_demo cossdk ${POCO_LIBS} ${OPENSSL_LIBS} ${SYSTEM_LIBS})
44-
target_link_libraries(put_get_object_acl_demo cossdk ${POCO_LIBS} ${OPENSSL_LIBS} ${SYSTEM_LIBS})
45-
target_link_libraries(put_get_delete_object_tagging_demo cossdk ${POCO_LIBS} ${OPENSSL_LIBS} ${SYSTEM_LIBS})
46-
target_link_libraries(restore_object_demo cossdk ${POCO_LIBS} ${OPENSSL_LIBS} ${SYSTEM_LIBS})
47-
target_link_libraries(copy_move_object_demo cossdk ${POCO_LIBS} ${OPENSSL_LIBS} ${SYSTEM_LIBS})
48-
target_link_libraries(multi_put_object_demo cossdk ${POCO_LIBS} ${OPENSSL_LIBS} ${SYSTEM_LIBS})
49-
target_link_libraries(multi_get_object_demo cossdk ${POCO_LIBS} ${OPENSSL_LIBS} ${SYSTEM_LIBS})
50-
target_link_libraries(select_objec_demo cossdk ${POCO_LIBS} ${OPENSSL_LIBS} ${SYSTEM_LIBS})
38+
target_link_libraries(cos_demo cossdk ssl crypto ${POCO_LIBS} ${OPENSSL_LIBS} ${SYSTEM_LIBS})
39+
target_link_libraries(put_object_demo ssl crypto cossdk ${POCO_LIBS} ${OPENSSL_LIBS} ${SYSTEM_LIBS})
40+
target_link_libraries(get_object_url_demo ssl crypto cossdk ${POCO_LIBS} ${OPENSSL_LIBS} ${SYSTEM_LIBS})
41+
target_link_libraries(head_object_demo ssl crypto cossdk ${POCO_LIBS} ${OPENSSL_LIBS} ${SYSTEM_LIBS})
42+
target_link_libraries(get_object_demo ssl crypto cossdk ${POCO_LIBS} ${OPENSSL_LIBS} ${SYSTEM_LIBS})
43+
target_link_libraries(delete_object_demo ssl crypto cossdk ${POCO_LIBS} ${OPENSSL_LIBS} ${SYSTEM_LIBS})
44+
target_link_libraries(put_get_object_acl_demo ssl crypto cossdk ${POCO_LIBS} ${OPENSSL_LIBS} ${SYSTEM_LIBS})
45+
target_link_libraries(put_get_delete_object_tagging_demo ssl crypto cossdk ${POCO_LIBS} ${OPENSSL_LIBS} ${SYSTEM_LIBS})
46+
target_link_libraries(restore_object_demo cossdk ssl crypto ${POCO_LIBS} ${OPENSSL_LIBS} ${SYSTEM_LIBS})
47+
target_link_libraries(copy_move_object_demo cossdk ssl crypto ${POCO_LIBS} ${OPENSSL_LIBS} ${SYSTEM_LIBS})
48+
target_link_libraries(multi_put_object_demo cossdk ssl crypto ${POCO_LIBS} ${OPENSSL_LIBS} ${SYSTEM_LIBS})
49+
target_link_libraries(multi_get_object_demo cossdk ssl crypto ${POCO_LIBS} ${OPENSSL_LIBS} ${SYSTEM_LIBS})
50+
target_link_libraries(select_objec_demo ssl crypto cossdk ${POCO_LIBS} ${OPENSSL_LIBS} ${SYSTEM_LIBS})
5151

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

demo/object_op_demo/get_object_demo.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "cos_api.h"
99
#include "cos_sys_config.h"
1010
#include "util/auth_tool.h"
11+
#include <openssl/ssl.h>
1112

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

26+
/**
27+
* 本方法为 SSL_CTX 的回调方法,用户可以在此方法中配置 SSL_CTX 信息
28+
*/
29+
int SslCtxCallback(void *ssl_ctx, void *data) {
30+
std::cout << "ssl_ctx: " << ssl_ctx << " data: " << data << std::endl;
31+
32+
SSL_CTX *ctx = (SSL_CTX *)ssl_ctx;
33+
std::cout << "ssl_ctx in" << std::endl;
34+
SSL_CTX_use_PrivateKey_file(ctx, "/data/cert/client.key", SSL_FILETYPE_PEM);
35+
SSL_CTX_use_certificate_chain_file(ctx, "/data/cert/client.crt");
36+
std::cout << "ssl_ctx out" << std::endl;
37+
38+
return 0;
39+
}
40+
41+
2542
/*
2643
* 本方法包含调用是否正常的判断,和请求结果的输出
2744
* 可通过本方法判断是否请求成功,并输出结果信息
@@ -82,6 +99,24 @@ void GetObjectByStreamDemo(qcloud_cos::CosAPI& cos) {
8299
std::cout << os.str() << std::endl;
83100
}
84101

102+
/**
103+
* 使用 https 双向认证
104+
*/
105+
void GetObjectByStreamDemoWithMutualAuthentication(qcloud_cos::CosAPI& cos) {
106+
std::string object_name = "index.html";
107+
std::ostringstream os;
108+
qcloud_cos::GetObjectByStreamReq req(bucket_name, object_name, os);
109+
req.SetHttps();
110+
req.SetSSLCtxCallback(SslCtxCallback, nullptr);
111+
qcloud_cos::GetObjectByStreamResp resp;
112+
113+
qcloud_cos::CosResult result = cos.GetObject(req, &resp);
114+
std::cout << "===================GetObjectResponse=====================" << std::endl;
115+
PrintResult(result, resp);
116+
std::cout << "=========================================================" << std::endl;
117+
std::cout << os.str() << std::endl;
118+
}
119+
85120
void GetBucketDemo(qcloud_cos::CosAPI& cos) {
86121
qcloud_cos::GetBucketReq req(bucket_name);
87122
// 设置列出的对象名以 prefix 为前缀
@@ -127,5 +162,6 @@ int main() {
127162
CosSysConfig::SetLogLevel((LOG_LEVEL)COS_LOG_ERR);
128163
GetObjectByFileDemo(cos);
129164
GetObjectByStreamDemo(cos);
165+
// GetObjectByStreamDemoWithMutualAuthentication(cos);
130166
GetBucketDemo(cos);
131167
}

include/cos_defines.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@
66

77
#include <string>
88
#include <vector>
9+
#include <functional>
910

1011
#include "util/log_util.h"
1112

1213
namespace qcloud_cos {
1314

14-
#define COS_CPP_SDK_VERSON "v5.5.14"
15+
#define COS_CPP_SDK_VERSON "v5.5.15"
1516

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

43+
using SSLCtxCallback = std::function<int(void *ssl_ctx, void* user_data)>;
44+
4245
const bool COS_CHANGE_BACKUP_DOMAIN = true;
4346
typedef enum log_out_type {
4447
COS_LOG_NULL = 0,

include/op/file_copy_task.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
#include "Poco/Foundation.h"
99
#include "Poco/Runnable.h"
10+
#include "cos_defines.h"
1011

1112
namespace qcloud_cos {
1213

@@ -35,6 +36,7 @@ class FileCopyTask : public Poco::Runnable {
3536

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

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

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

5961
bool m_verify_cert;
6062
std::string m_ca_location;
63+
SSLCtxCallback m_ssl_ctx_cb;
64+
void *m_user_data;
6165
};
6266

6367
} // namespace qcloud_cos

include/op/file_download_task.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ class FileDownTask : public Poco::Runnable {
2828
uint64_t offset = 0, unsigned char* pbuf = NULL,
2929
const size_t data_len = 0,
3030
bool verify_cert = true,
31-
const std::string& ca_lication = "");
31+
const std::string& ca_lication = "",
32+
SSLCtxCallback ssl_ctx_cb = nullptr,
33+
void *user_data = nullptr);
3234

3335
~FileDownTask() {}
3436

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

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

4447
std::string GetTaskResp();
4548

@@ -72,6 +75,8 @@ class FileDownTask : public Poco::Runnable {
7275

7376
bool m_verify_cert;
7477
std::string m_ca_location;
78+
SSLCtxCallback m_ssl_ctx_cb;
79+
void *m_user_data;
7580

7681
SharedConfig m_config;
7782
};

include/op/file_upload_task.h

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,29 @@ class FileUploadTask : public Poco::Runnable {
1616
uint64_t recv_timeout_in_ms, unsigned char* pbuf = NULL,
1717
const size_t data_len = 0,
1818
bool verify_cert = true,
19-
const std::string& ca_location = "");
19+
const std::string& ca_location = "",
20+
SSLCtxCallback ssl_ctx_cb = nullptr,
21+
void *user_data = nullptr);
2022

2123
FileUploadTask(const std::string& full_url,
2224
const std::map<std::string, std::string>& headers,
2325
const std::map<std::string, std::string>& params,
2426
uint64_t conn_timeout_in_ms, uint64_t recv_timeout_in_ms,
2527
const SharedTransferHandler& handler,
2628
bool verify_cert = true,
27-
const std::string& ca_location = "");
29+
const std::string& ca_location = "",
30+
SSLCtxCallback ssl_ctx_cb = nullptr,
31+
void *user_data = nullptr);
2832

2933
FileUploadTask(const std::string& full_url,
3034
const std::map<std::string, std::string>& headers,
3135
const std::map<std::string, std::string>& params,
3236
uint64_t conn_timeout_in_ms, uint64_t recv_timeout_in_ms,
3337
unsigned char* pbuf = NULL, const size_t data_len = 0,
3438
bool verify_cert = true,
35-
const std::string& ca_location = "");
39+
const std::string& ca_location = "",
40+
SSLCtxCallback ssl_ctx_cb = nullptr,
41+
void *user_data = nullptr);
3642

3743
~FileUploadTask() {}
3844

@@ -74,6 +80,7 @@ class FileUploadTask : public Poco::Runnable {
7480

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

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

96103
bool m_verify_cert;
97104
std::string m_ca_location;
105+
SSLCtxCallback m_ssl_ctx_cb;
106+
void *m_user_data;
98107
};
99108

100109
} // namespace qcloud_cos

include/op/object_op.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,8 @@ class ObjectOp : public BaseOp {
440440
const std::string& range,
441441
const std::map<std::string, std::string>& headers,
442442
const std::map<std::string, std::string>& params,
443-
bool verify_cert,const std::string& ca_location,
443+
bool verify_cert,const std::string& ca_location,
444+
SSLCtxCallback ssl_ctx_cb, void *user_data,
444445
FileCopyTask* task, bool sign_header_host);
445446

446447
/// \brief 检查是否可以走断点下载

include/request/base_req.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,13 @@ class BaseReq {
8282
void SetHttps() { m_is_https = true; }
8383
bool IsHttps() const { return m_is_https; }
8484

85+
void SetSSLCtxCallback(const SSLCtxCallback& ssl_ctx_cb, void *user_data) {
86+
m_ssl_ctx_cb = ssl_ctx_cb;
87+
m_user_data = user_data;
88+
}
89+
const SSLCtxCallback& GetSSLCtxCallback() const { return m_ssl_ctx_cb; }
90+
void *GetSSLCtxCbData() const { return m_user_data; }
91+
8592
/// \brief set whether check content md5 each request, used for close range
8693
/// download check
8794
void SetCheckMD5(bool check_md5) { mb_check_md5 = check_md5; }
@@ -128,6 +135,9 @@ class BaseReq {
128135
bool mb_verify_cert; // default is true
129136
bool m_sign_header_host; // 是否对header host进行签名
130137
std::string m_ca_location;
138+
139+
SSLCtxCallback m_ssl_ctx_cb;
140+
void* m_user_data;
131141
};
132142

133143
} // namespace qcloud_cos

include/request/object_req.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1855,7 +1855,7 @@ class PutDirectoryReq : public PutObjectReq {
18551855
virtual ~PutDirectoryReq() {}
18561856
};
18571857

1858-
class MoveObjectReq {
1858+
class MoveObjectReq : public ObjectReq {
18591859
public:
18601860
MoveObjectReq(const std::string& bucket_name,
18611861
const std::string& src_object_name,

include/util/http_sender.h

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@ class HttpSender {
3232
std::string* resp_body, std::string* err_msg,
3333
bool is_check_md5 = false,
3434
bool is_verify_cert = true,
35-
const std::string& ca_location = "");
35+
const std::string& ca_location = "",
36+
SSLCtxCallback ssl_ctx_cb = nullptr,
37+
void *user_data = nullptr);
3638

3739
static int SendRequest(const SharedTransferHandler& handler,
3840
const std::string& http_method,
@@ -46,7 +48,9 @@ class HttpSender {
4648
std::ostream& resp_stream, std::string* err_msg,
4749
bool is_check_md5 = false,
4850
bool is_verify_cert = true,
49-
const std::string& ca_location = "");
51+
const std::string& ca_location = "",
52+
SSLCtxCallback ssl_ctx_cb = nullptr,
53+
void *user_data = nullptr);
5054

5155
static int SendRequest(const SharedTransferHandler& handler,
5256
const std::string& http_method,
@@ -59,7 +63,9 @@ class HttpSender {
5963
std::string* resp_body, std::string* err_msg,
6064
bool is_check_md5 = false,
6165
bool is_verify_cert = true,
62-
const std::string& ca_location = "");
66+
const std::string& ca_location = "",
67+
SSLCtxCallback ssl_ctx_cb = nullptr,
68+
void *user_data = nullptr);
6369

6470
static int SendRequest(const SharedTransferHandler& handler,
6571
const std::string& http_method,
@@ -72,7 +78,9 @@ class HttpSender {
7278
std::ostream& resp_stream, std::string* err_msg,
7379
bool is_check_md5 = false,
7480
bool is_verify_cert = true,
75-
const std::string& ca_location = "");
81+
const std::string& ca_location = "",
82+
SSLCtxCallback ssl_ctx_cb = nullptr,
83+
void *user_data = nullptr);
7684

7785
static int SendRequest(const SharedTransferHandler& handler,
7886
const std::string& http_method,
@@ -87,7 +95,9 @@ class HttpSender {
8795
std::string* err_msg, uint64_t* real_byte,
8896
bool is_check_md5 = false,
8997
bool is_verify_cert = true,
90-
const std::string& ca_location = "");
98+
const std::string& ca_location = "",
99+
SSLCtxCallback ssl_ctx_cb = nullptr,
100+
void *user_data = nullptr);
91101
};
92102

93103
} // namespace qcloud_cos

0 commit comments

Comments
 (0)