1//
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//
16
17#include "update_engine/libcurl_http_fetcher.h"
18
19#include <sys/types.h>
20#include <unistd.h>
21
22#include <algorithm>
23#include <string>
24
25#include <base/bind.h>
26#include <base/format_macros.h>
27#include <base/location.h>
28#include <base/logging.h>
29#include <base/strings/string_util.h>
30#include <base/strings/stringprintf.h>
31
32#ifdef __ANDROID__
33#include <cutils/qtaguid.h>
34#include <private/android_filesystem_config.h>
35#endif  // __ANDROID__
36
37#include "update_engine/certificate_checker.h"
38#include "update_engine/common/hardware_interface.h"
39#include "update_engine/common/platform_constants.h"
40
41using base::TimeDelta;
42using brillo::MessageLoop;
43using std::max;
44using std::string;
45
46// This is a concrete implementation of HttpFetcher that uses libcurl to do the
47// http work.
48
49namespace chromeos_update_engine {
50
51namespace {
52
53const int kNoNetworkRetrySeconds = 10;
54
55// libcurl's CURLOPT_SOCKOPTFUNCTION callback function. Called after the socket
56// is created but before it is connected. This callback tags the created socket
57// so the network usage can be tracked in Android.
58int LibcurlSockoptCallback(void* /* clientp */,
59                           curl_socket_t curlfd,
60                           curlsocktype /* purpose */) {
61#ifdef __ANDROID__
62  // Socket tag used by all network sockets. See qtaguid kernel module for
63  // stats.
64  const int kUpdateEngineSocketTag = 0x55417243;  // "CrAU" in little-endian.
65  qtaguid_tagSocket(curlfd, kUpdateEngineSocketTag, AID_OTA_UPDATE);
66#endif  // __ANDROID__
67  return CURL_SOCKOPT_OK;
68}
69
70}  // namespace
71
72// static
73int LibcurlHttpFetcher::LibcurlCloseSocketCallback(void* clientp,
74                                                   curl_socket_t item) {
75#ifdef __ANDROID__
76  qtaguid_untagSocket(item);
77#endif  // __ANDROID__
78  LibcurlHttpFetcher* fetcher = static_cast<LibcurlHttpFetcher*>(clientp);
79  // Stop watching the socket before closing it.
80  for (size_t t = 0; t < arraysize(fetcher->fd_task_maps_); ++t) {
81    const auto fd_task_pair = fetcher->fd_task_maps_[t].find(item);
82    if (fd_task_pair != fetcher->fd_task_maps_[t].end()) {
83      if (!MessageLoop::current()->CancelTask(fd_task_pair->second)) {
84        LOG(WARNING) << "Error canceling the watch task "
85                     << fd_task_pair->second << " for "
86                     << (t ? "writing" : "reading") << " the fd " << item;
87      }
88      fetcher->fd_task_maps_[t].erase(item);
89    }
90  }
91
92  // Documentation for this callback says to return 0 on success or 1 on error.
93  if (!IGNORE_EINTR(close(item)))
94    return 0;
95  return 1;
96}
97
98LibcurlHttpFetcher::LibcurlHttpFetcher(ProxyResolver* proxy_resolver,
99                                       HardwareInterface* hardware)
100    : HttpFetcher(proxy_resolver), hardware_(hardware) {
101  // Dev users want a longer timeout (180 seconds) because they may
102  // be waiting on the dev server to build an image.
103  if (!hardware_->IsOfficialBuild())
104    low_speed_time_seconds_ = kDownloadDevModeLowSpeedTimeSeconds;
105  if (hardware_->IsOOBEEnabled() && !hardware_->IsOOBEComplete(nullptr))
106    max_retry_count_ = kDownloadMaxRetryCountOobeNotComplete;
107}
108
109LibcurlHttpFetcher::~LibcurlHttpFetcher() {
110  LOG_IF(ERROR, transfer_in_progress_)
111      << "Destroying the fetcher while a transfer is in progress.";
112  CancelProxyResolution();
113  CleanUp();
114}
115
116bool LibcurlHttpFetcher::GetProxyType(const string& proxy,
117                                      curl_proxytype* out_type) {
118  if (base::StartsWith(
119          proxy, "socks5://", base::CompareCase::INSENSITIVE_ASCII) ||
120      base::StartsWith(
121          proxy, "socks://", base::CompareCase::INSENSITIVE_ASCII)) {
122    *out_type = CURLPROXY_SOCKS5_HOSTNAME;
123    return true;
124  }
125  if (base::StartsWith(
126          proxy, "socks4://", base::CompareCase::INSENSITIVE_ASCII)) {
127    *out_type = CURLPROXY_SOCKS4A;
128    return true;
129  }
130  if (base::StartsWith(
131          proxy, "http://", base::CompareCase::INSENSITIVE_ASCII) ||
132      base::StartsWith(
133          proxy, "https://", base::CompareCase::INSENSITIVE_ASCII)) {
134    *out_type = CURLPROXY_HTTP;
135    return true;
136  }
137  if (base::StartsWith(proxy, kNoProxy, base::CompareCase::INSENSITIVE_ASCII)) {
138    // known failure case. don't log.
139    return false;
140  }
141  LOG(INFO) << "Unknown proxy type: " << proxy;
142  return false;
143}
144
145void LibcurlHttpFetcher::ResumeTransfer(const string& url) {
146  LOG(INFO) << "Starting/Resuming transfer";
147  CHECK(!transfer_in_progress_);
148  url_ = url;
149  curl_multi_handle_ = curl_multi_init();
150  CHECK(curl_multi_handle_);
151
152  curl_handle_ = curl_easy_init();
153  CHECK(curl_handle_);
154  ignore_failure_ = false;
155
156  // Tag and untag the socket for network usage stats.
157  curl_easy_setopt(
158      curl_handle_, CURLOPT_SOCKOPTFUNCTION, LibcurlSockoptCallback);
159  curl_easy_setopt(
160      curl_handle_, CURLOPT_CLOSESOCKETFUNCTION, LibcurlCloseSocketCallback);
161  curl_easy_setopt(curl_handle_, CURLOPT_CLOSESOCKETDATA, this);
162
163  CHECK(HasProxy());
164  bool is_direct = (GetCurrentProxy() == kNoProxy);
165  LOG(INFO) << "Using proxy: " << (is_direct ? "no" : "yes");
166  if (is_direct) {
167    CHECK_EQ(curl_easy_setopt(curl_handle_,
168                              CURLOPT_PROXY,
169                              ""), CURLE_OK);
170  } else {
171    CHECK_EQ(curl_easy_setopt(curl_handle_,
172                              CURLOPT_PROXY,
173                              GetCurrentProxy().c_str()), CURLE_OK);
174    // Curl seems to require us to set the protocol
175    curl_proxytype type;
176    if (GetProxyType(GetCurrentProxy(), &type)) {
177      CHECK_EQ(curl_easy_setopt(curl_handle_,
178                                CURLOPT_PROXYTYPE,
179                                type), CURLE_OK);
180    }
181  }
182
183  if (post_data_set_) {
184    CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_POST, 1), CURLE_OK);
185    CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_POSTFIELDS,
186                              post_data_.data()),
187             CURLE_OK);
188    CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_POSTFIELDSIZE,
189                              post_data_.size()),
190             CURLE_OK);
191  }
192
193  // Setup extra HTTP headers.
194  if (curl_http_headers_) {
195    curl_slist_free_all(curl_http_headers_);
196    curl_http_headers_ = nullptr;
197  }
198  for (const auto& header : extra_headers_) {
199    // curl_slist_append() copies the string.
200    curl_http_headers_ =
201        curl_slist_append(curl_http_headers_, header.second.c_str());
202  }
203  if (post_data_set_) {
204    // Set the Content-Type HTTP header, if one was specifically set.
205    if (post_content_type_ != kHttpContentTypeUnspecified) {
206      const string content_type_attr = base::StringPrintf(
207          "Content-Type: %s", GetHttpContentTypeString(post_content_type_));
208      curl_http_headers_ =
209          curl_slist_append(curl_http_headers_, content_type_attr.c_str());
210    } else {
211      LOG(WARNING) << "no content type set, using libcurl default";
212    }
213  }
214  CHECK_EQ(
215      curl_easy_setopt(curl_handle_, CURLOPT_HTTPHEADER, curl_http_headers_),
216      CURLE_OK);
217
218  if (bytes_downloaded_ > 0 || download_length_) {
219    // Resume from where we left off.
220    resume_offset_ = bytes_downloaded_;
221    CHECK_GE(resume_offset_, 0);
222
223    // Compute end offset, if one is specified. As per HTTP specification, this
224    // is an inclusive boundary. Make sure it doesn't overflow.
225    size_t end_offset = 0;
226    if (download_length_) {
227      end_offset = static_cast<size_t>(resume_offset_) + download_length_ - 1;
228      CHECK_LE((size_t) resume_offset_, end_offset);
229    }
230
231    // Create a string representation of the desired range.
232    string range_str = base::StringPrintf(
233        "%" PRIu64 "-", static_cast<uint64_t>(resume_offset_));
234    if (end_offset)
235      range_str += std::to_string(end_offset);
236    CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_RANGE, range_str.c_str()),
237             CURLE_OK);
238  }
239
240  CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_WRITEDATA, this), CURLE_OK);
241  CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_WRITEFUNCTION,
242                            StaticLibcurlWrite), CURLE_OK);
243  CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_URL, url_.c_str()),
244           CURLE_OK);
245
246  // If the connection drops under |low_speed_limit_bps_| (10
247  // bytes/sec by default) for |low_speed_time_seconds_| (90 seconds,
248  // 180 on non-official builds), reconnect.
249  CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_LOW_SPEED_LIMIT,
250                            low_speed_limit_bps_),
251           CURLE_OK);
252  CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_LOW_SPEED_TIME,
253                            low_speed_time_seconds_),
254           CURLE_OK);
255  CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_CONNECTTIMEOUT,
256                            connect_timeout_seconds_),
257           CURLE_OK);
258
259  // By default, libcurl doesn't follow redirections. Allow up to
260  // |kDownloadMaxRedirects| redirections.
261  CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_FOLLOWLOCATION, 1), CURLE_OK);
262  CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_MAXREDIRS,
263                            kDownloadMaxRedirects),
264           CURLE_OK);
265
266  // Lock down the appropriate curl options for HTTP or HTTPS depending on
267  // the url.
268  if (hardware_->IsOfficialBuild()) {
269    if (base::StartsWith(
270            url_, "http://", base::CompareCase::INSENSITIVE_ASCII)) {
271      SetCurlOptionsForHttp();
272    } else if (base::StartsWith(
273                   url_, "https://", base::CompareCase::INSENSITIVE_ASCII)) {
274      SetCurlOptionsForHttps();
275#if !defined(__CHROMEOS__) && !defined(__BRILLO__)
276    } else if (base::StartsWith(
277                   url_, "file://", base::CompareCase::INSENSITIVE_ASCII)) {
278      SetCurlOptionsForFile();
279#endif
280    } else {
281      LOG(ERROR) << "Received invalid URI: " << url_;
282      // Lock down to no protocol supported for the transfer.
283      CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_PROTOCOLS, 0), CURLE_OK);
284    }
285  } else {
286    LOG(INFO) << "Not setting http(s) curl options because we are "
287              << "running a dev/test image";
288  }
289
290  CHECK_EQ(curl_multi_add_handle(curl_multi_handle_, curl_handle_), CURLM_OK);
291  transfer_in_progress_ = true;
292}
293
294// Lock down only the protocol in case of HTTP.
295void LibcurlHttpFetcher::SetCurlOptionsForHttp() {
296  LOG(INFO) << "Setting up curl options for HTTP";
297  CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_PROTOCOLS, CURLPROTO_HTTP),
298           CURLE_OK);
299  CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_REDIR_PROTOCOLS,
300                            CURLPROTO_HTTP),
301           CURLE_OK);
302}
303
304// Security lock-down in official builds: makes sure that peer certificate
305// verification is enabled, restricts the set of trusted certificates,
306// restricts protocols to HTTPS, restricts ciphers to HIGH.
307void LibcurlHttpFetcher::SetCurlOptionsForHttps() {
308  LOG(INFO) << "Setting up curl options for HTTPS";
309  CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_SSL_VERIFYPEER, 1),
310           CURLE_OK);
311  CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_SSL_VERIFYHOST, 2),
312           CURLE_OK);
313  CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_CAPATH,
314                            constants::kCACertificatesPath),
315           CURLE_OK);
316  CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_PROTOCOLS, CURLPROTO_HTTPS),
317           CURLE_OK);
318  CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_REDIR_PROTOCOLS,
319                            CURLPROTO_HTTPS),
320           CURLE_OK);
321  CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_SSL_CIPHER_LIST, "HIGH:!ADH"),
322           CURLE_OK);
323  if (server_to_check_ != ServerToCheck::kNone) {
324    CHECK_EQ(
325        curl_easy_setopt(curl_handle_, CURLOPT_SSL_CTX_DATA, &server_to_check_),
326        CURLE_OK);
327    CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_SSL_CTX_FUNCTION,
328                              CertificateChecker::ProcessSSLContext),
329             CURLE_OK);
330  }
331}
332
333// Lock down only the protocol in case of a local file.
334void LibcurlHttpFetcher::SetCurlOptionsForFile() {
335  LOG(INFO) << "Setting up curl options for FILE";
336  CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_PROTOCOLS, CURLPROTO_FILE),
337           CURLE_OK);
338  CHECK_EQ(
339      curl_easy_setopt(curl_handle_, CURLOPT_REDIR_PROTOCOLS, CURLPROTO_FILE),
340      CURLE_OK);
341}
342
343// Begins the transfer, which must not have already been started.
344void LibcurlHttpFetcher::BeginTransfer(const string& url) {
345  CHECK(!transfer_in_progress_);
346  url_ = url;
347  auto closure = base::Bind(&LibcurlHttpFetcher::ProxiesResolved,
348                            base::Unretained(this));
349  ResolveProxiesForUrl(url_, closure);
350}
351
352void LibcurlHttpFetcher::ProxiesResolved() {
353  transfer_size_ = -1;
354  resume_offset_ = 0;
355  retry_count_ = 0;
356  no_network_retry_count_ = 0;
357  http_response_code_ = 0;
358  terminate_requested_ = false;
359  sent_byte_ = false;
360
361  // If we are paused, we delay these two operations until Unpause is called.
362  if (transfer_paused_) {
363    restart_transfer_on_unpause_ = true;
364    return;
365  }
366  ResumeTransfer(url_);
367  CurlPerformOnce();
368}
369
370void LibcurlHttpFetcher::ForceTransferTermination() {
371  CancelProxyResolution();
372  CleanUp();
373  if (delegate_) {
374    // Note that after the callback returns this object may be destroyed.
375    delegate_->TransferTerminated(this);
376  }
377}
378
379void LibcurlHttpFetcher::TerminateTransfer() {
380  if (in_write_callback_) {
381    terminate_requested_ = true;
382  } else {
383    ForceTransferTermination();
384  }
385}
386
387void LibcurlHttpFetcher::SetHeader(const string& header_name,
388                                   const string& header_value) {
389  string header_line = header_name + ": " + header_value;
390  // Avoid the space if no data on the right side of the semicolon.
391  if (header_value.empty())
392    header_line = header_name + ":";
393  TEST_AND_RETURN(header_line.find('\n') == string::npos);
394  TEST_AND_RETURN(header_name.find(':') == string::npos);
395  extra_headers_[base::ToLowerASCII(header_name)] = header_line;
396}
397
398void LibcurlHttpFetcher::CurlPerformOnce() {
399  CHECK(transfer_in_progress_);
400  int running_handles = 0;
401  CURLMcode retcode = CURLM_CALL_MULTI_PERFORM;
402
403  // libcurl may request that we immediately call curl_multi_perform after it
404  // returns, so we do. libcurl promises that curl_multi_perform will not block.
405  while (CURLM_CALL_MULTI_PERFORM == retcode) {
406    retcode = curl_multi_perform(curl_multi_handle_, &running_handles);
407    if (terminate_requested_) {
408      ForceTransferTermination();
409      return;
410    }
411  }
412
413  // If the transfer completes while paused, we should ignore the failure once
414  // the fetcher is unpaused.
415  if (running_handles == 0 && transfer_paused_ && !ignore_failure_) {
416    LOG(INFO) << "Connection closed while paused, ignoring failure.";
417    ignore_failure_ = true;
418  }
419
420  if (running_handles != 0 || transfer_paused_) {
421    // There's either more work to do or we are paused, so we just keep the
422    // file descriptors to watch up to date and exit, until we are done with the
423    // work and we are not paused.
424    SetupMessageLoopSources();
425    return;
426  }
427
428  // At this point, the transfer was completed in some way (error, connection
429  // closed or download finished).
430
431  GetHttpResponseCode();
432  if (http_response_code_) {
433    LOG(INFO) << "HTTP response code: " << http_response_code_;
434    no_network_retry_count_ = 0;
435  } else {
436    LOG(ERROR) << "Unable to get http response code.";
437  }
438
439  // we're done!
440  CleanUp();
441
442  // TODO(petkov): This temporary code tries to deal with the case where the
443  // update engine performs an update check while the network is not ready
444  // (e.g., right after resume). Longer term, we should check if the network
445  // is online/offline and return an appropriate error code.
446  if (!sent_byte_ &&
447      http_response_code_ == 0 &&
448      no_network_retry_count_ < no_network_max_retries_) {
449    no_network_retry_count_++;
450    retry_task_id_ = MessageLoop::current()->PostDelayedTask(
451        FROM_HERE,
452        base::Bind(&LibcurlHttpFetcher::RetryTimeoutCallback,
453                   base::Unretained(this)),
454        TimeDelta::FromSeconds(kNoNetworkRetrySeconds));
455    LOG(INFO) << "No HTTP response, retry " << no_network_retry_count_;
456  } else if ((!sent_byte_ && !IsHttpResponseSuccess()) ||
457             IsHttpResponseError()) {
458    // The transfer completed w/ error and we didn't get any bytes.
459    // If we have another proxy to try, try that.
460    //
461    // TODO(garnold) in fact there are two separate cases here: one case is an
462    // other-than-success return code (including no return code) and no
463    // received bytes, which is necessary due to the way callbacks are
464    // currently processing error conditions;  the second is an explicit HTTP
465    // error code, where some data may have been received (as in the case of a
466    // semi-successful multi-chunk fetch).  This is a confusing behavior and
467    // should be unified into a complete, coherent interface.
468    LOG(INFO) << "Transfer resulted in an error (" << http_response_code_
469              << "), " << bytes_downloaded_ << " bytes downloaded";
470
471    PopProxy();  // Delete the proxy we just gave up on.
472
473    if (HasProxy()) {
474      // We have another proxy. Retry immediately.
475      LOG(INFO) << "Retrying with next proxy setting";
476      retry_task_id_ = MessageLoop::current()->PostTask(
477          FROM_HERE,
478          base::Bind(&LibcurlHttpFetcher::RetryTimeoutCallback,
479                     base::Unretained(this)));
480    } else {
481      // Out of proxies. Give up.
482      LOG(INFO) << "No further proxies, indicating transfer complete";
483      if (delegate_)
484        delegate_->TransferComplete(this, false);  // signal fail
485      return;
486    }
487  } else if ((transfer_size_ >= 0) && (bytes_downloaded_ < transfer_size_)) {
488    if (!ignore_failure_)
489      retry_count_++;
490    LOG(INFO) << "Transfer interrupted after downloading "
491              << bytes_downloaded_ << " of " << transfer_size_ << " bytes. "
492              << transfer_size_ - bytes_downloaded_ << " bytes remaining "
493              << "after " << retry_count_ << " attempt(s)";
494
495    if (retry_count_ > max_retry_count_) {
496      LOG(INFO) << "Reached max attempts (" << retry_count_ << ")";
497      if (delegate_)
498        delegate_->TransferComplete(this, false);  // signal fail
499      return;
500    }
501    // Need to restart transfer
502    LOG(INFO) << "Restarting transfer to download the remaining bytes";
503    retry_task_id_ = MessageLoop::current()->PostDelayedTask(
504        FROM_HERE,
505        base::Bind(&LibcurlHttpFetcher::RetryTimeoutCallback,
506                   base::Unretained(this)),
507        TimeDelta::FromSeconds(retry_seconds_));
508  } else {
509    LOG(INFO) << "Transfer completed (" << http_response_code_
510              << "), " << bytes_downloaded_ << " bytes downloaded";
511    if (delegate_) {
512      bool success = IsHttpResponseSuccess();
513      delegate_->TransferComplete(this, success);
514    }
515    return;
516  }
517  // If we reach this point is because TransferComplete() was not called in any
518  // of the previous branches. The delegate is allowed to destroy the object
519  // once TransferComplete is called so this would be illegal.
520  ignore_failure_ = false;
521}
522
523size_t LibcurlHttpFetcher::LibcurlWrite(void *ptr, size_t size, size_t nmemb) {
524  // Update HTTP response first.
525  GetHttpResponseCode();
526  const size_t payload_size = size * nmemb;
527
528  // Do nothing if no payload or HTTP response is an error.
529  if (payload_size == 0 || !IsHttpResponseSuccess()) {
530    LOG(INFO) << "HTTP response unsuccessful (" << http_response_code_
531              << ") or no payload (" << payload_size << "), nothing to do";
532    return 0;
533  }
534
535  sent_byte_ = true;
536  {
537    double transfer_size_double;
538    CHECK_EQ(curl_easy_getinfo(curl_handle_,
539                               CURLINFO_CONTENT_LENGTH_DOWNLOAD,
540                               &transfer_size_double), CURLE_OK);
541    off_t new_transfer_size = static_cast<off_t>(transfer_size_double);
542    if (new_transfer_size > 0) {
543      transfer_size_ = resume_offset_ + new_transfer_size;
544    }
545  }
546  bytes_downloaded_ += payload_size;
547  in_write_callback_ = true;
548  if (delegate_)
549    delegate_->ReceivedBytes(this, ptr, payload_size);
550  in_write_callback_ = false;
551  return payload_size;
552}
553
554void LibcurlHttpFetcher::Pause() {
555  if (transfer_paused_) {
556    LOG(ERROR) << "Fetcher already paused.";
557    return;
558  }
559  transfer_paused_ = true;
560  if (!transfer_in_progress_) {
561    // If pause before we started a connection, we don't need to notify curl
562    // about that, we will simply not start the connection later.
563    return;
564  }
565  CHECK(curl_handle_);
566  CHECK_EQ(curl_easy_pause(curl_handle_, CURLPAUSE_ALL), CURLE_OK);
567}
568
569void LibcurlHttpFetcher::Unpause() {
570  if (!transfer_paused_) {
571    LOG(ERROR) << "Resume attempted when fetcher not paused.";
572    return;
573  }
574  transfer_paused_ = false;
575  if (restart_transfer_on_unpause_) {
576    restart_transfer_on_unpause_ = false;
577    ResumeTransfer(url_);
578    CurlPerformOnce();
579    return;
580  }
581  if (!transfer_in_progress_) {
582    // If resumed before starting the connection, there's no need to notify
583    // anybody. We will simply start the connection once it is time.
584    return;
585  }
586  CHECK(curl_handle_);
587  CHECK_EQ(curl_easy_pause(curl_handle_, CURLPAUSE_CONT), CURLE_OK);
588  // Since the transfer is in progress, we need to dispatch a CurlPerformOnce()
589  // now to let the connection continue, otherwise it would be called by the
590  // TimeoutCallback but with a delay.
591  CurlPerformOnce();
592}
593
594// This method sets up callbacks with the MessageLoop.
595void LibcurlHttpFetcher::SetupMessageLoopSources() {
596  fd_set fd_read;
597  fd_set fd_write;
598  fd_set fd_exc;
599
600  FD_ZERO(&fd_read);
601  FD_ZERO(&fd_write);
602  FD_ZERO(&fd_exc);
603
604  int fd_max = 0;
605
606  // Ask libcurl for the set of file descriptors we should track on its
607  // behalf.
608  CHECK_EQ(curl_multi_fdset(curl_multi_handle_, &fd_read, &fd_write,
609                            &fd_exc, &fd_max), CURLM_OK);
610
611  // We should iterate through all file descriptors up to libcurl's fd_max or
612  // the highest one we're tracking, whichever is larger.
613  for (size_t t = 0; t < arraysize(fd_task_maps_); ++t) {
614    if (!fd_task_maps_[t].empty())
615      fd_max = max(fd_max, fd_task_maps_[t].rbegin()->first);
616  }
617
618  // For each fd, if we're not tracking it, track it. If we are tracking it, but
619  // libcurl doesn't care about it anymore, stop tracking it. After this loop,
620  // there should be exactly as many tasks scheduled in fd_task_maps_[0|1] as
621  // there are read/write fds that we're tracking.
622  for (int fd = 0; fd <= fd_max; ++fd) {
623    // Note that fd_exc is unused in the current version of libcurl so is_exc
624    // should always be false.
625    bool is_exc = FD_ISSET(fd, &fd_exc) != 0;
626    bool must_track[2] = {
627      is_exc || (FD_ISSET(fd, &fd_read) != 0),  // track 0 -- read
628      is_exc || (FD_ISSET(fd, &fd_write) != 0)  // track 1 -- write
629    };
630    MessageLoop::WatchMode watch_modes[2] = {
631      MessageLoop::WatchMode::kWatchRead,
632      MessageLoop::WatchMode::kWatchWrite,
633    };
634
635    for (size_t t = 0; t < arraysize(fd_task_maps_); ++t) {
636      auto fd_task_it = fd_task_maps_[t].find(fd);
637      bool tracked = fd_task_it != fd_task_maps_[t].end();
638
639      if (!must_track[t]) {
640        // If we have an outstanding io_channel, remove it.
641        if (tracked) {
642          MessageLoop::current()->CancelTask(fd_task_it->second);
643          fd_task_maps_[t].erase(fd_task_it);
644        }
645        continue;
646      }
647
648      // If we are already tracking this fd, continue -- nothing to do.
649      if (tracked)
650        continue;
651
652      // Track a new fd.
653      fd_task_maps_[t][fd] = MessageLoop::current()->WatchFileDescriptor(
654          FROM_HERE,
655          fd,
656          watch_modes[t],
657          true,  // persistent
658          base::Bind(&LibcurlHttpFetcher::CurlPerformOnce,
659                     base::Unretained(this)));
660
661      static int io_counter = 0;
662      io_counter++;
663      if (io_counter % 50 == 0) {
664        LOG(INFO) << "io_counter = " << io_counter;
665      }
666    }
667  }
668
669  // Set up a timeout callback for libcurl.
670  if (timeout_id_ == MessageLoop::kTaskIdNull) {
671    VLOG(1) << "Setting up timeout source: " << idle_seconds_ << " seconds.";
672    timeout_id_ = MessageLoop::current()->PostDelayedTask(
673        FROM_HERE,
674        base::Bind(&LibcurlHttpFetcher::TimeoutCallback,
675                   base::Unretained(this)),
676        TimeDelta::FromSeconds(idle_seconds_));
677  }
678}
679
680void LibcurlHttpFetcher::RetryTimeoutCallback() {
681  retry_task_id_ = MessageLoop::kTaskIdNull;
682  if (transfer_paused_) {
683    restart_transfer_on_unpause_ = true;
684    return;
685  }
686  ResumeTransfer(url_);
687  CurlPerformOnce();
688}
689
690void LibcurlHttpFetcher::TimeoutCallback() {
691  // We always re-schedule the callback, even if we don't want to be called
692  // anymore. We will remove the event source separately if we don't want to
693  // be called back.
694  timeout_id_ = MessageLoop::current()->PostDelayedTask(
695      FROM_HERE,
696      base::Bind(&LibcurlHttpFetcher::TimeoutCallback, base::Unretained(this)),
697      TimeDelta::FromSeconds(idle_seconds_));
698
699  // CurlPerformOnce() may call CleanUp(), so we need to schedule our callback
700  // first, since it could be canceled by this call.
701  if (transfer_in_progress_)
702    CurlPerformOnce();
703}
704
705void LibcurlHttpFetcher::CleanUp() {
706  MessageLoop::current()->CancelTask(retry_task_id_);
707  retry_task_id_ = MessageLoop::kTaskIdNull;
708
709  MessageLoop::current()->CancelTask(timeout_id_);
710  timeout_id_ = MessageLoop::kTaskIdNull;
711
712  for (size_t t = 0; t < arraysize(fd_task_maps_); ++t) {
713    for (const auto& fd_taks_pair : fd_task_maps_[t]) {
714      if (!MessageLoop::current()->CancelTask(fd_taks_pair.second)) {
715        LOG(WARNING) << "Error canceling the watch task "
716                     << fd_taks_pair.second << " for "
717                     << (t ? "writing" : "reading") << " the fd "
718                     << fd_taks_pair.first;
719      }
720    }
721    fd_task_maps_[t].clear();
722  }
723
724  if (curl_http_headers_) {
725    curl_slist_free_all(curl_http_headers_);
726    curl_http_headers_ = nullptr;
727  }
728  if (curl_handle_) {
729    if (curl_multi_handle_) {
730      CHECK_EQ(curl_multi_remove_handle(curl_multi_handle_, curl_handle_),
731               CURLM_OK);
732    }
733    curl_easy_cleanup(curl_handle_);
734    curl_handle_ = nullptr;
735  }
736  if (curl_multi_handle_) {
737    CHECK_EQ(curl_multi_cleanup(curl_multi_handle_), CURLM_OK);
738    curl_multi_handle_ = nullptr;
739  }
740  transfer_in_progress_ = false;
741  transfer_paused_ = false;
742  restart_transfer_on_unpause_ = false;
743}
744
745void LibcurlHttpFetcher::GetHttpResponseCode() {
746  long http_response_code = 0;  // NOLINT(runtime/int) - curl needs long.
747  if (base::StartsWith(url_, "file://", base::CompareCase::INSENSITIVE_ASCII)) {
748    // Fake out a valid response code for file:// URLs.
749    http_response_code_ = 299;
750  } else if (curl_easy_getinfo(curl_handle_,
751                               CURLINFO_RESPONSE_CODE,
752                               &http_response_code) == CURLE_OK) {
753    http_response_code_ = static_cast<int>(http_response_code);
754  }
755}
756
757}  // namespace chromeos_update_engine
758