Alex Deymo | aea4c1c | 2015-08-19 20:24:43 -0700 | [diff] [blame] | 1 | // |
| 2 | // Copyright (C) 2009 The Android Open Source Project |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | // |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 16 | |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 17 | #include "update_engine/common/libcurl_http_fetcher.h" |
Andrew de los Reyes | d57d147 | 2010-10-21 13:34:08 -0700 | [diff] [blame] | 18 | |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 19 | #include <algorithm> |
Andrew de los Reyes | d57d147 | 2010-10-21 13:34:08 -0700 | [diff] [blame] | 20 | #include <string> |
| 21 | |
Alex Vakulenko | 4906c1c | 2014-08-21 13:17:44 -0700 | [diff] [blame] | 22 | #include <base/bind.h> |
Alex Deymo | c00c98a | 2015-03-17 17:38:00 -0700 | [diff] [blame] | 23 | #include <base/format_macros.h> |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 24 | #include <base/location.h> |
Andrew de los Reyes | d57d147 | 2010-10-21 13:34:08 -0700 | [diff] [blame] | 25 | #include <base/logging.h> |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 26 | #include <base/strings/string_util.h> |
| 27 | #include <base/strings/stringprintf.h> |
Andrew de los Reyes | d57d147 | 2010-10-21 13:34:08 -0700 | [diff] [blame] | 28 | |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 29 | #include "update_engine/common/certificate_checker.h" |
| 30 | #include "update_engine/common/hardware_interface.h" |
| 31 | #include "update_engine/common/platform_constants.h" |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 32 | |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 33 | using base::TimeDelta; |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 34 | using brillo::MessageLoop; |
Alex Deymo | c4acdf4 | 2014-05-28 21:07:10 -0700 | [diff] [blame] | 35 | using std::max; |
Andrew de los Reyes | d57d147 | 2010-10-21 13:34:08 -0700 | [diff] [blame] | 36 | using std::string; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 37 | |
| 38 | // This is a concrete implementation of HttpFetcher that uses libcurl to do the |
| 39 | // http work. |
| 40 | |
| 41 | namespace chromeos_update_engine { |
| 42 | |
Andrew de los Reyes | 9bbd187 | 2010-07-16 14:52:29 -0700 | [diff] [blame] | 43 | namespace { |
Andrew de los Reyes | 5d0783d | 2010-11-29 18:14:16 -0800 | [diff] [blame] | 44 | const int kNoNetworkRetrySeconds = 10; |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 45 | } // namespace |
Andrew de los Reyes | 9bbd187 | 2010-07-16 14:52:29 -0700 | [diff] [blame] | 46 | |
Alex Deymo | 33e91e7 | 2015-12-01 18:26:08 -0300 | [diff] [blame] | 47 | LibcurlHttpFetcher::LibcurlHttpFetcher(ProxyResolver* proxy_resolver, |
| 48 | HardwareInterface* hardware) |
| 49 | : HttpFetcher(proxy_resolver), hardware_(hardware) { |
Alex Deymo | c1c17b4 | 2015-11-23 03:53:15 -0300 | [diff] [blame] | 50 | // Dev users want a longer timeout (180 seconds) because they may |
| 51 | // be waiting on the dev server to build an image. |
| 52 | if (!hardware_->IsOfficialBuild()) |
| 53 | low_speed_time_seconds_ = kDownloadDevModeLowSpeedTimeSeconds; |
| 54 | if (!hardware_->IsOOBEComplete(nullptr)) |
| 55 | max_retry_count_ = kDownloadMaxRetryCountOobeNotComplete; |
| 56 | } |
| 57 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 58 | LibcurlHttpFetcher::~LibcurlHttpFetcher() { |
Darin Petkov | 9ce452b | 2010-11-17 14:33:28 -0800 | [diff] [blame] | 59 | LOG_IF(ERROR, transfer_in_progress_) |
| 60 | << "Destroying the fetcher while a transfer is in progress."; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 61 | CleanUp(); |
| 62 | } |
| 63 | |
Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 64 | bool LibcurlHttpFetcher::GetProxyType(const string& proxy, |
Gilad Arnold | 59d9e01 | 2013-07-23 16:41:43 -0700 | [diff] [blame] | 65 | curl_proxytype* out_type) { |
Alex Deymo | 56ccb07 | 2016-02-05 00:50:48 -0800 | [diff] [blame] | 66 | if (base::StartsWith( |
| 67 | proxy, "socks5://", base::CompareCase::INSENSITIVE_ASCII) || |
| 68 | base::StartsWith( |
| 69 | proxy, "socks://", base::CompareCase::INSENSITIVE_ASCII)) { |
Gilad Arnold | 59d9e01 | 2013-07-23 16:41:43 -0700 | [diff] [blame] | 70 | *out_type = CURLPROXY_SOCKS5_HOSTNAME; |
| 71 | return true; |
| 72 | } |
Alex Deymo | 56ccb07 | 2016-02-05 00:50:48 -0800 | [diff] [blame] | 73 | if (base::StartsWith( |
| 74 | proxy, "socks4://", base::CompareCase::INSENSITIVE_ASCII)) { |
Gilad Arnold | 59d9e01 | 2013-07-23 16:41:43 -0700 | [diff] [blame] | 75 | *out_type = CURLPROXY_SOCKS4A; |
| 76 | return true; |
| 77 | } |
Alex Deymo | 56ccb07 | 2016-02-05 00:50:48 -0800 | [diff] [blame] | 78 | if (base::StartsWith( |
| 79 | proxy, "http://", base::CompareCase::INSENSITIVE_ASCII) || |
| 80 | base::StartsWith( |
| 81 | proxy, "https://", base::CompareCase::INSENSITIVE_ASCII)) { |
Gilad Arnold | 59d9e01 | 2013-07-23 16:41:43 -0700 | [diff] [blame] | 82 | *out_type = CURLPROXY_HTTP; |
| 83 | return true; |
| 84 | } |
Alex Deymo | 56ccb07 | 2016-02-05 00:50:48 -0800 | [diff] [blame] | 85 | if (base::StartsWith(proxy, kNoProxy, base::CompareCase::INSENSITIVE_ASCII)) { |
Gilad Arnold | 59d9e01 | 2013-07-23 16:41:43 -0700 | [diff] [blame] | 86 | // known failure case. don't log. |
| 87 | return false; |
| 88 | } |
| 89 | LOG(INFO) << "Unknown proxy type: " << proxy; |
| 90 | return false; |
| 91 | } |
| 92 | |
Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 93 | void LibcurlHttpFetcher::ResumeTransfer(const string& url) { |
Andrew de los Reyes | 3270f74 | 2010-07-15 22:28:14 -0700 | [diff] [blame] | 94 | LOG(INFO) << "Starting/Resuming transfer"; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 95 | CHECK(!transfer_in_progress_); |
| 96 | url_ = url; |
| 97 | curl_multi_handle_ = curl_multi_init(); |
| 98 | CHECK(curl_multi_handle_); |
| 99 | |
| 100 | curl_handle_ = curl_easy_init(); |
| 101 | CHECK(curl_handle_); |
Alex Deymo | f285857 | 2016-02-25 11:20:13 -0800 | [diff] [blame^] | 102 | ignore_failure_ = false; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 103 | |
Andrew de los Reyes | 4516810 | 2010-11-22 11:13:50 -0800 | [diff] [blame] | 104 | CHECK(HasProxy()); |
Gilad Arnold | fbaee24 | 2012-04-04 15:59:43 -0700 | [diff] [blame] | 105 | bool is_direct = (GetCurrentProxy() == kNoProxy); |
| 106 | LOG(INFO) << "Using proxy: " << (is_direct ? "no" : "yes"); |
| 107 | if (is_direct) { |
Andrew de los Reyes | 4516810 | 2010-11-22 11:13:50 -0800 | [diff] [blame] | 108 | CHECK_EQ(curl_easy_setopt(curl_handle_, |
| 109 | CURLOPT_PROXY, |
| 110 | ""), CURLE_OK); |
| 111 | } else { |
| 112 | CHECK_EQ(curl_easy_setopt(curl_handle_, |
| 113 | CURLOPT_PROXY, |
| 114 | GetCurrentProxy().c_str()), CURLE_OK); |
| 115 | // Curl seems to require us to set the protocol |
| 116 | curl_proxytype type; |
Gilad Arnold | 59d9e01 | 2013-07-23 16:41:43 -0700 | [diff] [blame] | 117 | if (GetProxyType(GetCurrentProxy(), &type)) { |
Andrew de los Reyes | 4516810 | 2010-11-22 11:13:50 -0800 | [diff] [blame] | 118 | CHECK_EQ(curl_easy_setopt(curl_handle_, |
| 119 | CURLOPT_PROXYTYPE, |
| 120 | type), CURLE_OK); |
| 121 | } |
| 122 | } |
| 123 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 124 | if (post_data_set_) { |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 125 | CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_POST, 1), CURLE_OK); |
| 126 | CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_POSTFIELDS, |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 127 | post_data_.data()), |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 128 | CURLE_OK); |
| 129 | CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_POSTFIELDSIZE, |
| 130 | post_data_.size()), |
| 131 | CURLE_OK); |
Gilad Arnold | 9dd1e7c | 2012-02-16 12:13:36 -0800 | [diff] [blame] | 132 | |
| 133 | // Set the Content-Type HTTP header, if one was specifically set. |
| 134 | CHECK(!curl_http_headers_); |
| 135 | if (post_content_type_ != kHttpContentTypeUnspecified) { |
| 136 | const string content_type_attr = |
| 137 | base::StringPrintf("Content-Type: %s", |
| 138 | GetHttpContentTypeString(post_content_type_)); |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 139 | curl_http_headers_ = curl_slist_append(nullptr, |
| 140 | content_type_attr.c_str()); |
Gilad Arnold | 9dd1e7c | 2012-02-16 12:13:36 -0800 | [diff] [blame] | 141 | CHECK(curl_http_headers_); |
| 142 | CHECK_EQ( |
| 143 | curl_easy_setopt(curl_handle_, CURLOPT_HTTPHEADER, |
| 144 | curl_http_headers_), |
| 145 | CURLE_OK); |
| 146 | } else { |
| 147 | LOG(WARNING) << "no content type set, using libcurl default"; |
| 148 | } |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 149 | } |
| 150 | |
Gilad Arnold | e4ad250 | 2011-12-29 17:08:54 -0800 | [diff] [blame] | 151 | if (bytes_downloaded_ > 0 || download_length_) { |
| 152 | // Resume from where we left off. |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 153 | resume_offset_ = bytes_downloaded_; |
Gilad Arnold | e4ad250 | 2011-12-29 17:08:54 -0800 | [diff] [blame] | 154 | CHECK_GE(resume_offset_, 0); |
| 155 | |
| 156 | // Compute end offset, if one is specified. As per HTTP specification, this |
| 157 | // is an inclusive boundary. Make sure it doesn't overflow. |
| 158 | size_t end_offset = 0; |
| 159 | if (download_length_) { |
| 160 | end_offset = static_cast<size_t>(resume_offset_) + download_length_ - 1; |
| 161 | CHECK_LE((size_t) resume_offset_, end_offset); |
| 162 | } |
| 163 | |
| 164 | // Create a string representation of the desired range. |
Alex Deymo | c00c98a | 2015-03-17 17:38:00 -0700 | [diff] [blame] | 165 | string range_str = base::StringPrintf( |
| 166 | "%" PRIu64 "-", static_cast<uint64_t>(resume_offset_)); |
| 167 | if (end_offset) |
| 168 | range_str += std::to_string(end_offset); |
Gilad Arnold | e4ad250 | 2011-12-29 17:08:54 -0800 | [diff] [blame] | 169 | CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_RANGE, range_str.c_str()), |
| 170 | CURLE_OK); |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_WRITEDATA, this), CURLE_OK); |
| 174 | CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_WRITEFUNCTION, |
| 175 | StaticLibcurlWrite), CURLE_OK); |
Chris Sosa | 77f79e8 | 2014-06-02 18:16:24 -0700 | [diff] [blame] | 176 | 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] | 177 | CURLE_OK); |
Andrew de los Reyes | 3270f74 | 2010-07-15 22:28:14 -0700 | [diff] [blame] | 178 | |
David Zeuthen | 34135a9 | 2013-08-06 11:16:16 -0700 | [diff] [blame] | 179 | // If the connection drops under |low_speed_limit_bps_| (10 |
| 180 | // bytes/sec by default) for |low_speed_time_seconds_| (90 seconds, |
| 181 | // 180 on non-official builds), reconnect. |
| 182 | CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_LOW_SPEED_LIMIT, |
| 183 | low_speed_limit_bps_), |
Andrew de los Reyes | 3270f74 | 2010-07-15 22:28:14 -0700 | [diff] [blame] | 184 | CURLE_OK); |
David Zeuthen | 34135a9 | 2013-08-06 11:16:16 -0700 | [diff] [blame] | 185 | CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_LOW_SPEED_TIME, |
| 186 | low_speed_time_seconds_), |
Andrew de los Reyes | 3270f74 | 2010-07-15 22:28:14 -0700 | [diff] [blame] | 187 | CURLE_OK); |
David Zeuthen | 34135a9 | 2013-08-06 11:16:16 -0700 | [diff] [blame] | 188 | CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_CONNECTTIMEOUT, |
| 189 | connect_timeout_seconds_), |
Andrew de los Reyes | e72f9c0 | 2011-04-20 10:47:40 -0700 | [diff] [blame] | 190 | CURLE_OK); |
Andrew de los Reyes | 3270f74 | 2010-07-15 22:28:14 -0700 | [diff] [blame] | 191 | |
Darin Petkov | 41c2fcf | 2010-08-25 13:14:48 -0700 | [diff] [blame] | 192 | // By default, libcurl doesn't follow redirections. Allow up to |
David Zeuthen | 34135a9 | 2013-08-06 11:16:16 -0700 | [diff] [blame] | 193 | // |kDownloadMaxRedirects| redirections. |
Darin Petkov | 3a4016a | 2010-09-28 13:54:17 -0700 | [diff] [blame] | 194 | CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_FOLLOWLOCATION, 1), CURLE_OK); |
David Zeuthen | 34135a9 | 2013-08-06 11:16:16 -0700 | [diff] [blame] | 195 | CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_MAXREDIRS, |
| 196 | kDownloadMaxRedirects), |
Darin Petkov | 41c2fcf | 2010-08-25 13:14:48 -0700 | [diff] [blame] | 197 | CURLE_OK); |
| 198 | |
Nam T. Nguyen | 7d623eb | 2014-05-13 16:06:28 -0700 | [diff] [blame] | 199 | // Lock down the appropriate curl options for HTTP or HTTPS depending on |
| 200 | // the url. |
Alex Deymo | c1c17b4 | 2015-11-23 03:53:15 -0300 | [diff] [blame] | 201 | if (hardware_->IsOfficialBuild()) { |
Alex Deymo | 56ccb07 | 2016-02-05 00:50:48 -0800 | [diff] [blame] | 202 | if (base::StartsWith( |
| 203 | url_, "http://", base::CompareCase::INSENSITIVE_ASCII)) { |
Jay Srinivasan | b3f5540 | 2012-12-03 18:12:04 -0800 | [diff] [blame] | 204 | SetCurlOptionsForHttp(); |
Alex Deymo | 56ccb07 | 2016-02-05 00:50:48 -0800 | [diff] [blame] | 205 | } else if (base::StartsWith( |
| 206 | url_, "https://", base::CompareCase::INSENSITIVE_ASCII)) { |
Jay Srinivasan | b3f5540 | 2012-12-03 18:12:04 -0800 | [diff] [blame] | 207 | SetCurlOptionsForHttps(); |
Alex Deymo | 56ccb07 | 2016-02-05 00:50:48 -0800 | [diff] [blame] | 208 | #if !defined(__CHROMEOS__) && !defined(__BRILLO__) |
| 209 | } else if (base::StartsWith( |
| 210 | url_, "file://", base::CompareCase::INSENSITIVE_ASCII)) { |
| 211 | SetCurlOptionsForFile(); |
| 212 | #endif |
| 213 | } else { |
| 214 | LOG(ERROR) << "Received invalid URI: " << url_; |
| 215 | // Lock down to no protocol supported for the transfer. |
| 216 | CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_PROTOCOLS, 0), CURLE_OK); |
| 217 | } |
Jay Srinivasan | b3f5540 | 2012-12-03 18:12:04 -0800 | [diff] [blame] | 218 | } else { |
Nam T. Nguyen | 7d623eb | 2014-05-13 16:06:28 -0700 | [diff] [blame] | 219 | LOG(INFO) << "Not setting http(s) curl options because we are " |
| 220 | << "running a dev/test image"; |
Darin Petkov | fc7a0ce | 2010-10-25 10:38:37 -0700 | [diff] [blame] | 221 | } |
| 222 | |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 223 | 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] | 224 | transfer_in_progress_ = true; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 225 | } |
| 226 | |
Jay Srinivasan | b3f5540 | 2012-12-03 18:12:04 -0800 | [diff] [blame] | 227 | // Lock down only the protocol in case of HTTP. |
| 228 | void LibcurlHttpFetcher::SetCurlOptionsForHttp() { |
| 229 | LOG(INFO) << "Setting up curl options for HTTP"; |
| 230 | CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_PROTOCOLS, CURLPROTO_HTTP), |
| 231 | CURLE_OK); |
| 232 | CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_REDIR_PROTOCOLS, |
| 233 | CURLPROTO_HTTP), |
| 234 | CURLE_OK); |
| 235 | } |
| 236 | |
| 237 | // Security lock-down in official builds: makes sure that peer certificate |
| 238 | // verification is enabled, restricts the set of trusted certificates, |
| 239 | // restricts protocols to HTTPS, restricts ciphers to HIGH. |
| 240 | void LibcurlHttpFetcher::SetCurlOptionsForHttps() { |
| 241 | LOG(INFO) << "Setting up curl options for HTTPS"; |
| 242 | CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_SSL_VERIFYPEER, 1), |
| 243 | CURLE_OK); |
Alex Deymo | 35b3584 | 2015-10-20 11:21:56 -0700 | [diff] [blame] | 244 | CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_CAPATH, |
| 245 | constants::kCACertificatesPath), |
Jay Srinivasan | b3f5540 | 2012-12-03 18:12:04 -0800 | [diff] [blame] | 246 | CURLE_OK); |
| 247 | CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_PROTOCOLS, CURLPROTO_HTTPS), |
| 248 | CURLE_OK); |
| 249 | CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_REDIR_PROTOCOLS, |
| 250 | CURLPROTO_HTTPS), |
| 251 | CURLE_OK); |
| 252 | CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_SSL_CIPHER_LIST, "HIGH:!ADH"), |
| 253 | CURLE_OK); |
Alex Deymo | 33e91e7 | 2015-12-01 18:26:08 -0300 | [diff] [blame] | 254 | if (server_to_check_ != ServerToCheck::kNone) { |
| 255 | CHECK_EQ( |
| 256 | curl_easy_setopt(curl_handle_, CURLOPT_SSL_CTX_DATA, &server_to_check_), |
| 257 | CURLE_OK); |
Jay Srinivasan | b3f5540 | 2012-12-03 18:12:04 -0800 | [diff] [blame] | 258 | CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_SSL_CTX_FUNCTION, |
| 259 | CertificateChecker::ProcessSSLContext), |
| 260 | CURLE_OK); |
| 261 | } |
| 262 | } |
| 263 | |
Alex Deymo | 56ccb07 | 2016-02-05 00:50:48 -0800 | [diff] [blame] | 264 | // Lock down only the protocol in case of a local file. |
| 265 | void LibcurlHttpFetcher::SetCurlOptionsForFile() { |
| 266 | LOG(INFO) << "Setting up curl options for FILE"; |
| 267 | CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_PROTOCOLS, CURLPROTO_FILE), |
| 268 | CURLE_OK); |
| 269 | CHECK_EQ( |
| 270 | curl_easy_setopt(curl_handle_, CURLOPT_REDIR_PROTOCOLS, CURLPROTO_FILE), |
| 271 | CURLE_OK); |
| 272 | } |
Jay Srinivasan | b3f5540 | 2012-12-03 18:12:04 -0800 | [diff] [blame] | 273 | |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 274 | // Begins the transfer, which must not have already been started. |
Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 275 | void LibcurlHttpFetcher::BeginTransfer(const string& url) { |
Andrew de los Reyes | f3ed8e7 | 2011-02-16 10:35:46 -0800 | [diff] [blame] | 276 | CHECK(!transfer_in_progress_); |
| 277 | url_ = url; |
Alex Vakulenko | 4906c1c | 2014-08-21 13:17:44 -0700 | [diff] [blame] | 278 | auto closure = base::Bind(&LibcurlHttpFetcher::ProxiesResolved, |
| 279 | base::Unretained(this)); |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 280 | if (!ResolveProxiesForUrl(url_, closure)) { |
Andrew de los Reyes | f3ed8e7 | 2011-02-16 10:35:46 -0800 | [diff] [blame] | 281 | LOG(ERROR) << "Couldn't resolve proxies"; |
| 282 | if (delegate_) |
| 283 | delegate_->TransferComplete(this, false); |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | void LibcurlHttpFetcher::ProxiesResolved() { |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 288 | transfer_size_ = -1; |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 289 | resume_offset_ = 0; |
Andrew de los Reyes | 9bbd187 | 2010-07-16 14:52:29 -0700 | [diff] [blame] | 290 | retry_count_ = 0; |
Darin Petkov | a092955 | 2010-11-29 14:19:06 -0800 | [diff] [blame] | 291 | no_network_retry_count_ = 0; |
Darin Petkov | cb46621 | 2010-08-26 09:40:11 -0700 | [diff] [blame] | 292 | http_response_code_ = 0; |
Andrew de los Reyes | 819fef2 | 2010-12-17 11:33:58 -0800 | [diff] [blame] | 293 | terminate_requested_ = false; |
Gilad Arnold | a2dee1d | 2012-04-12 11:50:37 -0700 | [diff] [blame] | 294 | sent_byte_ = false; |
Alex Deymo | f285857 | 2016-02-25 11:20:13 -0800 | [diff] [blame^] | 295 | |
| 296 | // If we are paused, we delay these two operations until Unpause is called. |
| 297 | if (transfer_paused_) { |
| 298 | restart_transfer_on_unpause_ = true; |
| 299 | return; |
| 300 | } |
Andrew de los Reyes | f3ed8e7 | 2011-02-16 10:35:46 -0800 | [diff] [blame] | 301 | ResumeTransfer(url_); |
Andrew de los Reyes | 9bbd187 | 2010-07-16 14:52:29 -0700 | [diff] [blame] | 302 | CurlPerformOnce(); |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 303 | } |
| 304 | |
Darin Petkov | 9ce452b | 2010-11-17 14:33:28 -0800 | [diff] [blame] | 305 | void LibcurlHttpFetcher::ForceTransferTermination() { |
| 306 | CleanUp(); |
| 307 | if (delegate_) { |
| 308 | // Note that after the callback returns this object may be destroyed. |
| 309 | delegate_->TransferTerminated(this); |
| 310 | } |
| 311 | } |
| 312 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 313 | void LibcurlHttpFetcher::TerminateTransfer() { |
Darin Petkov | 9ce452b | 2010-11-17 14:33:28 -0800 | [diff] [blame] | 314 | if (in_write_callback_) { |
Andrew de los Reyes | 3fd5d30 | 2010-10-07 20:07:18 -0700 | [diff] [blame] | 315 | terminate_requested_ = true; |
Darin Petkov | 9ce452b | 2010-11-17 14:33:28 -0800 | [diff] [blame] | 316 | } else { |
| 317 | ForceTransferTermination(); |
| 318 | } |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 319 | } |
| 320 | |
Andrew de los Reyes | cb31933 | 2010-07-19 10:55:01 -0700 | [diff] [blame] | 321 | void LibcurlHttpFetcher::CurlPerformOnce() { |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 322 | CHECK(transfer_in_progress_); |
| 323 | int running_handles = 0; |
| 324 | CURLMcode retcode = CURLM_CALL_MULTI_PERFORM; |
| 325 | |
| 326 | // libcurl may request that we immediately call curl_multi_perform after it |
| 327 | // returns, so we do. libcurl promises that curl_multi_perform will not block. |
| 328 | while (CURLM_CALL_MULTI_PERFORM == retcode) { |
| 329 | retcode = curl_multi_perform(curl_multi_handle_, &running_handles); |
Andrew de los Reyes | 3fd5d30 | 2010-10-07 20:07:18 -0700 | [diff] [blame] | 330 | if (terminate_requested_) { |
Darin Petkov | 9ce452b | 2010-11-17 14:33:28 -0800 | [diff] [blame] | 331 | ForceTransferTermination(); |
Andrew de los Reyes | 3fd5d30 | 2010-10-07 20:07:18 -0700 | [diff] [blame] | 332 | return; |
| 333 | } |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 334 | } |
Alex Deymo | f285857 | 2016-02-25 11:20:13 -0800 | [diff] [blame^] | 335 | |
| 336 | // If the transfer completes while paused, we should ignore the failure once |
| 337 | // the fetcher is unpaused. |
| 338 | if (running_handles == 0 && transfer_paused_ && !ignore_failure_) { |
| 339 | LOG(INFO) << "Connection closed while paused, ignoring failure."; |
| 340 | ignore_failure_ = true; |
| 341 | } |
| 342 | |
| 343 | if (running_handles != 0 || transfer_paused_) { |
| 344 | // There's either more work to do or we are paused, so we just keep the |
| 345 | // file descriptors to watch up to date and exit, until we are done with the |
| 346 | // work and we are not paused. |
| 347 | SetupMessageLoopSources(); |
| 348 | return; |
| 349 | } |
| 350 | |
| 351 | // At this point, the transfer was completed in some way (error, connection |
| 352 | // closed or download finished). |
| 353 | |
| 354 | GetHttpResponseCode(); |
| 355 | if (http_response_code_) { |
| 356 | LOG(INFO) << "HTTP response code: " << http_response_code_; |
| 357 | no_network_retry_count_ = 0; |
| 358 | } else { |
| 359 | LOG(ERROR) << "Unable to get http response code."; |
| 360 | } |
| 361 | |
| 362 | // we're done! |
| 363 | CleanUp(); |
| 364 | |
| 365 | // TODO(petkov): This temporary code tries to deal with the case where the |
| 366 | // update engine performs an update check while the network is not ready |
| 367 | // (e.g., right after resume). Longer term, we should check if the network |
| 368 | // is online/offline and return an appropriate error code. |
| 369 | if (!sent_byte_ && |
| 370 | http_response_code_ == 0 && |
| 371 | no_network_retry_count_ < no_network_max_retries_) { |
| 372 | no_network_retry_count_++; |
| 373 | MessageLoop::current()->PostDelayedTask( |
| 374 | FROM_HERE, |
| 375 | base::Bind(&LibcurlHttpFetcher::RetryTimeoutCallback, |
| 376 | base::Unretained(this)), |
| 377 | TimeDelta::FromSeconds(kNoNetworkRetrySeconds)); |
| 378 | LOG(INFO) << "No HTTP response, retry " << no_network_retry_count_; |
| 379 | } else if ((!sent_byte_ && !IsHttpResponseSuccess()) || |
| 380 | IsHttpResponseError()) { |
| 381 | // The transfer completed w/ error and we didn't get any bytes. |
| 382 | // If we have another proxy to try, try that. |
| 383 | // |
| 384 | // TODO(garnold) in fact there are two separate cases here: one case is an |
| 385 | // other-than-success return code (including no return code) and no |
| 386 | // received bytes, which is necessary due to the way callbacks are |
| 387 | // currently processing error conditions; the second is an explicit HTTP |
| 388 | // error code, where some data may have been received (as in the case of a |
| 389 | // semi-successful multi-chunk fetch). This is a confusing behavior and |
| 390 | // should be unified into a complete, coherent interface. |
| 391 | LOG(INFO) << "Transfer resulted in an error (" << http_response_code_ |
| 392 | << "), " << bytes_downloaded_ << " bytes downloaded"; |
| 393 | |
| 394 | PopProxy(); // Delete the proxy we just gave up on. |
| 395 | |
| 396 | if (HasProxy()) { |
| 397 | // We have another proxy. Retry immediately. |
| 398 | LOG(INFO) << "Retrying with next proxy setting"; |
| 399 | MessageLoop::current()->PostTask( |
| 400 | FROM_HERE, |
| 401 | base::Bind(&LibcurlHttpFetcher::RetryTimeoutCallback, |
| 402 | base::Unretained(this))); |
Andrew de los Reyes | 9bbd187 | 2010-07-16 14:52:29 -0700 | [diff] [blame] | 403 | } else { |
Alex Deymo | f285857 | 2016-02-25 11:20:13 -0800 | [diff] [blame^] | 404 | // Out of proxies. Give up. |
| 405 | LOG(INFO) << "No further proxies, indicating transfer complete"; |
| 406 | if (delegate_) |
| 407 | delegate_->TransferComplete(this, false); // signal fail |
Andrew de los Reyes | 9bbd187 | 2010-07-16 14:52:29 -0700 | [diff] [blame] | 408 | } |
Alex Deymo | f285857 | 2016-02-25 11:20:13 -0800 | [diff] [blame^] | 409 | } else if ((transfer_size_ >= 0) && (bytes_downloaded_ < transfer_size_)) { |
| 410 | if (!ignore_failure_) |
| 411 | retry_count_++; |
| 412 | LOG(INFO) << "Transfer interrupted after downloading " |
| 413 | << bytes_downloaded_ << " of " << transfer_size_ << " bytes. " |
| 414 | << transfer_size_ - bytes_downloaded_ << " bytes remaining " |
| 415 | << "after " << retry_count_ << " attempt(s)"; |
Darin Petkov | 192ced4 | 2010-07-23 16:20:24 -0700 | [diff] [blame] | 416 | |
Alex Deymo | f285857 | 2016-02-25 11:20:13 -0800 | [diff] [blame^] | 417 | if (retry_count_ > max_retry_count_) { |
| 418 | LOG(INFO) << "Reached max attempts (" << retry_count_ << ")"; |
| 419 | if (delegate_) |
| 420 | delegate_->TransferComplete(this, false); // signal fail |
| 421 | } else { |
| 422 | // Need to restart transfer |
| 423 | LOG(INFO) << "Restarting transfer to download the remaining bytes"; |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 424 | MessageLoop::current()->PostDelayedTask( |
| 425 | FROM_HERE, |
| 426 | base::Bind(&LibcurlHttpFetcher::RetryTimeoutCallback, |
| 427 | base::Unretained(this)), |
Alex Deymo | f285857 | 2016-02-25 11:20:13 -0800 | [diff] [blame^] | 428 | TimeDelta::FromSeconds(retry_seconds_)); |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 429 | } |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 430 | } else { |
Alex Deymo | f285857 | 2016-02-25 11:20:13 -0800 | [diff] [blame^] | 431 | LOG(INFO) << "Transfer completed (" << http_response_code_ |
| 432 | << "), " << bytes_downloaded_ << " bytes downloaded"; |
| 433 | if (delegate_) { |
| 434 | bool success = IsHttpResponseSuccess(); |
| 435 | delegate_->TransferComplete(this, success); |
| 436 | } |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 437 | } |
Alex Deymo | f285857 | 2016-02-25 11:20:13 -0800 | [diff] [blame^] | 438 | ignore_failure_ = false; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 439 | } |
| 440 | |
| 441 | size_t LibcurlHttpFetcher::LibcurlWrite(void *ptr, size_t size, size_t nmemb) { |
Gilad Arnold | 48085ba | 2011-11-16 09:36:08 -0800 | [diff] [blame] | 442 | // Update HTTP response first. |
Andrew de los Reyes | 3fd5d30 | 2010-10-07 20:07:18 -0700 | [diff] [blame] | 443 | GetHttpResponseCode(); |
Gilad Arnold | 48085ba | 2011-11-16 09:36:08 -0800 | [diff] [blame] | 444 | const size_t payload_size = size * nmemb; |
| 445 | |
| 446 | // Do nothing if no payload or HTTP response is an error. |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 447 | if (payload_size == 0 || !IsHttpResponseSuccess()) { |
Gilad Arnold | 48085ba | 2011-11-16 09:36:08 -0800 | [diff] [blame] | 448 | LOG(INFO) << "HTTP response unsuccessful (" << http_response_code_ |
| 449 | << ") or no payload (" << payload_size << "), nothing to do"; |
| 450 | return 0; |
| 451 | } |
| 452 | |
| 453 | sent_byte_ = true; |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 454 | { |
| 455 | double transfer_size_double; |
| 456 | CHECK_EQ(curl_easy_getinfo(curl_handle_, |
| 457 | CURLINFO_CONTENT_LENGTH_DOWNLOAD, |
| 458 | &transfer_size_double), CURLE_OK); |
| 459 | off_t new_transfer_size = static_cast<off_t>(transfer_size_double); |
| 460 | if (new_transfer_size > 0) { |
| 461 | transfer_size_ = resume_offset_ + new_transfer_size; |
| 462 | } |
| 463 | } |
Gilad Arnold | 48085ba | 2011-11-16 09:36:08 -0800 | [diff] [blame] | 464 | bytes_downloaded_ += payload_size; |
Andrew de los Reyes | 3fd5d30 | 2010-10-07 20:07:18 -0700 | [diff] [blame] | 465 | in_write_callback_ = true; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 466 | if (delegate_) |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 467 | delegate_->ReceivedBytes(this, ptr, payload_size); |
Andrew de los Reyes | 3fd5d30 | 2010-10-07 20:07:18 -0700 | [diff] [blame] | 468 | in_write_callback_ = false; |
Gilad Arnold | 48085ba | 2011-11-16 09:36:08 -0800 | [diff] [blame] | 469 | return payload_size; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 470 | } |
| 471 | |
| 472 | void LibcurlHttpFetcher::Pause() { |
Alex Deymo | f285857 | 2016-02-25 11:20:13 -0800 | [diff] [blame^] | 473 | if (transfer_paused_) { |
| 474 | LOG(ERROR) << "Fetcher already paused."; |
| 475 | return; |
| 476 | } |
| 477 | transfer_paused_ = true; |
| 478 | if (!transfer_in_progress_) { |
| 479 | // If pause before we started a connection, we don't need to notify curl |
| 480 | // about that, we will simply not start the connection later. |
| 481 | return; |
| 482 | } |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 483 | CHECK(curl_handle_); |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 484 | CHECK_EQ(curl_easy_pause(curl_handle_, CURLPAUSE_ALL), CURLE_OK); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 485 | } |
| 486 | |
| 487 | void LibcurlHttpFetcher::Unpause() { |
Alex Deymo | f285857 | 2016-02-25 11:20:13 -0800 | [diff] [blame^] | 488 | if (!transfer_paused_) { |
| 489 | LOG(ERROR) << "Resume attempted when fetcher not paused."; |
| 490 | return; |
| 491 | } |
| 492 | transfer_paused_ = false; |
| 493 | if (restart_transfer_on_unpause_) { |
| 494 | restart_transfer_on_unpause_ = false; |
| 495 | ResumeTransfer(url_); |
| 496 | CurlPerformOnce(); |
| 497 | return; |
| 498 | } |
| 499 | if (!transfer_in_progress_) { |
| 500 | // If resumed before starting the connection, there's no need to notify |
| 501 | // anybody. We will simply start the connection once it is time. |
| 502 | return; |
| 503 | } |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 504 | CHECK(curl_handle_); |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 505 | CHECK_EQ(curl_easy_pause(curl_handle_, CURLPAUSE_CONT), CURLE_OK); |
Alex Deymo | f285857 | 2016-02-25 11:20:13 -0800 | [diff] [blame^] | 506 | // Since the transfer is in progress, we need to dispatch a CurlPerformOnce() |
| 507 | // now to let the connection continue, otherwise it would be called by the |
| 508 | // TimeoutCallback but with a delay. |
| 509 | CurlPerformOnce(); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 510 | } |
| 511 | |
Alex Deymo | 29b8153 | 2015-07-09 11:51:49 -0700 | [diff] [blame] | 512 | // This method sets up callbacks with the MessageLoop. |
| 513 | void LibcurlHttpFetcher::SetupMessageLoopSources() { |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 514 | fd_set fd_read; |
| 515 | fd_set fd_write; |
Darin Petkov | 60e1415 | 2010-10-27 16:57:04 -0700 | [diff] [blame] | 516 | fd_set fd_exc; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 517 | |
| 518 | FD_ZERO(&fd_read); |
| 519 | FD_ZERO(&fd_write); |
Darin Petkov | 60e1415 | 2010-10-27 16:57:04 -0700 | [diff] [blame] | 520 | FD_ZERO(&fd_exc); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 521 | |
| 522 | int fd_max = 0; |
| 523 | |
| 524 | // Ask libcurl for the set of file descriptors we should track on its |
| 525 | // behalf. |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 526 | CHECK_EQ(curl_multi_fdset(curl_multi_handle_, &fd_read, &fd_write, |
Darin Petkov | 60e1415 | 2010-10-27 16:57:04 -0700 | [diff] [blame] | 527 | &fd_exc, &fd_max), CURLM_OK); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 528 | |
| 529 | // 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] | 530 | // the highest one we're tracking, whichever is larger. |
Alex Deymo | 29b8153 | 2015-07-09 11:51:49 -0700 | [diff] [blame] | 531 | for (size_t t = 0; t < arraysize(fd_task_maps_); ++t) { |
| 532 | if (!fd_task_maps_[t].empty()) |
| 533 | fd_max = max(fd_max, fd_task_maps_[t].rbegin()->first); |
Darin Petkov | 60e1415 | 2010-10-27 16:57:04 -0700 | [diff] [blame] | 534 | } |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 535 | |
Darin Petkov | 60e1415 | 2010-10-27 16:57:04 -0700 | [diff] [blame] | 536 | // For each fd, if we're not tracking it, track it. If we are tracking it, but |
| 537 | // libcurl doesn't care about it anymore, stop tracking it. After this loop, |
Alex Deymo | 29b8153 | 2015-07-09 11:51:49 -0700 | [diff] [blame] | 538 | // there should be exactly as many tasks scheduled in fd_task_maps_[0|1] as |
Darin Petkov | 60e1415 | 2010-10-27 16:57:04 -0700 | [diff] [blame] | 539 | // there are read/write fds that we're tracking. |
| 540 | for (int fd = 0; fd <= fd_max; ++fd) { |
| 541 | // Note that fd_exc is unused in the current version of libcurl so is_exc |
| 542 | // should always be false. |
| 543 | bool is_exc = FD_ISSET(fd, &fd_exc) != 0; |
| 544 | bool must_track[2] = { |
| 545 | is_exc || (FD_ISSET(fd, &fd_read) != 0), // track 0 -- read |
| 546 | is_exc || (FD_ISSET(fd, &fd_write) != 0) // track 1 -- write |
| 547 | }; |
Alex Deymo | 29b8153 | 2015-07-09 11:51:49 -0700 | [diff] [blame] | 548 | MessageLoop::WatchMode watch_modes[2] = { |
| 549 | MessageLoop::WatchMode::kWatchRead, |
| 550 | MessageLoop::WatchMode::kWatchWrite, |
| 551 | }; |
Darin Petkov | 60e1415 | 2010-10-27 16:57:04 -0700 | [diff] [blame] | 552 | |
Alex Deymo | 29b8153 | 2015-07-09 11:51:49 -0700 | [diff] [blame] | 553 | for (size_t t = 0; t < arraysize(fd_task_maps_); ++t) { |
| 554 | auto fd_task_it = fd_task_maps_[t].find(fd); |
| 555 | bool tracked = fd_task_it != fd_task_maps_[t].end(); |
Darin Petkov | 60e1415 | 2010-10-27 16:57:04 -0700 | [diff] [blame] | 556 | |
| 557 | if (!must_track[t]) { |
| 558 | // If we have an outstanding io_channel, remove it. |
| 559 | if (tracked) { |
Alex Deymo | 29b8153 | 2015-07-09 11:51:49 -0700 | [diff] [blame] | 560 | MessageLoop::current()->CancelTask(fd_task_it->second); |
| 561 | fd_task_maps_[t].erase(fd_task_it); |
Darin Petkov | 60e1415 | 2010-10-27 16:57:04 -0700 | [diff] [blame] | 562 | } |
| 563 | continue; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 564 | } |
Darin Petkov | 60e1415 | 2010-10-27 16:57:04 -0700 | [diff] [blame] | 565 | |
| 566 | // If we are already tracking this fd, continue -- nothing to do. |
| 567 | if (tracked) |
| 568 | continue; |
| 569 | |
Darin Petkov | 60e1415 | 2010-10-27 16:57:04 -0700 | [diff] [blame] | 570 | // Track a new fd. |
Alex Deymo | 29b8153 | 2015-07-09 11:51:49 -0700 | [diff] [blame] | 571 | fd_task_maps_[t][fd] = MessageLoop::current()->WatchFileDescriptor( |
| 572 | FROM_HERE, |
| 573 | fd, |
| 574 | watch_modes[t], |
| 575 | true, // persistent |
| 576 | base::Bind(&LibcurlHttpFetcher::CurlPerformOnce, |
| 577 | base::Unretained(this))); |
Darin Petkov | 60e1415 | 2010-10-27 16:57:04 -0700 | [diff] [blame] | 578 | |
Darin Petkov | 60e1415 | 2010-10-27 16:57:04 -0700 | [diff] [blame] | 579 | static int io_counter = 0; |
| 580 | io_counter++; |
| 581 | if (io_counter % 50 == 0) { |
| 582 | LOG(INFO) << "io_counter = " << io_counter; |
| 583 | } |
Andrew de los Reyes | 3270f74 | 2010-07-15 22:28:14 -0700 | [diff] [blame] | 584 | } |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 585 | } |
| 586 | |
Darin Petkov | b83371f | 2010-08-17 09:34:49 -0700 | [diff] [blame] | 587 | // Set up a timeout callback for libcurl. |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 588 | if (timeout_id_ == MessageLoop::kTaskIdNull) { |
Alex Deymo | f285857 | 2016-02-25 11:20:13 -0800 | [diff] [blame^] | 589 | VLOG(1) << "Setting up timeout source: " << idle_seconds_ << " seconds."; |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 590 | timeout_id_ = MessageLoop::current()->PostDelayedTask( |
| 591 | FROM_HERE, |
| 592 | base::Bind(&LibcurlHttpFetcher::TimeoutCallback, |
| 593 | base::Unretained(this)), |
| 594 | TimeDelta::FromSeconds(idle_seconds_)); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 595 | } |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 596 | } |
| 597 | |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 598 | void LibcurlHttpFetcher::RetryTimeoutCallback() { |
Alex Deymo | f285857 | 2016-02-25 11:20:13 -0800 | [diff] [blame^] | 599 | if (transfer_paused_) { |
| 600 | restart_transfer_on_unpause_ = true; |
| 601 | return; |
| 602 | } |
Andrew de los Reyes | 9bbd187 | 2010-07-16 14:52:29 -0700 | [diff] [blame] | 603 | ResumeTransfer(url_); |
| 604 | CurlPerformOnce(); |
Andrew de los Reyes | 9bbd187 | 2010-07-16 14:52:29 -0700 | [diff] [blame] | 605 | } |
| 606 | |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 607 | void LibcurlHttpFetcher::TimeoutCallback() { |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 608 | // We always re-schedule the callback, even if we don't want to be called |
| 609 | // anymore. We will remove the event source separately if we don't want to |
Andrew de los Reyes | cb31933 | 2010-07-19 10:55:01 -0700 | [diff] [blame] | 610 | // be called back. |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 611 | timeout_id_ = MessageLoop::current()->PostDelayedTask( |
| 612 | FROM_HERE, |
| 613 | base::Bind(&LibcurlHttpFetcher::TimeoutCallback, base::Unretained(this)), |
| 614 | TimeDelta::FromSeconds(idle_seconds_)); |
Alex Deymo | f123ae2 | 2015-09-24 14:59:43 -0700 | [diff] [blame] | 615 | |
| 616 | // CurlPerformOnce() may call CleanUp(), so we need to schedule our callback |
| 617 | // first, since it could be canceled by this call. |
| 618 | if (transfer_in_progress_) |
| 619 | CurlPerformOnce(); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 620 | } |
| 621 | |
| 622 | void LibcurlHttpFetcher::CleanUp() { |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 623 | MessageLoop::current()->CancelTask(timeout_id_); |
| 624 | timeout_id_ = MessageLoop::kTaskIdNull; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 625 | |
Alex Deymo | 29b8153 | 2015-07-09 11:51:49 -0700 | [diff] [blame] | 626 | for (size_t t = 0; t < arraysize(fd_task_maps_); ++t) { |
| 627 | for (const auto& fd_taks_pair : fd_task_maps_[t]) { |
| 628 | if (!MessageLoop::current()->CancelTask(fd_taks_pair.second)) { |
| 629 | LOG(WARNING) << "Error canceling the watch task " |
| 630 | << fd_taks_pair.second << " for " |
| 631 | << (t ? "writing" : "reading") << " the fd " |
| 632 | << fd_taks_pair.first; |
| 633 | } |
Darin Petkov | 60e1415 | 2010-10-27 16:57:04 -0700 | [diff] [blame] | 634 | } |
Alex Deymo | 29b8153 | 2015-07-09 11:51:49 -0700 | [diff] [blame] | 635 | fd_task_maps_[t].clear(); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 636 | } |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 637 | |
Gilad Arnold | 9dd1e7c | 2012-02-16 12:13:36 -0800 | [diff] [blame] | 638 | if (curl_http_headers_) { |
| 639 | curl_slist_free_all(curl_http_headers_); |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 640 | curl_http_headers_ = nullptr; |
Gilad Arnold | 9dd1e7c | 2012-02-16 12:13:36 -0800 | [diff] [blame] | 641 | } |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 642 | if (curl_handle_) { |
| 643 | if (curl_multi_handle_) { |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 644 | CHECK_EQ(curl_multi_remove_handle(curl_multi_handle_, curl_handle_), |
| 645 | CURLM_OK); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 646 | } |
| 647 | curl_easy_cleanup(curl_handle_); |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 648 | curl_handle_ = nullptr; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 649 | } |
| 650 | if (curl_multi_handle_) { |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 651 | CHECK_EQ(curl_multi_cleanup(curl_multi_handle_), CURLM_OK); |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 652 | curl_multi_handle_ = nullptr; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 653 | } |
| 654 | transfer_in_progress_ = false; |
Alex Deymo | f285857 | 2016-02-25 11:20:13 -0800 | [diff] [blame^] | 655 | transfer_paused_ = false; |
| 656 | restart_transfer_on_unpause_ = false; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 657 | } |
| 658 | |
Andrew de los Reyes | 3fd5d30 | 2010-10-07 20:07:18 -0700 | [diff] [blame] | 659 | void LibcurlHttpFetcher::GetHttpResponseCode() { |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 660 | long http_response_code = 0; // NOLINT(runtime/int) - curl needs long. |
Alex Deymo | 56ccb07 | 2016-02-05 00:50:48 -0800 | [diff] [blame] | 661 | if (base::StartsWith(url_, "file://", base::CompareCase::INSENSITIVE_ASCII)) { |
| 662 | // Fake out a valid response code for file:// URLs. |
| 663 | http_response_code_ = 299; |
| 664 | } else if (curl_easy_getinfo(curl_handle_, |
| 665 | CURLINFO_RESPONSE_CODE, |
| 666 | &http_response_code) == CURLE_OK) { |
Andrew de los Reyes | 3fd5d30 | 2010-10-07 20:07:18 -0700 | [diff] [blame] | 667 | http_response_code_ = static_cast<int>(http_response_code); |
| 668 | } |
| 669 | } |
| 670 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 671 | } // namespace chromeos_update_engine |