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