rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 1 | // Copyright (c) 2009 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 5 | #include "update_engine/libcurl_http_fetcher.h" |
Andrew de los Reyes | d57d147 | 2010-10-21 13:34:08 -0700 | [diff] [blame] | 6 | |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 7 | #include <algorithm> |
Andrew de los Reyes | d57d147 | 2010-10-21 13:34:08 -0700 | [diff] [blame] | 8 | #include <string> |
| 9 | |
Alex Vakulenko | 4906c1c | 2014-08-21 13:17:44 -0700 | [diff] [blame] | 10 | #include <base/bind.h> |
Alex Deymo | c00c98a | 2015-03-17 17:38:00 -0700 | [diff] [blame^] | 11 | #include <base/format_macros.h> |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 12 | #include <base/location.h> |
Andrew de los Reyes | d57d147 | 2010-10-21 13:34:08 -0700 | [diff] [blame] | 13 | #include <base/logging.h> |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 14 | #include <base/strings/string_util.h> |
| 15 | #include <base/strings/stringprintf.h> |
Andrew de los Reyes | d57d147 | 2010-10-21 13:34:08 -0700 | [diff] [blame] | 16 | |
Bruno Rocha | 7f9aea2 | 2011-09-12 14:31:24 -0700 | [diff] [blame] | 17 | #include "update_engine/certificate_checker.h" |
J. Richard Barnette | 056b0ab | 2013-10-29 15:24:56 -0700 | [diff] [blame] | 18 | #include "update_engine/hardware_interface.h" |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 19 | |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 20 | using base::TimeDelta; |
| 21 | using chromeos::MessageLoop; |
Alex Deymo | c4acdf4 | 2014-05-28 21:07:10 -0700 | [diff] [blame] | 22 | using std::max; |
Andrew de los Reyes | d57d147 | 2010-10-21 13:34:08 -0700 | [diff] [blame] | 23 | using std::string; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 24 | |
| 25 | // This is a concrete implementation of HttpFetcher that uses libcurl to do the |
| 26 | // http work. |
| 27 | |
| 28 | namespace chromeos_update_engine { |
| 29 | |
Andrew de los Reyes | 9bbd187 | 2010-07-16 14:52:29 -0700 | [diff] [blame] | 30 | namespace { |
Andrew de los Reyes | 5d0783d | 2010-11-29 18:14:16 -0800 | [diff] [blame] | 31 | const int kNoNetworkRetrySeconds = 10; |
Ken Mixter | b2bf122 | 2010-11-18 17:29:38 -0800 | [diff] [blame] | 32 | const char kCACertificatesPath[] = "/usr/share/chromeos-ca-certificates"; |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 33 | } // namespace |
Andrew de los Reyes | 9bbd187 | 2010-07-16 14:52:29 -0700 | [diff] [blame] | 34 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 35 | LibcurlHttpFetcher::~LibcurlHttpFetcher() { |
Darin Petkov | 9ce452b | 2010-11-17 14:33:28 -0800 | [diff] [blame] | 36 | LOG_IF(ERROR, transfer_in_progress_) |
| 37 | << "Destroying the fetcher while a transfer is in progress."; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 38 | CleanUp(); |
| 39 | } |
| 40 | |
Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 41 | bool LibcurlHttpFetcher::GetProxyType(const string& proxy, |
Gilad Arnold | 59d9e01 | 2013-07-23 16:41:43 -0700 | [diff] [blame] | 42 | curl_proxytype* out_type) { |
Alex Vakulenko | 6a9d349 | 2015-06-15 12:53:22 -0700 | [diff] [blame] | 43 | if (base::StartsWithASCII(proxy, "socks5://", true) || |
| 44 | base::StartsWithASCII(proxy, "socks://", true)) { |
Gilad Arnold | 59d9e01 | 2013-07-23 16:41:43 -0700 | [diff] [blame] | 45 | *out_type = CURLPROXY_SOCKS5_HOSTNAME; |
| 46 | return true; |
| 47 | } |
Alex Vakulenko | 6a9d349 | 2015-06-15 12:53:22 -0700 | [diff] [blame] | 48 | if (base::StartsWithASCII(proxy, "socks4://", true)) { |
Gilad Arnold | 59d9e01 | 2013-07-23 16:41:43 -0700 | [diff] [blame] | 49 | *out_type = CURLPROXY_SOCKS4A; |
| 50 | return true; |
| 51 | } |
Alex Vakulenko | 6a9d349 | 2015-06-15 12:53:22 -0700 | [diff] [blame] | 52 | if (base::StartsWithASCII(proxy, "http://", true) || |
| 53 | base::StartsWithASCII(proxy, "https://", true)) { |
Gilad Arnold | 59d9e01 | 2013-07-23 16:41:43 -0700 | [diff] [blame] | 54 | *out_type = CURLPROXY_HTTP; |
| 55 | return true; |
| 56 | } |
Alex Vakulenko | 6a9d349 | 2015-06-15 12:53:22 -0700 | [diff] [blame] | 57 | if (base::StartsWithASCII(proxy, kNoProxy, true)) { |
Gilad Arnold | 59d9e01 | 2013-07-23 16:41:43 -0700 | [diff] [blame] | 58 | // known failure case. don't log. |
| 59 | return false; |
| 60 | } |
| 61 | LOG(INFO) << "Unknown proxy type: " << proxy; |
| 62 | return false; |
| 63 | } |
| 64 | |
Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 65 | void LibcurlHttpFetcher::ResumeTransfer(const string& url) { |
Andrew de los Reyes | 3270f74 | 2010-07-15 22:28:14 -0700 | [diff] [blame] | 66 | LOG(INFO) << "Starting/Resuming transfer"; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 67 | CHECK(!transfer_in_progress_); |
| 68 | url_ = url; |
| 69 | curl_multi_handle_ = curl_multi_init(); |
| 70 | CHECK(curl_multi_handle_); |
| 71 | |
| 72 | curl_handle_ = curl_easy_init(); |
| 73 | CHECK(curl_handle_); |
| 74 | |
Andrew de los Reyes | 4516810 | 2010-11-22 11:13:50 -0800 | [diff] [blame] | 75 | CHECK(HasProxy()); |
Gilad Arnold | fbaee24 | 2012-04-04 15:59:43 -0700 | [diff] [blame] | 76 | bool is_direct = (GetCurrentProxy() == kNoProxy); |
| 77 | LOG(INFO) << "Using proxy: " << (is_direct ? "no" : "yes"); |
| 78 | if (is_direct) { |
Andrew de los Reyes | 4516810 | 2010-11-22 11:13:50 -0800 | [diff] [blame] | 79 | CHECK_EQ(curl_easy_setopt(curl_handle_, |
| 80 | CURLOPT_PROXY, |
| 81 | ""), CURLE_OK); |
| 82 | } else { |
| 83 | CHECK_EQ(curl_easy_setopt(curl_handle_, |
| 84 | CURLOPT_PROXY, |
| 85 | GetCurrentProxy().c_str()), CURLE_OK); |
| 86 | // Curl seems to require us to set the protocol |
| 87 | curl_proxytype type; |
Gilad Arnold | 59d9e01 | 2013-07-23 16:41:43 -0700 | [diff] [blame] | 88 | if (GetProxyType(GetCurrentProxy(), &type)) { |
Andrew de los Reyes | 4516810 | 2010-11-22 11:13:50 -0800 | [diff] [blame] | 89 | CHECK_EQ(curl_easy_setopt(curl_handle_, |
| 90 | CURLOPT_PROXYTYPE, |
| 91 | type), CURLE_OK); |
| 92 | } |
| 93 | } |
| 94 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 95 | if (post_data_set_) { |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 96 | CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_POST, 1), CURLE_OK); |
| 97 | CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_POSTFIELDS, |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 98 | post_data_.data()), |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 99 | CURLE_OK); |
| 100 | CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_POSTFIELDSIZE, |
| 101 | post_data_.size()), |
| 102 | CURLE_OK); |
Gilad Arnold | 9dd1e7c | 2012-02-16 12:13:36 -0800 | [diff] [blame] | 103 | |
| 104 | // Set the Content-Type HTTP header, if one was specifically set. |
| 105 | CHECK(!curl_http_headers_); |
| 106 | if (post_content_type_ != kHttpContentTypeUnspecified) { |
| 107 | const string content_type_attr = |
| 108 | base::StringPrintf("Content-Type: %s", |
| 109 | GetHttpContentTypeString(post_content_type_)); |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 110 | curl_http_headers_ = curl_slist_append(nullptr, |
| 111 | content_type_attr.c_str()); |
Gilad Arnold | 9dd1e7c | 2012-02-16 12:13:36 -0800 | [diff] [blame] | 112 | CHECK(curl_http_headers_); |
| 113 | CHECK_EQ( |
| 114 | curl_easy_setopt(curl_handle_, CURLOPT_HTTPHEADER, |
| 115 | curl_http_headers_), |
| 116 | CURLE_OK); |
| 117 | } else { |
| 118 | LOG(WARNING) << "no content type set, using libcurl default"; |
| 119 | } |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 120 | } |
| 121 | |
Gilad Arnold | e4ad250 | 2011-12-29 17:08:54 -0800 | [diff] [blame] | 122 | if (bytes_downloaded_ > 0 || download_length_) { |
| 123 | // Resume from where we left off. |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 124 | resume_offset_ = bytes_downloaded_; |
Gilad Arnold | e4ad250 | 2011-12-29 17:08:54 -0800 | [diff] [blame] | 125 | CHECK_GE(resume_offset_, 0); |
| 126 | |
| 127 | // Compute end offset, if one is specified. As per HTTP specification, this |
| 128 | // is an inclusive boundary. Make sure it doesn't overflow. |
| 129 | size_t end_offset = 0; |
| 130 | if (download_length_) { |
| 131 | end_offset = static_cast<size_t>(resume_offset_) + download_length_ - 1; |
| 132 | CHECK_LE((size_t) resume_offset_, end_offset); |
| 133 | } |
| 134 | |
| 135 | // Create a string representation of the desired range. |
Alex Deymo | c00c98a | 2015-03-17 17:38:00 -0700 | [diff] [blame^] | 136 | string range_str = base::StringPrintf( |
| 137 | "%" PRIu64 "-", static_cast<uint64_t>(resume_offset_)); |
| 138 | if (end_offset) |
| 139 | range_str += std::to_string(end_offset); |
Gilad Arnold | e4ad250 | 2011-12-29 17:08:54 -0800 | [diff] [blame] | 140 | CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_RANGE, range_str.c_str()), |
| 141 | CURLE_OK); |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_WRITEDATA, this), CURLE_OK); |
| 145 | CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_WRITEFUNCTION, |
| 146 | StaticLibcurlWrite), CURLE_OK); |
Chris Sosa | 77f79e8 | 2014-06-02 18:16:24 -0700 | [diff] [blame] | 147 | CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_URL, url_.c_str()), |
Andrew de los Reyes | d57d147 | 2010-10-21 13:34:08 -0700 | [diff] [blame] | 148 | CURLE_OK); |
Andrew de los Reyes | 3270f74 | 2010-07-15 22:28:14 -0700 | [diff] [blame] | 149 | |
David Zeuthen | 34135a9 | 2013-08-06 11:16:16 -0700 | [diff] [blame] | 150 | // If the connection drops under |low_speed_limit_bps_| (10 |
| 151 | // bytes/sec by default) for |low_speed_time_seconds_| (90 seconds, |
| 152 | // 180 on non-official builds), reconnect. |
| 153 | CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_LOW_SPEED_LIMIT, |
| 154 | low_speed_limit_bps_), |
Andrew de los Reyes | 3270f74 | 2010-07-15 22:28:14 -0700 | [diff] [blame] | 155 | CURLE_OK); |
David Zeuthen | 34135a9 | 2013-08-06 11:16:16 -0700 | [diff] [blame] | 156 | CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_LOW_SPEED_TIME, |
| 157 | low_speed_time_seconds_), |
Andrew de los Reyes | 3270f74 | 2010-07-15 22:28:14 -0700 | [diff] [blame] | 158 | CURLE_OK); |
David Zeuthen | 34135a9 | 2013-08-06 11:16:16 -0700 | [diff] [blame] | 159 | CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_CONNECTTIMEOUT, |
| 160 | connect_timeout_seconds_), |
Andrew de los Reyes | e72f9c0 | 2011-04-20 10:47:40 -0700 | [diff] [blame] | 161 | CURLE_OK); |
Andrew de los Reyes | 3270f74 | 2010-07-15 22:28:14 -0700 | [diff] [blame] | 162 | |
Darin Petkov | 41c2fcf | 2010-08-25 13:14:48 -0700 | [diff] [blame] | 163 | // By default, libcurl doesn't follow redirections. Allow up to |
David Zeuthen | 34135a9 | 2013-08-06 11:16:16 -0700 | [diff] [blame] | 164 | // |kDownloadMaxRedirects| redirections. |
Darin Petkov | 3a4016a | 2010-09-28 13:54:17 -0700 | [diff] [blame] | 165 | CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_FOLLOWLOCATION, 1), CURLE_OK); |
David Zeuthen | 34135a9 | 2013-08-06 11:16:16 -0700 | [diff] [blame] | 166 | CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_MAXREDIRS, |
| 167 | kDownloadMaxRedirects), |
Darin Petkov | 41c2fcf | 2010-08-25 13:14:48 -0700 | [diff] [blame] | 168 | CURLE_OK); |
| 169 | |
Nam T. Nguyen | 7d623eb | 2014-05-13 16:06:28 -0700 | [diff] [blame] | 170 | // Lock down the appropriate curl options for HTTP or HTTPS depending on |
| 171 | // the url. |
| 172 | if (GetSystemState()->hardware()->IsOfficialBuild()) { |
Alex Vakulenko | 6a9d349 | 2015-06-15 12:53:22 -0700 | [diff] [blame] | 173 | if (base::StartsWithASCII(url_, "http://", false)) |
Jay Srinivasan | b3f5540 | 2012-12-03 18:12:04 -0800 | [diff] [blame] | 174 | SetCurlOptionsForHttp(); |
| 175 | else |
| 176 | SetCurlOptionsForHttps(); |
| 177 | } else { |
Nam T. Nguyen | 7d623eb | 2014-05-13 16:06:28 -0700 | [diff] [blame] | 178 | LOG(INFO) << "Not setting http(s) curl options because we are " |
| 179 | << "running a dev/test image"; |
Darin Petkov | fc7a0ce | 2010-10-25 10:38:37 -0700 | [diff] [blame] | 180 | } |
| 181 | |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 182 | CHECK_EQ(curl_multi_add_handle(curl_multi_handle_, curl_handle_), CURLM_OK); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 183 | transfer_in_progress_ = true; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 184 | } |
| 185 | |
Jay Srinivasan | b3f5540 | 2012-12-03 18:12:04 -0800 | [diff] [blame] | 186 | // Lock down only the protocol in case of HTTP. |
| 187 | void LibcurlHttpFetcher::SetCurlOptionsForHttp() { |
| 188 | LOG(INFO) << "Setting up curl options for HTTP"; |
| 189 | CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_PROTOCOLS, CURLPROTO_HTTP), |
| 190 | CURLE_OK); |
| 191 | CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_REDIR_PROTOCOLS, |
| 192 | CURLPROTO_HTTP), |
| 193 | CURLE_OK); |
| 194 | } |
| 195 | |
| 196 | // Security lock-down in official builds: makes sure that peer certificate |
| 197 | // verification is enabled, restricts the set of trusted certificates, |
| 198 | // restricts protocols to HTTPS, restricts ciphers to HIGH. |
| 199 | void LibcurlHttpFetcher::SetCurlOptionsForHttps() { |
| 200 | LOG(INFO) << "Setting up curl options for HTTPS"; |
| 201 | CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_SSL_VERIFYPEER, 1), |
| 202 | CURLE_OK); |
| 203 | CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_CAPATH, kCACertificatesPath), |
| 204 | CURLE_OK); |
| 205 | CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_PROTOCOLS, CURLPROTO_HTTPS), |
| 206 | CURLE_OK); |
| 207 | CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_REDIR_PROTOCOLS, |
| 208 | CURLPROTO_HTTPS), |
| 209 | CURLE_OK); |
| 210 | CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_SSL_CIPHER_LIST, "HIGH:!ADH"), |
| 211 | CURLE_OK); |
| 212 | if (check_certificate_ != CertificateChecker::kNone) { |
| 213 | CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_SSL_CTX_DATA, |
| 214 | &check_certificate_), |
| 215 | CURLE_OK); |
| 216 | CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_SSL_CTX_FUNCTION, |
| 217 | CertificateChecker::ProcessSSLContext), |
| 218 | CURLE_OK); |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 223 | // Begins the transfer, which must not have already been started. |
Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 224 | void LibcurlHttpFetcher::BeginTransfer(const string& url) { |
Andrew de los Reyes | f3ed8e7 | 2011-02-16 10:35:46 -0800 | [diff] [blame] | 225 | CHECK(!transfer_in_progress_); |
| 226 | url_ = url; |
Alex Vakulenko | 4906c1c | 2014-08-21 13:17:44 -0700 | [diff] [blame] | 227 | auto closure = base::Bind(&LibcurlHttpFetcher::ProxiesResolved, |
| 228 | base::Unretained(this)); |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 229 | if (!ResolveProxiesForUrl(url_, closure)) { |
Andrew de los Reyes | f3ed8e7 | 2011-02-16 10:35:46 -0800 | [diff] [blame] | 230 | LOG(ERROR) << "Couldn't resolve proxies"; |
| 231 | if (delegate_) |
| 232 | delegate_->TransferComplete(this, false); |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | void LibcurlHttpFetcher::ProxiesResolved() { |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 237 | transfer_size_ = -1; |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 238 | resume_offset_ = 0; |
Andrew de los Reyes | 9bbd187 | 2010-07-16 14:52:29 -0700 | [diff] [blame] | 239 | retry_count_ = 0; |
Darin Petkov | a092955 | 2010-11-29 14:19:06 -0800 | [diff] [blame] | 240 | no_network_retry_count_ = 0; |
Darin Petkov | cb46621 | 2010-08-26 09:40:11 -0700 | [diff] [blame] | 241 | http_response_code_ = 0; |
Andrew de los Reyes | 819fef2 | 2010-12-17 11:33:58 -0800 | [diff] [blame] | 242 | terminate_requested_ = false; |
Gilad Arnold | a2dee1d | 2012-04-12 11:50:37 -0700 | [diff] [blame] | 243 | sent_byte_ = false; |
Andrew de los Reyes | f3ed8e7 | 2011-02-16 10:35:46 -0800 | [diff] [blame] | 244 | ResumeTransfer(url_); |
Andrew de los Reyes | 9bbd187 | 2010-07-16 14:52:29 -0700 | [diff] [blame] | 245 | CurlPerformOnce(); |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 246 | } |
| 247 | |
Darin Petkov | 9ce452b | 2010-11-17 14:33:28 -0800 | [diff] [blame] | 248 | void LibcurlHttpFetcher::ForceTransferTermination() { |
| 249 | CleanUp(); |
| 250 | if (delegate_) { |
| 251 | // Note that after the callback returns this object may be destroyed. |
| 252 | delegate_->TransferTerminated(this); |
| 253 | } |
| 254 | } |
| 255 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 256 | void LibcurlHttpFetcher::TerminateTransfer() { |
Darin Petkov | 9ce452b | 2010-11-17 14:33:28 -0800 | [diff] [blame] | 257 | if (in_write_callback_) { |
Andrew de los Reyes | 3fd5d30 | 2010-10-07 20:07:18 -0700 | [diff] [blame] | 258 | terminate_requested_ = true; |
Darin Petkov | 9ce452b | 2010-11-17 14:33:28 -0800 | [diff] [blame] | 259 | } else { |
| 260 | ForceTransferTermination(); |
| 261 | } |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 262 | } |
| 263 | |
Andrew de los Reyes | cb31933 | 2010-07-19 10:55:01 -0700 | [diff] [blame] | 264 | void LibcurlHttpFetcher::CurlPerformOnce() { |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 265 | CHECK(transfer_in_progress_); |
| 266 | int running_handles = 0; |
| 267 | CURLMcode retcode = CURLM_CALL_MULTI_PERFORM; |
| 268 | |
| 269 | // libcurl may request that we immediately call curl_multi_perform after it |
| 270 | // returns, so we do. libcurl promises that curl_multi_perform will not block. |
| 271 | while (CURLM_CALL_MULTI_PERFORM == retcode) { |
| 272 | retcode = curl_multi_perform(curl_multi_handle_, &running_handles); |
Andrew de los Reyes | 3fd5d30 | 2010-10-07 20:07:18 -0700 | [diff] [blame] | 273 | if (terminate_requested_) { |
Darin Petkov | 9ce452b | 2010-11-17 14:33:28 -0800 | [diff] [blame] | 274 | ForceTransferTermination(); |
Andrew de los Reyes | 3fd5d30 | 2010-10-07 20:07:18 -0700 | [diff] [blame] | 275 | return; |
| 276 | } |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 277 | } |
| 278 | if (0 == running_handles) { |
Andrew de los Reyes | 3fd5d30 | 2010-10-07 20:07:18 -0700 | [diff] [blame] | 279 | GetHttpResponseCode(); |
| 280 | if (http_response_code_) { |
| 281 | LOG(INFO) << "HTTP response code: " << http_response_code_; |
Darin Petkov | a092955 | 2010-11-29 14:19:06 -0800 | [diff] [blame] | 282 | no_network_retry_count_ = 0; |
Andrew de los Reyes | 9bbd187 | 2010-07-16 14:52:29 -0700 | [diff] [blame] | 283 | } else { |
| 284 | LOG(ERROR) << "Unable to get http response code."; |
| 285 | } |
Darin Petkov | 192ced4 | 2010-07-23 16:20:24 -0700 | [diff] [blame] | 286 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 287 | // we're done! |
| 288 | CleanUp(); |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 289 | |
Darin Petkov | a092955 | 2010-11-29 14:19:06 -0800 | [diff] [blame] | 290 | // TODO(petkov): This temporary code tries to deal with the case where the |
| 291 | // update engine performs an update check while the network is not ready |
| 292 | // (e.g., right after resume). Longer term, we should check if the network |
| 293 | // is online/offline and return an appropriate error code. |
| 294 | if (!sent_byte_ && |
| 295 | http_response_code_ == 0 && |
| 296 | no_network_retry_count_ < no_network_max_retries_) { |
| 297 | no_network_retry_count_++; |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 298 | MessageLoop::current()->PostDelayedTask( |
| 299 | FROM_HERE, |
| 300 | base::Bind(&LibcurlHttpFetcher::RetryTimeoutCallback, |
| 301 | base::Unretained(this)), |
| 302 | TimeDelta::FromSeconds(kNoNetworkRetrySeconds)); |
Darin Petkov | a092955 | 2010-11-29 14:19:06 -0800 | [diff] [blame] | 303 | LOG(INFO) << "No HTTP response, retry " << no_network_retry_count_; |
| 304 | return; |
| 305 | } |
| 306 | |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 307 | if ((!sent_byte_ && !IsHttpResponseSuccess()) || IsHttpResponseError()) { |
Andrew de los Reyes | 4516810 | 2010-11-22 11:13:50 -0800 | [diff] [blame] | 308 | // The transfer completed w/ error and we didn't get any bytes. |
| 309 | // If we have another proxy to try, try that. |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 310 | // |
| 311 | // TODO(garnold) in fact there are two separate cases here: one case is an |
| 312 | // other-than-success return code (including no return code) and no |
| 313 | // received bytes, which is necessary due to the way callbacks are |
| 314 | // currently processing error conditions; the second is an explicit HTTP |
| 315 | // error code, where some data may have been received (as in the case of a |
| 316 | // semi-successful multi-chunk fetch). This is a confusing behavior and |
| 317 | // should be unified into a complete, coherent interface. |
| 318 | LOG(INFO) << "Transfer resulted in an error (" << http_response_code_ |
| 319 | << "), " << bytes_downloaded_ << " bytes downloaded"; |
Andrew de los Reyes | 4516810 | 2010-11-22 11:13:50 -0800 | [diff] [blame] | 320 | |
| 321 | PopProxy(); // Delete the proxy we just gave up on. |
| 322 | |
| 323 | if (HasProxy()) { |
| 324 | // We have another proxy. Retry immediately. |
Gilad Arnold | fbaee24 | 2012-04-04 15:59:43 -0700 | [diff] [blame] | 325 | LOG(INFO) << "Retrying with next proxy setting"; |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 326 | MessageLoop::current()->PostTask( |
| 327 | FROM_HERE, |
| 328 | base::Bind(&LibcurlHttpFetcher::RetryTimeoutCallback, |
| 329 | base::Unretained(this))); |
Andrew de los Reyes | 4516810 | 2010-11-22 11:13:50 -0800 | [diff] [blame] | 330 | } else { |
| 331 | // Out of proxies. Give up. |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 332 | LOG(INFO) << "No further proxies, indicating transfer complete"; |
Andrew de los Reyes | 4516810 | 2010-11-22 11:13:50 -0800 | [diff] [blame] | 333 | if (delegate_) |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 334 | delegate_->TransferComplete(this, false); // signal fail |
Andrew de los Reyes | 4516810 | 2010-11-22 11:13:50 -0800 | [diff] [blame] | 335 | } |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 336 | } else if ((transfer_size_ >= 0) && (bytes_downloaded_ < transfer_size_)) { |
Andrew de los Reyes | 9bbd187 | 2010-07-16 14:52:29 -0700 | [diff] [blame] | 337 | retry_count_++; |
Jay Srinivasan | 32f2357 | 2012-06-05 13:45:07 -0700 | [diff] [blame] | 338 | LOG(INFO) << "Transfer interrupted after downloading " |
| 339 | << bytes_downloaded_ << " of " << transfer_size_ << " bytes. " |
| 340 | << transfer_size_ - bytes_downloaded_ << " bytes remaining " |
| 341 | << "after " << retry_count_ << " attempt(s)"; |
| 342 | |
| 343 | if (retry_count_ > max_retry_count_) { |
| 344 | LOG(INFO) << "Reached max attempts (" << retry_count_ << ")"; |
Andrew de los Reyes | 9bbd187 | 2010-07-16 14:52:29 -0700 | [diff] [blame] | 345 | if (delegate_) |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 346 | delegate_->TransferComplete(this, false); // signal fail |
Andrew de los Reyes | 9bbd187 | 2010-07-16 14:52:29 -0700 | [diff] [blame] | 347 | } else { |
Jay Srinivasan | 32f2357 | 2012-06-05 13:45:07 -0700 | [diff] [blame] | 348 | // Need to restart transfer |
| 349 | LOG(INFO) << "Restarting transfer to download the remaining bytes"; |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 350 | MessageLoop::current()->PostDelayedTask( |
| 351 | FROM_HERE, |
| 352 | base::Bind(&LibcurlHttpFetcher::RetryTimeoutCallback, |
| 353 | base::Unretained(this)), |
| 354 | TimeDelta::FromSeconds(retry_seconds_)); |
Andrew de los Reyes | 9bbd187 | 2010-07-16 14:52:29 -0700 | [diff] [blame] | 355 | } |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 356 | } else { |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 357 | LOG(INFO) << "Transfer completed (" << http_response_code_ |
| 358 | << "), " << bytes_downloaded_ << " bytes downloaded"; |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 359 | if (delegate_) { |
Gilad Arnold | 48085ba | 2011-11-16 09:36:08 -0800 | [diff] [blame] | 360 | bool success = IsHttpResponseSuccess(); |
Andrew de los Reyes | fb4ad7d | 2010-07-19 10:43:46 -0700 | [diff] [blame] | 361 | delegate_->TransferComplete(this, success); |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 362 | } |
| 363 | } |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 364 | } else { |
| 365 | // set up callback |
Alex Deymo | 29b8153 | 2015-07-09 11:51:49 -0700 | [diff] [blame] | 366 | SetupMessageLoopSources(); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 367 | } |
| 368 | } |
| 369 | |
| 370 | size_t LibcurlHttpFetcher::LibcurlWrite(void *ptr, size_t size, size_t nmemb) { |
Gilad Arnold | 48085ba | 2011-11-16 09:36:08 -0800 | [diff] [blame] | 371 | // Update HTTP response first. |
Andrew de los Reyes | 3fd5d30 | 2010-10-07 20:07:18 -0700 | [diff] [blame] | 372 | GetHttpResponseCode(); |
Gilad Arnold | 48085ba | 2011-11-16 09:36:08 -0800 | [diff] [blame] | 373 | const size_t payload_size = size * nmemb; |
| 374 | |
| 375 | // Do nothing if no payload or HTTP response is an error. |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 376 | if (payload_size == 0 || !IsHttpResponseSuccess()) { |
Gilad Arnold | 48085ba | 2011-11-16 09:36:08 -0800 | [diff] [blame] | 377 | LOG(INFO) << "HTTP response unsuccessful (" << http_response_code_ |
| 378 | << ") or no payload (" << payload_size << "), nothing to do"; |
| 379 | return 0; |
| 380 | } |
| 381 | |
| 382 | sent_byte_ = true; |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 383 | { |
| 384 | double transfer_size_double; |
| 385 | CHECK_EQ(curl_easy_getinfo(curl_handle_, |
| 386 | CURLINFO_CONTENT_LENGTH_DOWNLOAD, |
| 387 | &transfer_size_double), CURLE_OK); |
| 388 | off_t new_transfer_size = static_cast<off_t>(transfer_size_double); |
| 389 | if (new_transfer_size > 0) { |
| 390 | transfer_size_ = resume_offset_ + new_transfer_size; |
| 391 | } |
| 392 | } |
Gilad Arnold | 48085ba | 2011-11-16 09:36:08 -0800 | [diff] [blame] | 393 | bytes_downloaded_ += payload_size; |
Andrew de los Reyes | 3fd5d30 | 2010-10-07 20:07:18 -0700 | [diff] [blame] | 394 | in_write_callback_ = true; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 395 | if (delegate_) |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 396 | delegate_->ReceivedBytes(this, ptr, payload_size); |
Andrew de los Reyes | 3fd5d30 | 2010-10-07 20:07:18 -0700 | [diff] [blame] | 397 | in_write_callback_ = false; |
Gilad Arnold | 48085ba | 2011-11-16 09:36:08 -0800 | [diff] [blame] | 398 | return payload_size; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 399 | } |
| 400 | |
| 401 | void LibcurlHttpFetcher::Pause() { |
| 402 | CHECK(curl_handle_); |
| 403 | CHECK(transfer_in_progress_); |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 404 | CHECK_EQ(curl_easy_pause(curl_handle_, CURLPAUSE_ALL), CURLE_OK); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | void LibcurlHttpFetcher::Unpause() { |
| 408 | CHECK(curl_handle_); |
| 409 | CHECK(transfer_in_progress_); |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 410 | CHECK_EQ(curl_easy_pause(curl_handle_, CURLPAUSE_CONT), CURLE_OK); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 411 | } |
| 412 | |
Alex Deymo | 29b8153 | 2015-07-09 11:51:49 -0700 | [diff] [blame] | 413 | // This method sets up callbacks with the MessageLoop. |
| 414 | void LibcurlHttpFetcher::SetupMessageLoopSources() { |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 415 | fd_set fd_read; |
| 416 | fd_set fd_write; |
Darin Petkov | 60e1415 | 2010-10-27 16:57:04 -0700 | [diff] [blame] | 417 | fd_set fd_exc; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 418 | |
| 419 | FD_ZERO(&fd_read); |
| 420 | FD_ZERO(&fd_write); |
Darin Petkov | 60e1415 | 2010-10-27 16:57:04 -0700 | [diff] [blame] | 421 | FD_ZERO(&fd_exc); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 422 | |
| 423 | int fd_max = 0; |
| 424 | |
| 425 | // Ask libcurl for the set of file descriptors we should track on its |
| 426 | // behalf. |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 427 | CHECK_EQ(curl_multi_fdset(curl_multi_handle_, &fd_read, &fd_write, |
Darin Petkov | 60e1415 | 2010-10-27 16:57:04 -0700 | [diff] [blame] | 428 | &fd_exc, &fd_max), CURLM_OK); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 429 | |
| 430 | // We should iterate through all file descriptors up to libcurl's fd_max or |
Darin Petkov | 60e1415 | 2010-10-27 16:57:04 -0700 | [diff] [blame] | 431 | // the highest one we're tracking, whichever is larger. |
Alex Deymo | 29b8153 | 2015-07-09 11:51:49 -0700 | [diff] [blame] | 432 | for (size_t t = 0; t < arraysize(fd_task_maps_); ++t) { |
| 433 | if (!fd_task_maps_[t].empty()) |
| 434 | fd_max = max(fd_max, fd_task_maps_[t].rbegin()->first); |
Darin Petkov | 60e1415 | 2010-10-27 16:57:04 -0700 | [diff] [blame] | 435 | } |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 436 | |
Darin Petkov | 60e1415 | 2010-10-27 16:57:04 -0700 | [diff] [blame] | 437 | // For each fd, if we're not tracking it, track it. If we are tracking it, but |
| 438 | // libcurl doesn't care about it anymore, stop tracking it. After this loop, |
Alex Deymo | 29b8153 | 2015-07-09 11:51:49 -0700 | [diff] [blame] | 439 | // there should be exactly as many tasks scheduled in fd_task_maps_[0|1] as |
Darin Petkov | 60e1415 | 2010-10-27 16:57:04 -0700 | [diff] [blame] | 440 | // there are read/write fds that we're tracking. |
| 441 | for (int fd = 0; fd <= fd_max; ++fd) { |
| 442 | // Note that fd_exc is unused in the current version of libcurl so is_exc |
| 443 | // should always be false. |
| 444 | bool is_exc = FD_ISSET(fd, &fd_exc) != 0; |
| 445 | bool must_track[2] = { |
| 446 | is_exc || (FD_ISSET(fd, &fd_read) != 0), // track 0 -- read |
| 447 | is_exc || (FD_ISSET(fd, &fd_write) != 0) // track 1 -- write |
| 448 | }; |
Alex Deymo | 29b8153 | 2015-07-09 11:51:49 -0700 | [diff] [blame] | 449 | MessageLoop::WatchMode watch_modes[2] = { |
| 450 | MessageLoop::WatchMode::kWatchRead, |
| 451 | MessageLoop::WatchMode::kWatchWrite, |
| 452 | }; |
Darin Petkov | 60e1415 | 2010-10-27 16:57:04 -0700 | [diff] [blame] | 453 | |
Alex Deymo | 29b8153 | 2015-07-09 11:51:49 -0700 | [diff] [blame] | 454 | for (size_t t = 0; t < arraysize(fd_task_maps_); ++t) { |
| 455 | auto fd_task_it = fd_task_maps_[t].find(fd); |
| 456 | bool tracked = fd_task_it != fd_task_maps_[t].end(); |
Darin Petkov | 60e1415 | 2010-10-27 16:57:04 -0700 | [diff] [blame] | 457 | |
| 458 | if (!must_track[t]) { |
| 459 | // If we have an outstanding io_channel, remove it. |
| 460 | if (tracked) { |
Alex Deymo | 29b8153 | 2015-07-09 11:51:49 -0700 | [diff] [blame] | 461 | MessageLoop::current()->CancelTask(fd_task_it->second); |
| 462 | fd_task_maps_[t].erase(fd_task_it); |
Darin Petkov | 60e1415 | 2010-10-27 16:57:04 -0700 | [diff] [blame] | 463 | } |
| 464 | continue; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 465 | } |
Darin Petkov | 60e1415 | 2010-10-27 16:57:04 -0700 | [diff] [blame] | 466 | |
| 467 | // If we are already tracking this fd, continue -- nothing to do. |
| 468 | if (tracked) |
| 469 | continue; |
| 470 | |
Darin Petkov | 60e1415 | 2010-10-27 16:57:04 -0700 | [diff] [blame] | 471 | // Track a new fd. |
Alex Deymo | 29b8153 | 2015-07-09 11:51:49 -0700 | [diff] [blame] | 472 | fd_task_maps_[t][fd] = MessageLoop::current()->WatchFileDescriptor( |
| 473 | FROM_HERE, |
| 474 | fd, |
| 475 | watch_modes[t], |
| 476 | true, // persistent |
| 477 | base::Bind(&LibcurlHttpFetcher::CurlPerformOnce, |
| 478 | base::Unretained(this))); |
Darin Petkov | 60e1415 | 2010-10-27 16:57:04 -0700 | [diff] [blame] | 479 | |
Darin Petkov | 60e1415 | 2010-10-27 16:57:04 -0700 | [diff] [blame] | 480 | static int io_counter = 0; |
| 481 | io_counter++; |
| 482 | if (io_counter % 50 == 0) { |
| 483 | LOG(INFO) << "io_counter = " << io_counter; |
| 484 | } |
Andrew de los Reyes | 3270f74 | 2010-07-15 22:28:14 -0700 | [diff] [blame] | 485 | } |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 486 | } |
| 487 | |
Darin Petkov | b83371f | 2010-08-17 09:34:49 -0700 | [diff] [blame] | 488 | // Set up a timeout callback for libcurl. |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 489 | if (timeout_id_ == MessageLoop::kTaskIdNull) { |
Darin Petkov | b83371f | 2010-08-17 09:34:49 -0700 | [diff] [blame] | 490 | LOG(INFO) << "Setting up timeout source: " << idle_seconds_ << " seconds."; |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 491 | timeout_id_ = MessageLoop::current()->PostDelayedTask( |
| 492 | FROM_HERE, |
| 493 | base::Bind(&LibcurlHttpFetcher::TimeoutCallback, |
| 494 | base::Unretained(this)), |
| 495 | TimeDelta::FromSeconds(idle_seconds_)); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 496 | } |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 497 | } |
| 498 | |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 499 | void LibcurlHttpFetcher::RetryTimeoutCallback() { |
Andrew de los Reyes | 9bbd187 | 2010-07-16 14:52:29 -0700 | [diff] [blame] | 500 | ResumeTransfer(url_); |
| 501 | CurlPerformOnce(); |
Andrew de los Reyes | 9bbd187 | 2010-07-16 14:52:29 -0700 | [diff] [blame] | 502 | } |
| 503 | |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 504 | void LibcurlHttpFetcher::TimeoutCallback() { |
| 505 | if (transfer_in_progress_) |
| 506 | CurlPerformOnce(); |
| 507 | |
| 508 | // We always re-schedule the callback, even if we don't want to be called |
| 509 | // anymore. We will remove the event source separately if we don't want to |
Andrew de los Reyes | cb31933 | 2010-07-19 10:55:01 -0700 | [diff] [blame] | 510 | // be called back. |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 511 | timeout_id_ = MessageLoop::current()->PostDelayedTask( |
| 512 | FROM_HERE, |
| 513 | base::Bind(&LibcurlHttpFetcher::TimeoutCallback, base::Unretained(this)), |
| 514 | TimeDelta::FromSeconds(idle_seconds_)); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 515 | } |
| 516 | |
| 517 | void LibcurlHttpFetcher::CleanUp() { |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 518 | MessageLoop::current()->CancelTask(timeout_id_); |
| 519 | timeout_id_ = MessageLoop::kTaskIdNull; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 520 | |
Alex Deymo | 29b8153 | 2015-07-09 11:51:49 -0700 | [diff] [blame] | 521 | for (size_t t = 0; t < arraysize(fd_task_maps_); ++t) { |
| 522 | for (const auto& fd_taks_pair : fd_task_maps_[t]) { |
| 523 | if (!MessageLoop::current()->CancelTask(fd_taks_pair.second)) { |
| 524 | LOG(WARNING) << "Error canceling the watch task " |
| 525 | << fd_taks_pair.second << " for " |
| 526 | << (t ? "writing" : "reading") << " the fd " |
| 527 | << fd_taks_pair.first; |
| 528 | } |
Darin Petkov | 60e1415 | 2010-10-27 16:57:04 -0700 | [diff] [blame] | 529 | } |
Alex Deymo | 29b8153 | 2015-07-09 11:51:49 -0700 | [diff] [blame] | 530 | fd_task_maps_[t].clear(); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 531 | } |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 532 | |
Gilad Arnold | 9dd1e7c | 2012-02-16 12:13:36 -0800 | [diff] [blame] | 533 | if (curl_http_headers_) { |
| 534 | curl_slist_free_all(curl_http_headers_); |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 535 | curl_http_headers_ = nullptr; |
Gilad Arnold | 9dd1e7c | 2012-02-16 12:13:36 -0800 | [diff] [blame] | 536 | } |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 537 | if (curl_handle_) { |
| 538 | if (curl_multi_handle_) { |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 539 | CHECK_EQ(curl_multi_remove_handle(curl_multi_handle_, curl_handle_), |
| 540 | CURLM_OK); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 541 | } |
| 542 | curl_easy_cleanup(curl_handle_); |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 543 | curl_handle_ = nullptr; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 544 | } |
| 545 | if (curl_multi_handle_) { |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 546 | CHECK_EQ(curl_multi_cleanup(curl_multi_handle_), CURLM_OK); |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 547 | curl_multi_handle_ = nullptr; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 548 | } |
| 549 | transfer_in_progress_ = false; |
| 550 | } |
| 551 | |
Andrew de los Reyes | 3fd5d30 | 2010-10-07 20:07:18 -0700 | [diff] [blame] | 552 | void LibcurlHttpFetcher::GetHttpResponseCode() { |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 553 | long http_response_code = 0; // NOLINT(runtime/int) - curl needs long. |
Andrew de los Reyes | 3fd5d30 | 2010-10-07 20:07:18 -0700 | [diff] [blame] | 554 | if (curl_easy_getinfo(curl_handle_, |
| 555 | CURLINFO_RESPONSE_CODE, |
| 556 | &http_response_code) == CURLE_OK) { |
| 557 | http_response_code_ = static_cast<int>(http_response_code); |
| 558 | } |
| 559 | } |
| 560 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 561 | } // namespace chromeos_update_engine |