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