| 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 |  | 
| Xiaochu Liu | b5ba797 | 2019-07-11 09:51:06 -0700 | [diff] [blame] | 19 | #include <netinet/in.h> | 
|  | 20 | #include <resolv.h> | 
| Alex Deymo | 63cfcf4 | 2017-02-23 15:29:47 -0800 | [diff] [blame] | 21 | #include <sys/types.h> | 
|  | 22 | #include <unistd.h> | 
|  | 23 |  | 
| adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 24 | #include <algorithm> | 
| Andrew de los Reyes | d57d147 | 2010-10-21 13:34:08 -0700 | [diff] [blame] | 25 | #include <string> | 
|  | 26 |  | 
| Alex Vakulenko | 4906c1c | 2014-08-21 13:17:44 -0700 | [diff] [blame] | 27 | #include <base/bind.h> | 
| Alex Deymo | c00c98a | 2015-03-17 17:38:00 -0700 | [diff] [blame] | 28 | #include <base/format_macros.h> | 
| Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 29 | #include <base/location.h> | 
| Andrew de los Reyes | d57d147 | 2010-10-21 13:34:08 -0700 | [diff] [blame] | 30 | #include <base/logging.h> | 
| Jae Hoon Kim | 0ae8fe1 | 2019-06-26 14:32:50 -0700 | [diff] [blame] | 31 | #include <base/strings/string_split.h> | 
| Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 32 | #include <base/strings/string_util.h> | 
|  | 33 | #include <base/strings/stringprintf.h> | 
| Tianjie | 934b847 | 2020-06-24 23:10:49 -0700 | [diff] [blame] | 34 | #include <base/threading/thread_task_runner_handle.h> | 
|  | 35 |  | 
| Alex Deymo | 63cfcf4 | 2017-02-23 15:29:47 -0800 | [diff] [blame] | 36 | #ifdef __ANDROID__ | 
|  | 37 | #include <cutils/qtaguid.h> | 
| Alex Deymo | eb25655 | 2017-03-21 22:52:54 -0700 | [diff] [blame] | 38 | #include <private/android_filesystem_config.h> | 
| Alex Deymo | 63cfcf4 | 2017-02-23 15:29:47 -0800 | [diff] [blame] | 39 | #endif  // __ANDROID__ | 
|  | 40 |  | 
| Alex Deymo | 14c0da8 | 2016-07-20 16:45:45 -0700 | [diff] [blame] | 41 | #include "update_engine/certificate_checker.h" | 
| Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 42 | #include "update_engine/common/hardware_interface.h" | 
|  | 43 | #include "update_engine/common/platform_constants.h" | 
| Kelvin Zhang | c7a1d1f | 2022-07-29 13:36:29 -0700 | [diff] [blame] | 44 | #include "update_engine/common/utils.h" | 
| adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 45 |  | 
| Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 46 | using base::TimeDelta; | 
| Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 47 | using brillo::MessageLoop; | 
| Alex Deymo | c4acdf4 | 2014-05-28 21:07:10 -0700 | [diff] [blame] | 48 | using std::max; | 
| Andrew de los Reyes | d57d147 | 2010-10-21 13:34:08 -0700 | [diff] [blame] | 49 | using std::string; | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 50 |  | 
|  | 51 | // This is a concrete implementation of HttpFetcher that uses libcurl to do the | 
|  | 52 | // http work. | 
|  | 53 |  | 
|  | 54 | namespace chromeos_update_engine { | 
|  | 55 |  | 
| Andrew de los Reyes | 9bbd187 | 2010-07-16 14:52:29 -0700 | [diff] [blame] | 56 | namespace { | 
| Alex Deymo | 63cfcf4 | 2017-02-23 15:29:47 -0800 | [diff] [blame] | 57 |  | 
| Andrew de los Reyes | 5d0783d | 2010-11-29 18:14:16 -0800 | [diff] [blame] | 58 | const int kNoNetworkRetrySeconds = 10; | 
| Alex Deymo | 63cfcf4 | 2017-02-23 15:29:47 -0800 | [diff] [blame] | 59 |  | 
| Alex Deymo | 63cfcf4 | 2017-02-23 15:29:47 -0800 | [diff] [blame] | 60 | // libcurl's CURLOPT_SOCKOPTFUNCTION callback function. Called after the socket | 
|  | 61 | // is created but before it is connected. This callback tags the created socket | 
|  | 62 | // so the network usage can be tracked in Android. | 
|  | 63 | int LibcurlSockoptCallback(void* /* clientp */, | 
|  | 64 | curl_socket_t curlfd, | 
|  | 65 | curlsocktype /* purpose */) { | 
|  | 66 | #ifdef __ANDROID__ | 
| Alex Deymo | 558fe6a | 2017-05-19 13:16:20 -0700 | [diff] [blame] | 67 | // Socket tag used by all network sockets. See qtaguid kernel module for | 
|  | 68 | // stats. | 
|  | 69 | const int kUpdateEngineSocketTag = 0x55417243;  // "CrAU" in little-endian. | 
| Alex Deymo | eb25655 | 2017-03-21 22:52:54 -0700 | [diff] [blame] | 70 | qtaguid_tagSocket(curlfd, kUpdateEngineSocketTag, AID_OTA_UPDATE); | 
| Alex Deymo | 63cfcf4 | 2017-02-23 15:29:47 -0800 | [diff] [blame] | 71 | #endif  // __ANDROID__ | 
|  | 72 | return CURL_SOCKOPT_OK; | 
|  | 73 | } | 
|  | 74 |  | 
| Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 75 | }  // namespace | 
| Andrew de los Reyes | 9bbd187 | 2010-07-16 14:52:29 -0700 | [diff] [blame] | 76 |  | 
| Alex Deymo | 13e9518 | 2017-03-16 19:06:13 -0700 | [diff] [blame] | 77 | // static | 
|  | 78 | int LibcurlHttpFetcher::LibcurlCloseSocketCallback(void* clientp, | 
|  | 79 | curl_socket_t item) { | 
|  | 80 | #ifdef __ANDROID__ | 
|  | 81 | qtaguid_untagSocket(item); | 
|  | 82 | #endif  // __ANDROID__ | 
| Tianjie | 55abd3c | 2020-06-19 00:22:59 -0700 | [diff] [blame] | 83 |  | 
| Alex Deymo | 13e9518 | 2017-03-16 19:06:13 -0700 | [diff] [blame] | 84 | LibcurlHttpFetcher* fetcher = static_cast<LibcurlHttpFetcher*>(clientp); | 
|  | 85 | // Stop watching the socket before closing it. | 
| hscham | 00b6aa2 | 2020-02-20 12:32:06 +0900 | [diff] [blame] | 86 | for (size_t t = 0; t < base::size(fetcher->fd_controller_maps_); ++t) { | 
| Hidehiko Abe | 493fecb | 2019-07-10 23:30:50 +0900 | [diff] [blame] | 87 | fetcher->fd_controller_maps_[t].erase(item); | 
| Alex Deymo | 13e9518 | 2017-03-16 19:06:13 -0700 | [diff] [blame] | 88 | } | 
|  | 89 |  | 
|  | 90 | // Documentation for this callback says to return 0 on success or 1 on error. | 
|  | 91 | if (!IGNORE_EINTR(close(item))) | 
|  | 92 | return 0; | 
|  | 93 | return 1; | 
|  | 94 | } | 
|  | 95 |  | 
| Kelvin Zhang | c7a1d1f | 2022-07-29 13:36:29 -0700 | [diff] [blame] | 96 | LibcurlHttpFetcher::LibcurlHttpFetcher(HardwareInterface* hardware) | 
|  | 97 | : hardware_(hardware) { | 
| Alex Deymo | c1c17b4 | 2015-11-23 03:53:15 -0300 | [diff] [blame] | 98 | // Dev users want a longer timeout (180 seconds) because they may | 
|  | 99 | // be waiting on the dev server to build an image. | 
|  | 100 | if (!hardware_->IsOfficialBuild()) | 
|  | 101 | low_speed_time_seconds_ = kDownloadDevModeLowSpeedTimeSeconds; | 
| Alex Deymo | 46a9aae | 2016-05-04 20:20:11 -0700 | [diff] [blame] | 102 | if (hardware_->IsOOBEEnabled() && !hardware_->IsOOBEComplete(nullptr)) | 
| Alex Deymo | c1c17b4 | 2015-11-23 03:53:15 -0300 | [diff] [blame] | 103 | max_retry_count_ = kDownloadMaxRetryCountOobeNotComplete; | 
|  | 104 | } | 
|  | 105 |  | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 106 | LibcurlHttpFetcher::~LibcurlHttpFetcher() { | 
| Darin Petkov | 9ce452b | 2010-11-17 14:33:28 -0800 | [diff] [blame] | 107 | LOG_IF(ERROR, transfer_in_progress_) | 
|  | 108 | << "Destroying the fetcher while a transfer is in progress."; | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 109 | CleanUp(); | 
|  | 110 | } | 
|  | 111 |  | 
| Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 112 | bool LibcurlHttpFetcher::GetProxyType(const string& proxy, | 
| Gilad Arnold | 59d9e01 | 2013-07-23 16:41:43 -0700 | [diff] [blame] | 113 | curl_proxytype* out_type) { | 
| Alex Deymo | 56ccb07 | 2016-02-05 00:50:48 -0800 | [diff] [blame] | 114 | if (base::StartsWith( | 
|  | 115 | proxy, "socks5://", base::CompareCase::INSENSITIVE_ASCII) || | 
|  | 116 | base::StartsWith( | 
|  | 117 | proxy, "socks://", base::CompareCase::INSENSITIVE_ASCII)) { | 
| Gilad Arnold | 59d9e01 | 2013-07-23 16:41:43 -0700 | [diff] [blame] | 118 | *out_type = CURLPROXY_SOCKS5_HOSTNAME; | 
|  | 119 | return true; | 
|  | 120 | } | 
| Alex Deymo | 56ccb07 | 2016-02-05 00:50:48 -0800 | [diff] [blame] | 121 | if (base::StartsWith( | 
|  | 122 | proxy, "socks4://", base::CompareCase::INSENSITIVE_ASCII)) { | 
| Gilad Arnold | 59d9e01 | 2013-07-23 16:41:43 -0700 | [diff] [blame] | 123 | *out_type = CURLPROXY_SOCKS4A; | 
|  | 124 | return true; | 
|  | 125 | } | 
| Alex Deymo | 56ccb07 | 2016-02-05 00:50:48 -0800 | [diff] [blame] | 126 | if (base::StartsWith( | 
|  | 127 | proxy, "http://", base::CompareCase::INSENSITIVE_ASCII) || | 
|  | 128 | base::StartsWith( | 
|  | 129 | proxy, "https://", base::CompareCase::INSENSITIVE_ASCII)) { | 
| Gilad Arnold | 59d9e01 | 2013-07-23 16:41:43 -0700 | [diff] [blame] | 130 | *out_type = CURLPROXY_HTTP; | 
|  | 131 | return true; | 
|  | 132 | } | 
| Alex Deymo | 56ccb07 | 2016-02-05 00:50:48 -0800 | [diff] [blame] | 133 | if (base::StartsWith(proxy, kNoProxy, base::CompareCase::INSENSITIVE_ASCII)) { | 
| Gilad Arnold | 59d9e01 | 2013-07-23 16:41:43 -0700 | [diff] [blame] | 134 | // known failure case. don't log. | 
|  | 135 | return false; | 
|  | 136 | } | 
|  | 137 | LOG(INFO) << "Unknown proxy type: " << proxy; | 
|  | 138 | return false; | 
|  | 139 | } | 
|  | 140 |  | 
| Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 141 | void LibcurlHttpFetcher::ResumeTransfer(const string& url) { | 
| Andrew de los Reyes | 3270f74 | 2010-07-15 22:28:14 -0700 | [diff] [blame] | 142 | LOG(INFO) << "Starting/Resuming transfer"; | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 143 | CHECK(!transfer_in_progress_); | 
|  | 144 | url_ = url; | 
|  | 145 | curl_multi_handle_ = curl_multi_init(); | 
|  | 146 | CHECK(curl_multi_handle_); | 
|  | 147 |  | 
|  | 148 | curl_handle_ = curl_easy_init(); | 
|  | 149 | CHECK(curl_handle_); | 
| Alex Deymo | f285857 | 2016-02-25 11:20:13 -0800 | [diff] [blame] | 150 | ignore_failure_ = false; | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 151 |  | 
| Alex Deymo | 13e9518 | 2017-03-16 19:06:13 -0700 | [diff] [blame] | 152 | // Tag and untag the socket for network usage stats. | 
| Alex Deymo | 63cfcf4 | 2017-02-23 15:29:47 -0800 | [diff] [blame] | 153 | curl_easy_setopt( | 
|  | 154 | curl_handle_, CURLOPT_SOCKOPTFUNCTION, LibcurlSockoptCallback); | 
| Alex Deymo | 13e9518 | 2017-03-16 19:06:13 -0700 | [diff] [blame] | 155 | curl_easy_setopt( | 
|  | 156 | curl_handle_, CURLOPT_CLOSESOCKETFUNCTION, LibcurlCloseSocketCallback); | 
|  | 157 | curl_easy_setopt(curl_handle_, CURLOPT_CLOSESOCKETDATA, this); | 
| Alex Deymo | 63cfcf4 | 2017-02-23 15:29:47 -0800 | [diff] [blame] | 158 |  | 
| Andrew de los Reyes | 4516810 | 2010-11-22 11:13:50 -0800 | [diff] [blame] | 159 | CHECK(HasProxy()); | 
| Gilad Arnold | fbaee24 | 2012-04-04 15:59:43 -0700 | [diff] [blame] | 160 | bool is_direct = (GetCurrentProxy() == kNoProxy); | 
|  | 161 | LOG(INFO) << "Using proxy: " << (is_direct ? "no" : "yes"); | 
|  | 162 | if (is_direct) { | 
| Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 163 | CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_PROXY, ""), CURLE_OK); | 
| Andrew de los Reyes | 4516810 | 2010-11-22 11:13:50 -0800 | [diff] [blame] | 164 | } else { | 
| Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 165 | CHECK_EQ(curl_easy_setopt( | 
|  | 166 | curl_handle_, CURLOPT_PROXY, GetCurrentProxy().c_str()), | 
|  | 167 | CURLE_OK); | 
| Andrew de los Reyes | 4516810 | 2010-11-22 11:13:50 -0800 | [diff] [blame] | 168 | // Curl seems to require us to set the protocol | 
| Daniel Zheng | da4f729 | 2022-09-02 22:59:32 +0000 | [diff] [blame] | 169 | curl_proxytype type{}; | 
| Gilad Arnold | 59d9e01 | 2013-07-23 16:41:43 -0700 | [diff] [blame] | 170 | if (GetProxyType(GetCurrentProxy(), &type)) { | 
| Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 171 | CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_PROXYTYPE, type), | 
|  | 172 | CURLE_OK); | 
| Andrew de los Reyes | 4516810 | 2010-11-22 11:13:50 -0800 | [diff] [blame] | 173 | } | 
|  | 174 | } | 
|  | 175 |  | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 176 | if (post_data_set_) { | 
| adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 177 | CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_POST, 1), CURLE_OK); | 
| Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 178 | CHECK_EQ( | 
|  | 179 | curl_easy_setopt(curl_handle_, CURLOPT_POSTFIELDS, post_data_.data()), | 
|  | 180 | CURLE_OK); | 
|  | 181 | CHECK_EQ(curl_easy_setopt( | 
|  | 182 | curl_handle_, CURLOPT_POSTFIELDSIZE, post_data_.size()), | 
| adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 183 | CURLE_OK); | 
| Alex Deymo | fdd6dec | 2016-03-03 22:35:43 -0800 | [diff] [blame] | 184 | } | 
| Gilad Arnold | 9dd1e7c | 2012-02-16 12:13:36 -0800 | [diff] [blame] | 185 |  | 
| Alex Deymo | fdd6dec | 2016-03-03 22:35:43 -0800 | [diff] [blame] | 186 | // Setup extra HTTP headers. | 
|  | 187 | if (curl_http_headers_) { | 
|  | 188 | curl_slist_free_all(curl_http_headers_); | 
|  | 189 | curl_http_headers_ = nullptr; | 
|  | 190 | } | 
|  | 191 | for (const auto& header : extra_headers_) { | 
|  | 192 | // curl_slist_append() copies the string. | 
|  | 193 | curl_http_headers_ = | 
|  | 194 | curl_slist_append(curl_http_headers_, header.second.c_str()); | 
|  | 195 | } | 
|  | 196 | if (post_data_set_) { | 
| Gilad Arnold | 9dd1e7c | 2012-02-16 12:13:36 -0800 | [diff] [blame] | 197 | // Set the Content-Type HTTP header, if one was specifically set. | 
| Gilad Arnold | 9dd1e7c | 2012-02-16 12:13:36 -0800 | [diff] [blame] | 198 | if (post_content_type_ != kHttpContentTypeUnspecified) { | 
| Alex Deymo | fdd6dec | 2016-03-03 22:35:43 -0800 | [diff] [blame] | 199 | const string content_type_attr = base::StringPrintf( | 
|  | 200 | "Content-Type: %s", GetHttpContentTypeString(post_content_type_)); | 
|  | 201 | curl_http_headers_ = | 
|  | 202 | curl_slist_append(curl_http_headers_, content_type_attr.c_str()); | 
| Gilad Arnold | 9dd1e7c | 2012-02-16 12:13:36 -0800 | [diff] [blame] | 203 | } else { | 
|  | 204 | LOG(WARNING) << "no content type set, using libcurl default"; | 
|  | 205 | } | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 206 | } | 
| Alex Deymo | fdd6dec | 2016-03-03 22:35:43 -0800 | [diff] [blame] | 207 | CHECK_EQ( | 
|  | 208 | curl_easy_setopt(curl_handle_, CURLOPT_HTTPHEADER, curl_http_headers_), | 
|  | 209 | CURLE_OK); | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 210 |  | 
| Gilad Arnold | e4ad250 | 2011-12-29 17:08:54 -0800 | [diff] [blame] | 211 | if (bytes_downloaded_ > 0 || download_length_) { | 
|  | 212 | // Resume from where we left off. | 
| adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 213 | resume_offset_ = bytes_downloaded_; | 
| Gilad Arnold | e4ad250 | 2011-12-29 17:08:54 -0800 | [diff] [blame] | 214 | CHECK_GE(resume_offset_, 0); | 
|  | 215 |  | 
|  | 216 | // Compute end offset, if one is specified. As per HTTP specification, this | 
|  | 217 | // is an inclusive boundary. Make sure it doesn't overflow. | 
|  | 218 | size_t end_offset = 0; | 
|  | 219 | if (download_length_) { | 
|  | 220 | end_offset = static_cast<size_t>(resume_offset_) + download_length_ - 1; | 
| Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 221 | CHECK_LE((size_t)resume_offset_, end_offset); | 
| Gilad Arnold | e4ad250 | 2011-12-29 17:08:54 -0800 | [diff] [blame] | 222 | } | 
|  | 223 |  | 
|  | 224 | // Create a string representation of the desired range. | 
| Alex Deymo | c00c98a | 2015-03-17 17:38:00 -0700 | [diff] [blame] | 225 | string range_str = base::StringPrintf( | 
|  | 226 | "%" PRIu64 "-", static_cast<uint64_t>(resume_offset_)); | 
|  | 227 | if (end_offset) | 
|  | 228 | range_str += std::to_string(end_offset); | 
| Gilad Arnold | e4ad250 | 2011-12-29 17:08:54 -0800 | [diff] [blame] | 229 | CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_RANGE, range_str.c_str()), | 
|  | 230 | CURLE_OK); | 
| adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 231 | } | 
|  | 232 |  | 
|  | 233 | CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_WRITEDATA, this), CURLE_OK); | 
| Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 234 | CHECK_EQ( | 
|  | 235 | curl_easy_setopt(curl_handle_, CURLOPT_WRITEFUNCTION, StaticLibcurlWrite), | 
|  | 236 | CURLE_OK); | 
|  | 237 | CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_URL, url_.c_str()), CURLE_OK); | 
| Andrew de los Reyes | 3270f74 | 2010-07-15 22:28:14 -0700 | [diff] [blame] | 238 |  | 
| David Zeuthen | 34135a9 | 2013-08-06 11:16:16 -0700 | [diff] [blame] | 239 | // If the connection drops under |low_speed_limit_bps_| (10 | 
|  | 240 | // bytes/sec by default) for |low_speed_time_seconds_| (90 seconds, | 
|  | 241 | // 180 on non-official builds), reconnect. | 
| Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 242 | CHECK_EQ(curl_easy_setopt( | 
|  | 243 | curl_handle_, CURLOPT_LOW_SPEED_LIMIT, low_speed_limit_bps_), | 
| Andrew de los Reyes | 3270f74 | 2010-07-15 22:28:14 -0700 | [diff] [blame] | 244 | CURLE_OK); | 
| Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 245 | CHECK_EQ(curl_easy_setopt( | 
|  | 246 | curl_handle_, CURLOPT_LOW_SPEED_TIME, low_speed_time_seconds_), | 
| Andrew de los Reyes | 3270f74 | 2010-07-15 22:28:14 -0700 | [diff] [blame] | 247 | CURLE_OK); | 
| Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 248 | CHECK_EQ(curl_easy_setopt( | 
|  | 249 | curl_handle_, CURLOPT_CONNECTTIMEOUT, connect_timeout_seconds_), | 
| Andrew de los Reyes | e72f9c0 | 2011-04-20 10:47:40 -0700 | [diff] [blame] | 250 | CURLE_OK); | 
| Andrew de los Reyes | 3270f74 | 2010-07-15 22:28:14 -0700 | [diff] [blame] | 251 |  | 
| Darin Petkov | 41c2fcf | 2010-08-25 13:14:48 -0700 | [diff] [blame] | 252 | // By default, libcurl doesn't follow redirections. Allow up to | 
| David Zeuthen | 34135a9 | 2013-08-06 11:16:16 -0700 | [diff] [blame] | 253 | // |kDownloadMaxRedirects| redirections. | 
| Darin Petkov | 3a4016a | 2010-09-28 13:54:17 -0700 | [diff] [blame] | 254 | CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_FOLLOWLOCATION, 1), CURLE_OK); | 
| Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 255 | CHECK_EQ( | 
|  | 256 | curl_easy_setopt(curl_handle_, CURLOPT_MAXREDIRS, kDownloadMaxRedirects), | 
|  | 257 | CURLE_OK); | 
| Darin Petkov | 41c2fcf | 2010-08-25 13:14:48 -0700 | [diff] [blame] | 258 |  | 
| Nam T. Nguyen | 7d623eb | 2014-05-13 16:06:28 -0700 | [diff] [blame] | 259 | // Lock down the appropriate curl options for HTTP or HTTPS depending on | 
|  | 260 | // the url. | 
| Alex Deymo | c1c17b4 | 2015-11-23 03:53:15 -0300 | [diff] [blame] | 261 | if (hardware_->IsOfficialBuild()) { | 
| Alex Deymo | 56ccb07 | 2016-02-05 00:50:48 -0800 | [diff] [blame] | 262 | if (base::StartsWith( | 
|  | 263 | url_, "http://", base::CompareCase::INSENSITIVE_ASCII)) { | 
| Jay Srinivasan | b3f5540 | 2012-12-03 18:12:04 -0800 | [diff] [blame] | 264 | SetCurlOptionsForHttp(); | 
| Alex Deymo | 56ccb07 | 2016-02-05 00:50:48 -0800 | [diff] [blame] | 265 | } else if (base::StartsWith( | 
|  | 266 | url_, "https://", base::CompareCase::INSENSITIVE_ASCII)) { | 
| Jay Srinivasan | b3f5540 | 2012-12-03 18:12:04 -0800 | [diff] [blame] | 267 | SetCurlOptionsForHttps(); | 
| Amin Hassani | 565331e | 2019-06-24 14:11:29 -0700 | [diff] [blame] | 268 | #ifdef __ANDROID__ | 
| Alex Deymo | 56ccb07 | 2016-02-05 00:50:48 -0800 | [diff] [blame] | 269 | } else if (base::StartsWith( | 
|  | 270 | url_, "file://", base::CompareCase::INSENSITIVE_ASCII)) { | 
|  | 271 | SetCurlOptionsForFile(); | 
| Amin Hassani | 565331e | 2019-06-24 14:11:29 -0700 | [diff] [blame] | 272 | #endif  // __ANDROID__ | 
| Alex Deymo | 56ccb07 | 2016-02-05 00:50:48 -0800 | [diff] [blame] | 273 | } else { | 
|  | 274 | LOG(ERROR) << "Received invalid URI: " << url_; | 
|  | 275 | // Lock down to no protocol supported for the transfer. | 
|  | 276 | CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_PROTOCOLS, 0), CURLE_OK); | 
|  | 277 | } | 
| Jay Srinivasan | b3f5540 | 2012-12-03 18:12:04 -0800 | [diff] [blame] | 278 | } else { | 
| Nam T. Nguyen | 7d623eb | 2014-05-13 16:06:28 -0700 | [diff] [blame] | 279 | LOG(INFO) << "Not setting http(s) curl options because we are " | 
|  | 280 | << "running a dev/test image"; | 
| Darin Petkov | fc7a0ce | 2010-10-25 10:38:37 -0700 | [diff] [blame] | 281 | } | 
|  | 282 |  | 
| adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 283 | 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] | 284 | transfer_in_progress_ = true; | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 285 | } | 
|  | 286 |  | 
| Jay Srinivasan | b3f5540 | 2012-12-03 18:12:04 -0800 | [diff] [blame] | 287 | // Lock down only the protocol in case of HTTP. | 
|  | 288 | void LibcurlHttpFetcher::SetCurlOptionsForHttp() { | 
|  | 289 | LOG(INFO) << "Setting up curl options for HTTP"; | 
|  | 290 | CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_PROTOCOLS, CURLPROTO_HTTP), | 
|  | 291 | CURLE_OK); | 
| Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 292 | CHECK_EQ( | 
|  | 293 | curl_easy_setopt(curl_handle_, CURLOPT_REDIR_PROTOCOLS, CURLPROTO_HTTP), | 
|  | 294 | CURLE_OK); | 
| Jay Srinivasan | b3f5540 | 2012-12-03 18:12:04 -0800 | [diff] [blame] | 295 | } | 
|  | 296 |  | 
|  | 297 | // Security lock-down in official builds: makes sure that peer certificate | 
|  | 298 | // verification is enabled, restricts the set of trusted certificates, | 
|  | 299 | // restricts protocols to HTTPS, restricts ciphers to HIGH. | 
|  | 300 | void LibcurlHttpFetcher::SetCurlOptionsForHttps() { | 
|  | 301 | LOG(INFO) << "Setting up curl options for HTTPS"; | 
| Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 302 | CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_SSL_VERIFYPEER, 1), CURLE_OK); | 
|  | 303 | CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_SSL_VERIFYHOST, 2), CURLE_OK); | 
| Alex Khouderchah | 84634dc | 2019-04-04 09:25:39 -0700 | [diff] [blame] | 304 | CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_CAINFO, nullptr), CURLE_OK); | 
| Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 305 | CHECK_EQ(curl_easy_setopt( | 
|  | 306 | curl_handle_, CURLOPT_CAPATH, constants::kCACertificatesPath), | 
| Jay Srinivasan | b3f5540 | 2012-12-03 18:12:04 -0800 | [diff] [blame] | 307 | CURLE_OK); | 
|  | 308 | CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_PROTOCOLS, CURLPROTO_HTTPS), | 
|  | 309 | CURLE_OK); | 
| Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 310 | CHECK_EQ( | 
|  | 311 | curl_easy_setopt(curl_handle_, CURLOPT_REDIR_PROTOCOLS, CURLPROTO_HTTPS), | 
|  | 312 | CURLE_OK); | 
| Jay Srinivasan | b3f5540 | 2012-12-03 18:12:04 -0800 | [diff] [blame] | 313 | CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_SSL_CIPHER_LIST, "HIGH:!ADH"), | 
|  | 314 | CURLE_OK); | 
| Alex Deymo | 33e91e7 | 2015-12-01 18:26:08 -0300 | [diff] [blame] | 315 | if (server_to_check_ != ServerToCheck::kNone) { | 
|  | 316 | CHECK_EQ( | 
|  | 317 | curl_easy_setopt(curl_handle_, CURLOPT_SSL_CTX_DATA, &server_to_check_), | 
|  | 318 | CURLE_OK); | 
| Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 319 | CHECK_EQ(curl_easy_setopt(curl_handle_, | 
|  | 320 | CURLOPT_SSL_CTX_FUNCTION, | 
| Jay Srinivasan | b3f5540 | 2012-12-03 18:12:04 -0800 | [diff] [blame] | 321 | CertificateChecker::ProcessSSLContext), | 
|  | 322 | CURLE_OK); | 
|  | 323 | } | 
|  | 324 | } | 
|  | 325 |  | 
| Alex Deymo | 56ccb07 | 2016-02-05 00:50:48 -0800 | [diff] [blame] | 326 | // Lock down only the protocol in case of a local file. | 
|  | 327 | void LibcurlHttpFetcher::SetCurlOptionsForFile() { | 
|  | 328 | LOG(INFO) << "Setting up curl options for FILE"; | 
|  | 329 | CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_PROTOCOLS, CURLPROTO_FILE), | 
|  | 330 | CURLE_OK); | 
|  | 331 | CHECK_EQ( | 
|  | 332 | curl_easy_setopt(curl_handle_, CURLOPT_REDIR_PROTOCOLS, CURLPROTO_FILE), | 
|  | 333 | CURLE_OK); | 
|  | 334 | } | 
| Jay Srinivasan | b3f5540 | 2012-12-03 18:12:04 -0800 | [diff] [blame] | 335 |  | 
| adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 336 | // Begins the transfer, which must not have already been started. | 
| Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 337 | void LibcurlHttpFetcher::BeginTransfer(const string& url) { | 
| Andrew de los Reyes | f3ed8e7 | 2011-02-16 10:35:46 -0800 | [diff] [blame] | 338 | CHECK(!transfer_in_progress_); | 
|  | 339 | url_ = url; | 
| Andrew de los Reyes | f3ed8e7 | 2011-02-16 10:35:46 -0800 | [diff] [blame] | 340 |  | 
| adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 341 | transfer_size_ = -1; | 
| adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 342 | resume_offset_ = 0; | 
| Andrew de los Reyes | 9bbd187 | 2010-07-16 14:52:29 -0700 | [diff] [blame] | 343 | retry_count_ = 0; | 
| Darin Petkov | a092955 | 2010-11-29 14:19:06 -0800 | [diff] [blame] | 344 | no_network_retry_count_ = 0; | 
| Darin Petkov | cb46621 | 2010-08-26 09:40:11 -0700 | [diff] [blame] | 345 | http_response_code_ = 0; | 
| Andrew de los Reyes | 819fef2 | 2010-12-17 11:33:58 -0800 | [diff] [blame] | 346 | terminate_requested_ = false; | 
| Gilad Arnold | a2dee1d | 2012-04-12 11:50:37 -0700 | [diff] [blame] | 347 | sent_byte_ = false; | 
| Alex Deymo | f285857 | 2016-02-25 11:20:13 -0800 | [diff] [blame] | 348 |  | 
|  | 349 | // If we are paused, we delay these two operations until Unpause is called. | 
|  | 350 | if (transfer_paused_) { | 
|  | 351 | restart_transfer_on_unpause_ = true; | 
|  | 352 | return; | 
|  | 353 | } | 
| Andrew de los Reyes | f3ed8e7 | 2011-02-16 10:35:46 -0800 | [diff] [blame] | 354 | ResumeTransfer(url_); | 
| Andrew de los Reyes | 9bbd187 | 2010-07-16 14:52:29 -0700 | [diff] [blame] | 355 | CurlPerformOnce(); | 
| adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 356 | } | 
|  | 357 |  | 
| Darin Petkov | 9ce452b | 2010-11-17 14:33:28 -0800 | [diff] [blame] | 358 | void LibcurlHttpFetcher::ForceTransferTermination() { | 
|  | 359 | CleanUp(); | 
|  | 360 | if (delegate_) { | 
|  | 361 | // Note that after the callback returns this object may be destroyed. | 
|  | 362 | delegate_->TransferTerminated(this); | 
|  | 363 | } | 
|  | 364 | } | 
|  | 365 |  | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 366 | void LibcurlHttpFetcher::TerminateTransfer() { | 
| Darin Petkov | 9ce452b | 2010-11-17 14:33:28 -0800 | [diff] [blame] | 367 | if (in_write_callback_) { | 
| Andrew de los Reyes | 3fd5d30 | 2010-10-07 20:07:18 -0700 | [diff] [blame] | 368 | terminate_requested_ = true; | 
| Darin Petkov | 9ce452b | 2010-11-17 14:33:28 -0800 | [diff] [blame] | 369 | } else { | 
|  | 370 | ForceTransferTermination(); | 
|  | 371 | } | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 372 | } | 
|  | 373 |  | 
| Alex Deymo | fdd6dec | 2016-03-03 22:35:43 -0800 | [diff] [blame] | 374 | void LibcurlHttpFetcher::SetHeader(const string& header_name, | 
|  | 375 | const string& header_value) { | 
|  | 376 | string header_line = header_name + ": " + header_value; | 
|  | 377 | // Avoid the space if no data on the right side of the semicolon. | 
|  | 378 | if (header_value.empty()) | 
|  | 379 | header_line = header_name + ":"; | 
|  | 380 | TEST_AND_RETURN(header_line.find('\n') == string::npos); | 
|  | 381 | TEST_AND_RETURN(header_name.find(':') == string::npos); | 
|  | 382 | extra_headers_[base::ToLowerASCII(header_name)] = header_line; | 
|  | 383 | } | 
|  | 384 |  | 
| Jae Hoon Kim | 0ae8fe1 | 2019-06-26 14:32:50 -0700 | [diff] [blame] | 385 | // Inputs: header_name, header_value | 
|  | 386 | // Example: | 
|  | 387 | //   extra_headers_ = { {"foo":"foo: 123"}, {"bar":"bar:"} } | 
|  | 388 | //   string tmp = "gibberish"; | 
|  | 389 | //   Case 1: | 
|  | 390 | //     GetHeader("foo", &tmp) -> tmp = "123", return true. | 
|  | 391 | //   Case 2: | 
|  | 392 | //     GetHeader("bar", &tmp) -> tmp = "", return true. | 
|  | 393 | //   Case 3: | 
|  | 394 | //     GetHeader("moo", &tmp) -> tmp = "", return false. | 
|  | 395 | bool LibcurlHttpFetcher::GetHeader(const string& header_name, | 
|  | 396 | string* header_value) const { | 
|  | 397 | // Initially clear |header_value| to handle both success and failures without | 
|  | 398 | // leaving |header_value| in a unclear state. | 
|  | 399 | header_value->clear(); | 
|  | 400 | auto header_key = base::ToLowerASCII(header_name); | 
|  | 401 | auto header_line_itr = extra_headers_.find(header_key); | 
|  | 402 | // If the |header_name| was never set, indicate so by returning false. | 
|  | 403 | if (header_line_itr == extra_headers_.end()) | 
|  | 404 | return false; | 
|  | 405 | // From |SetHeader()| the check for |header_name| to not include ":" is | 
|  | 406 | // verified, so finding the first index of ":" is a safe operation. | 
|  | 407 | auto header_line = header_line_itr->second; | 
|  | 408 | *header_value = header_line.substr(header_line.find(':') + 1); | 
|  | 409 | // The following is neccessary to remove the leading ' ' before the header | 
|  | 410 | // value that was place only if |header_value| passed to |SetHeader()| was | 
|  | 411 | // a non-empty string. | 
|  | 412 | header_value->erase(0, 1); | 
|  | 413 | return true; | 
|  | 414 | } | 
|  | 415 |  | 
| Andrew de los Reyes | cb31933 | 2010-07-19 10:55:01 -0700 | [diff] [blame] | 416 | void LibcurlHttpFetcher::CurlPerformOnce() { | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 417 | CHECK(transfer_in_progress_); | 
|  | 418 | int running_handles = 0; | 
|  | 419 | CURLMcode retcode = CURLM_CALL_MULTI_PERFORM; | 
|  | 420 |  | 
|  | 421 | // libcurl may request that we immediately call curl_multi_perform after it | 
|  | 422 | // returns, so we do. libcurl promises that curl_multi_perform will not block. | 
|  | 423 | while (CURLM_CALL_MULTI_PERFORM == retcode) { | 
|  | 424 | retcode = curl_multi_perform(curl_multi_handle_, &running_handles); | 
| Andrew de los Reyes | 3fd5d30 | 2010-10-07 20:07:18 -0700 | [diff] [blame] | 425 | if (terminate_requested_) { | 
| Darin Petkov | 9ce452b | 2010-11-17 14:33:28 -0800 | [diff] [blame] | 426 | ForceTransferTermination(); | 
| Andrew de los Reyes | 3fd5d30 | 2010-10-07 20:07:18 -0700 | [diff] [blame] | 427 | return; | 
|  | 428 | } | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 429 | } | 
| Alex Deymo | f285857 | 2016-02-25 11:20:13 -0800 | [diff] [blame] | 430 |  | 
| Xiaochu Liu | 4a1173a | 2019-04-10 10:49:08 -0700 | [diff] [blame] | 431 | // When retcode is not |CURLM_OK| at this point, libcurl has an internal error | 
|  | 432 | // that it is less likely to recover from (libcurl bug, out-of-memory, etc.). | 
|  | 433 | // In case of an update check, we send UMA metrics and log the error. | 
|  | 434 | if (is_update_check_ && | 
|  | 435 | (retcode == CURLM_OUT_OF_MEMORY || retcode == CURLM_INTERNAL_ERROR)) { | 
| Amin Hassani | d3d8421 | 2019-08-17 00:27:44 -0700 | [diff] [blame] | 436 | auxiliary_error_code_ = ErrorCode::kInternalLibCurlError; | 
| Xiaochu Liu | a4fa660 | 2019-08-05 17:06:35 +0000 | [diff] [blame] | 437 | LOG(ERROR) << "curl_multi_perform is in an unrecoverable error condition: " | 
| Xiaochu Liu | 4a1173a | 2019-04-10 10:49:08 -0700 | [diff] [blame] | 438 | << retcode; | 
|  | 439 | } else if (retcode != CURLM_OK) { | 
|  | 440 | LOG(ERROR) << "curl_multi_perform returns error: " << retcode; | 
|  | 441 | } | 
|  | 442 |  | 
| Alex Deymo | f285857 | 2016-02-25 11:20:13 -0800 | [diff] [blame] | 443 | // If the transfer completes while paused, we should ignore the failure once | 
|  | 444 | // the fetcher is unpaused. | 
|  | 445 | if (running_handles == 0 && transfer_paused_ && !ignore_failure_) { | 
|  | 446 | LOG(INFO) << "Connection closed while paused, ignoring failure."; | 
|  | 447 | ignore_failure_ = true; | 
|  | 448 | } | 
|  | 449 |  | 
|  | 450 | if (running_handles != 0 || transfer_paused_) { | 
|  | 451 | // There's either more work to do or we are paused, so we just keep the | 
|  | 452 | // file descriptors to watch up to date and exit, until we are done with the | 
|  | 453 | // work and we are not paused. | 
| Amin Hassani | ce99ee7 | 2020-10-07 22:24:33 -0700 | [diff] [blame] | 454 | // | 
|  | 455 | // When there's no |base::SingleThreadTaskRunner| on current thread, it's | 
|  | 456 | // not possible to watch file descriptors. Just poll it later. This usually | 
|  | 457 | // happens if |brillo::FakeMessageLoop| is used. | 
| Tianjie | 934b847 | 2020-06-24 23:10:49 -0700 | [diff] [blame] | 458 | if (!base::ThreadTaskRunnerHandle::IsSet()) { | 
| Amin Hassani | 9956320 | 2020-09-15 15:30:09 -0700 | [diff] [blame] | 459 | MessageLoop::current()->PostDelayedTask( | 
|  | 460 | FROM_HERE, | 
|  | 461 | base::Bind(&LibcurlHttpFetcher::CurlPerformOnce, | 
|  | 462 | base::Unretained(this)), | 
|  | 463 | TimeDelta::FromSeconds(1)); | 
|  | 464 | return; | 
| Tianjie | 934b847 | 2020-06-24 23:10:49 -0700 | [diff] [blame] | 465 | } | 
| Alex Deymo | f285857 | 2016-02-25 11:20:13 -0800 | [diff] [blame] | 466 | SetupMessageLoopSources(); | 
|  | 467 | return; | 
|  | 468 | } | 
|  | 469 |  | 
|  | 470 | // At this point, the transfer was completed in some way (error, connection | 
|  | 471 | // closed or download finished). | 
|  | 472 |  | 
|  | 473 | GetHttpResponseCode(); | 
|  | 474 | if (http_response_code_) { | 
|  | 475 | LOG(INFO) << "HTTP response code: " << http_response_code_; | 
|  | 476 | no_network_retry_count_ = 0; | 
| Xiaochu Liu | b5ba797 | 2019-07-11 09:51:06 -0700 | [diff] [blame] | 477 | unresolved_host_state_machine_.UpdateState(false); | 
| Alex Deymo | f285857 | 2016-02-25 11:20:13 -0800 | [diff] [blame] | 478 | } else { | 
|  | 479 | LOG(ERROR) << "Unable to get http response code."; | 
| Xiaochu Liu | b5ba797 | 2019-07-11 09:51:06 -0700 | [diff] [blame] | 480 | CURLcode curl_code = GetCurlCode(); | 
|  | 481 | LOG(ERROR) << "Return code for the transfer: " << curl_code; | 
|  | 482 | if (curl_code == CURLE_COULDNT_RESOLVE_HOST) { | 
|  | 483 | LOG(ERROR) << "libcurl can not resolve host."; | 
|  | 484 | unresolved_host_state_machine_.UpdateState(true); | 
| Amin Hassani | d3d8421 | 2019-08-17 00:27:44 -0700 | [diff] [blame] | 485 | auxiliary_error_code_ = ErrorCode::kUnresolvedHostError; | 
| Xiaochu Liu | b5ba797 | 2019-07-11 09:51:06 -0700 | [diff] [blame] | 486 | } | 
| Alex Deymo | f285857 | 2016-02-25 11:20:13 -0800 | [diff] [blame] | 487 | } | 
|  | 488 |  | 
|  | 489 | // we're done! | 
|  | 490 | CleanUp(); | 
|  | 491 |  | 
| Amin Hassani | d3d8421 | 2019-08-17 00:27:44 -0700 | [diff] [blame] | 492 | if (unresolved_host_state_machine_.GetState() == | 
| Xiaochu Liu | b5ba797 | 2019-07-11 09:51:06 -0700 | [diff] [blame] | 493 | UnresolvedHostStateMachine::State::kRetry) { | 
|  | 494 | // Based on | 
|  | 495 | // https://curl.haxx.se/docs/todo.html#updated_DNS_server_while_running, | 
|  | 496 | // update_engine process should call res_init() and unconditionally retry. | 
|  | 497 | res_init(); | 
|  | 498 | no_network_max_retries_++; | 
|  | 499 | LOG(INFO) << "Will retry after reloading resolv.conf because last attempt " | 
|  | 500 | "failed to resolve host."; | 
| Amin Hassani | d3d8421 | 2019-08-17 00:27:44 -0700 | [diff] [blame] | 501 | } else if (unresolved_host_state_machine_.GetState() == | 
| Xiaochu Liu | b5ba797 | 2019-07-11 09:51:06 -0700 | [diff] [blame] | 502 | UnresolvedHostStateMachine::State::kRetriedSuccess) { | 
| Amin Hassani | d3d8421 | 2019-08-17 00:27:44 -0700 | [diff] [blame] | 503 | auxiliary_error_code_ = ErrorCode::kUnresolvedHostRecovered; | 
| Xiaochu Liu | b5ba797 | 2019-07-11 09:51:06 -0700 | [diff] [blame] | 504 | } | 
|  | 505 |  | 
| Alex Deymo | f285857 | 2016-02-25 11:20:13 -0800 | [diff] [blame] | 506 | // TODO(petkov): This temporary code tries to deal with the case where the | 
|  | 507 | // update engine performs an update check while the network is not ready | 
|  | 508 | // (e.g., right after resume). Longer term, we should check if the network | 
|  | 509 | // is online/offline and return an appropriate error code. | 
| Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 510 | if (!sent_byte_ && http_response_code_ == 0 && | 
| Alex Deymo | f285857 | 2016-02-25 11:20:13 -0800 | [diff] [blame] | 511 | no_network_retry_count_ < no_network_max_retries_) { | 
|  | 512 | no_network_retry_count_++; | 
| Alex Deymo | b20de69 | 2017-02-05 07:47:37 +0000 | [diff] [blame] | 513 | retry_task_id_ = MessageLoop::current()->PostDelayedTask( | 
| Alex Deymo | f285857 | 2016-02-25 11:20:13 -0800 | [diff] [blame] | 514 | FROM_HERE, | 
|  | 515 | base::Bind(&LibcurlHttpFetcher::RetryTimeoutCallback, | 
|  | 516 | base::Unretained(this)), | 
|  | 517 | TimeDelta::FromSeconds(kNoNetworkRetrySeconds)); | 
|  | 518 | LOG(INFO) << "No HTTP response, retry " << no_network_retry_count_; | 
|  | 519 | } else if ((!sent_byte_ && !IsHttpResponseSuccess()) || | 
|  | 520 | IsHttpResponseError()) { | 
|  | 521 | // The transfer completed w/ error and we didn't get any bytes. | 
|  | 522 | // If we have another proxy to try, try that. | 
|  | 523 | // | 
|  | 524 | // TODO(garnold) in fact there are two separate cases here: one case is an | 
|  | 525 | // other-than-success return code (including no return code) and no | 
|  | 526 | // received bytes, which is necessary due to the way callbacks are | 
|  | 527 | // currently processing error conditions;  the second is an explicit HTTP | 
|  | 528 | // error code, where some data may have been received (as in the case of a | 
|  | 529 | // semi-successful multi-chunk fetch).  This is a confusing behavior and | 
|  | 530 | // should be unified into a complete, coherent interface. | 
|  | 531 | LOG(INFO) << "Transfer resulted in an error (" << http_response_code_ | 
|  | 532 | << "), " << bytes_downloaded_ << " bytes downloaded"; | 
|  | 533 |  | 
|  | 534 | PopProxy();  // Delete the proxy we just gave up on. | 
|  | 535 |  | 
|  | 536 | if (HasProxy()) { | 
|  | 537 | // We have another proxy. Retry immediately. | 
|  | 538 | LOG(INFO) << "Retrying with next proxy setting"; | 
| Alex Deymo | b20de69 | 2017-02-05 07:47:37 +0000 | [diff] [blame] | 539 | retry_task_id_ = MessageLoop::current()->PostTask( | 
| Alex Deymo | f285857 | 2016-02-25 11:20:13 -0800 | [diff] [blame] | 540 | FROM_HERE, | 
|  | 541 | base::Bind(&LibcurlHttpFetcher::RetryTimeoutCallback, | 
|  | 542 | base::Unretained(this))); | 
| Andrew de los Reyes | 9bbd187 | 2010-07-16 14:52:29 -0700 | [diff] [blame] | 543 | } else { | 
| Alex Deymo | f285857 | 2016-02-25 11:20:13 -0800 | [diff] [blame] | 544 | // Out of proxies. Give up. | 
|  | 545 | LOG(INFO) << "No further proxies, indicating transfer complete"; | 
|  | 546 | if (delegate_) | 
|  | 547 | delegate_->TransferComplete(this, false);  // signal fail | 
| Alex Deymo | 021a45e | 2016-03-15 13:12:05 -0700 | [diff] [blame] | 548 | return; | 
| Andrew de los Reyes | 9bbd187 | 2010-07-16 14:52:29 -0700 | [diff] [blame] | 549 | } | 
| Alex Deymo | f285857 | 2016-02-25 11:20:13 -0800 | [diff] [blame] | 550 | } else if ((transfer_size_ >= 0) && (bytes_downloaded_ < transfer_size_)) { | 
|  | 551 | if (!ignore_failure_) | 
|  | 552 | retry_count_++; | 
| Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 553 | LOG(INFO) << "Transfer interrupted after downloading " << bytes_downloaded_ | 
|  | 554 | << " of " << transfer_size_ << " bytes. " | 
| Alex Deymo | f285857 | 2016-02-25 11:20:13 -0800 | [diff] [blame] | 555 | << transfer_size_ - bytes_downloaded_ << " bytes remaining " | 
|  | 556 | << "after " << retry_count_ << " attempt(s)"; | 
| Darin Petkov | 192ced4 | 2010-07-23 16:20:24 -0700 | [diff] [blame] | 557 |  | 
| Alex Deymo | f285857 | 2016-02-25 11:20:13 -0800 | [diff] [blame] | 558 | if (retry_count_ > max_retry_count_) { | 
|  | 559 | LOG(INFO) << "Reached max attempts (" << retry_count_ << ")"; | 
|  | 560 | if (delegate_) | 
|  | 561 | delegate_->TransferComplete(this, false);  // signal fail | 
| Alex Deymo | 021a45e | 2016-03-15 13:12:05 -0700 | [diff] [blame] | 562 | return; | 
| adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 563 | } | 
| Alex Deymo | 021a45e | 2016-03-15 13:12:05 -0700 | [diff] [blame] | 564 | // Need to restart transfer | 
|  | 565 | LOG(INFO) << "Restarting transfer to download the remaining bytes"; | 
| Alex Deymo | b20de69 | 2017-02-05 07:47:37 +0000 | [diff] [blame] | 566 | retry_task_id_ = MessageLoop::current()->PostDelayedTask( | 
| Alex Deymo | 021a45e | 2016-03-15 13:12:05 -0700 | [diff] [blame] | 567 | FROM_HERE, | 
|  | 568 | base::Bind(&LibcurlHttpFetcher::RetryTimeoutCallback, | 
|  | 569 | base::Unretained(this)), | 
|  | 570 | TimeDelta::FromSeconds(retry_seconds_)); | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 571 | } else { | 
| Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 572 | LOG(INFO) << "Transfer completed (" << http_response_code_ << "), " | 
|  | 573 | << bytes_downloaded_ << " bytes downloaded"; | 
| Alex Deymo | f285857 | 2016-02-25 11:20:13 -0800 | [diff] [blame] | 574 | if (delegate_) { | 
|  | 575 | bool success = IsHttpResponseSuccess(); | 
|  | 576 | delegate_->TransferComplete(this, success); | 
|  | 577 | } | 
| Alex Deymo | 021a45e | 2016-03-15 13:12:05 -0700 | [diff] [blame] | 578 | return; | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 579 | } | 
| Alex Deymo | 021a45e | 2016-03-15 13:12:05 -0700 | [diff] [blame] | 580 | // If we reach this point is because TransferComplete() was not called in any | 
|  | 581 | // of the previous branches. The delegate is allowed to destroy the object | 
|  | 582 | // once TransferComplete is called so this would be illegal. | 
| Alex Deymo | f285857 | 2016-02-25 11:20:13 -0800 | [diff] [blame] | 583 | ignore_failure_ = false; | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 584 | } | 
|  | 585 |  | 
| Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 586 | size_t LibcurlHttpFetcher::LibcurlWrite(void* ptr, size_t size, size_t nmemb) { | 
| Gilad Arnold | 48085ba | 2011-11-16 09:36:08 -0800 | [diff] [blame] | 587 | // Update HTTP response first. | 
| Andrew de los Reyes | 3fd5d30 | 2010-10-07 20:07:18 -0700 | [diff] [blame] | 588 | GetHttpResponseCode(); | 
| Gilad Arnold | 48085ba | 2011-11-16 09:36:08 -0800 | [diff] [blame] | 589 | const size_t payload_size = size * nmemb; | 
|  | 590 |  | 
|  | 591 | // Do nothing if no payload or HTTP response is an error. | 
| Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 592 | if (payload_size == 0 || !IsHttpResponseSuccess()) { | 
| Gilad Arnold | 48085ba | 2011-11-16 09:36:08 -0800 | [diff] [blame] | 593 | LOG(INFO) << "HTTP response unsuccessful (" << http_response_code_ | 
|  | 594 | << ") or no payload (" << payload_size << "), nothing to do"; | 
|  | 595 | return 0; | 
|  | 596 | } | 
|  | 597 |  | 
|  | 598 | sent_byte_ = true; | 
| adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 599 | { | 
| Daniel Zheng | da4f729 | 2022-09-02 22:59:32 +0000 | [diff] [blame] | 600 | double transfer_size_double{}; | 
| adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 601 | CHECK_EQ(curl_easy_getinfo(curl_handle_, | 
|  | 602 | CURLINFO_CONTENT_LENGTH_DOWNLOAD, | 
| Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 603 | &transfer_size_double), | 
|  | 604 | CURLE_OK); | 
| adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 605 | off_t new_transfer_size = static_cast<off_t>(transfer_size_double); | 
|  | 606 | if (new_transfer_size > 0) { | 
|  | 607 | transfer_size_ = resume_offset_ + new_transfer_size; | 
|  | 608 | } | 
|  | 609 | } | 
| Gilad Arnold | 48085ba | 2011-11-16 09:36:08 -0800 | [diff] [blame] | 610 | bytes_downloaded_ += payload_size; | 
| Tao Bao | 028ea41 | 2018-12-27 14:12:14 -0800 | [diff] [blame] | 611 | if (delegate_) { | 
|  | 612 | in_write_callback_ = true; | 
|  | 613 | auto should_terminate = !delegate_->ReceivedBytes(this, ptr, payload_size); | 
|  | 614 | in_write_callback_ = false; | 
|  | 615 | if (should_terminate) { | 
|  | 616 | LOG(INFO) << "Requesting libcurl to terminate transfer."; | 
|  | 617 | // Returning an amount that differs from the received size signals an | 
|  | 618 | // error condition to libcurl, which will cause the transfer to be | 
|  | 619 | // aborted. | 
|  | 620 | return 0; | 
|  | 621 | } | 
|  | 622 | } | 
| Gilad Arnold | 48085ba | 2011-11-16 09:36:08 -0800 | [diff] [blame] | 623 | return payload_size; | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 624 | } | 
|  | 625 |  | 
|  | 626 | void LibcurlHttpFetcher::Pause() { | 
| Alex Deymo | f285857 | 2016-02-25 11:20:13 -0800 | [diff] [blame] | 627 | if (transfer_paused_) { | 
|  | 628 | LOG(ERROR) << "Fetcher already paused."; | 
|  | 629 | return; | 
|  | 630 | } | 
|  | 631 | transfer_paused_ = true; | 
|  | 632 | if (!transfer_in_progress_) { | 
|  | 633 | // If pause before we started a connection, we don't need to notify curl | 
|  | 634 | // about that, we will simply not start the connection later. | 
|  | 635 | return; | 
|  | 636 | } | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 637 | CHECK(curl_handle_); | 
| adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 638 | CHECK_EQ(curl_easy_pause(curl_handle_, CURLPAUSE_ALL), CURLE_OK); | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 639 | } | 
|  | 640 |  | 
|  | 641 | void LibcurlHttpFetcher::Unpause() { | 
| Alex Deymo | f285857 | 2016-02-25 11:20:13 -0800 | [diff] [blame] | 642 | if (!transfer_paused_) { | 
|  | 643 | LOG(ERROR) << "Resume attempted when fetcher not paused."; | 
|  | 644 | return; | 
|  | 645 | } | 
|  | 646 | transfer_paused_ = false; | 
|  | 647 | if (restart_transfer_on_unpause_) { | 
|  | 648 | restart_transfer_on_unpause_ = false; | 
|  | 649 | ResumeTransfer(url_); | 
|  | 650 | CurlPerformOnce(); | 
|  | 651 | return; | 
|  | 652 | } | 
|  | 653 | if (!transfer_in_progress_) { | 
|  | 654 | // If resumed before starting the connection, there's no need to notify | 
|  | 655 | // anybody. We will simply start the connection once it is time. | 
|  | 656 | return; | 
|  | 657 | } | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 658 | CHECK(curl_handle_); | 
| adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 659 | CHECK_EQ(curl_easy_pause(curl_handle_, CURLPAUSE_CONT), CURLE_OK); | 
| Alex Deymo | f285857 | 2016-02-25 11:20:13 -0800 | [diff] [blame] | 660 | // Since the transfer is in progress, we need to dispatch a CurlPerformOnce() | 
|  | 661 | // now to let the connection continue, otherwise it would be called by the | 
|  | 662 | // TimeoutCallback but with a delay. | 
|  | 663 | CurlPerformOnce(); | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 664 | } | 
|  | 665 |  | 
| Alex Deymo | 29b8153 | 2015-07-09 11:51:49 -0700 | [diff] [blame] | 666 | // This method sets up callbacks with the MessageLoop. | 
|  | 667 | void LibcurlHttpFetcher::SetupMessageLoopSources() { | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 668 | fd_set fd_read; | 
|  | 669 | fd_set fd_write; | 
| Darin Petkov | 60e1415 | 2010-10-27 16:57:04 -0700 | [diff] [blame] | 670 | fd_set fd_exc; | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 671 |  | 
|  | 672 | FD_ZERO(&fd_read); | 
|  | 673 | FD_ZERO(&fd_write); | 
| Darin Petkov | 60e1415 | 2010-10-27 16:57:04 -0700 | [diff] [blame] | 674 | FD_ZERO(&fd_exc); | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 675 |  | 
|  | 676 | int fd_max = 0; | 
|  | 677 |  | 
|  | 678 | // Ask libcurl for the set of file descriptors we should track on its | 
|  | 679 | // behalf. | 
| Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 680 | CHECK_EQ(curl_multi_fdset( | 
|  | 681 | curl_multi_handle_, &fd_read, &fd_write, &fd_exc, &fd_max), | 
|  | 682 | CURLM_OK); | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 683 |  | 
|  | 684 | // 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] | 685 | // the highest one we're tracking, whichever is larger. | 
| hscham | 00b6aa2 | 2020-02-20 12:32:06 +0900 | [diff] [blame] | 686 | for (size_t t = 0; t < base::size(fd_controller_maps_); ++t) { | 
| Hidehiko Abe | 493fecb | 2019-07-10 23:30:50 +0900 | [diff] [blame] | 687 | if (!fd_controller_maps_[t].empty()) | 
|  | 688 | fd_max = max(fd_max, fd_controller_maps_[t].rbegin()->first); | 
| Darin Petkov | 60e1415 | 2010-10-27 16:57:04 -0700 | [diff] [blame] | 689 | } | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 690 |  | 
| Darin Petkov | 60e1415 | 2010-10-27 16:57:04 -0700 | [diff] [blame] | 691 | // For each fd, if we're not tracking it, track it. If we are tracking it, but | 
|  | 692 | // libcurl doesn't care about it anymore, stop tracking it. After this loop, | 
| Hidehiko Abe | 493fecb | 2019-07-10 23:30:50 +0900 | [diff] [blame] | 693 | // there should be exactly as many tasks scheduled in | 
|  | 694 | // fd_controller_maps_[0|1] as there are read/write fds that we're tracking. | 
| Darin Petkov | 60e1415 | 2010-10-27 16:57:04 -0700 | [diff] [blame] | 695 | for (int fd = 0; fd <= fd_max; ++fd) { | 
|  | 696 | // Note that fd_exc is unused in the current version of libcurl so is_exc | 
|  | 697 | // should always be false. | 
|  | 698 | bool is_exc = FD_ISSET(fd, &fd_exc) != 0; | 
|  | 699 | bool must_track[2] = { | 
| Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 700 | is_exc || (FD_ISSET(fd, &fd_read) != 0),  // track 0 -- read | 
|  | 701 | is_exc || (FD_ISSET(fd, &fd_write) != 0)  // track 1 -- write | 
| Darin Petkov | 60e1415 | 2010-10-27 16:57:04 -0700 | [diff] [blame] | 702 | }; | 
|  | 703 |  | 
| hscham | 00b6aa2 | 2020-02-20 12:32:06 +0900 | [diff] [blame] | 704 | for (size_t t = 0; t < base::size(fd_controller_maps_); ++t) { | 
| Hidehiko Abe | 493fecb | 2019-07-10 23:30:50 +0900 | [diff] [blame] | 705 | bool tracked = | 
|  | 706 | fd_controller_maps_[t].find(fd) != fd_controller_maps_[t].end(); | 
| Darin Petkov | 60e1415 | 2010-10-27 16:57:04 -0700 | [diff] [blame] | 707 |  | 
|  | 708 | if (!must_track[t]) { | 
|  | 709 | // If we have an outstanding io_channel, remove it. | 
| Hidehiko Abe | 493fecb | 2019-07-10 23:30:50 +0900 | [diff] [blame] | 710 | fd_controller_maps_[t].erase(fd); | 
| Darin Petkov | 60e1415 | 2010-10-27 16:57:04 -0700 | [diff] [blame] | 711 | continue; | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 712 | } | 
| Darin Petkov | 60e1415 | 2010-10-27 16:57:04 -0700 | [diff] [blame] | 713 |  | 
|  | 714 | // If we are already tracking this fd, continue -- nothing to do. | 
|  | 715 | if (tracked) | 
|  | 716 | continue; | 
|  | 717 |  | 
| Darin Petkov | 60e1415 | 2010-10-27 16:57:04 -0700 | [diff] [blame] | 718 | // Track a new fd. | 
| Hidehiko Abe | 493fecb | 2019-07-10 23:30:50 +0900 | [diff] [blame] | 719 | switch (t) { | 
|  | 720 | case 0:  // Read | 
|  | 721 | fd_controller_maps_[t][fd] = | 
|  | 722 | base::FileDescriptorWatcher::WatchReadable( | 
|  | 723 | fd, | 
|  | 724 | base::BindRepeating(&LibcurlHttpFetcher::CurlPerformOnce, | 
|  | 725 | base::Unretained(this))); | 
|  | 726 | break; | 
|  | 727 | case 1:  // Write | 
|  | 728 | fd_controller_maps_[t][fd] = | 
|  | 729 | base::FileDescriptorWatcher::WatchWritable( | 
|  | 730 | fd, | 
|  | 731 | base::BindRepeating(&LibcurlHttpFetcher::CurlPerformOnce, | 
|  | 732 | base::Unretained(this))); | 
|  | 733 | } | 
| Darin Petkov | 60e1415 | 2010-10-27 16:57:04 -0700 | [diff] [blame] | 734 | static int io_counter = 0; | 
|  | 735 | io_counter++; | 
|  | 736 | if (io_counter % 50 == 0) { | 
|  | 737 | LOG(INFO) << "io_counter = " << io_counter; | 
|  | 738 | } | 
| Andrew de los Reyes | 3270f74 | 2010-07-15 22:28:14 -0700 | [diff] [blame] | 739 | } | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 740 | } | 
|  | 741 |  | 
| Darin Petkov | b83371f | 2010-08-17 09:34:49 -0700 | [diff] [blame] | 742 | // Set up a timeout callback for libcurl. | 
| Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 743 | if (timeout_id_ == MessageLoop::kTaskIdNull) { | 
| Alex Deymo | f285857 | 2016-02-25 11:20:13 -0800 | [diff] [blame] | 744 | VLOG(1) << "Setting up timeout source: " << idle_seconds_ << " seconds."; | 
| Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 745 | timeout_id_ = MessageLoop::current()->PostDelayedTask( | 
|  | 746 | FROM_HERE, | 
|  | 747 | base::Bind(&LibcurlHttpFetcher::TimeoutCallback, | 
|  | 748 | base::Unretained(this)), | 
|  | 749 | TimeDelta::FromSeconds(idle_seconds_)); | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 750 | } | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 751 | } | 
|  | 752 |  | 
| Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 753 | void LibcurlHttpFetcher::RetryTimeoutCallback() { | 
| Alex Deymo | b20de69 | 2017-02-05 07:47:37 +0000 | [diff] [blame] | 754 | retry_task_id_ = MessageLoop::kTaskIdNull; | 
| Alex Deymo | f285857 | 2016-02-25 11:20:13 -0800 | [diff] [blame] | 755 | if (transfer_paused_) { | 
|  | 756 | restart_transfer_on_unpause_ = true; | 
|  | 757 | return; | 
|  | 758 | } | 
| Andrew de los Reyes | 9bbd187 | 2010-07-16 14:52:29 -0700 | [diff] [blame] | 759 | ResumeTransfer(url_); | 
|  | 760 | CurlPerformOnce(); | 
| Andrew de los Reyes | 9bbd187 | 2010-07-16 14:52:29 -0700 | [diff] [blame] | 761 | } | 
|  | 762 |  | 
| Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 763 | void LibcurlHttpFetcher::TimeoutCallback() { | 
| Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 764 | // We always re-schedule the callback, even if we don't want to be called | 
|  | 765 | // 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] | 766 | // be called back. | 
| Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 767 | timeout_id_ = MessageLoop::current()->PostDelayedTask( | 
|  | 768 | FROM_HERE, | 
|  | 769 | base::Bind(&LibcurlHttpFetcher::TimeoutCallback, base::Unretained(this)), | 
|  | 770 | TimeDelta::FromSeconds(idle_seconds_)); | 
| Alex Deymo | f123ae2 | 2015-09-24 14:59:43 -0700 | [diff] [blame] | 771 |  | 
|  | 772 | // CurlPerformOnce() may call CleanUp(), so we need to schedule our callback | 
|  | 773 | // first, since it could be canceled by this call. | 
|  | 774 | if (transfer_in_progress_) | 
|  | 775 | CurlPerformOnce(); | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 776 | } | 
|  | 777 |  | 
|  | 778 | void LibcurlHttpFetcher::CleanUp() { | 
| Alex Deymo | b20de69 | 2017-02-05 07:47:37 +0000 | [diff] [blame] | 779 | MessageLoop::current()->CancelTask(retry_task_id_); | 
|  | 780 | retry_task_id_ = MessageLoop::kTaskIdNull; | 
|  | 781 |  | 
| Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 782 | MessageLoop::current()->CancelTask(timeout_id_); | 
|  | 783 | timeout_id_ = MessageLoop::kTaskIdNull; | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 784 |  | 
| hscham | 00b6aa2 | 2020-02-20 12:32:06 +0900 | [diff] [blame] | 785 | for (size_t t = 0; t < base::size(fd_controller_maps_); ++t) { | 
| Hidehiko Abe | 493fecb | 2019-07-10 23:30:50 +0900 | [diff] [blame] | 786 | fd_controller_maps_[t].clear(); | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 787 | } | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 788 |  | 
| Gilad Arnold | 9dd1e7c | 2012-02-16 12:13:36 -0800 | [diff] [blame] | 789 | if (curl_http_headers_) { | 
|  | 790 | curl_slist_free_all(curl_http_headers_); | 
| Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 791 | curl_http_headers_ = nullptr; | 
| Gilad Arnold | 9dd1e7c | 2012-02-16 12:13:36 -0800 | [diff] [blame] | 792 | } | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 793 | if (curl_handle_) { | 
|  | 794 | if (curl_multi_handle_) { | 
| adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 795 | CHECK_EQ(curl_multi_remove_handle(curl_multi_handle_, curl_handle_), | 
|  | 796 | CURLM_OK); | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 797 | } | 
|  | 798 | curl_easy_cleanup(curl_handle_); | 
| Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 799 | curl_handle_ = nullptr; | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 800 | } | 
|  | 801 | if (curl_multi_handle_) { | 
| adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 802 | CHECK_EQ(curl_multi_cleanup(curl_multi_handle_), CURLM_OK); | 
| Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 803 | curl_multi_handle_ = nullptr; | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 804 | } | 
|  | 805 | transfer_in_progress_ = false; | 
| Alex Deymo | f285857 | 2016-02-25 11:20:13 -0800 | [diff] [blame] | 806 | transfer_paused_ = false; | 
|  | 807 | restart_transfer_on_unpause_ = false; | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 808 | } | 
|  | 809 |  | 
| Andrew de los Reyes | 3fd5d30 | 2010-10-07 20:07:18 -0700 | [diff] [blame] | 810 | void LibcurlHttpFetcher::GetHttpResponseCode() { | 
| Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 811 | long http_response_code = 0;  // NOLINT(runtime/int) - curl needs long. | 
| Alex Deymo | 56ccb07 | 2016-02-05 00:50:48 -0800 | [diff] [blame] | 812 | if (base::StartsWith(url_, "file://", base::CompareCase::INSENSITIVE_ASCII)) { | 
|  | 813 | // Fake out a valid response code for file:// URLs. | 
|  | 814 | http_response_code_ = 299; | 
|  | 815 | } else if (curl_easy_getinfo(curl_handle_, | 
|  | 816 | CURLINFO_RESPONSE_CODE, | 
|  | 817 | &http_response_code) == CURLE_OK) { | 
| Andrew de los Reyes | 3fd5d30 | 2010-10-07 20:07:18 -0700 | [diff] [blame] | 818 | http_response_code_ = static_cast<int>(http_response_code); | 
| Xiaochu Liu | 4a1173a | 2019-04-10 10:49:08 -0700 | [diff] [blame] | 819 | } else { | 
|  | 820 | LOG(ERROR) << "Unable to get http response code from curl_easy_getinfo"; | 
|  | 821 | } | 
|  | 822 | } | 
|  | 823 |  | 
| Xiaochu Liu | b5ba797 | 2019-07-11 09:51:06 -0700 | [diff] [blame] | 824 | CURLcode LibcurlHttpFetcher::GetCurlCode() { | 
|  | 825 | CURLcode curl_code = CURLE_OK; | 
| Xiaochu Liu | 4a1173a | 2019-04-10 10:49:08 -0700 | [diff] [blame] | 826 | while (true) { | 
|  | 827 | // Repeated calls to |curl_multi_info_read| will return a new struct each | 
|  | 828 | // time, until a NULL is returned as a signal that there is no more to get | 
|  | 829 | // at this point. | 
| Daniel Zheng | da4f729 | 2022-09-02 22:59:32 +0000 | [diff] [blame] | 830 | int msgs_in_queue{}; | 
| Xiaochu Liu | 4a1173a | 2019-04-10 10:49:08 -0700 | [diff] [blame] | 831 | CURLMsg* curl_msg = | 
|  | 832 | curl_multi_info_read(curl_multi_handle_, &msgs_in_queue); | 
|  | 833 | if (curl_msg == nullptr) | 
|  | 834 | break; | 
|  | 835 | // When |curl_msg| is |CURLMSG_DONE|, a transfer of an easy handle is done, | 
|  | 836 | // and then data contains the return code for this transfer. | 
|  | 837 | if (curl_msg->msg == CURLMSG_DONE) { | 
|  | 838 | // Make sure |curl_multi_handle_| has one and only one easy handle | 
|  | 839 | // |curl_handle_|. | 
|  | 840 | CHECK_EQ(curl_handle_, curl_msg->easy_handle); | 
|  | 841 | // Transfer return code reference: | 
|  | 842 | // https://curl.haxx.se/libcurl/c/libcurl-errors.html | 
| Xiaochu Liu | b5ba797 | 2019-07-11 09:51:06 -0700 | [diff] [blame] | 843 | curl_code = curl_msg->data.result; | 
| Xiaochu Liu | 4a1173a | 2019-04-10 10:49:08 -0700 | [diff] [blame] | 844 | } | 
|  | 845 | } | 
|  | 846 |  | 
|  | 847 | // Gets connection error if exists. | 
|  | 848 | long connect_error = 0;  // NOLINT(runtime/int) - curl needs long. | 
|  | 849 | CURLcode res = | 
|  | 850 | curl_easy_getinfo(curl_handle_, CURLINFO_OS_ERRNO, &connect_error); | 
|  | 851 | if (res == CURLE_OK && connect_error) { | 
|  | 852 | LOG(ERROR) << "Connect error code from the OS: " << connect_error; | 
| Andrew de los Reyes | 3fd5d30 | 2010-10-07 20:07:18 -0700 | [diff] [blame] | 853 | } | 
| Xiaochu Liu | b5ba797 | 2019-07-11 09:51:06 -0700 | [diff] [blame] | 854 |  | 
|  | 855 | return curl_code; | 
|  | 856 | } | 
|  | 857 |  | 
|  | 858 | void UnresolvedHostStateMachine::UpdateState(bool failed_to_resolve_host) { | 
|  | 859 | switch (state_) { | 
|  | 860 | case State::kInit: | 
|  | 861 | if (failed_to_resolve_host) { | 
|  | 862 | state_ = State::kRetry; | 
|  | 863 | } | 
|  | 864 | break; | 
|  | 865 | case State::kRetry: | 
|  | 866 | if (failed_to_resolve_host) { | 
|  | 867 | state_ = State::kNotRetry; | 
|  | 868 | } else { | 
|  | 869 | state_ = State::kRetriedSuccess; | 
|  | 870 | } | 
|  | 871 | break; | 
|  | 872 | case State::kNotRetry: | 
|  | 873 | break; | 
|  | 874 | case State::kRetriedSuccess: | 
|  | 875 | break; | 
|  | 876 | default: | 
|  | 877 | NOTREACHED(); | 
|  | 878 | break; | 
| Andrew de los Reyes | 3fd5d30 | 2010-10-07 20:07:18 -0700 | [diff] [blame] | 879 | } | 
|  | 880 | } | 
|  | 881 |  | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 882 | }  // namespace chromeos_update_engine |