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