blob: a128fd88ffd721fd02afef895f907140b0f3d955 [file] [log] [blame]
Alex Deymoaea4c1c2015-08-19 20:24:43 -07001//
2// Copyright (C) 2009 The Android Open Source Project
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15//
rspangler@google.com49fdf182009-10-10 00:57:34 +000016
Alex Deymo39910dc2015-11-09 17:04:30 -080017#include "update_engine/common/libcurl_http_fetcher.h"
Andrew de los Reyesd57d1472010-10-21 13:34:08 -070018
adlr@google.comc98a7ed2009-12-04 18:54:03 +000019#include <algorithm>
Andrew de los Reyesd57d1472010-10-21 13:34:08 -070020#include <string>
21
Alex Vakulenko4906c1c2014-08-21 13:17:44 -070022#include <base/bind.h>
Alex Deymoc00c98a2015-03-17 17:38:00 -070023#include <base/format_macros.h>
Alex Deymo60ca1a72015-06-18 18:19:15 -070024#include <base/location.h>
Andrew de los Reyesd57d1472010-10-21 13:34:08 -070025#include <base/logging.h>
Alex Vakulenko75039d72014-03-25 12:36:28 -070026#include <base/strings/string_util.h>
27#include <base/strings/stringprintf.h>
Andrew de los Reyesd57d1472010-10-21 13:34:08 -070028
Alex Deymo39910dc2015-11-09 17:04:30 -080029#include "update_engine/common/certificate_checker.h"
30#include "update_engine/common/hardware_interface.h"
31#include "update_engine/common/platform_constants.h"
adlr@google.comc98a7ed2009-12-04 18:54:03 +000032
Alex Deymo60ca1a72015-06-18 18:19:15 -070033using base::TimeDelta;
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070034using brillo::MessageLoop;
Alex Deymoc4acdf42014-05-28 21:07:10 -070035using std::max;
Andrew de los Reyesd57d1472010-10-21 13:34:08 -070036using std::string;
rspangler@google.com49fdf182009-10-10 00:57:34 +000037
38// This is a concrete implementation of HttpFetcher that uses libcurl to do the
39// http work.
40
41namespace chromeos_update_engine {
42
Andrew de los Reyes9bbd1872010-07-16 14:52:29 -070043namespace {
Andrew de los Reyes5d0783d2010-11-29 18:14:16 -080044const int kNoNetworkRetrySeconds = 10;
Alex Vakulenkod2779df2014-06-16 13:19:00 -070045} // namespace
Andrew de los Reyes9bbd1872010-07-16 14:52:29 -070046
Alex Deymo33e91e72015-12-01 18:26:08 -030047LibcurlHttpFetcher::LibcurlHttpFetcher(ProxyResolver* proxy_resolver,
48 HardwareInterface* hardware)
49 : HttpFetcher(proxy_resolver), hardware_(hardware) {
Alex Deymoc1c17b42015-11-23 03:53:15 -030050 // Dev users want a longer timeout (180 seconds) because they may
51 // be waiting on the dev server to build an image.
52 if (!hardware_->IsOfficialBuild())
53 low_speed_time_seconds_ = kDownloadDevModeLowSpeedTimeSeconds;
Alex Deymo46a9aae2016-05-04 20:20:11 -070054 if (hardware_->IsOOBEEnabled() && !hardware_->IsOOBEComplete(nullptr))
Alex Deymoc1c17b42015-11-23 03:53:15 -030055 max_retry_count_ = kDownloadMaxRetryCountOobeNotComplete;
56}
57
rspangler@google.com49fdf182009-10-10 00:57:34 +000058LibcurlHttpFetcher::~LibcurlHttpFetcher() {
Darin Petkov9ce452b2010-11-17 14:33:28 -080059 LOG_IF(ERROR, transfer_in_progress_)
60 << "Destroying the fetcher while a transfer is in progress.";
rspangler@google.com49fdf182009-10-10 00:57:34 +000061 CleanUp();
62}
63
Alex Deymof329b932014-10-30 01:37:48 -070064bool LibcurlHttpFetcher::GetProxyType(const string& proxy,
Gilad Arnold59d9e012013-07-23 16:41:43 -070065 curl_proxytype* out_type) {
Alex Deymo56ccb072016-02-05 00:50:48 -080066 if (base::StartsWith(
67 proxy, "socks5://", base::CompareCase::INSENSITIVE_ASCII) ||
68 base::StartsWith(
69 proxy, "socks://", base::CompareCase::INSENSITIVE_ASCII)) {
Gilad Arnold59d9e012013-07-23 16:41:43 -070070 *out_type = CURLPROXY_SOCKS5_HOSTNAME;
71 return true;
72 }
Alex Deymo56ccb072016-02-05 00:50:48 -080073 if (base::StartsWith(
74 proxy, "socks4://", base::CompareCase::INSENSITIVE_ASCII)) {
Gilad Arnold59d9e012013-07-23 16:41:43 -070075 *out_type = CURLPROXY_SOCKS4A;
76 return true;
77 }
Alex Deymo56ccb072016-02-05 00:50:48 -080078 if (base::StartsWith(
79 proxy, "http://", base::CompareCase::INSENSITIVE_ASCII) ||
80 base::StartsWith(
81 proxy, "https://", base::CompareCase::INSENSITIVE_ASCII)) {
Gilad Arnold59d9e012013-07-23 16:41:43 -070082 *out_type = CURLPROXY_HTTP;
83 return true;
84 }
Alex Deymo56ccb072016-02-05 00:50:48 -080085 if (base::StartsWith(proxy, kNoProxy, base::CompareCase::INSENSITIVE_ASCII)) {
Gilad Arnold59d9e012013-07-23 16:41:43 -070086 // known failure case. don't log.
87 return false;
88 }
89 LOG(INFO) << "Unknown proxy type: " << proxy;
90 return false;
91}
92
Alex Deymof329b932014-10-30 01:37:48 -070093void LibcurlHttpFetcher::ResumeTransfer(const string& url) {
Andrew de los Reyes3270f742010-07-15 22:28:14 -070094 LOG(INFO) << "Starting/Resuming transfer";
rspangler@google.com49fdf182009-10-10 00:57:34 +000095 CHECK(!transfer_in_progress_);
96 url_ = url;
97 curl_multi_handle_ = curl_multi_init();
98 CHECK(curl_multi_handle_);
99
100 curl_handle_ = curl_easy_init();
101 CHECK(curl_handle_);
Alex Deymof2858572016-02-25 11:20:13 -0800102 ignore_failure_ = false;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000103
Andrew de los Reyes45168102010-11-22 11:13:50 -0800104 CHECK(HasProxy());
Gilad Arnoldfbaee242012-04-04 15:59:43 -0700105 bool is_direct = (GetCurrentProxy() == kNoProxy);
106 LOG(INFO) << "Using proxy: " << (is_direct ? "no" : "yes");
107 if (is_direct) {
Andrew de los Reyes45168102010-11-22 11:13:50 -0800108 CHECK_EQ(curl_easy_setopt(curl_handle_,
109 CURLOPT_PROXY,
110 ""), CURLE_OK);
111 } else {
112 CHECK_EQ(curl_easy_setopt(curl_handle_,
113 CURLOPT_PROXY,
114 GetCurrentProxy().c_str()), CURLE_OK);
115 // Curl seems to require us to set the protocol
116 curl_proxytype type;
Gilad Arnold59d9e012013-07-23 16:41:43 -0700117 if (GetProxyType(GetCurrentProxy(), &type)) {
Andrew de los Reyes45168102010-11-22 11:13:50 -0800118 CHECK_EQ(curl_easy_setopt(curl_handle_,
119 CURLOPT_PROXYTYPE,
120 type), CURLE_OK);
121 }
122 }
123
rspangler@google.com49fdf182009-10-10 00:57:34 +0000124 if (post_data_set_) {
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000125 CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_POST, 1), CURLE_OK);
126 CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_POSTFIELDS,
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800127 post_data_.data()),
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000128 CURLE_OK);
129 CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_POSTFIELDSIZE,
130 post_data_.size()),
131 CURLE_OK);
Alex Deymofdd6dec2016-03-03 22:35:43 -0800132 }
Gilad Arnold9dd1e7c2012-02-16 12:13:36 -0800133
Alex Deymofdd6dec2016-03-03 22:35:43 -0800134 // Setup extra HTTP headers.
135 if (curl_http_headers_) {
136 curl_slist_free_all(curl_http_headers_);
137 curl_http_headers_ = nullptr;
138 }
139 for (const auto& header : extra_headers_) {
140 // curl_slist_append() copies the string.
141 curl_http_headers_ =
142 curl_slist_append(curl_http_headers_, header.second.c_str());
143 }
144 if (post_data_set_) {
Gilad Arnold9dd1e7c2012-02-16 12:13:36 -0800145 // Set the Content-Type HTTP header, if one was specifically set.
Gilad Arnold9dd1e7c2012-02-16 12:13:36 -0800146 if (post_content_type_ != kHttpContentTypeUnspecified) {
Alex Deymofdd6dec2016-03-03 22:35:43 -0800147 const string content_type_attr = base::StringPrintf(
148 "Content-Type: %s", GetHttpContentTypeString(post_content_type_));
149 curl_http_headers_ =
150 curl_slist_append(curl_http_headers_, content_type_attr.c_str());
Gilad Arnold9dd1e7c2012-02-16 12:13:36 -0800151 } else {
152 LOG(WARNING) << "no content type set, using libcurl default";
153 }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000154 }
Alex Deymofdd6dec2016-03-03 22:35:43 -0800155 CHECK_EQ(
156 curl_easy_setopt(curl_handle_, CURLOPT_HTTPHEADER, curl_http_headers_),
157 CURLE_OK);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000158
Gilad Arnolde4ad2502011-12-29 17:08:54 -0800159 if (bytes_downloaded_ > 0 || download_length_) {
160 // Resume from where we left off.
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000161 resume_offset_ = bytes_downloaded_;
Gilad Arnolde4ad2502011-12-29 17:08:54 -0800162 CHECK_GE(resume_offset_, 0);
163
164 // Compute end offset, if one is specified. As per HTTP specification, this
165 // is an inclusive boundary. Make sure it doesn't overflow.
166 size_t end_offset = 0;
167 if (download_length_) {
168 end_offset = static_cast<size_t>(resume_offset_) + download_length_ - 1;
169 CHECK_LE((size_t) resume_offset_, end_offset);
170 }
171
172 // Create a string representation of the desired range.
Alex Deymoc00c98a2015-03-17 17:38:00 -0700173 string range_str = base::StringPrintf(
174 "%" PRIu64 "-", static_cast<uint64_t>(resume_offset_));
175 if (end_offset)
176 range_str += std::to_string(end_offset);
Gilad Arnolde4ad2502011-12-29 17:08:54 -0800177 CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_RANGE, range_str.c_str()),
178 CURLE_OK);
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000179 }
180
181 CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_WRITEDATA, this), CURLE_OK);
182 CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_WRITEFUNCTION,
183 StaticLibcurlWrite), CURLE_OK);
Chris Sosa77f79e82014-06-02 18:16:24 -0700184 CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_URL, url_.c_str()),
Andrew de los Reyesd57d1472010-10-21 13:34:08 -0700185 CURLE_OK);
Andrew de los Reyes3270f742010-07-15 22:28:14 -0700186
David Zeuthen34135a92013-08-06 11:16:16 -0700187 // If the connection drops under |low_speed_limit_bps_| (10
188 // bytes/sec by default) for |low_speed_time_seconds_| (90 seconds,
189 // 180 on non-official builds), reconnect.
190 CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_LOW_SPEED_LIMIT,
191 low_speed_limit_bps_),
Andrew de los Reyes3270f742010-07-15 22:28:14 -0700192 CURLE_OK);
David Zeuthen34135a92013-08-06 11:16:16 -0700193 CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_LOW_SPEED_TIME,
194 low_speed_time_seconds_),
Andrew de los Reyes3270f742010-07-15 22:28:14 -0700195 CURLE_OK);
David Zeuthen34135a92013-08-06 11:16:16 -0700196 CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_CONNECTTIMEOUT,
197 connect_timeout_seconds_),
Andrew de los Reyese72f9c02011-04-20 10:47:40 -0700198 CURLE_OK);
Andrew de los Reyes3270f742010-07-15 22:28:14 -0700199
Darin Petkov41c2fcf2010-08-25 13:14:48 -0700200 // By default, libcurl doesn't follow redirections. Allow up to
David Zeuthen34135a92013-08-06 11:16:16 -0700201 // |kDownloadMaxRedirects| redirections.
Darin Petkov3a4016a2010-09-28 13:54:17 -0700202 CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_FOLLOWLOCATION, 1), CURLE_OK);
David Zeuthen34135a92013-08-06 11:16:16 -0700203 CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_MAXREDIRS,
204 kDownloadMaxRedirects),
Darin Petkov41c2fcf2010-08-25 13:14:48 -0700205 CURLE_OK);
206
Nam T. Nguyen7d623eb2014-05-13 16:06:28 -0700207 // Lock down the appropriate curl options for HTTP or HTTPS depending on
208 // the url.
Alex Deymoc1c17b42015-11-23 03:53:15 -0300209 if (hardware_->IsOfficialBuild()) {
Alex Deymo56ccb072016-02-05 00:50:48 -0800210 if (base::StartsWith(
211 url_, "http://", base::CompareCase::INSENSITIVE_ASCII)) {
Jay Srinivasanb3f55402012-12-03 18:12:04 -0800212 SetCurlOptionsForHttp();
Alex Deymo56ccb072016-02-05 00:50:48 -0800213 } else if (base::StartsWith(
214 url_, "https://", base::CompareCase::INSENSITIVE_ASCII)) {
Jay Srinivasanb3f55402012-12-03 18:12:04 -0800215 SetCurlOptionsForHttps();
Alex Deymo56ccb072016-02-05 00:50:48 -0800216#if !defined(__CHROMEOS__) && !defined(__BRILLO__)
217 } else if (base::StartsWith(
218 url_, "file://", base::CompareCase::INSENSITIVE_ASCII)) {
219 SetCurlOptionsForFile();
220#endif
221 } else {
222 LOG(ERROR) << "Received invalid URI: " << url_;
223 // Lock down to no protocol supported for the transfer.
224 CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_PROTOCOLS, 0), CURLE_OK);
225 }
Jay Srinivasanb3f55402012-12-03 18:12:04 -0800226 } else {
Nam T. Nguyen7d623eb2014-05-13 16:06:28 -0700227 LOG(INFO) << "Not setting http(s) curl options because we are "
228 << "running a dev/test image";
Darin Petkovfc7a0ce2010-10-25 10:38:37 -0700229 }
230
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000231 CHECK_EQ(curl_multi_add_handle(curl_multi_handle_, curl_handle_), CURLM_OK);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000232 transfer_in_progress_ = true;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000233}
234
Jay Srinivasanb3f55402012-12-03 18:12:04 -0800235// Lock down only the protocol in case of HTTP.
236void LibcurlHttpFetcher::SetCurlOptionsForHttp() {
237 LOG(INFO) << "Setting up curl options for HTTP";
238 CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_PROTOCOLS, CURLPROTO_HTTP),
239 CURLE_OK);
240 CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_REDIR_PROTOCOLS,
241 CURLPROTO_HTTP),
242 CURLE_OK);
243}
244
245// Security lock-down in official builds: makes sure that peer certificate
246// verification is enabled, restricts the set of trusted certificates,
247// restricts protocols to HTTPS, restricts ciphers to HIGH.
248void LibcurlHttpFetcher::SetCurlOptionsForHttps() {
249 LOG(INFO) << "Setting up curl options for HTTPS";
250 CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_SSL_VERIFYPEER, 1),
251 CURLE_OK);
Alex Deymo35b35842015-10-20 11:21:56 -0700252 CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_CAPATH,
253 constants::kCACertificatesPath),
Jay Srinivasanb3f55402012-12-03 18:12:04 -0800254 CURLE_OK);
255 CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_PROTOCOLS, CURLPROTO_HTTPS),
256 CURLE_OK);
257 CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_REDIR_PROTOCOLS,
258 CURLPROTO_HTTPS),
259 CURLE_OK);
260 CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_SSL_CIPHER_LIST, "HIGH:!ADH"),
261 CURLE_OK);
Alex Deymo33e91e72015-12-01 18:26:08 -0300262 if (server_to_check_ != ServerToCheck::kNone) {
263 CHECK_EQ(
264 curl_easy_setopt(curl_handle_, CURLOPT_SSL_CTX_DATA, &server_to_check_),
265 CURLE_OK);
Jay Srinivasanb3f55402012-12-03 18:12:04 -0800266 CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_SSL_CTX_FUNCTION,
267 CertificateChecker::ProcessSSLContext),
268 CURLE_OK);
269 }
270}
271
Alex Deymo56ccb072016-02-05 00:50:48 -0800272// Lock down only the protocol in case of a local file.
273void LibcurlHttpFetcher::SetCurlOptionsForFile() {
274 LOG(INFO) << "Setting up curl options for FILE";
275 CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_PROTOCOLS, CURLPROTO_FILE),
276 CURLE_OK);
277 CHECK_EQ(
278 curl_easy_setopt(curl_handle_, CURLOPT_REDIR_PROTOCOLS, CURLPROTO_FILE),
279 CURLE_OK);
280}
Jay Srinivasanb3f55402012-12-03 18:12:04 -0800281
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000282// Begins the transfer, which must not have already been started.
Alex Deymof329b932014-10-30 01:37:48 -0700283void LibcurlHttpFetcher::BeginTransfer(const string& url) {
Andrew de los Reyesf3ed8e72011-02-16 10:35:46 -0800284 CHECK(!transfer_in_progress_);
285 url_ = url;
Alex Vakulenko4906c1c2014-08-21 13:17:44 -0700286 auto closure = base::Bind(&LibcurlHttpFetcher::ProxiesResolved,
287 base::Unretained(this));
Daniel Erat941cf232017-04-20 12:09:58 -0600288 ResolveProxiesForUrl(url_, closure);
Andrew de los Reyesf3ed8e72011-02-16 10:35:46 -0800289}
290
291void LibcurlHttpFetcher::ProxiesResolved() {
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000292 transfer_size_ = -1;
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000293 resume_offset_ = 0;
Andrew de los Reyes9bbd1872010-07-16 14:52:29 -0700294 retry_count_ = 0;
Darin Petkova0929552010-11-29 14:19:06 -0800295 no_network_retry_count_ = 0;
Darin Petkovcb466212010-08-26 09:40:11 -0700296 http_response_code_ = 0;
Andrew de los Reyes819fef22010-12-17 11:33:58 -0800297 terminate_requested_ = false;
Gilad Arnolda2dee1d2012-04-12 11:50:37 -0700298 sent_byte_ = false;
Alex Deymof2858572016-02-25 11:20:13 -0800299
300 // If we are paused, we delay these two operations until Unpause is called.
301 if (transfer_paused_) {
302 restart_transfer_on_unpause_ = true;
303 return;
304 }
Andrew de los Reyesf3ed8e72011-02-16 10:35:46 -0800305 ResumeTransfer(url_);
Andrew de los Reyes9bbd1872010-07-16 14:52:29 -0700306 CurlPerformOnce();
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000307}
308
Darin Petkov9ce452b2010-11-17 14:33:28 -0800309void LibcurlHttpFetcher::ForceTransferTermination() {
310 CleanUp();
311 if (delegate_) {
312 // Note that after the callback returns this object may be destroyed.
313 delegate_->TransferTerminated(this);
314 }
315}
316
rspangler@google.com49fdf182009-10-10 00:57:34 +0000317void LibcurlHttpFetcher::TerminateTransfer() {
Darin Petkov9ce452b2010-11-17 14:33:28 -0800318 if (in_write_callback_) {
Andrew de los Reyes3fd5d302010-10-07 20:07:18 -0700319 terminate_requested_ = true;
Darin Petkov9ce452b2010-11-17 14:33:28 -0800320 } else {
321 ForceTransferTermination();
322 }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000323}
324
Alex Deymofdd6dec2016-03-03 22:35:43 -0800325void LibcurlHttpFetcher::SetHeader(const string& header_name,
326 const string& header_value) {
327 string header_line = header_name + ": " + header_value;
328 // Avoid the space if no data on the right side of the semicolon.
329 if (header_value.empty())
330 header_line = header_name + ":";
331 TEST_AND_RETURN(header_line.find('\n') == string::npos);
332 TEST_AND_RETURN(header_name.find(':') == string::npos);
333 extra_headers_[base::ToLowerASCII(header_name)] = header_line;
334}
335
Andrew de los Reyescb319332010-07-19 10:55:01 -0700336void LibcurlHttpFetcher::CurlPerformOnce() {
rspangler@google.com49fdf182009-10-10 00:57:34 +0000337 CHECK(transfer_in_progress_);
338 int running_handles = 0;
339 CURLMcode retcode = CURLM_CALL_MULTI_PERFORM;
340
341 // libcurl may request that we immediately call curl_multi_perform after it
342 // returns, so we do. libcurl promises that curl_multi_perform will not block.
343 while (CURLM_CALL_MULTI_PERFORM == retcode) {
344 retcode = curl_multi_perform(curl_multi_handle_, &running_handles);
Andrew de los Reyes3fd5d302010-10-07 20:07:18 -0700345 if (terminate_requested_) {
Darin Petkov9ce452b2010-11-17 14:33:28 -0800346 ForceTransferTermination();
Andrew de los Reyes3fd5d302010-10-07 20:07:18 -0700347 return;
348 }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000349 }
Alex Deymof2858572016-02-25 11:20:13 -0800350
351 // If the transfer completes while paused, we should ignore the failure once
352 // the fetcher is unpaused.
353 if (running_handles == 0 && transfer_paused_ && !ignore_failure_) {
354 LOG(INFO) << "Connection closed while paused, ignoring failure.";
355 ignore_failure_ = true;
356 }
357
358 if (running_handles != 0 || transfer_paused_) {
359 // There's either more work to do or we are paused, so we just keep the
360 // file descriptors to watch up to date and exit, until we are done with the
361 // work and we are not paused.
362 SetupMessageLoopSources();
363 return;
364 }
365
366 // At this point, the transfer was completed in some way (error, connection
367 // closed or download finished).
368
369 GetHttpResponseCode();
370 if (http_response_code_) {
371 LOG(INFO) << "HTTP response code: " << http_response_code_;
372 no_network_retry_count_ = 0;
373 } else {
374 LOG(ERROR) << "Unable to get http response code.";
375 }
376
377 // we're done!
378 CleanUp();
379
380 // TODO(petkov): This temporary code tries to deal with the case where the
381 // update engine performs an update check while the network is not ready
382 // (e.g., right after resume). Longer term, we should check if the network
383 // is online/offline and return an appropriate error code.
384 if (!sent_byte_ &&
385 http_response_code_ == 0 &&
386 no_network_retry_count_ < no_network_max_retries_) {
387 no_network_retry_count_++;
388 MessageLoop::current()->PostDelayedTask(
389 FROM_HERE,
390 base::Bind(&LibcurlHttpFetcher::RetryTimeoutCallback,
391 base::Unretained(this)),
392 TimeDelta::FromSeconds(kNoNetworkRetrySeconds));
393 LOG(INFO) << "No HTTP response, retry " << no_network_retry_count_;
394 } else if ((!sent_byte_ && !IsHttpResponseSuccess()) ||
395 IsHttpResponseError()) {
396 // The transfer completed w/ error and we didn't get any bytes.
397 // If we have another proxy to try, try that.
398 //
399 // TODO(garnold) in fact there are two separate cases here: one case is an
400 // other-than-success return code (including no return code) and no
401 // received bytes, which is necessary due to the way callbacks are
402 // currently processing error conditions; the second is an explicit HTTP
403 // error code, where some data may have been received (as in the case of a
404 // semi-successful multi-chunk fetch). This is a confusing behavior and
405 // should be unified into a complete, coherent interface.
406 LOG(INFO) << "Transfer resulted in an error (" << http_response_code_
407 << "), " << bytes_downloaded_ << " bytes downloaded";
408
409 PopProxy(); // Delete the proxy we just gave up on.
410
411 if (HasProxy()) {
412 // We have another proxy. Retry immediately.
413 LOG(INFO) << "Retrying with next proxy setting";
414 MessageLoop::current()->PostTask(
415 FROM_HERE,
416 base::Bind(&LibcurlHttpFetcher::RetryTimeoutCallback,
417 base::Unretained(this)));
Andrew de los Reyes9bbd1872010-07-16 14:52:29 -0700418 } else {
Alex Deymof2858572016-02-25 11:20:13 -0800419 // Out of proxies. Give up.
420 LOG(INFO) << "No further proxies, indicating transfer complete";
421 if (delegate_)
422 delegate_->TransferComplete(this, false); // signal fail
Alex Deymo021a45e2016-03-15 13:12:05 -0700423 return;
Andrew de los Reyes9bbd1872010-07-16 14:52:29 -0700424 }
Alex Deymof2858572016-02-25 11:20:13 -0800425 } else if ((transfer_size_ >= 0) && (bytes_downloaded_ < transfer_size_)) {
426 if (!ignore_failure_)
427 retry_count_++;
428 LOG(INFO) << "Transfer interrupted after downloading "
429 << bytes_downloaded_ << " of " << transfer_size_ << " bytes. "
430 << transfer_size_ - bytes_downloaded_ << " bytes remaining "
431 << "after " << retry_count_ << " attempt(s)";
Darin Petkov192ced42010-07-23 16:20:24 -0700432
Alex Deymof2858572016-02-25 11:20:13 -0800433 if (retry_count_ > max_retry_count_) {
434 LOG(INFO) << "Reached max attempts (" << retry_count_ << ")";
435 if (delegate_)
436 delegate_->TransferComplete(this, false); // signal fail
Alex Deymo021a45e2016-03-15 13:12:05 -0700437 return;
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000438 }
Alex Deymo021a45e2016-03-15 13:12:05 -0700439 // Need to restart transfer
440 LOG(INFO) << "Restarting transfer to download the remaining bytes";
441 MessageLoop::current()->PostDelayedTask(
442 FROM_HERE,
443 base::Bind(&LibcurlHttpFetcher::RetryTimeoutCallback,
444 base::Unretained(this)),
445 TimeDelta::FromSeconds(retry_seconds_));
rspangler@google.com49fdf182009-10-10 00:57:34 +0000446 } else {
Alex Deymof2858572016-02-25 11:20:13 -0800447 LOG(INFO) << "Transfer completed (" << http_response_code_
448 << "), " << bytes_downloaded_ << " bytes downloaded";
449 if (delegate_) {
450 bool success = IsHttpResponseSuccess();
451 delegate_->TransferComplete(this, success);
452 }
Alex Deymo021a45e2016-03-15 13:12:05 -0700453 return;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000454 }
Alex Deymo021a45e2016-03-15 13:12:05 -0700455 // If we reach this point is because TransferComplete() was not called in any
456 // of the previous branches. The delegate is allowed to destroy the object
457 // once TransferComplete is called so this would be illegal.
Alex Deymof2858572016-02-25 11:20:13 -0800458 ignore_failure_ = false;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000459}
460
461size_t LibcurlHttpFetcher::LibcurlWrite(void *ptr, size_t size, size_t nmemb) {
Gilad Arnold48085ba2011-11-16 09:36:08 -0800462 // Update HTTP response first.
Andrew de los Reyes3fd5d302010-10-07 20:07:18 -0700463 GetHttpResponseCode();
Gilad Arnold48085ba2011-11-16 09:36:08 -0800464 const size_t payload_size = size * nmemb;
465
466 // Do nothing if no payload or HTTP response is an error.
Gilad Arnold9bedeb52011-11-17 16:19:57 -0800467 if (payload_size == 0 || !IsHttpResponseSuccess()) {
Gilad Arnold48085ba2011-11-16 09:36:08 -0800468 LOG(INFO) << "HTTP response unsuccessful (" << http_response_code_
469 << ") or no payload (" << payload_size << "), nothing to do";
470 return 0;
471 }
472
473 sent_byte_ = true;
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000474 {
475 double transfer_size_double;
476 CHECK_EQ(curl_easy_getinfo(curl_handle_,
477 CURLINFO_CONTENT_LENGTH_DOWNLOAD,
478 &transfer_size_double), CURLE_OK);
479 off_t new_transfer_size = static_cast<off_t>(transfer_size_double);
480 if (new_transfer_size > 0) {
481 transfer_size_ = resume_offset_ + new_transfer_size;
482 }
483 }
Gilad Arnold48085ba2011-11-16 09:36:08 -0800484 bytes_downloaded_ += payload_size;
Andrew de los Reyes3fd5d302010-10-07 20:07:18 -0700485 in_write_callback_ = true;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000486 if (delegate_)
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800487 delegate_->ReceivedBytes(this, ptr, payload_size);
Andrew de los Reyes3fd5d302010-10-07 20:07:18 -0700488 in_write_callback_ = false;
Gilad Arnold48085ba2011-11-16 09:36:08 -0800489 return payload_size;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000490}
491
492void LibcurlHttpFetcher::Pause() {
Alex Deymof2858572016-02-25 11:20:13 -0800493 if (transfer_paused_) {
494 LOG(ERROR) << "Fetcher already paused.";
495 return;
496 }
497 transfer_paused_ = true;
498 if (!transfer_in_progress_) {
499 // If pause before we started a connection, we don't need to notify curl
500 // about that, we will simply not start the connection later.
501 return;
502 }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000503 CHECK(curl_handle_);
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000504 CHECK_EQ(curl_easy_pause(curl_handle_, CURLPAUSE_ALL), CURLE_OK);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000505}
506
507void LibcurlHttpFetcher::Unpause() {
Alex Deymof2858572016-02-25 11:20:13 -0800508 if (!transfer_paused_) {
509 LOG(ERROR) << "Resume attempted when fetcher not paused.";
510 return;
511 }
512 transfer_paused_ = false;
513 if (restart_transfer_on_unpause_) {
514 restart_transfer_on_unpause_ = false;
515 ResumeTransfer(url_);
516 CurlPerformOnce();
517 return;
518 }
519 if (!transfer_in_progress_) {
520 // If resumed before starting the connection, there's no need to notify
521 // anybody. We will simply start the connection once it is time.
522 return;
523 }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000524 CHECK(curl_handle_);
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000525 CHECK_EQ(curl_easy_pause(curl_handle_, CURLPAUSE_CONT), CURLE_OK);
Alex Deymof2858572016-02-25 11:20:13 -0800526 // Since the transfer is in progress, we need to dispatch a CurlPerformOnce()
527 // now to let the connection continue, otherwise it would be called by the
528 // TimeoutCallback but with a delay.
529 CurlPerformOnce();
rspangler@google.com49fdf182009-10-10 00:57:34 +0000530}
531
Alex Deymo29b81532015-07-09 11:51:49 -0700532// This method sets up callbacks with the MessageLoop.
533void LibcurlHttpFetcher::SetupMessageLoopSources() {
rspangler@google.com49fdf182009-10-10 00:57:34 +0000534 fd_set fd_read;
535 fd_set fd_write;
Darin Petkov60e14152010-10-27 16:57:04 -0700536 fd_set fd_exc;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000537
538 FD_ZERO(&fd_read);
539 FD_ZERO(&fd_write);
Darin Petkov60e14152010-10-27 16:57:04 -0700540 FD_ZERO(&fd_exc);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000541
542 int fd_max = 0;
543
544 // Ask libcurl for the set of file descriptors we should track on its
545 // behalf.
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000546 CHECK_EQ(curl_multi_fdset(curl_multi_handle_, &fd_read, &fd_write,
Darin Petkov60e14152010-10-27 16:57:04 -0700547 &fd_exc, &fd_max), CURLM_OK);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000548
549 // We should iterate through all file descriptors up to libcurl's fd_max or
Darin Petkov60e14152010-10-27 16:57:04 -0700550 // the highest one we're tracking, whichever is larger.
Alex Deymo29b81532015-07-09 11:51:49 -0700551 for (size_t t = 0; t < arraysize(fd_task_maps_); ++t) {
552 if (!fd_task_maps_[t].empty())
553 fd_max = max(fd_max, fd_task_maps_[t].rbegin()->first);
Darin Petkov60e14152010-10-27 16:57:04 -0700554 }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000555
Darin Petkov60e14152010-10-27 16:57:04 -0700556 // For each fd, if we're not tracking it, track it. If we are tracking it, but
557 // libcurl doesn't care about it anymore, stop tracking it. After this loop,
Alex Deymo29b81532015-07-09 11:51:49 -0700558 // there should be exactly as many tasks scheduled in fd_task_maps_[0|1] as
Darin Petkov60e14152010-10-27 16:57:04 -0700559 // there are read/write fds that we're tracking.
560 for (int fd = 0; fd <= fd_max; ++fd) {
561 // Note that fd_exc is unused in the current version of libcurl so is_exc
562 // should always be false.
563 bool is_exc = FD_ISSET(fd, &fd_exc) != 0;
564 bool must_track[2] = {
565 is_exc || (FD_ISSET(fd, &fd_read) != 0), // track 0 -- read
566 is_exc || (FD_ISSET(fd, &fd_write) != 0) // track 1 -- write
567 };
Alex Deymo29b81532015-07-09 11:51:49 -0700568 MessageLoop::WatchMode watch_modes[2] = {
569 MessageLoop::WatchMode::kWatchRead,
570 MessageLoop::WatchMode::kWatchWrite,
571 };
Darin Petkov60e14152010-10-27 16:57:04 -0700572
Alex Deymo29b81532015-07-09 11:51:49 -0700573 for (size_t t = 0; t < arraysize(fd_task_maps_); ++t) {
574 auto fd_task_it = fd_task_maps_[t].find(fd);
575 bool tracked = fd_task_it != fd_task_maps_[t].end();
Darin Petkov60e14152010-10-27 16:57:04 -0700576
577 if (!must_track[t]) {
578 // If we have an outstanding io_channel, remove it.
579 if (tracked) {
Alex Deymo29b81532015-07-09 11:51:49 -0700580 MessageLoop::current()->CancelTask(fd_task_it->second);
581 fd_task_maps_[t].erase(fd_task_it);
Darin Petkov60e14152010-10-27 16:57:04 -0700582 }
583 continue;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000584 }
Darin Petkov60e14152010-10-27 16:57:04 -0700585
586 // If we are already tracking this fd, continue -- nothing to do.
587 if (tracked)
588 continue;
589
Darin Petkov60e14152010-10-27 16:57:04 -0700590 // Track a new fd.
Alex Deymo29b81532015-07-09 11:51:49 -0700591 fd_task_maps_[t][fd] = MessageLoop::current()->WatchFileDescriptor(
592 FROM_HERE,
593 fd,
594 watch_modes[t],
595 true, // persistent
596 base::Bind(&LibcurlHttpFetcher::CurlPerformOnce,
597 base::Unretained(this)));
Darin Petkov60e14152010-10-27 16:57:04 -0700598
Darin Petkov60e14152010-10-27 16:57:04 -0700599 static int io_counter = 0;
600 io_counter++;
601 if (io_counter % 50 == 0) {
602 LOG(INFO) << "io_counter = " << io_counter;
603 }
Andrew de los Reyes3270f742010-07-15 22:28:14 -0700604 }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000605 }
606
Darin Petkovb83371f2010-08-17 09:34:49 -0700607 // Set up a timeout callback for libcurl.
Alex Deymo60ca1a72015-06-18 18:19:15 -0700608 if (timeout_id_ == MessageLoop::kTaskIdNull) {
Alex Deymof2858572016-02-25 11:20:13 -0800609 VLOG(1) << "Setting up timeout source: " << idle_seconds_ << " seconds.";
Alex Deymo60ca1a72015-06-18 18:19:15 -0700610 timeout_id_ = MessageLoop::current()->PostDelayedTask(
611 FROM_HERE,
612 base::Bind(&LibcurlHttpFetcher::TimeoutCallback,
613 base::Unretained(this)),
614 TimeDelta::FromSeconds(idle_seconds_));
rspangler@google.com49fdf182009-10-10 00:57:34 +0000615 }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000616}
617
Alex Deymo60ca1a72015-06-18 18:19:15 -0700618void LibcurlHttpFetcher::RetryTimeoutCallback() {
Alex Deymof2858572016-02-25 11:20:13 -0800619 if (transfer_paused_) {
620 restart_transfer_on_unpause_ = true;
621 return;
622 }
Andrew de los Reyes9bbd1872010-07-16 14:52:29 -0700623 ResumeTransfer(url_);
624 CurlPerformOnce();
Andrew de los Reyes9bbd1872010-07-16 14:52:29 -0700625}
626
Alex Deymo60ca1a72015-06-18 18:19:15 -0700627void LibcurlHttpFetcher::TimeoutCallback() {
Alex Deymo60ca1a72015-06-18 18:19:15 -0700628 // We always re-schedule the callback, even if we don't want to be called
629 // anymore. We will remove the event source separately if we don't want to
Andrew de los Reyescb319332010-07-19 10:55:01 -0700630 // be called back.
Alex Deymo60ca1a72015-06-18 18:19:15 -0700631 timeout_id_ = MessageLoop::current()->PostDelayedTask(
632 FROM_HERE,
633 base::Bind(&LibcurlHttpFetcher::TimeoutCallback, base::Unretained(this)),
634 TimeDelta::FromSeconds(idle_seconds_));
Alex Deymof123ae22015-09-24 14:59:43 -0700635
636 // CurlPerformOnce() may call CleanUp(), so we need to schedule our callback
637 // first, since it could be canceled by this call.
638 if (transfer_in_progress_)
639 CurlPerformOnce();
rspangler@google.com49fdf182009-10-10 00:57:34 +0000640}
641
642void LibcurlHttpFetcher::CleanUp() {
Alex Deymo60ca1a72015-06-18 18:19:15 -0700643 MessageLoop::current()->CancelTask(timeout_id_);
644 timeout_id_ = MessageLoop::kTaskIdNull;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000645
Alex Deymo29b81532015-07-09 11:51:49 -0700646 for (size_t t = 0; t < arraysize(fd_task_maps_); ++t) {
647 for (const auto& fd_taks_pair : fd_task_maps_[t]) {
648 if (!MessageLoop::current()->CancelTask(fd_taks_pair.second)) {
649 LOG(WARNING) << "Error canceling the watch task "
650 << fd_taks_pair.second << " for "
651 << (t ? "writing" : "reading") << " the fd "
652 << fd_taks_pair.first;
653 }
Darin Petkov60e14152010-10-27 16:57:04 -0700654 }
Alex Deymo29b81532015-07-09 11:51:49 -0700655 fd_task_maps_[t].clear();
rspangler@google.com49fdf182009-10-10 00:57:34 +0000656 }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000657
Gilad Arnold9dd1e7c2012-02-16 12:13:36 -0800658 if (curl_http_headers_) {
659 curl_slist_free_all(curl_http_headers_);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700660 curl_http_headers_ = nullptr;
Gilad Arnold9dd1e7c2012-02-16 12:13:36 -0800661 }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000662 if (curl_handle_) {
663 if (curl_multi_handle_) {
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000664 CHECK_EQ(curl_multi_remove_handle(curl_multi_handle_, curl_handle_),
665 CURLM_OK);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000666 }
667 curl_easy_cleanup(curl_handle_);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700668 curl_handle_ = nullptr;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000669 }
670 if (curl_multi_handle_) {
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000671 CHECK_EQ(curl_multi_cleanup(curl_multi_handle_), CURLM_OK);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700672 curl_multi_handle_ = nullptr;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000673 }
674 transfer_in_progress_ = false;
Alex Deymof2858572016-02-25 11:20:13 -0800675 transfer_paused_ = false;
676 restart_transfer_on_unpause_ = false;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000677}
678
Andrew de los Reyes3fd5d302010-10-07 20:07:18 -0700679void LibcurlHttpFetcher::GetHttpResponseCode() {
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700680 long http_response_code = 0; // NOLINT(runtime/int) - curl needs long.
Alex Deymo56ccb072016-02-05 00:50:48 -0800681 if (base::StartsWith(url_, "file://", base::CompareCase::INSENSITIVE_ASCII)) {
682 // Fake out a valid response code for file:// URLs.
683 http_response_code_ = 299;
684 } else if (curl_easy_getinfo(curl_handle_,
685 CURLINFO_RESPONSE_CODE,
686 &http_response_code) == CURLE_OK) {
Andrew de los Reyes3fd5d302010-10-07 20:07:18 -0700687 http_response_code_ = static_cast<int>(http_response_code);
688 }
689}
690
rspangler@google.com49fdf182009-10-10 00:57:34 +0000691} // namespace chromeos_update_engine