Skip to content

Commit bdbcc96

Browse files
authored
Merge pull request #190 from tencentyun/feature_zackcao_e5e98908
Feature zackcao e5e98908
2 parents 620fe3d + 39aa29a commit bdbcc96

File tree

10 files changed

+211
-256
lines changed

10 files changed

+211
-256
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22
.github
33
.git
44
.vscode
5-
build/*
5+
build/*
6+
cmake-build-debug/*

CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,10 @@ if (${OS_TYPE} STREQUAL "WINDOWS")
4444
message(STATUS "Build type: ${CMAKE_BUILD_TYPE}")
4545

4646
set(POCO_LINK_DIR ${CMAKE_SOURCE_DIR}/third_party/lib/${BUILD_TARGET}/poco)
47+
set(OPENSSL_LINK_DIR ${CMAKE_SOURCE_DIR}/third_party/lib/${BUILD_TARGET}/openssl)
4748

4849
set(SYSTEM_LIBS "")
50+
set(OPENSSL_LIBS libssl libcrypto)
4951
#需要加该参数,不然VS会报错
5052
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /utf-8")
5153
elseif(${OS_TYPE} STREQUAL "iOS")

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/trsf/transfer_handler.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,8 +173,6 @@ class TransferHandler : public std::enable_shared_from_this<TransferHandler> {
173173
std::string m_object_name;
174174
std::string m_local_file_path;
175175
uint64_t m_total_size;
176-
// The m_current_progress best to use the atomic. but can not support c11 for
177-
// now, so use the mutex.
178176
uint64_t m_current_progress;
179177
TransferStatus m_status;
180178
std::string m_uploadid;

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: 10 additions & 14 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"
@@ -49,22 +48,20 @@ static std::string GetNameForStatus(TransferStatus status) {
4948
}
5049

5150
void TransferHandler::UpdateProgress(uint64_t update_prog) {
52-
{
53-
std::lock_guard<std::mutex> locker(m_lock_prog);
51+
std::lock_guard<std::mutex> locker(m_lock_prog);
5452

55-
m_current_progress += update_prog;
53+
m_current_progress += update_prog;
5654

57-
// Notice the progress there can not backwards, but the each parts has retry
58-
// counts, should limit the progress no bigger than the total size. s3 has
59-
// two invariants:(1) Never lock; (2) Never go backwards. Complete me.
60-
if (m_current_progress > m_total_size) {
55+
// Notice the progress there can not backwards, but the each parts has retry
56+
// counts, should limit the progress no bigger than the total size. s3 has
57+
// two invariants:(1) Never lock; (2) Never go backwards. Complete me.
58+
if (m_current_progress > m_total_size) {
6159
m_current_progress = m_total_size;
62-
}
6360
}
6461

6562
// trigger progress callback
6663
if (m_progress_cb) {
67-
m_progress_cb(m_current_progress, m_total_size, m_user_data);
64+
m_progress_cb(m_current_progress, m_total_size, m_user_data);
6865
}
6966
}
7067

@@ -211,10 +208,9 @@ HandleStreamCopier::handleCopyStream(const SharedTransferHandler& handler,
211208
return len;
212209
}
213210

214-
std::streamsize
215-
HandleStreamCopier::handleCopyStream(const SharedTransferHandler& handler,
216-
std::istream& istr, std::ostream& ostr,
217-
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) {
218214
poco_assert(bufferSize > 0);
219215

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

0 commit comments

Comments
 (0)