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