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