Skip to content

Commit b8b2575

Browse files
chen-hongganggavinhgchen
andauthored
增加SetVerifyCert函数,支持配置不校验服务端证书(默认校验) (#122)
* 增加SetVerifyCert函数,支持配置不校验服务端证书(默认校验) Co-authored-by: gavinhgchen <[email protected]>
1 parent 4470fd3 commit b8b2575

13 files changed

+93
-25
lines changed

include/op/file_copy_task.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ class FileCopyTask : public Poco::Runnable {
3333

3434
void SetHeaders(const std::map<std::string, std::string>& headers);
3535

36+
void SetVerifyCert(bool verify_cert);
3637
void SetCaLocation(const std::string& ca_location);
3738

3839
std::string GetErrMsg() const { return m_err_msg; }
@@ -54,6 +55,8 @@ class FileCopyTask : public Poco::Runnable {
5455
std::string m_err_msg;
5556
std::string m_etag;
5657
std::string m_last_modified;
58+
59+
bool m_verify_cert;
5760
std::string m_ca_location;
5861
};
5962

include/op/file_download_task.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ class FileDownTask : public Poco::Runnable {
2626
uint64_t conn_timeout_in_ms, uint64_t recv_timeout_in_ms,
2727
const SharedTransferHandler& handler = nullptr,
2828
uint64_t offset = 0, unsigned char* pbuf = NULL,
29-
const size_t data_len = 0, const std::string& ca_lication = "");
29+
const size_t data_len = 0,
30+
bool verify_cert = true,
31+
const std::string& ca_lication = "");
3032

3133
~FileDownTask() {}
3234

@@ -36,6 +38,7 @@ class FileDownTask : public Poco::Runnable {
3638

3739
void SetDownParams(unsigned char* pdatabuf, size_t datalen, uint64_t offset);
3840

41+
void SetVerifyCert(bool verify_cert);
3942
void SetCaLocation(const std::string& ca_location);
4043

4144
std::string GetTaskResp();
@@ -67,6 +70,7 @@ class FileDownTask : public Poco::Runnable {
6770
std::map<std::string, std::string> m_resp_headers;
6871
std::string m_err_msg;
6972

73+
bool m_verify_cert;
7074
std::string m_ca_location;
7175

7276
SharedConfig m_config;

include/op/file_upload_task.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,24 @@ class FileUploadTask : public Poco::Runnable {
1414
public:
1515
FileUploadTask(const std::string& full_url, uint64_t conn_timeout_in_ms,
1616
uint64_t recv_timeout_in_ms, unsigned char* pbuf = NULL,
17-
const size_t data_len = 0, const std::string& ca_location = "");
17+
const size_t data_len = 0,
18+
bool verify_cert = true,
19+
const std::string& ca_location = "");
1820

1921
FileUploadTask(const std::string& full_url,
2022
const std::map<std::string, std::string>& headers,
2123
const std::map<std::string, std::string>& params,
2224
uint64_t conn_timeout_in_ms, uint64_t recv_timeout_in_ms,
2325
const SharedTransferHandler& handler,
26+
bool verify_cert = true,
2427
const std::string& ca_location = "");
2528

2629
FileUploadTask(const std::string& full_url,
2730
const std::map<std::string, std::string>& headers,
2831
const std::map<std::string, std::string>& params,
2932
uint64_t conn_timeout_in_ms, uint64_t recv_timeout_in_ms,
3033
unsigned char* pbuf = NULL, const size_t data_len = 0,
34+
bool verify_cert = true,
3135
const std::string& ca_location = "");
3236

3337
~FileUploadTask() {}
@@ -68,6 +72,7 @@ class FileUploadTask : public Poco::Runnable {
6872

6973
uint64_t GetPartNumber() const { return m_part_number; }
7074

75+
void SetVerifyCert(bool verify_cert);
7176
void SetCaLocation(const std::string& ca_location);
7277

7378
private:
@@ -88,6 +93,7 @@ class FileUploadTask : public Poco::Runnable {
8893
uint64_t m_part_number;
8994
SharedTransferHandler m_handler;
9095

96+
bool m_verify_cert;
9197
std::string m_ca_location;
9298
};
9399

include/op/object_op.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,8 @@ class ObjectOp : public BaseOp {
390390
const std::string& range,
391391
const std::map<std::string, std::string>& headers,
392392
const std::map<std::string, std::string>& params,
393-
const std::string& ca_location, FileCopyTask* task);
393+
bool verify_cert,const std::string& ca_location,
394+
FileCopyTask* task);
394395

395396
/// \brief 检查是否可以走断点下载
396397
/// \param req PutObjectByFile请求

include/request/base_req.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,9 @@ class BaseReq {
9393
void SetCaLocation(const std::string& ca_location) { m_ca_location = ca_location; }
9494
const std::string& GetCaLocation() const { return m_ca_location; }
9595

96+
void SetVerifyCert(bool verify_cert) { mb_verify_cert = verify_cert; }
97+
bool GetVerifyCert() const { return mb_verify_cert; }
98+
9699
/// \brief 输出请求的header和param信息
97100
std::string DebugString() const;
98101

@@ -114,6 +117,7 @@ class BaseReq {
114117
bool mb_check_md5; // default is true
115118
bool mb_check_crc64; // default is false
116119

120+
bool mb_verify_cert; // default is true
117121
std::string m_ca_location;
118122
};
119123

include/util/http_sender.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ class HttpSender {
3131
std::map<std::string, std::string>* resp_headers,
3232
std::string* resp_body, std::string* err_msg,
3333
bool is_check_md5 = false,
34+
bool is_verify_cert = true,
3435
const std::string& ca_location = "");
3536

3637
static int SendRequest(const SharedTransferHandler& handler,
@@ -44,6 +45,7 @@ class HttpSender {
4445
std::map<std::string, std::string>* resp_headers,
4546
std::ostream& resp_stream, std::string* err_msg,
4647
bool is_check_md5 = false,
48+
bool is_verify_cert = true,
4749
const std::string& ca_location = "");
4850

4951
static int SendRequest(const SharedTransferHandler& handler,
@@ -56,6 +58,7 @@ class HttpSender {
5658
std::map<std::string, std::string>* resp_headers,
5759
std::string* resp_body, std::string* err_msg,
5860
bool is_check_md5 = false,
61+
bool is_verify_cert = true,
5962
const std::string& ca_location = "");
6063

6164
static int SendRequest(const SharedTransferHandler& handler,
@@ -68,6 +71,7 @@ class HttpSender {
6871
std::map<std::string, std::string>* resp_headers,
6972
std::ostream& resp_stream, std::string* err_msg,
7073
bool is_check_md5 = false,
74+
bool is_verify_cert = true,
7175
const std::string& ca_location = "");
7276

7377
static int SendRequest(const SharedTransferHandler& handler,
@@ -82,6 +86,7 @@ class HttpSender {
8286
std::string* xml_err_str, std::ostream& resp_stream,
8387
std::string* err_msg, uint64_t* real_byte,
8488
bool is_check_md5 = false,
89+
bool is_verify_cert = true,
8590
const std::string& ca_location = "");
8691
};
8792

src/op/base_op.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ CosResult BaseOp::NormalAction(
104104
int http_code = HttpSender::SendRequest(nullptr,
105105
req.GetMethod(), dest_url, req_params, req_headers, req_body,
106106
req.GetConnTimeoutInms(), req.GetRecvTimeoutInms(), &resp_headers,
107-
&resp_body, &err_msg, false, req.GetCaLocation());
107+
&resp_body, &err_msg, false, req.GetVerifyCert(), req.GetCaLocation());
108108
if (http_code == -1) {
109109
result.SetErrorMsg(err_msg);
110110
return result;
@@ -185,7 +185,8 @@ CosResult BaseOp::DownloadAction(const std::string& host,
185185
int http_code = HttpSender::SendRequest(
186186
handler, req.GetMethod(), dest_url, req_params, req_headers, "",
187187
req.GetConnTimeoutInms(), req.GetRecvTimeoutInms(), &resp_headers,
188-
&xml_err_str, os, &err_msg, &real_byte, req.CheckMD5(), req.GetCaLocation());
188+
&xml_err_str, os, &err_msg, &real_byte, req.CheckMD5(),
189+
req.GetVerifyCert(), req.GetCaLocation());
189190
if (http_code == -1) {
190191
result.SetErrorMsg(err_msg);
191192
resp->ParseFromHeaders(resp_headers);
@@ -272,7 +273,7 @@ CosResult BaseOp::UploadAction(
272273
int http_code = HttpSender::SendRequest(
273274
handler, req.GetMethod(), dest_url, req_params, req_headers, is,
274275
req.GetConnTimeoutInms(), req.GetRecvTimeoutInms(), &resp_headers,
275-
&resp_body, &err_msg, false, req.GetCaLocation());
276+
&resp_body, &err_msg, false, req.GetVerifyCert(), req.GetCaLocation());
276277
if (http_code == -1) {
277278
result.SetErrorMsg(err_msg);
278279
return result;

src/op/file_copy_task.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ FileCopyTask::FileCopyTask(const std::string& full_url,
1414
m_conn_timeout_in_ms(conn_timeout_in_ms),
1515
m_recv_timeout_in_ms(recv_timeout_in_ms),
1616
m_is_task_success(false),
17-
m_etag("") {}
17+
m_etag(""),
18+
m_verify_cert(true) {}
1819

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

@@ -37,6 +38,10 @@ void FileCopyTask::SetHeaders(
3738
m_headers.insert(headers.begin(), headers.end());
3839
}
3940

41+
void FileCopyTask::SetVerifyCert(bool verify_cert) {
42+
m_verify_cert = verify_cert;
43+
}
44+
4045
void FileCopyTask::SetCaLocation(const std::string& ca_location) {
4146
m_ca_location = ca_location;
4247
}
@@ -56,7 +61,7 @@ void FileCopyTask::CopyTask() {
5661
m_http_status = HttpSender::SendRequest(nullptr,
5762
"PUT", m_full_url, m_params, m_headers, "", m_conn_timeout_in_ms,
5863
m_recv_timeout_in_ms, &m_resp_headers, &m_resp, &m_err_msg,
59-
false, m_ca_location);
64+
false, m_verify_cert, m_ca_location);
6065

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

src/op/file_download_task.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ FileDownTask::FileDownTask(const std::string& full_url,
1717
const SharedTransferHandler& handler,
1818
uint64_t offset, unsigned char* pbuf,
1919
const size_t data_len,
20+
bool verify_cert,
2021
const std::string& ca_lication)
2122
: m_full_url(full_url),
2223
m_headers(headers),
@@ -30,6 +31,7 @@ FileDownTask::FileDownTask(const std::string& full_url,
3031
m_resp(""),
3132
m_is_task_success(false),
3233
m_real_down_len(0),
34+
m_verify_cert(verify_cert),
3335
m_ca_location(ca_lication) {}
3436

3537
void FileDownTask::run() {
@@ -45,6 +47,10 @@ void FileDownTask::SetDownParams(unsigned char* pbuf, size_t data_len,
4547
m_offset = offset;
4648
}
4749

50+
void FileDownTask::SetVerifyCert(bool verify_cert) {
51+
m_verify_cert = verify_cert;
52+
}
53+
4854
void FileDownTask::SetCaLocation(const std::string& ca_location) {
4955
m_ca_location = ca_location;
5056
}
@@ -86,7 +92,7 @@ void FileDownTask::DownTask() {
8692
m_http_status = HttpSender::SendRequest(
8793
m_handler, "GET", m_full_url, m_params, m_headers, "",
8894
m_conn_timeout_in_ms, m_recv_timeout_in_ms, &m_resp_headers, &m_resp,
89-
&m_err_msg, false, m_ca_location);
95+
&m_err_msg, false, m_verify_cert, m_ca_location);
9096
//}
9197
//当实际长度小于请求的数据长度时httpcode为206
9298
if (m_http_status != 200 && m_http_status != 206) {

src/op/file_upload_task.cpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ namespace qcloud_cos {
1212
FileUploadTask::FileUploadTask(const std::string& full_url,
1313
uint64_t conn_timeout_in_ms,
1414
uint64_t recv_timeout_in_ms, unsigned char* pbuf,
15-
const size_t data_len, const std::string& ca_location)
15+
const size_t data_len,
16+
bool verify_cert,
17+
const std::string& ca_location)
1618
: m_full_url(full_url),
1719
m_conn_timeout_in_ms(conn_timeout_in_ms),
1820
m_recv_timeout_in_ms(recv_timeout_in_ms),
@@ -22,6 +24,7 @@ FileUploadTask::FileUploadTask(const std::string& full_url,
2224
m_is_task_success(false),
2325
m_is_resume(false),
2426
m_handler(NULL),
27+
m_verify_cert(verify_cert),
2528
m_ca_location(ca_location) {}
2629

2730
FileUploadTask::FileUploadTask(
@@ -30,6 +33,7 @@ FileUploadTask::FileUploadTask(
3033
const std::map<std::string, std::string>& params,
3134
uint64_t conn_timeout_in_ms, uint64_t recv_timeout_in_ms,
3235
const SharedTransferHandler& handler,
36+
bool verify_cert,
3337
const std::string& ca_location)
3438
: m_full_url(full_url),
3539
m_headers(headers),
@@ -42,6 +46,7 @@ FileUploadTask::FileUploadTask(
4246
m_is_task_success(false),
4347
m_is_resume(false),
4448
m_handler(handler),
49+
m_verify_cert(verify_cert),
4550
m_ca_location(ca_location) {}
4651

4752
FileUploadTask::FileUploadTask(
@@ -50,6 +55,7 @@ FileUploadTask::FileUploadTask(
5055
const std::map<std::string, std::string>& params,
5156
uint64_t conn_timeout_in_ms, uint64_t recv_timeout_in_ms,
5257
unsigned char* pbuf, const size_t data_len,
58+
bool verify_cert,
5359
const std::string& ca_location)
5460
: m_full_url(full_url),
5561
m_headers(headers),
@@ -62,6 +68,7 @@ FileUploadTask::FileUploadTask(
6268
m_is_task_success(false),
6369
m_is_resume(false),
6470
m_handler(NULL),
71+
m_verify_cert(verify_cert),
6572
m_ca_location(ca_location) {}
6673

6774
void FileUploadTask::run() {
@@ -117,6 +124,10 @@ void FileUploadTask::SetPartNumber(uint64_t part_number) {
117124
m_part_number = part_number;
118125
}
119126

127+
void FileUploadTask::SetVerifyCert(bool verify_cert) {
128+
m_verify_cert = verify_cert;
129+
}
130+
120131
void FileUploadTask::SetCaLocation(const std::string& ca_location) {
121132
m_ca_location = ca_location;
122133
}
@@ -150,7 +161,7 @@ void FileUploadTask::UploadTask() {
150161
m_http_status = HttpSender::SendRequest(
151162
m_handler, "PUT", m_full_url, m_params, m_headers, body,
152163
m_conn_timeout_in_ms, m_recv_timeout_in_ms, &m_resp_headers, &m_resp,
153-
&m_err_msg, false, m_ca_location);
164+
&m_err_msg, false, m_verify_cert, m_ca_location);
154165
//}
155166

156167
if (m_http_status != 200) {

0 commit comments

Comments
 (0)