Skip to content

Commit 39aa29a

Browse files
author
zackcao
committed
Merge branch 'dev-add-check-response-body-length' into 'master' (merge request !66)
修复下载对象时没校验实际接收的数据长度,客户端网络环境差时可能导致下载数据不全但不报错的异常问题
2 parents 360bb50 + 6aa0c12 commit 39aa29a

File tree

6 files changed

+197
-239
lines changed

6 files changed

+197
-239
lines changed

include/cos_defines.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
namespace qcloud_cos {
1414

15-
#define COS_CPP_SDK_VERSON "v5.5.18"
15+
#define COS_CPP_SDK_VERSON "v5.5.19"
1616

1717
/// 路径分隔符
1818
const char kPathDelimiter[] = "/";

include/util/http_sender.h

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class HttpSender {
3333
bool is_check_md5 = false,
3434
bool is_verify_cert = true,
3535
const std::string& ca_location = "",
36-
SSLCtxCallback ssl_ctx_cb = nullptr,
36+
const SSLCtxCallback& ssl_ctx_cb = nullptr,
3737
void *user_data = nullptr);
3838

3939
static int SendRequest(const SharedTransferHandler& handler,
@@ -48,43 +48,45 @@ class HttpSender {
4848
bool is_check_md5 = false,
4949
bool is_verify_cert = true,
5050
const std::string& ca_location = "",
51-
SSLCtxCallback ssl_ctx_cb = nullptr,
51+
const SSLCtxCallback& ssl_ctx_cb = nullptr,
5252
void *user_data = nullptr);
5353

5454
static int SendRequest(const SharedTransferHandler& handler,
5555
const std::string& http_method,
5656
const std::string& url_str,
5757
const std::map<std::string, std::string>& req_params,
5858
const std::map<std::string, std::string>& req_headers,
59-
std::istream& is,
59+
std::istream& is, // 流式输入,用于传输请求正文
6060
uint64_t conn_timeout_in_ms,
6161
uint64_t recv_timeout_in_ms,
6262
std::map<std::string, std::string>* resp_headers,
63-
std::ostream& resp_stream,
63+
std::ostream& resp_stream, // 流式输出,用于接收响应正文
6464
std::string* err_msg,
6565
bool is_check_md5 = false,
6666
bool is_verify_cert = true,
6767
const std::string& ca_location = "",
68-
SSLCtxCallback ssl_ctx_cb = nullptr,
68+
const SSLCtxCallback& ssl_ctx_cb = nullptr,
6969
void *user_data = nullptr,
70-
const char *req_body_buf = nullptr,
70+
const char *req_body_buf = nullptr, // 可选的缓冲区
7171
size_t req_body_len = 0);
7272

7373
static int SendRequest(const SharedTransferHandler& handler,
7474
const std::string& http_method,
7575
const std::string& url_str,
7676
const std::map<std::string, std::string>& req_params,
7777
const std::map<std::string, std::string>& req_headers,
78-
const std::string& req_body,
78+
const std::string& req_body, // 字符串输入,用于传输请求正文
7979
uint64_t conn_timeout_in_ms,
8080
uint64_t recv_timeout_in_ms,
8181
std::map<std::string, std::string>* resp_headers,
82-
std::string* xml_err_str, std::ostream& resp_stream,
83-
std::string* err_msg, uint64_t* real_byte,
82+
std::string* xml_err_str, // 额外的错误信息, 用于响应返回非 2xx 错误码时, 传输报错响应信息
83+
std::ostream& resp_stream, // 流式输出, 用于传输响应正文
84+
std::string* err_msg,
85+
uint64_t* real_byte, // 实际接收字节数
8486
bool is_check_md5 = false,
8587
bool is_verify_cert = true,
8688
const std::string& ca_location = "",
87-
SSLCtxCallback ssl_ctx_cb = nullptr,
89+
const SSLCtxCallback& ssl_ctx_cb = nullptr,
8890
void *user_data = nullptr);
8991
};
9092

src/op/base_op.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ CosResult BaseOp::DownloadRequest(const std::string &host, const std::string &pa
320320
result.SetFail();
321321
result.SetErrorMsg("Download failed with incomplete file");
322322
SDK_LOG_ERR("Response content length %" PRIu64
323-
"is not same to real recv byte %" PRIu64,
323+
" is not same to real recv byte %" PRIu64,
324324
resp->GetContentLength(), real_byte);
325325
}
326326

src/op/file_download_task.cpp

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -116,24 +116,27 @@ void FileDownTask::DownTask() {
116116
}
117117

118118
void FileDownTask::SendRequestOnce(std::string domain) {
119-
std::string full_url = m_op_util.GetRealUrl(domain, m_path, m_is_https);
120-
m_http_status = HttpSender::SendRequest(m_handler, "GET", full_url, m_params, m_headers, "", m_conn_timeout_in_ms,
121-
m_recv_timeout_in_ms, &m_resp_headers, &m_resp, &m_err_msg, false, m_verify_cert, m_ca_location, m_ssl_ctx_cb,
122-
m_user_data);
123-
//}
124-
// 当实际长度小于请求的数据长度时httpcode为206
125-
if (m_http_status != 200 && m_http_status != 206) {
126-
m_is_task_success = false;
127-
m_real_down_len = 0;
128-
return;
129-
}
119+
m_resp_headers.clear();
120+
m_resp = "";
130121

131-
size_t buf_max_size = m_data_len;
132-
size_t len = std::min(m_resp.length(), buf_max_size);
133-
memcpy(m_data_buf_ptr, m_resp.c_str(), len);
134-
m_real_down_len = len;
135-
m_is_task_success = true;
136-
m_resp = "";
122+
std::string full_url = m_op_util.GetRealUrl(domain, m_path, m_is_https);
123+
m_http_status = HttpSender::SendRequest(m_handler, "GET", full_url, m_params, m_headers, "", m_conn_timeout_in_ms,
124+
m_recv_timeout_in_ms, &m_resp_headers, &m_resp, &m_err_msg, false, m_verify_cert, m_ca_location, m_ssl_ctx_cb,
125+
m_user_data);
126+
127+
// 当实际长度小于请求的数据长度时httpcode为206
128+
if (m_http_status != 200 && m_http_status != 206) {
129+
m_is_task_success = false;
130+
m_real_down_len = 0;
131+
return;
132+
}
133+
134+
size_t buf_max_size = m_data_len;
135+
size_t len = std::min(m_resp.length(), buf_max_size);
136+
memcpy(m_data_buf_ptr, m_resp.c_str(), len);
137+
m_real_down_len = len;
138+
m_is_task_success = true;
139+
m_resp = "";
137140
}
138141

139142
} // namespace qcloud_cos

src/trsf/transfer_handler.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#include "trsf/transfer_handler.h"
22
#include <iostream>
33
#include "Poco/Buffer.h"
4-
#include "Poco/StreamCopier.h"
54
#include "response/object_resp.h"
65
#include "request/object_req.h"
76
#include "trsf/async_context.h"
@@ -209,10 +208,9 @@ HandleStreamCopier::handleCopyStream(const SharedTransferHandler& handler,
209208
return len;
210209
}
211210

212-
std::streamsize
213-
HandleStreamCopier::handleCopyStream(const SharedTransferHandler& handler,
214-
std::istream& istr, std::ostream& ostr,
215-
std::size_t bufferSize) {
211+
// 代码主要逻辑复制了 Poco::StreamCopier::copyStream(io_tmp, resp_stream) 的代码, 内部加入了客户取消操作的判断逻
212+
std::streamsize HandleStreamCopier::handleCopyStream(
213+
const SharedTransferHandler& handler, std::istream& istr, std::ostream& ostr, std::size_t bufferSize) {
216214
poco_assert(bufferSize > 0);
217215

218216
Poco::Buffer<char> buffer(bufferSize);

0 commit comments

Comments
 (0)