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