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