55#include < sstream>
66#include " cos_sys_config.h"
77#include " util/http_sender.h"
8+ #include " util/base_op_util.h"
89
910namespace qcloud_cos {
1011
11- FileDownTask::FileDownTask (const std::string& full_url,
12+ FileDownTask::FileDownTask (const std::string& host,
13+ const std::string& path,
14+ const bool is_https,
15+ const BaseOpUtil& op_util,
1216 const std::map<std::string, std::string>& headers,
1317 const std::map<std::string, std::string>& params,
1418 uint64_t conn_timeout_in_ms,
@@ -20,7 +24,10 @@ FileDownTask::FileDownTask(const std::string& full_url,
2024 const std::string& ca_lication,
2125 SSLCtxCallback ssl_ctx_cb,
2226 void *user_data)
23- : m_full_url(full_url),
27+ : m_host(host),
28+ m_path (path),
29+ m_is_https(is_https),
30+ m_op_util(op_util),
2431 m_headers(headers),
2532 m_params(params),
2633 m_conn_timeout_in_ms(conn_timeout_in_ms),
@@ -76,39 +83,49 @@ std::map<std::string, std::string> FileDownTask::GetRespHeaders() {
7683}
7784
7885void FileDownTask::DownTask () {
79- char range_head[128 ];
80- memset (range_head, 0 , sizeof (range_head));
81- snprintf (range_head, sizeof (range_head), " bytes=%" PRIu64 " -%" PRIu64, m_offset,
82- (m_offset + m_data_len - 1 ));
83-
84- // 增加Range头域,避免大文件时将整个文件下载
85- m_headers[" Range" ] = range_head;
86-
87- int try_times = 0 ;
88- do {
89- try_times++;
90- // if (m_handler) {
91- // SDK_LOG_INFO("transfer send GET request");
92- // std::istringstream iss("");
93- // std::ostringstream oss;
94- // m_http_status = HttpSender::TransferSendRequest(
95- // m_handler, "GET", m_full_url, m_params, m_headers, iss,
96- // m_conn_timeout_in_ms, m_recv_timeout_in_ms, &m_resp_headers, oss,
97- // &m_err_msg, false);
98- // m_resp = oss.str();
99- // } else {
100- m_http_status = HttpSender::SendRequest (
101- m_handler, " GET" , m_full_url, m_params, m_headers, " " ,
102- m_conn_timeout_in_ms, m_recv_timeout_in_ms, &m_resp_headers, &m_resp,
103- &m_err_msg, false , m_verify_cert, m_ca_location, m_ssl_ctx_cb, m_user_data);
86+ char range_head[128 ];
87+ memset (range_head, 0 , sizeof (range_head));
88+ snprintf (range_head, sizeof (range_head), " bytes=%" PRIu64 " -%" PRIu64, m_offset, (m_offset + m_data_len - 1 ));
89+
90+ // 增加Range头域,避免大文件时将整个文件下载
91+ m_headers[" Range" ] = range_head;
92+
93+ std::string domain = m_host;
94+ for (int i = 0 ;; i++) {
95+ SendRequestOnce (domain);
96+ if (i >= m_op_util.GetMaxRetryTimes ()) {
97+ break ;
98+ }
99+ if (m_is_task_success) {
100+ break ;
101+ }
102+ SDK_LOG_ERR (" FileDownload: host(%s) path(%s) fail, httpcode:%d, resp: %s, try_times: %d" , domain.c_str (),
103+ m_path.c_str (), m_http_status, m_resp.c_str (), i);
104+ if (m_http_status >= 400 && m_http_status < 500 ) {
105+ break ;
106+ }
107+ CosResult result;
108+ result.SetHttpStatus (m_http_status);
109+ result.ParseFromHttpResponse (m_resp_headers, m_resp);
110+ if (m_op_util.ShouldChangeBackupDomain (result, i)) {
111+ domain = m_op_util.ChangeHostSuffix (domain);
112+ }
113+ m_op_util.SleepBeforeRetry (i);
114+ }
115+ return ;
116+ }
117+
118+ 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);
104123 // }
105- // 当实际长度小于请求的数据长度时httpcode为206
124+ // 当实际长度小于请求的数据长度时httpcode为206
106125 if (m_http_status != 200 && m_http_status != 206 ) {
107- SDK_LOG_ERR (" FileDownload: url(%s) fail, httpcode:%d, resp: %s, try_times:%d" ,
108- m_full_url.c_str (), m_http_status, m_resp.c_str (), try_times);
109- m_is_task_success = false ;
110- m_real_down_len = 0 ;
111- continue ;
126+ m_is_task_success = false ;
127+ m_real_down_len = 0 ;
128+ return ;
112129 }
113130
114131 size_t buf_max_size = m_data_len;
@@ -117,10 +134,6 @@ void FileDownTask::DownTask() {
117134 m_real_down_len = len;
118135 m_is_task_success = true ;
119136 m_resp = " " ;
120- return ;
121- } while (!m_is_task_success && try_times <= kMaxRetryTimes );
122-
123- return ;
124137}
125138
126139} // namespace qcloud_cos
0 commit comments