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