blob: 789f46efe454d6a4183b26dcf61b650c480c97d7 [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;
54 if (!hardware_->IsOOBEComplete(nullptr))
55 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);
Gilad Arnold9dd1e7c2012-02-16 12:13:36 -0800132
133 // Set the Content-Type HTTP header, if one was specifically set.
134 CHECK(!curl_http_headers_);
135 if (post_content_type_ != kHttpContentTypeUnspecified) {
136 const string content_type_attr =
137 base::StringPrintf("Content-Type: %s",
138 GetHttpContentTypeString(post_content_type_));
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700139 curl_http_headers_ = curl_slist_append(nullptr,
140 content_type_attr.c_str());
Gilad Arnold9dd1e7c2012-02-16 12:13:36 -0800141 CHECK(curl_http_headers_);
142 CHECK_EQ(
143 curl_easy_setopt(curl_handle_, CURLOPT_HTTPHEADER,
144 curl_http_headers_),
145 CURLE_OK);
146 } else {
147 LOG(WARNING) << "no content type set, using libcurl default";
148 }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000149 }
150
Gilad Arnolde4ad2502011-12-29 17:08:54 -0800151 if (bytes_downloaded_ > 0 || download_length_) {
152 // Resume from where we left off.
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000153 resume_offset_ = bytes_downloaded_;
Gilad Arnolde4ad2502011-12-29 17:08:54 -0800154 CHECK_GE(resume_offset_, 0);
155
156 // Compute end offset, if one is specified. As per HTTP specification, this
157 // is an inclusive boundary. Make sure it doesn't overflow.
158 size_t end_offset = 0;
159 if (download_length_) {
160 end_offset = static_cast<size_t>(resume_offset_) + download_length_ - 1;
161 CHECK_LE((size_t) resume_offset_, end_offset);
162 }
163
164 // Create a string representation of the desired range.
Alex Deymoc00c98a2015-03-17 17:38:00 -0700165 string range_str = base::StringPrintf(
166 "%" PRIu64 "-", static_cast<uint64_t>(resume_offset_));
167 if (end_offset)
168 range_str += std::to_string(end_offset);
Gilad Arnolde4ad2502011-12-29 17:08:54 -0800169 CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_RANGE, range_str.c_str()),
170 CURLE_OK);
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000171 }
172
173 CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_WRITEDATA, this), CURLE_OK);
174 CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_WRITEFUNCTION,
175 StaticLibcurlWrite), CURLE_OK);
Chris Sosa77f79e82014-06-02 18:16:24 -0700176 CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_URL, url_.c_str()),
Andrew de los Reyesd57d1472010-10-21 13:34:08 -0700177 CURLE_OK);
Andrew de los Reyes3270f742010-07-15 22:28:14 -0700178
David Zeuthen34135a92013-08-06 11:16:16 -0700179 // If the connection drops under |low_speed_limit_bps_| (10
180 // bytes/sec by default) for |low_speed_time_seconds_| (90 seconds,
181 // 180 on non-official builds), reconnect.
182 CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_LOW_SPEED_LIMIT,
183 low_speed_limit_bps_),
Andrew de los Reyes3270f742010-07-15 22:28:14 -0700184 CURLE_OK);
David Zeuthen34135a92013-08-06 11:16:16 -0700185 CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_LOW_SPEED_TIME,
186 low_speed_time_seconds_),
Andrew de los Reyes3270f742010-07-15 22:28:14 -0700187 CURLE_OK);
David Zeuthen34135a92013-08-06 11:16:16 -0700188 CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_CONNECTTIMEOUT,
189 connect_timeout_seconds_),
Andrew de los Reyese72f9c02011-04-20 10:47:40 -0700190 CURLE_OK);
Andrew de los Reyes3270f742010-07-15 22:28:14 -0700191
Darin Petkov41c2fcf2010-08-25 13:14:48 -0700192 // By default, libcurl doesn't follow redirections. Allow up to
David Zeuthen34135a92013-08-06 11:16:16 -0700193 // |kDownloadMaxRedirects| redirections.
Darin Petkov3a4016a2010-09-28 13:54:17 -0700194 CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_FOLLOWLOCATION, 1), CURLE_OK);
David Zeuthen34135a92013-08-06 11:16:16 -0700195 CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_MAXREDIRS,
196 kDownloadMaxRedirects),
Darin Petkov41c2fcf2010-08-25 13:14:48 -0700197 CURLE_OK);
198
Nam T. Nguyen7d623eb2014-05-13 16:06:28 -0700199 // Lock down the appropriate curl options for HTTP or HTTPS depending on
200 // the url.
Alex Deymoc1c17b42015-11-23 03:53:15 -0300201 if (hardware_->IsOfficialBuild()) {
Alex Deymo56ccb072016-02-05 00:50:48 -0800202 if (base::StartsWith(
203 url_, "http://", base::CompareCase::INSENSITIVE_ASCII)) {
Jay Srinivasanb3f55402012-12-03 18:12:04 -0800204 SetCurlOptionsForHttp();
Alex Deymo56ccb072016-02-05 00:50:48 -0800205 } else if (base::StartsWith(
206 url_, "https://", base::CompareCase::INSENSITIVE_ASCII)) {
Jay Srinivasanb3f55402012-12-03 18:12:04 -0800207 SetCurlOptionsForHttps();
Alex Deymo56ccb072016-02-05 00:50:48 -0800208#if !defined(__CHROMEOS__) && !defined(__BRILLO__)
209 } else if (base::StartsWith(
210 url_, "file://", base::CompareCase::INSENSITIVE_ASCII)) {
211 SetCurlOptionsForFile();
212#endif
213 } else {
214 LOG(ERROR) << "Received invalid URI: " << url_;
215 // Lock down to no protocol supported for the transfer.
216 CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_PROTOCOLS, 0), CURLE_OK);
217 }
Jay Srinivasanb3f55402012-12-03 18:12:04 -0800218 } else {
Nam T. Nguyen7d623eb2014-05-13 16:06:28 -0700219 LOG(INFO) << "Not setting http(s) curl options because we are "
220 << "running a dev/test image";
Darin Petkovfc7a0ce2010-10-25 10:38:37 -0700221 }
222
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000223 CHECK_EQ(curl_multi_add_handle(curl_multi_handle_, curl_handle_), CURLM_OK);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000224 transfer_in_progress_ = true;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000225}
226
Jay Srinivasanb3f55402012-12-03 18:12:04 -0800227// Lock down only the protocol in case of HTTP.
228void LibcurlHttpFetcher::SetCurlOptionsForHttp() {
229 LOG(INFO) << "Setting up curl options for HTTP";
230 CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_PROTOCOLS, CURLPROTO_HTTP),
231 CURLE_OK);
232 CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_REDIR_PROTOCOLS,
233 CURLPROTO_HTTP),
234 CURLE_OK);
235}
236
237// Security lock-down in official builds: makes sure that peer certificate
238// verification is enabled, restricts the set of trusted certificates,
239// restricts protocols to HTTPS, restricts ciphers to HIGH.
240void LibcurlHttpFetcher::SetCurlOptionsForHttps() {
241 LOG(INFO) << "Setting up curl options for HTTPS";
242 CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_SSL_VERIFYPEER, 1),
243 CURLE_OK);
Alex Deymo35b35842015-10-20 11:21:56 -0700244 CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_CAPATH,
245 constants::kCACertificatesPath),
Jay Srinivasanb3f55402012-12-03 18:12:04 -0800246 CURLE_OK);
247 CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_PROTOCOLS, CURLPROTO_HTTPS),
248 CURLE_OK);
249 CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_REDIR_PROTOCOLS,
250 CURLPROTO_HTTPS),
251 CURLE_OK);
252 CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_SSL_CIPHER_LIST, "HIGH:!ADH"),
253 CURLE_OK);
Alex Deymo33e91e72015-12-01 18:26:08 -0300254 if (server_to_check_ != ServerToCheck::kNone) {
255 CHECK_EQ(
256 curl_easy_setopt(curl_handle_, CURLOPT_SSL_CTX_DATA, &server_to_check_),
257 CURLE_OK);
Jay Srinivasanb3f55402012-12-03 18:12:04 -0800258 CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_SSL_CTX_FUNCTION,
259 CertificateChecker::ProcessSSLContext),
260 CURLE_OK);
261 }
262}
263
Alex Deymo56ccb072016-02-05 00:50:48 -0800264// Lock down only the protocol in case of a local file.
265void LibcurlHttpFetcher::SetCurlOptionsForFile() {
266 LOG(INFO) << "Setting up curl options for FILE";
267 CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_PROTOCOLS, CURLPROTO_FILE),
268 CURLE_OK);
269 CHECK_EQ(
270 curl_easy_setopt(curl_handle_, CURLOPT_REDIR_PROTOCOLS, CURLPROTO_FILE),
271 CURLE_OK);
272}
Jay Srinivasanb3f55402012-12-03 18:12:04 -0800273
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000274// Begins the transfer, which must not have already been started.
Alex Deymof329b932014-10-30 01:37:48 -0700275void LibcurlHttpFetcher::BeginTransfer(const string& url) {
Andrew de los Reyesf3ed8e72011-02-16 10:35:46 -0800276 CHECK(!transfer_in_progress_);
277 url_ = url;
Alex Vakulenko4906c1c2014-08-21 13:17:44 -0700278 auto closure = base::Bind(&LibcurlHttpFetcher::ProxiesResolved,
279 base::Unretained(this));
Alex Deymo60ca1a72015-06-18 18:19:15 -0700280 if (!ResolveProxiesForUrl(url_, closure)) {
Andrew de los Reyesf3ed8e72011-02-16 10:35:46 -0800281 LOG(ERROR) << "Couldn't resolve proxies";
282 if (delegate_)
283 delegate_->TransferComplete(this, false);
284 }
285}
286
287void LibcurlHttpFetcher::ProxiesResolved() {
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000288 transfer_size_ = -1;
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000289 resume_offset_ = 0;
Andrew de los Reyes9bbd1872010-07-16 14:52:29 -0700290 retry_count_ = 0;
Darin Petkova0929552010-11-29 14:19:06 -0800291 no_network_retry_count_ = 0;
Darin Petkovcb466212010-08-26 09:40:11 -0700292 http_response_code_ = 0;
Andrew de los Reyes819fef22010-12-17 11:33:58 -0800293 terminate_requested_ = false;
Gilad Arnolda2dee1d2012-04-12 11:50:37 -0700294 sent_byte_ = false;
Alex Deymof2858572016-02-25 11:20:13 -0800295
296 // If we are paused, we delay these two operations until Unpause is called.
297 if (transfer_paused_) {
298 restart_transfer_on_unpause_ = true;
299 return;
300 }
Andrew de los Reyesf3ed8e72011-02-16 10:35:46 -0800301 ResumeTransfer(url_);
Andrew de los Reyes9bbd1872010-07-16 14:52:29 -0700302 CurlPerformOnce();
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000303}
304
Darin Petkov9ce452b2010-11-17 14:33:28 -0800305void LibcurlHttpFetcher::ForceTransferTermination() {
306 CleanUp();
307 if (delegate_) {
308 // Note that after the callback returns this object may be destroyed.
309 delegate_->TransferTerminated(this);
310 }
311}
312
rspangler@google.com49fdf182009-10-10 00:57:34 +0000313void LibcurlHttpFetcher::TerminateTransfer() {
Darin Petkov9ce452b2010-11-17 14:33:28 -0800314 if (in_write_callback_) {
Andrew de los Reyes3fd5d302010-10-07 20:07:18 -0700315 terminate_requested_ = true;
Darin Petkov9ce452b2010-11-17 14:33:28 -0800316 } else {
317 ForceTransferTermination();
318 }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000319}
320
Andrew de los Reyescb319332010-07-19 10:55:01 -0700321void LibcurlHttpFetcher::CurlPerformOnce() {
rspangler@google.com49fdf182009-10-10 00:57:34 +0000322 CHECK(transfer_in_progress_);
323 int running_handles = 0;
324 CURLMcode retcode = CURLM_CALL_MULTI_PERFORM;
325
326 // libcurl may request that we immediately call curl_multi_perform after it
327 // returns, so we do. libcurl promises that curl_multi_perform will not block.
328 while (CURLM_CALL_MULTI_PERFORM == retcode) {
329 retcode = curl_multi_perform(curl_multi_handle_, &running_handles);
Andrew de los Reyes3fd5d302010-10-07 20:07:18 -0700330 if (terminate_requested_) {
Darin Petkov9ce452b2010-11-17 14:33:28 -0800331 ForceTransferTermination();
Andrew de los Reyes3fd5d302010-10-07 20:07:18 -0700332 return;
333 }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000334 }
Alex Deymof2858572016-02-25 11:20:13 -0800335
336 // If the transfer completes while paused, we should ignore the failure once
337 // the fetcher is unpaused.
338 if (running_handles == 0 && transfer_paused_ && !ignore_failure_) {
339 LOG(INFO) << "Connection closed while paused, ignoring failure.";
340 ignore_failure_ = true;
341 }
342
343 if (running_handles != 0 || transfer_paused_) {
344 // There's either more work to do or we are paused, so we just keep the
345 // file descriptors to watch up to date and exit, until we are done with the
346 // work and we are not paused.
347 SetupMessageLoopSources();
348 return;
349 }
350
351 // At this point, the transfer was completed in some way (error, connection
352 // closed or download finished).
353
354 GetHttpResponseCode();
355 if (http_response_code_) {
356 LOG(INFO) << "HTTP response code: " << http_response_code_;
357 no_network_retry_count_ = 0;
358 } else {
359 LOG(ERROR) << "Unable to get http response code.";
360 }
361
362 // we're done!
363 CleanUp();
364
365 // TODO(petkov): This temporary code tries to deal with the case where the
366 // update engine performs an update check while the network is not ready
367 // (e.g., right after resume). Longer term, we should check if the network
368 // is online/offline and return an appropriate error code.
369 if (!sent_byte_ &&
370 http_response_code_ == 0 &&
371 no_network_retry_count_ < no_network_max_retries_) {
372 no_network_retry_count_++;
373 MessageLoop::current()->PostDelayedTask(
374 FROM_HERE,
375 base::Bind(&LibcurlHttpFetcher::RetryTimeoutCallback,
376 base::Unretained(this)),
377 TimeDelta::FromSeconds(kNoNetworkRetrySeconds));
378 LOG(INFO) << "No HTTP response, retry " << no_network_retry_count_;
379 } else if ((!sent_byte_ && !IsHttpResponseSuccess()) ||
380 IsHttpResponseError()) {
381 // The transfer completed w/ error and we didn't get any bytes.
382 // If we have another proxy to try, try that.
383 //
384 // TODO(garnold) in fact there are two separate cases here: one case is an
385 // other-than-success return code (including no return code) and no
386 // received bytes, which is necessary due to the way callbacks are
387 // currently processing error conditions; the second is an explicit HTTP
388 // error code, where some data may have been received (as in the case of a
389 // semi-successful multi-chunk fetch). This is a confusing behavior and
390 // should be unified into a complete, coherent interface.
391 LOG(INFO) << "Transfer resulted in an error (" << http_response_code_
392 << "), " << bytes_downloaded_ << " bytes downloaded";
393
394 PopProxy(); // Delete the proxy we just gave up on.
395
396 if (HasProxy()) {
397 // We have another proxy. Retry immediately.
398 LOG(INFO) << "Retrying with next proxy setting";
399 MessageLoop::current()->PostTask(
400 FROM_HERE,
401 base::Bind(&LibcurlHttpFetcher::RetryTimeoutCallback,
402 base::Unretained(this)));
Andrew de los Reyes9bbd1872010-07-16 14:52:29 -0700403 } else {
Alex Deymof2858572016-02-25 11:20:13 -0800404 // Out of proxies. Give up.
405 LOG(INFO) << "No further proxies, indicating transfer complete";
406 if (delegate_)
407 delegate_->TransferComplete(this, false); // signal fail
Andrew de los Reyes9bbd1872010-07-16 14:52:29 -0700408 }
Alex Deymof2858572016-02-25 11:20:13 -0800409 } else if ((transfer_size_ >= 0) && (bytes_downloaded_ < transfer_size_)) {
410 if (!ignore_failure_)
411 retry_count_++;
412 LOG(INFO) << "Transfer interrupted after downloading "
413 << bytes_downloaded_ << " of " << transfer_size_ << " bytes. "
414 << transfer_size_ - bytes_downloaded_ << " bytes remaining "
415 << "after " << retry_count_ << " attempt(s)";
Darin Petkov192ced42010-07-23 16:20:24 -0700416
Alex Deymof2858572016-02-25 11:20:13 -0800417 if (retry_count_ > max_retry_count_) {
418 LOG(INFO) << "Reached max attempts (" << retry_count_ << ")";
419 if (delegate_)
420 delegate_->TransferComplete(this, false); // signal fail
421 } else {
422 // Need to restart transfer
423 LOG(INFO) << "Restarting transfer to download the remaining bytes";
Alex Deymo60ca1a72015-06-18 18:19:15 -0700424 MessageLoop::current()->PostDelayedTask(
425 FROM_HERE,
426 base::Bind(&LibcurlHttpFetcher::RetryTimeoutCallback,
427 base::Unretained(this)),
Alex Deymof2858572016-02-25 11:20:13 -0800428 TimeDelta::FromSeconds(retry_seconds_));
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000429 }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000430 } else {
Alex Deymof2858572016-02-25 11:20:13 -0800431 LOG(INFO) << "Transfer completed (" << http_response_code_
432 << "), " << bytes_downloaded_ << " bytes downloaded";
433 if (delegate_) {
434 bool success = IsHttpResponseSuccess();
435 delegate_->TransferComplete(this, success);
436 }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000437 }
Alex Deymof2858572016-02-25 11:20:13 -0800438 ignore_failure_ = false;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000439}
440
441size_t LibcurlHttpFetcher::LibcurlWrite(void *ptr, size_t size, size_t nmemb) {
Gilad Arnold48085ba2011-11-16 09:36:08 -0800442 // Update HTTP response first.
Andrew de los Reyes3fd5d302010-10-07 20:07:18 -0700443 GetHttpResponseCode();
Gilad Arnold48085ba2011-11-16 09:36:08 -0800444 const size_t payload_size = size * nmemb;
445
446 // Do nothing if no payload or HTTP response is an error.
Gilad Arnold9bedeb52011-11-17 16:19:57 -0800447 if (payload_size == 0 || !IsHttpResponseSuccess()) {
Gilad Arnold48085ba2011-11-16 09:36:08 -0800448 LOG(INFO) << "HTTP response unsuccessful (" << http_response_code_
449 << ") or no payload (" << payload_size << "), nothing to do";
450 return 0;
451 }
452
453 sent_byte_ = true;
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000454 {
455 double transfer_size_double;
456 CHECK_EQ(curl_easy_getinfo(curl_handle_,
457 CURLINFO_CONTENT_LENGTH_DOWNLOAD,
458 &transfer_size_double), CURLE_OK);
459 off_t new_transfer_size = static_cast<off_t>(transfer_size_double);
460 if (new_transfer_size > 0) {
461 transfer_size_ = resume_offset_ + new_transfer_size;
462 }
463 }
Gilad Arnold48085ba2011-11-16 09:36:08 -0800464 bytes_downloaded_ += payload_size;
Andrew de los Reyes3fd5d302010-10-07 20:07:18 -0700465 in_write_callback_ = true;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000466 if (delegate_)
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800467 delegate_->ReceivedBytes(this, ptr, payload_size);
Andrew de los Reyes3fd5d302010-10-07 20:07:18 -0700468 in_write_callback_ = false;
Gilad Arnold48085ba2011-11-16 09:36:08 -0800469 return payload_size;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000470}
471
472void LibcurlHttpFetcher::Pause() {
Alex Deymof2858572016-02-25 11:20:13 -0800473 if (transfer_paused_) {
474 LOG(ERROR) << "Fetcher already paused.";
475 return;
476 }
477 transfer_paused_ = true;
478 if (!transfer_in_progress_) {
479 // If pause before we started a connection, we don't need to notify curl
480 // about that, we will simply not start the connection later.
481 return;
482 }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000483 CHECK(curl_handle_);
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000484 CHECK_EQ(curl_easy_pause(curl_handle_, CURLPAUSE_ALL), CURLE_OK);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000485}
486
487void LibcurlHttpFetcher::Unpause() {
Alex Deymof2858572016-02-25 11:20:13 -0800488 if (!transfer_paused_) {
489 LOG(ERROR) << "Resume attempted when fetcher not paused.";
490 return;
491 }
492 transfer_paused_ = false;
493 if (restart_transfer_on_unpause_) {
494 restart_transfer_on_unpause_ = false;
495 ResumeTransfer(url_);
496 CurlPerformOnce();
497 return;
498 }
499 if (!transfer_in_progress_) {
500 // If resumed before starting the connection, there's no need to notify
501 // anybody. We will simply start the connection once it is time.
502 return;
503 }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000504 CHECK(curl_handle_);
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000505 CHECK_EQ(curl_easy_pause(curl_handle_, CURLPAUSE_CONT), CURLE_OK);
Alex Deymof2858572016-02-25 11:20:13 -0800506 // Since the transfer is in progress, we need to dispatch a CurlPerformOnce()
507 // now to let the connection continue, otherwise it would be called by the
508 // TimeoutCallback but with a delay.
509 CurlPerformOnce();
rspangler@google.com49fdf182009-10-10 00:57:34 +0000510}
511
Alex Deymo29b81532015-07-09 11:51:49 -0700512// This method sets up callbacks with the MessageLoop.
513void LibcurlHttpFetcher::SetupMessageLoopSources() {
rspangler@google.com49fdf182009-10-10 00:57:34 +0000514 fd_set fd_read;
515 fd_set fd_write;
Darin Petkov60e14152010-10-27 16:57:04 -0700516 fd_set fd_exc;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000517
518 FD_ZERO(&fd_read);
519 FD_ZERO(&fd_write);
Darin Petkov60e14152010-10-27 16:57:04 -0700520 FD_ZERO(&fd_exc);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000521
522 int fd_max = 0;
523
524 // Ask libcurl for the set of file descriptors we should track on its
525 // behalf.
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000526 CHECK_EQ(curl_multi_fdset(curl_multi_handle_, &fd_read, &fd_write,
Darin Petkov60e14152010-10-27 16:57:04 -0700527 &fd_exc, &fd_max), CURLM_OK);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000528
529 // We should iterate through all file descriptors up to libcurl's fd_max or
Darin Petkov60e14152010-10-27 16:57:04 -0700530 // the highest one we're tracking, whichever is larger.
Alex Deymo29b81532015-07-09 11:51:49 -0700531 for (size_t t = 0; t < arraysize(fd_task_maps_); ++t) {
532 if (!fd_task_maps_[t].empty())
533 fd_max = max(fd_max, fd_task_maps_[t].rbegin()->first);
Darin Petkov60e14152010-10-27 16:57:04 -0700534 }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000535
Darin Petkov60e14152010-10-27 16:57:04 -0700536 // For each fd, if we're not tracking it, track it. If we are tracking it, but
537 // libcurl doesn't care about it anymore, stop tracking it. After this loop,
Alex Deymo29b81532015-07-09 11:51:49 -0700538 // there should be exactly as many tasks scheduled in fd_task_maps_[0|1] as
Darin Petkov60e14152010-10-27 16:57:04 -0700539 // there are read/write fds that we're tracking.
540 for (int fd = 0; fd <= fd_max; ++fd) {
541 // Note that fd_exc is unused in the current version of libcurl so is_exc
542 // should always be false.
543 bool is_exc = FD_ISSET(fd, &fd_exc) != 0;
544 bool must_track[2] = {
545 is_exc || (FD_ISSET(fd, &fd_read) != 0), // track 0 -- read
546 is_exc || (FD_ISSET(fd, &fd_write) != 0) // track 1 -- write
547 };
Alex Deymo29b81532015-07-09 11:51:49 -0700548 MessageLoop::WatchMode watch_modes[2] = {
549 MessageLoop::WatchMode::kWatchRead,
550 MessageLoop::WatchMode::kWatchWrite,
551 };
Darin Petkov60e14152010-10-27 16:57:04 -0700552
Alex Deymo29b81532015-07-09 11:51:49 -0700553 for (size_t t = 0; t < arraysize(fd_task_maps_); ++t) {
554 auto fd_task_it = fd_task_maps_[t].find(fd);
555 bool tracked = fd_task_it != fd_task_maps_[t].end();
Darin Petkov60e14152010-10-27 16:57:04 -0700556
557 if (!must_track[t]) {
558 // If we have an outstanding io_channel, remove it.
559 if (tracked) {
Alex Deymo29b81532015-07-09 11:51:49 -0700560 MessageLoop::current()->CancelTask(fd_task_it->second);
561 fd_task_maps_[t].erase(fd_task_it);
Darin Petkov60e14152010-10-27 16:57:04 -0700562 }
563 continue;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000564 }
Darin Petkov60e14152010-10-27 16:57:04 -0700565
566 // If we are already tracking this fd, continue -- nothing to do.
567 if (tracked)
568 continue;
569
Darin Petkov60e14152010-10-27 16:57:04 -0700570 // Track a new fd.
Alex Deymo29b81532015-07-09 11:51:49 -0700571 fd_task_maps_[t][fd] = MessageLoop::current()->WatchFileDescriptor(
572 FROM_HERE,
573 fd,
574 watch_modes[t],
575 true, // persistent
576 base::Bind(&LibcurlHttpFetcher::CurlPerformOnce,
577 base::Unretained(this)));
Darin Petkov60e14152010-10-27 16:57:04 -0700578
Darin Petkov60e14152010-10-27 16:57:04 -0700579 static int io_counter = 0;
580 io_counter++;
581 if (io_counter % 50 == 0) {
582 LOG(INFO) << "io_counter = " << io_counter;
583 }
Andrew de los Reyes3270f742010-07-15 22:28:14 -0700584 }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000585 }
586
Darin Petkovb83371f2010-08-17 09:34:49 -0700587 // Set up a timeout callback for libcurl.
Alex Deymo60ca1a72015-06-18 18:19:15 -0700588 if (timeout_id_ == MessageLoop::kTaskIdNull) {
Alex Deymof2858572016-02-25 11:20:13 -0800589 VLOG(1) << "Setting up timeout source: " << idle_seconds_ << " seconds.";
Alex Deymo60ca1a72015-06-18 18:19:15 -0700590 timeout_id_ = MessageLoop::current()->PostDelayedTask(
591 FROM_HERE,
592 base::Bind(&LibcurlHttpFetcher::TimeoutCallback,
593 base::Unretained(this)),
594 TimeDelta::FromSeconds(idle_seconds_));
rspangler@google.com49fdf182009-10-10 00:57:34 +0000595 }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000596}
597
Alex Deymo60ca1a72015-06-18 18:19:15 -0700598void LibcurlHttpFetcher::RetryTimeoutCallback() {
Alex Deymof2858572016-02-25 11:20:13 -0800599 if (transfer_paused_) {
600 restart_transfer_on_unpause_ = true;
601 return;
602 }
Andrew de los Reyes9bbd1872010-07-16 14:52:29 -0700603 ResumeTransfer(url_);
604 CurlPerformOnce();
Andrew de los Reyes9bbd1872010-07-16 14:52:29 -0700605}
606
Alex Deymo60ca1a72015-06-18 18:19:15 -0700607void LibcurlHttpFetcher::TimeoutCallback() {
Alex Deymo60ca1a72015-06-18 18:19:15 -0700608 // We always re-schedule the callback, even if we don't want to be called
609 // anymore. We will remove the event source separately if we don't want to
Andrew de los Reyescb319332010-07-19 10:55:01 -0700610 // be called back.
Alex Deymo60ca1a72015-06-18 18:19:15 -0700611 timeout_id_ = MessageLoop::current()->PostDelayedTask(
612 FROM_HERE,
613 base::Bind(&LibcurlHttpFetcher::TimeoutCallback, base::Unretained(this)),
614 TimeDelta::FromSeconds(idle_seconds_));
Alex Deymof123ae22015-09-24 14:59:43 -0700615
616 // CurlPerformOnce() may call CleanUp(), so we need to schedule our callback
617 // first, since it could be canceled by this call.
618 if (transfer_in_progress_)
619 CurlPerformOnce();
rspangler@google.com49fdf182009-10-10 00:57:34 +0000620}
621
622void LibcurlHttpFetcher::CleanUp() {
Alex Deymo60ca1a72015-06-18 18:19:15 -0700623 MessageLoop::current()->CancelTask(timeout_id_);
624 timeout_id_ = MessageLoop::kTaskIdNull;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000625
Alex Deymo29b81532015-07-09 11:51:49 -0700626 for (size_t t = 0; t < arraysize(fd_task_maps_); ++t) {
627 for (const auto& fd_taks_pair : fd_task_maps_[t]) {
628 if (!MessageLoop::current()->CancelTask(fd_taks_pair.second)) {
629 LOG(WARNING) << "Error canceling the watch task "
630 << fd_taks_pair.second << " for "
631 << (t ? "writing" : "reading") << " the fd "
632 << fd_taks_pair.first;
633 }
Darin Petkov60e14152010-10-27 16:57:04 -0700634 }
Alex Deymo29b81532015-07-09 11:51:49 -0700635 fd_task_maps_[t].clear();
rspangler@google.com49fdf182009-10-10 00:57:34 +0000636 }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000637
Gilad Arnold9dd1e7c2012-02-16 12:13:36 -0800638 if (curl_http_headers_) {
639 curl_slist_free_all(curl_http_headers_);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700640 curl_http_headers_ = nullptr;
Gilad Arnold9dd1e7c2012-02-16 12:13:36 -0800641 }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000642 if (curl_handle_) {
643 if (curl_multi_handle_) {
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000644 CHECK_EQ(curl_multi_remove_handle(curl_multi_handle_, curl_handle_),
645 CURLM_OK);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000646 }
647 curl_easy_cleanup(curl_handle_);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700648 curl_handle_ = nullptr;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000649 }
650 if (curl_multi_handle_) {
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000651 CHECK_EQ(curl_multi_cleanup(curl_multi_handle_), CURLM_OK);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700652 curl_multi_handle_ = nullptr;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000653 }
654 transfer_in_progress_ = false;
Alex Deymof2858572016-02-25 11:20:13 -0800655 transfer_paused_ = false;
656 restart_transfer_on_unpause_ = false;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000657}
658
Andrew de los Reyes3fd5d302010-10-07 20:07:18 -0700659void LibcurlHttpFetcher::GetHttpResponseCode() {
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700660 long http_response_code = 0; // NOLINT(runtime/int) - curl needs long.
Alex Deymo56ccb072016-02-05 00:50:48 -0800661 if (base::StartsWith(url_, "file://", base::CompareCase::INSENSITIVE_ASCII)) {
662 // Fake out a valid response code for file:// URLs.
663 http_response_code_ = 299;
664 } else if (curl_easy_getinfo(curl_handle_,
665 CURLINFO_RESPONSE_CODE,
666 &http_response_code) == CURLE_OK) {
Andrew de los Reyes3fd5d302010-10-07 20:07:18 -0700667 http_response_code_ = static_cast<int>(http_response_code);
668 }
669}
670
rspangler@google.com49fdf182009-10-10 00:57:34 +0000671} // namespace chromeos_update_engine