blob: 9d9f58b98ede87eccf2fda0a170616aef732ac86 [file] [log] [blame]
Alex Deymoaea4c1c2015-08-19 20:24:43 -07001//
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.com49fdf182009-10-10 00:57:34 +000016
Alex Deymo14c0da82016-07-20 16:45:45 -070017#include "update_engine/libcurl_http_fetcher.h"
Andrew de los Reyesd57d1472010-10-21 13:34:08 -070018
Alex Deymo63cfcf42017-02-23 15:29:47 -080019#include <sys/types.h>
20#include <unistd.h>
21
adlr@google.comc98a7ed2009-12-04 18:54:03 +000022#include <algorithm>
Andrew de los Reyesd57d1472010-10-21 13:34:08 -070023#include <string>
24
Alex Vakulenko4906c1c2014-08-21 13:17:44 -070025#include <base/bind.h>
Alex Deymoc00c98a2015-03-17 17:38:00 -070026#include <base/format_macros.h>
Alex Deymo60ca1a72015-06-18 18:19:15 -070027#include <base/location.h>
Andrew de los Reyesd57d1472010-10-21 13:34:08 -070028#include <base/logging.h>
Alex Vakulenko75039d72014-03-25 12:36:28 -070029#include <base/strings/string_util.h>
30#include <base/strings/stringprintf.h>
Andrew de los Reyesd57d1472010-10-21 13:34:08 -070031
Alex Deymo63cfcf42017-02-23 15:29:47 -080032#ifdef __ANDROID__
33#include <cutils/qtaguid.h>
34#endif // __ANDROID__
35
Alex Deymo14c0da82016-07-20 16:45:45 -070036#include "update_engine/certificate_checker.h"
Alex Deymo39910dc2015-11-09 17:04:30 -080037#include "update_engine/common/hardware_interface.h"
38#include "update_engine/common/platform_constants.h"
adlr@google.comc98a7ed2009-12-04 18:54:03 +000039
Alex Deymo60ca1a72015-06-18 18:19:15 -070040using base::TimeDelta;
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070041using brillo::MessageLoop;
Alex Deymoc4acdf42014-05-28 21:07:10 -070042using std::max;
Andrew de los Reyesd57d1472010-10-21 13:34:08 -070043using std::string;
rspangler@google.com49fdf182009-10-10 00:57:34 +000044
45// This is a concrete implementation of HttpFetcher that uses libcurl to do the
46// http work.
47
48namespace chromeos_update_engine {
49
Andrew de los Reyes9bbd1872010-07-16 14:52:29 -070050namespace {
Alex Deymo63cfcf42017-02-23 15:29:47 -080051
Andrew de los Reyes5d0783d2010-11-29 18:14:16 -080052const int kNoNetworkRetrySeconds = 10;
Alex Deymo63cfcf42017-02-23 15:29:47 -080053
54// Socket tag used by all network sockets. See qtaguid kernel module for stats.
55const int kUpdateEngineSocketTag = 0x55417243; // "CrAU" in little-endian.
56
57// libcurl's CURLOPT_SOCKOPTFUNCTION callback function. Called after the socket
58// is created but before it is connected. This callback tags the created socket
59// so the network usage can be tracked in Android.
60int LibcurlSockoptCallback(void* /* clientp */,
61 curl_socket_t curlfd,
62 curlsocktype /* purpose */) {
63#ifdef __ANDROID__
64 qtaguid_tagSocket(curlfd, kUpdateEngineSocketTag, getuid());
65#endif // __ANDROID__
66 return CURL_SOCKOPT_OK;
67}
68
Alex Vakulenkod2779df2014-06-16 13:19:00 -070069} // namespace
Andrew de los Reyes9bbd1872010-07-16 14:52:29 -070070
Alex Deymo13e95182017-03-16 19:06:13 -070071// static
72int LibcurlHttpFetcher::LibcurlCloseSocketCallback(void* clientp,
73 curl_socket_t item) {
74#ifdef __ANDROID__
75 qtaguid_untagSocket(item);
76#endif // __ANDROID__
77 LibcurlHttpFetcher* fetcher = static_cast<LibcurlHttpFetcher*>(clientp);
78 // Stop watching the socket before closing it.
79 for (size_t t = 0; t < arraysize(fetcher->fd_task_maps_); ++t) {
80 const auto fd_task_pair = fetcher->fd_task_maps_[t].find(item);
81 if (fd_task_pair != fetcher->fd_task_maps_[t].end()) {
82 if (!MessageLoop::current()->CancelTask(fd_task_pair->second)) {
83 LOG(WARNING) << "Error canceling the watch task "
84 << fd_task_pair->second << " for "
85 << (t ? "writing" : "reading") << " the fd " << item;
86 }
87 fetcher->fd_task_maps_[t].erase(item);
88 }
89 }
90
91 // Documentation for this callback says to return 0 on success or 1 on error.
92 if (!IGNORE_EINTR(close(item)))
93 return 0;
94 return 1;
95}
96
Alex Deymo33e91e72015-12-01 18:26:08 -030097LibcurlHttpFetcher::LibcurlHttpFetcher(ProxyResolver* proxy_resolver,
98 HardwareInterface* hardware)
99 : HttpFetcher(proxy_resolver), hardware_(hardware) {
Alex Deymoc1c17b42015-11-23 03:53:15 -0300100 // Dev users want a longer timeout (180 seconds) because they may
101 // be waiting on the dev server to build an image.
102 if (!hardware_->IsOfficialBuild())
103 low_speed_time_seconds_ = kDownloadDevModeLowSpeedTimeSeconds;
Alex Deymo46a9aae2016-05-04 20:20:11 -0700104 if (hardware_->IsOOBEEnabled() && !hardware_->IsOOBEComplete(nullptr))
Alex Deymoc1c17b42015-11-23 03:53:15 -0300105 max_retry_count_ = kDownloadMaxRetryCountOobeNotComplete;
106}
107
rspangler@google.com49fdf182009-10-10 00:57:34 +0000108LibcurlHttpFetcher::~LibcurlHttpFetcher() {
Darin Petkov9ce452b2010-11-17 14:33:28 -0800109 LOG_IF(ERROR, transfer_in_progress_)
110 << "Destroying the fetcher while a transfer is in progress.";
Alex Deymo71f67622017-02-03 21:30:24 -0800111 CancelProxyResolution();
rspangler@google.com49fdf182009-10-10 00:57:34 +0000112 CleanUp();
113}
114
Alex Deymof329b932014-10-30 01:37:48 -0700115bool LibcurlHttpFetcher::GetProxyType(const string& proxy,
Gilad Arnold59d9e012013-07-23 16:41:43 -0700116 curl_proxytype* out_type) {
Alex Deymo56ccb072016-02-05 00:50:48 -0800117 if (base::StartsWith(
118 proxy, "socks5://", base::CompareCase::INSENSITIVE_ASCII) ||
119 base::StartsWith(
120 proxy, "socks://", base::CompareCase::INSENSITIVE_ASCII)) {
Gilad Arnold59d9e012013-07-23 16:41:43 -0700121 *out_type = CURLPROXY_SOCKS5_HOSTNAME;
122 return true;
123 }
Alex Deymo56ccb072016-02-05 00:50:48 -0800124 if (base::StartsWith(
125 proxy, "socks4://", base::CompareCase::INSENSITIVE_ASCII)) {
Gilad Arnold59d9e012013-07-23 16:41:43 -0700126 *out_type = CURLPROXY_SOCKS4A;
127 return true;
128 }
Alex Deymo56ccb072016-02-05 00:50:48 -0800129 if (base::StartsWith(
130 proxy, "http://", base::CompareCase::INSENSITIVE_ASCII) ||
131 base::StartsWith(
132 proxy, "https://", base::CompareCase::INSENSITIVE_ASCII)) {
Gilad Arnold59d9e012013-07-23 16:41:43 -0700133 *out_type = CURLPROXY_HTTP;
134 return true;
135 }
Alex Deymo56ccb072016-02-05 00:50:48 -0800136 if (base::StartsWith(proxy, kNoProxy, base::CompareCase::INSENSITIVE_ASCII)) {
Gilad Arnold59d9e012013-07-23 16:41:43 -0700137 // known failure case. don't log.
138 return false;
139 }
140 LOG(INFO) << "Unknown proxy type: " << proxy;
141 return false;
142}
143
Alex Deymof329b932014-10-30 01:37:48 -0700144void LibcurlHttpFetcher::ResumeTransfer(const string& url) {
Andrew de los Reyes3270f742010-07-15 22:28:14 -0700145 LOG(INFO) << "Starting/Resuming transfer";
rspangler@google.com49fdf182009-10-10 00:57:34 +0000146 CHECK(!transfer_in_progress_);
147 url_ = url;
148 curl_multi_handle_ = curl_multi_init();
149 CHECK(curl_multi_handle_);
150
151 curl_handle_ = curl_easy_init();
152 CHECK(curl_handle_);
Alex Deymof2858572016-02-25 11:20:13 -0800153 ignore_failure_ = false;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000154
Alex Deymo13e95182017-03-16 19:06:13 -0700155 // Tag and untag the socket for network usage stats.
Alex Deymo63cfcf42017-02-23 15:29:47 -0800156 curl_easy_setopt(
157 curl_handle_, CURLOPT_SOCKOPTFUNCTION, LibcurlSockoptCallback);
Alex Deymo13e95182017-03-16 19:06:13 -0700158 curl_easy_setopt(
159 curl_handle_, CURLOPT_CLOSESOCKETFUNCTION, LibcurlCloseSocketCallback);
160 curl_easy_setopt(curl_handle_, CURLOPT_CLOSESOCKETDATA, this);
Alex Deymo63cfcf42017-02-23 15:29:47 -0800161
Andrew de los Reyes45168102010-11-22 11:13:50 -0800162 CHECK(HasProxy());
Gilad Arnoldfbaee242012-04-04 15:59:43 -0700163 bool is_direct = (GetCurrentProxy() == kNoProxy);
164 LOG(INFO) << "Using proxy: " << (is_direct ? "no" : "yes");
165 if (is_direct) {
Andrew de los Reyes45168102010-11-22 11:13:50 -0800166 CHECK_EQ(curl_easy_setopt(curl_handle_,
167 CURLOPT_PROXY,
168 ""), CURLE_OK);
169 } else {
170 CHECK_EQ(curl_easy_setopt(curl_handle_,
171 CURLOPT_PROXY,
172 GetCurrentProxy().c_str()), CURLE_OK);
173 // Curl seems to require us to set the protocol
174 curl_proxytype type;
Gilad Arnold59d9e012013-07-23 16:41:43 -0700175 if (GetProxyType(GetCurrentProxy(), &type)) {
Andrew de los Reyes45168102010-11-22 11:13:50 -0800176 CHECK_EQ(curl_easy_setopt(curl_handle_,
177 CURLOPT_PROXYTYPE,
178 type), CURLE_OK);
179 }
180 }
181
rspangler@google.com49fdf182009-10-10 00:57:34 +0000182 if (post_data_set_) {
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000183 CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_POST, 1), CURLE_OK);
184 CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_POSTFIELDS,
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800185 post_data_.data()),
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000186 CURLE_OK);
187 CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_POSTFIELDSIZE,
188 post_data_.size()),
189 CURLE_OK);
Alex Deymofdd6dec2016-03-03 22:35:43 -0800190 }
Gilad Arnold9dd1e7c2012-02-16 12:13:36 -0800191
Alex Deymofdd6dec2016-03-03 22:35:43 -0800192 // Setup extra HTTP headers.
193 if (curl_http_headers_) {
194 curl_slist_free_all(curl_http_headers_);
195 curl_http_headers_ = nullptr;
196 }
197 for (const auto& header : extra_headers_) {
198 // curl_slist_append() copies the string.
199 curl_http_headers_ =
200 curl_slist_append(curl_http_headers_, header.second.c_str());
201 }
202 if (post_data_set_) {
Gilad Arnold9dd1e7c2012-02-16 12:13:36 -0800203 // Set the Content-Type HTTP header, if one was specifically set.
Gilad Arnold9dd1e7c2012-02-16 12:13:36 -0800204 if (post_content_type_ != kHttpContentTypeUnspecified) {
Alex Deymofdd6dec2016-03-03 22:35:43 -0800205 const string content_type_attr = base::StringPrintf(
206 "Content-Type: %s", GetHttpContentTypeString(post_content_type_));
207 curl_http_headers_ =
208 curl_slist_append(curl_http_headers_, content_type_attr.c_str());
Gilad Arnold9dd1e7c2012-02-16 12:13:36 -0800209 } else {
210 LOG(WARNING) << "no content type set, using libcurl default";
211 }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000212 }
Alex Deymofdd6dec2016-03-03 22:35:43 -0800213 CHECK_EQ(
214 curl_easy_setopt(curl_handle_, CURLOPT_HTTPHEADER, curl_http_headers_),
215 CURLE_OK);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000216
Gilad Arnolde4ad2502011-12-29 17:08:54 -0800217 if (bytes_downloaded_ > 0 || download_length_) {
218 // Resume from where we left off.
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000219 resume_offset_ = bytes_downloaded_;
Gilad Arnolde4ad2502011-12-29 17:08:54 -0800220 CHECK_GE(resume_offset_, 0);
221
222 // Compute end offset, if one is specified. As per HTTP specification, this
223 // is an inclusive boundary. Make sure it doesn't overflow.
224 size_t end_offset = 0;
225 if (download_length_) {
226 end_offset = static_cast<size_t>(resume_offset_) + download_length_ - 1;
227 CHECK_LE((size_t) resume_offset_, end_offset);
228 }
229
230 // Create a string representation of the desired range.
Alex Deymoc00c98a2015-03-17 17:38:00 -0700231 string range_str = base::StringPrintf(
232 "%" PRIu64 "-", static_cast<uint64_t>(resume_offset_));
233 if (end_offset)
234 range_str += std::to_string(end_offset);
Gilad Arnolde4ad2502011-12-29 17:08:54 -0800235 CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_RANGE, range_str.c_str()),
236 CURLE_OK);
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000237 }
238
239 CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_WRITEDATA, this), CURLE_OK);
240 CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_WRITEFUNCTION,
241 StaticLibcurlWrite), CURLE_OK);
Chris Sosa77f79e82014-06-02 18:16:24 -0700242 CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_URL, url_.c_str()),
Andrew de los Reyesd57d1472010-10-21 13:34:08 -0700243 CURLE_OK);
Andrew de los Reyes3270f742010-07-15 22:28:14 -0700244
David Zeuthen34135a92013-08-06 11:16:16 -0700245 // If the connection drops under |low_speed_limit_bps_| (10
246 // bytes/sec by default) for |low_speed_time_seconds_| (90 seconds,
247 // 180 on non-official builds), reconnect.
248 CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_LOW_SPEED_LIMIT,
249 low_speed_limit_bps_),
Andrew de los Reyes3270f742010-07-15 22:28:14 -0700250 CURLE_OK);
David Zeuthen34135a92013-08-06 11:16:16 -0700251 CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_LOW_SPEED_TIME,
252 low_speed_time_seconds_),
Andrew de los Reyes3270f742010-07-15 22:28:14 -0700253 CURLE_OK);
David Zeuthen34135a92013-08-06 11:16:16 -0700254 CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_CONNECTTIMEOUT,
255 connect_timeout_seconds_),
Andrew de los Reyese72f9c02011-04-20 10:47:40 -0700256 CURLE_OK);
Andrew de los Reyes3270f742010-07-15 22:28:14 -0700257
Darin Petkov41c2fcf2010-08-25 13:14:48 -0700258 // By default, libcurl doesn't follow redirections. Allow up to
David Zeuthen34135a92013-08-06 11:16:16 -0700259 // |kDownloadMaxRedirects| redirections.
Darin Petkov3a4016a2010-09-28 13:54:17 -0700260 CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_FOLLOWLOCATION, 1), CURLE_OK);
David Zeuthen34135a92013-08-06 11:16:16 -0700261 CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_MAXREDIRS,
262 kDownloadMaxRedirects),
Darin Petkov41c2fcf2010-08-25 13:14:48 -0700263 CURLE_OK);
264
Nam T. Nguyen7d623eb2014-05-13 16:06:28 -0700265 // Lock down the appropriate curl options for HTTP or HTTPS depending on
266 // the url.
Alex Deymoc1c17b42015-11-23 03:53:15 -0300267 if (hardware_->IsOfficialBuild()) {
Alex Deymo56ccb072016-02-05 00:50:48 -0800268 if (base::StartsWith(
269 url_, "http://", base::CompareCase::INSENSITIVE_ASCII)) {
Jay Srinivasanb3f55402012-12-03 18:12:04 -0800270 SetCurlOptionsForHttp();
Alex Deymo56ccb072016-02-05 00:50:48 -0800271 } else if (base::StartsWith(
272 url_, "https://", base::CompareCase::INSENSITIVE_ASCII)) {
Jay Srinivasanb3f55402012-12-03 18:12:04 -0800273 SetCurlOptionsForHttps();
Alex Deymo56ccb072016-02-05 00:50:48 -0800274#if !defined(__CHROMEOS__) && !defined(__BRILLO__)
275 } else if (base::StartsWith(
276 url_, "file://", base::CompareCase::INSENSITIVE_ASCII)) {
277 SetCurlOptionsForFile();
278#endif
279 } else {
280 LOG(ERROR) << "Received invalid URI: " << url_;
281 // Lock down to no protocol supported for the transfer.
282 CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_PROTOCOLS, 0), CURLE_OK);
283 }
Jay Srinivasanb3f55402012-12-03 18:12:04 -0800284 } else {
Nam T. Nguyen7d623eb2014-05-13 16:06:28 -0700285 LOG(INFO) << "Not setting http(s) curl options because we are "
286 << "running a dev/test image";
Darin Petkovfc7a0ce2010-10-25 10:38:37 -0700287 }
288
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000289 CHECK_EQ(curl_multi_add_handle(curl_multi_handle_, curl_handle_), CURLM_OK);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000290 transfer_in_progress_ = true;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000291}
292
Jay Srinivasanb3f55402012-12-03 18:12:04 -0800293// Lock down only the protocol in case of HTTP.
294void LibcurlHttpFetcher::SetCurlOptionsForHttp() {
295 LOG(INFO) << "Setting up curl options for HTTP";
296 CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_PROTOCOLS, CURLPROTO_HTTP),
297 CURLE_OK);
298 CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_REDIR_PROTOCOLS,
299 CURLPROTO_HTTP),
300 CURLE_OK);
301}
302
303// Security lock-down in official builds: makes sure that peer certificate
304// verification is enabled, restricts the set of trusted certificates,
305// restricts protocols to HTTPS, restricts ciphers to HIGH.
306void LibcurlHttpFetcher::SetCurlOptionsForHttps() {
307 LOG(INFO) << "Setting up curl options for HTTPS";
308 CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_SSL_VERIFYPEER, 1),
309 CURLE_OK);
Alex Deymo8fd98d82016-06-23 18:22:08 -0700310 CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_SSL_VERIFYHOST, 2),
311 CURLE_OK);
Alex Deymo35b35842015-10-20 11:21:56 -0700312 CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_CAPATH,
313 constants::kCACertificatesPath),
Jay Srinivasanb3f55402012-12-03 18:12:04 -0800314 CURLE_OK);
315 CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_PROTOCOLS, CURLPROTO_HTTPS),
316 CURLE_OK);
317 CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_REDIR_PROTOCOLS,
318 CURLPROTO_HTTPS),
319 CURLE_OK);
320 CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_SSL_CIPHER_LIST, "HIGH:!ADH"),
321 CURLE_OK);
Alex Deymo33e91e72015-12-01 18:26:08 -0300322 if (server_to_check_ != ServerToCheck::kNone) {
323 CHECK_EQ(
324 curl_easy_setopt(curl_handle_, CURLOPT_SSL_CTX_DATA, &server_to_check_),
325 CURLE_OK);
Jay Srinivasanb3f55402012-12-03 18:12:04 -0800326 CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_SSL_CTX_FUNCTION,
327 CertificateChecker::ProcessSSLContext),
328 CURLE_OK);
329 }
330}
331
Alex Deymo56ccb072016-02-05 00:50:48 -0800332// Lock down only the protocol in case of a local file.
333void LibcurlHttpFetcher::SetCurlOptionsForFile() {
334 LOG(INFO) << "Setting up curl options for FILE";
335 CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_PROTOCOLS, CURLPROTO_FILE),
336 CURLE_OK);
337 CHECK_EQ(
338 curl_easy_setopt(curl_handle_, CURLOPT_REDIR_PROTOCOLS, CURLPROTO_FILE),
339 CURLE_OK);
340}
Jay Srinivasanb3f55402012-12-03 18:12:04 -0800341
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000342// Begins the transfer, which must not have already been started.
Alex Deymof329b932014-10-30 01:37:48 -0700343void LibcurlHttpFetcher::BeginTransfer(const string& url) {
Andrew de los Reyesf3ed8e72011-02-16 10:35:46 -0800344 CHECK(!transfer_in_progress_);
345 url_ = url;
Alex Vakulenko4906c1c2014-08-21 13:17:44 -0700346 auto closure = base::Bind(&LibcurlHttpFetcher::ProxiesResolved,
347 base::Unretained(this));
Alex Deymo60ca1a72015-06-18 18:19:15 -0700348 if (!ResolveProxiesForUrl(url_, closure)) {
Andrew de los Reyesf3ed8e72011-02-16 10:35:46 -0800349 LOG(ERROR) << "Couldn't resolve proxies";
350 if (delegate_)
351 delegate_->TransferComplete(this, false);
352 }
353}
354
355void LibcurlHttpFetcher::ProxiesResolved() {
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000356 transfer_size_ = -1;
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000357 resume_offset_ = 0;
Andrew de los Reyes9bbd1872010-07-16 14:52:29 -0700358 retry_count_ = 0;
Darin Petkova0929552010-11-29 14:19:06 -0800359 no_network_retry_count_ = 0;
Darin Petkovcb466212010-08-26 09:40:11 -0700360 http_response_code_ = 0;
Andrew de los Reyes819fef22010-12-17 11:33:58 -0800361 terminate_requested_ = false;
Gilad Arnolda2dee1d2012-04-12 11:50:37 -0700362 sent_byte_ = false;
Alex Deymof2858572016-02-25 11:20:13 -0800363
364 // If we are paused, we delay these two operations until Unpause is called.
365 if (transfer_paused_) {
366 restart_transfer_on_unpause_ = true;
367 return;
368 }
Andrew de los Reyesf3ed8e72011-02-16 10:35:46 -0800369 ResumeTransfer(url_);
Andrew de los Reyes9bbd1872010-07-16 14:52:29 -0700370 CurlPerformOnce();
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000371}
372
Darin Petkov9ce452b2010-11-17 14:33:28 -0800373void LibcurlHttpFetcher::ForceTransferTermination() {
Alex Deymo71f67622017-02-03 21:30:24 -0800374 CancelProxyResolution();
Darin Petkov9ce452b2010-11-17 14:33:28 -0800375 CleanUp();
376 if (delegate_) {
377 // Note that after the callback returns this object may be destroyed.
378 delegate_->TransferTerminated(this);
379 }
380}
381
rspangler@google.com49fdf182009-10-10 00:57:34 +0000382void LibcurlHttpFetcher::TerminateTransfer() {
Darin Petkov9ce452b2010-11-17 14:33:28 -0800383 if (in_write_callback_) {
Andrew de los Reyes3fd5d302010-10-07 20:07:18 -0700384 terminate_requested_ = true;
Darin Petkov9ce452b2010-11-17 14:33:28 -0800385 } else {
386 ForceTransferTermination();
387 }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000388}
389
Alex Deymofdd6dec2016-03-03 22:35:43 -0800390void LibcurlHttpFetcher::SetHeader(const string& header_name,
391 const string& header_value) {
392 string header_line = header_name + ": " + header_value;
393 // Avoid the space if no data on the right side of the semicolon.
394 if (header_value.empty())
395 header_line = header_name + ":";
396 TEST_AND_RETURN(header_line.find('\n') == string::npos);
397 TEST_AND_RETURN(header_name.find(':') == string::npos);
398 extra_headers_[base::ToLowerASCII(header_name)] = header_line;
399}
400
Andrew de los Reyescb319332010-07-19 10:55:01 -0700401void LibcurlHttpFetcher::CurlPerformOnce() {
rspangler@google.com49fdf182009-10-10 00:57:34 +0000402 CHECK(transfer_in_progress_);
403 int running_handles = 0;
404 CURLMcode retcode = CURLM_CALL_MULTI_PERFORM;
405
406 // libcurl may request that we immediately call curl_multi_perform after it
407 // returns, so we do. libcurl promises that curl_multi_perform will not block.
408 while (CURLM_CALL_MULTI_PERFORM == retcode) {
409 retcode = curl_multi_perform(curl_multi_handle_, &running_handles);
Andrew de los Reyes3fd5d302010-10-07 20:07:18 -0700410 if (terminate_requested_) {
Darin Petkov9ce452b2010-11-17 14:33:28 -0800411 ForceTransferTermination();
Andrew de los Reyes3fd5d302010-10-07 20:07:18 -0700412 return;
413 }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000414 }
Alex Deymof2858572016-02-25 11:20:13 -0800415
416 // If the transfer completes while paused, we should ignore the failure once
417 // the fetcher is unpaused.
418 if (running_handles == 0 && transfer_paused_ && !ignore_failure_) {
419 LOG(INFO) << "Connection closed while paused, ignoring failure.";
420 ignore_failure_ = true;
421 }
422
423 if (running_handles != 0 || transfer_paused_) {
424 // There's either more work to do or we are paused, so we just keep the
425 // file descriptors to watch up to date and exit, until we are done with the
426 // work and we are not paused.
427 SetupMessageLoopSources();
428 return;
429 }
430
431 // At this point, the transfer was completed in some way (error, connection
432 // closed or download finished).
433
434 GetHttpResponseCode();
435 if (http_response_code_) {
436 LOG(INFO) << "HTTP response code: " << http_response_code_;
437 no_network_retry_count_ = 0;
438 } else {
439 LOG(ERROR) << "Unable to get http response code.";
440 }
441
442 // we're done!
443 CleanUp();
444
445 // TODO(petkov): This temporary code tries to deal with the case where the
446 // update engine performs an update check while the network is not ready
447 // (e.g., right after resume). Longer term, we should check if the network
448 // is online/offline and return an appropriate error code.
449 if (!sent_byte_ &&
450 http_response_code_ == 0 &&
451 no_network_retry_count_ < no_network_max_retries_) {
452 no_network_retry_count_++;
Alex Deymob20de692017-02-05 07:47:37 +0000453 retry_task_id_ = MessageLoop::current()->PostDelayedTask(
Alex Deymof2858572016-02-25 11:20:13 -0800454 FROM_HERE,
455 base::Bind(&LibcurlHttpFetcher::RetryTimeoutCallback,
456 base::Unretained(this)),
457 TimeDelta::FromSeconds(kNoNetworkRetrySeconds));
458 LOG(INFO) << "No HTTP response, retry " << no_network_retry_count_;
459 } else if ((!sent_byte_ && !IsHttpResponseSuccess()) ||
460 IsHttpResponseError()) {
461 // The transfer completed w/ error and we didn't get any bytes.
462 // If we have another proxy to try, try that.
463 //
464 // TODO(garnold) in fact there are two separate cases here: one case is an
465 // other-than-success return code (including no return code) and no
466 // received bytes, which is necessary due to the way callbacks are
467 // currently processing error conditions; the second is an explicit HTTP
468 // error code, where some data may have been received (as in the case of a
469 // semi-successful multi-chunk fetch). This is a confusing behavior and
470 // should be unified into a complete, coherent interface.
471 LOG(INFO) << "Transfer resulted in an error (" << http_response_code_
472 << "), " << bytes_downloaded_ << " bytes downloaded";
473
474 PopProxy(); // Delete the proxy we just gave up on.
475
476 if (HasProxy()) {
477 // We have another proxy. Retry immediately.
478 LOG(INFO) << "Retrying with next proxy setting";
Alex Deymob20de692017-02-05 07:47:37 +0000479 retry_task_id_ = MessageLoop::current()->PostTask(
Alex Deymof2858572016-02-25 11:20:13 -0800480 FROM_HERE,
481 base::Bind(&LibcurlHttpFetcher::RetryTimeoutCallback,
482 base::Unretained(this)));
Andrew de los Reyes9bbd1872010-07-16 14:52:29 -0700483 } else {
Alex Deymof2858572016-02-25 11:20:13 -0800484 // Out of proxies. Give up.
485 LOG(INFO) << "No further proxies, indicating transfer complete";
486 if (delegate_)
487 delegate_->TransferComplete(this, false); // signal fail
Alex Deymo021a45e2016-03-15 13:12:05 -0700488 return;
Andrew de los Reyes9bbd1872010-07-16 14:52:29 -0700489 }
Alex Deymof2858572016-02-25 11:20:13 -0800490 } else if ((transfer_size_ >= 0) && (bytes_downloaded_ < transfer_size_)) {
491 if (!ignore_failure_)
492 retry_count_++;
493 LOG(INFO) << "Transfer interrupted after downloading "
494 << bytes_downloaded_ << " of " << transfer_size_ << " bytes. "
495 << transfer_size_ - bytes_downloaded_ << " bytes remaining "
496 << "after " << retry_count_ << " attempt(s)";
Darin Petkov192ced42010-07-23 16:20:24 -0700497
Alex Deymof2858572016-02-25 11:20:13 -0800498 if (retry_count_ > max_retry_count_) {
499 LOG(INFO) << "Reached max attempts (" << retry_count_ << ")";
500 if (delegate_)
501 delegate_->TransferComplete(this, false); // signal fail
Alex Deymo021a45e2016-03-15 13:12:05 -0700502 return;
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000503 }
Alex Deymo021a45e2016-03-15 13:12:05 -0700504 // Need to restart transfer
505 LOG(INFO) << "Restarting transfer to download the remaining bytes";
Alex Deymob20de692017-02-05 07:47:37 +0000506 retry_task_id_ = MessageLoop::current()->PostDelayedTask(
Alex Deymo021a45e2016-03-15 13:12:05 -0700507 FROM_HERE,
508 base::Bind(&LibcurlHttpFetcher::RetryTimeoutCallback,
509 base::Unretained(this)),
510 TimeDelta::FromSeconds(retry_seconds_));
rspangler@google.com49fdf182009-10-10 00:57:34 +0000511 } else {
Alex Deymof2858572016-02-25 11:20:13 -0800512 LOG(INFO) << "Transfer completed (" << http_response_code_
513 << "), " << bytes_downloaded_ << " bytes downloaded";
514 if (delegate_) {
515 bool success = IsHttpResponseSuccess();
516 delegate_->TransferComplete(this, success);
517 }
Alex Deymo021a45e2016-03-15 13:12:05 -0700518 return;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000519 }
Alex Deymo021a45e2016-03-15 13:12:05 -0700520 // If we reach this point is because TransferComplete() was not called in any
521 // of the previous branches. The delegate is allowed to destroy the object
522 // once TransferComplete is called so this would be illegal.
Alex Deymof2858572016-02-25 11:20:13 -0800523 ignore_failure_ = false;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000524}
525
526size_t LibcurlHttpFetcher::LibcurlWrite(void *ptr, size_t size, size_t nmemb) {
Gilad Arnold48085ba2011-11-16 09:36:08 -0800527 // Update HTTP response first.
Andrew de los Reyes3fd5d302010-10-07 20:07:18 -0700528 GetHttpResponseCode();
Gilad Arnold48085ba2011-11-16 09:36:08 -0800529 const size_t payload_size = size * nmemb;
530
531 // Do nothing if no payload or HTTP response is an error.
Gilad Arnold9bedeb52011-11-17 16:19:57 -0800532 if (payload_size == 0 || !IsHttpResponseSuccess()) {
Gilad Arnold48085ba2011-11-16 09:36:08 -0800533 LOG(INFO) << "HTTP response unsuccessful (" << http_response_code_
534 << ") or no payload (" << payload_size << "), nothing to do";
535 return 0;
536 }
537
538 sent_byte_ = true;
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000539 {
540 double transfer_size_double;
541 CHECK_EQ(curl_easy_getinfo(curl_handle_,
542 CURLINFO_CONTENT_LENGTH_DOWNLOAD,
543 &transfer_size_double), CURLE_OK);
544 off_t new_transfer_size = static_cast<off_t>(transfer_size_double);
545 if (new_transfer_size > 0) {
546 transfer_size_ = resume_offset_ + new_transfer_size;
547 }
548 }
Gilad Arnold48085ba2011-11-16 09:36:08 -0800549 bytes_downloaded_ += payload_size;
Andrew de los Reyes3fd5d302010-10-07 20:07:18 -0700550 in_write_callback_ = true;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000551 if (delegate_)
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800552 delegate_->ReceivedBytes(this, ptr, payload_size);
Andrew de los Reyes3fd5d302010-10-07 20:07:18 -0700553 in_write_callback_ = false;
Gilad Arnold48085ba2011-11-16 09:36:08 -0800554 return payload_size;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000555}
556
557void LibcurlHttpFetcher::Pause() {
Alex Deymof2858572016-02-25 11:20:13 -0800558 if (transfer_paused_) {
559 LOG(ERROR) << "Fetcher already paused.";
560 return;
561 }
562 transfer_paused_ = true;
563 if (!transfer_in_progress_) {
564 // If pause before we started a connection, we don't need to notify curl
565 // about that, we will simply not start the connection later.
566 return;
567 }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000568 CHECK(curl_handle_);
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000569 CHECK_EQ(curl_easy_pause(curl_handle_, CURLPAUSE_ALL), CURLE_OK);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000570}
571
572void LibcurlHttpFetcher::Unpause() {
Alex Deymof2858572016-02-25 11:20:13 -0800573 if (!transfer_paused_) {
574 LOG(ERROR) << "Resume attempted when fetcher not paused.";
575 return;
576 }
577 transfer_paused_ = false;
578 if (restart_transfer_on_unpause_) {
579 restart_transfer_on_unpause_ = false;
580 ResumeTransfer(url_);
581 CurlPerformOnce();
582 return;
583 }
584 if (!transfer_in_progress_) {
585 // If resumed before starting the connection, there's no need to notify
586 // anybody. We will simply start the connection once it is time.
587 return;
588 }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000589 CHECK(curl_handle_);
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000590 CHECK_EQ(curl_easy_pause(curl_handle_, CURLPAUSE_CONT), CURLE_OK);
Alex Deymof2858572016-02-25 11:20:13 -0800591 // Since the transfer is in progress, we need to dispatch a CurlPerformOnce()
592 // now to let the connection continue, otherwise it would be called by the
593 // TimeoutCallback but with a delay.
594 CurlPerformOnce();
rspangler@google.com49fdf182009-10-10 00:57:34 +0000595}
596
Alex Deymo29b81532015-07-09 11:51:49 -0700597// This method sets up callbacks with the MessageLoop.
598void LibcurlHttpFetcher::SetupMessageLoopSources() {
rspangler@google.com49fdf182009-10-10 00:57:34 +0000599 fd_set fd_read;
600 fd_set fd_write;
Darin Petkov60e14152010-10-27 16:57:04 -0700601 fd_set fd_exc;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000602
603 FD_ZERO(&fd_read);
604 FD_ZERO(&fd_write);
Darin Petkov60e14152010-10-27 16:57:04 -0700605 FD_ZERO(&fd_exc);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000606
607 int fd_max = 0;
608
609 // Ask libcurl for the set of file descriptors we should track on its
610 // behalf.
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000611 CHECK_EQ(curl_multi_fdset(curl_multi_handle_, &fd_read, &fd_write,
Darin Petkov60e14152010-10-27 16:57:04 -0700612 &fd_exc, &fd_max), CURLM_OK);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000613
614 // We should iterate through all file descriptors up to libcurl's fd_max or
Darin Petkov60e14152010-10-27 16:57:04 -0700615 // the highest one we're tracking, whichever is larger.
Alex Deymo29b81532015-07-09 11:51:49 -0700616 for (size_t t = 0; t < arraysize(fd_task_maps_); ++t) {
617 if (!fd_task_maps_[t].empty())
618 fd_max = max(fd_max, fd_task_maps_[t].rbegin()->first);
Darin Petkov60e14152010-10-27 16:57:04 -0700619 }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000620
Darin Petkov60e14152010-10-27 16:57:04 -0700621 // For each fd, if we're not tracking it, track it. If we are tracking it, but
622 // libcurl doesn't care about it anymore, stop tracking it. After this loop,
Alex Deymo29b81532015-07-09 11:51:49 -0700623 // there should be exactly as many tasks scheduled in fd_task_maps_[0|1] as
Darin Petkov60e14152010-10-27 16:57:04 -0700624 // there are read/write fds that we're tracking.
625 for (int fd = 0; fd <= fd_max; ++fd) {
626 // Note that fd_exc is unused in the current version of libcurl so is_exc
627 // should always be false.
628 bool is_exc = FD_ISSET(fd, &fd_exc) != 0;
629 bool must_track[2] = {
630 is_exc || (FD_ISSET(fd, &fd_read) != 0), // track 0 -- read
631 is_exc || (FD_ISSET(fd, &fd_write) != 0) // track 1 -- write
632 };
Alex Deymo29b81532015-07-09 11:51:49 -0700633 MessageLoop::WatchMode watch_modes[2] = {
634 MessageLoop::WatchMode::kWatchRead,
635 MessageLoop::WatchMode::kWatchWrite,
636 };
Darin Petkov60e14152010-10-27 16:57:04 -0700637
Alex Deymo29b81532015-07-09 11:51:49 -0700638 for (size_t t = 0; t < arraysize(fd_task_maps_); ++t) {
639 auto fd_task_it = fd_task_maps_[t].find(fd);
640 bool tracked = fd_task_it != fd_task_maps_[t].end();
Darin Petkov60e14152010-10-27 16:57:04 -0700641
642 if (!must_track[t]) {
643 // If we have an outstanding io_channel, remove it.
644 if (tracked) {
Alex Deymo29b81532015-07-09 11:51:49 -0700645 MessageLoop::current()->CancelTask(fd_task_it->second);
646 fd_task_maps_[t].erase(fd_task_it);
Darin Petkov60e14152010-10-27 16:57:04 -0700647 }
648 continue;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000649 }
Darin Petkov60e14152010-10-27 16:57:04 -0700650
651 // If we are already tracking this fd, continue -- nothing to do.
652 if (tracked)
653 continue;
654
Darin Petkov60e14152010-10-27 16:57:04 -0700655 // Track a new fd.
Alex Deymo29b81532015-07-09 11:51:49 -0700656 fd_task_maps_[t][fd] = MessageLoop::current()->WatchFileDescriptor(
657 FROM_HERE,
658 fd,
659 watch_modes[t],
660 true, // persistent
661 base::Bind(&LibcurlHttpFetcher::CurlPerformOnce,
662 base::Unretained(this)));
Darin Petkov60e14152010-10-27 16:57:04 -0700663
Darin Petkov60e14152010-10-27 16:57:04 -0700664 static int io_counter = 0;
665 io_counter++;
666 if (io_counter % 50 == 0) {
667 LOG(INFO) << "io_counter = " << io_counter;
668 }
Andrew de los Reyes3270f742010-07-15 22:28:14 -0700669 }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000670 }
671
Darin Petkovb83371f2010-08-17 09:34:49 -0700672 // Set up a timeout callback for libcurl.
Alex Deymo60ca1a72015-06-18 18:19:15 -0700673 if (timeout_id_ == MessageLoop::kTaskIdNull) {
Alex Deymof2858572016-02-25 11:20:13 -0800674 VLOG(1) << "Setting up timeout source: " << idle_seconds_ << " seconds.";
Alex Deymo60ca1a72015-06-18 18:19:15 -0700675 timeout_id_ = MessageLoop::current()->PostDelayedTask(
676 FROM_HERE,
677 base::Bind(&LibcurlHttpFetcher::TimeoutCallback,
678 base::Unretained(this)),
679 TimeDelta::FromSeconds(idle_seconds_));
rspangler@google.com49fdf182009-10-10 00:57:34 +0000680 }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000681}
682
Alex Deymo60ca1a72015-06-18 18:19:15 -0700683void LibcurlHttpFetcher::RetryTimeoutCallback() {
Alex Deymob20de692017-02-05 07:47:37 +0000684 retry_task_id_ = MessageLoop::kTaskIdNull;
Alex Deymof2858572016-02-25 11:20:13 -0800685 if (transfer_paused_) {
686 restart_transfer_on_unpause_ = true;
687 return;
688 }
Andrew de los Reyes9bbd1872010-07-16 14:52:29 -0700689 ResumeTransfer(url_);
690 CurlPerformOnce();
Andrew de los Reyes9bbd1872010-07-16 14:52:29 -0700691}
692
Alex Deymo60ca1a72015-06-18 18:19:15 -0700693void LibcurlHttpFetcher::TimeoutCallback() {
Alex Deymo60ca1a72015-06-18 18:19:15 -0700694 // We always re-schedule the callback, even if we don't want to be called
695 // anymore. We will remove the event source separately if we don't want to
Andrew de los Reyescb319332010-07-19 10:55:01 -0700696 // be called back.
Alex Deymo60ca1a72015-06-18 18:19:15 -0700697 timeout_id_ = MessageLoop::current()->PostDelayedTask(
698 FROM_HERE,
699 base::Bind(&LibcurlHttpFetcher::TimeoutCallback, base::Unretained(this)),
700 TimeDelta::FromSeconds(idle_seconds_));
Alex Deymof123ae22015-09-24 14:59:43 -0700701
702 // CurlPerformOnce() may call CleanUp(), so we need to schedule our callback
703 // first, since it could be canceled by this call.
704 if (transfer_in_progress_)
705 CurlPerformOnce();
rspangler@google.com49fdf182009-10-10 00:57:34 +0000706}
707
708void LibcurlHttpFetcher::CleanUp() {
Alex Deymob20de692017-02-05 07:47:37 +0000709 MessageLoop::current()->CancelTask(retry_task_id_);
710 retry_task_id_ = MessageLoop::kTaskIdNull;
711
Alex Deymo60ca1a72015-06-18 18:19:15 -0700712 MessageLoop::current()->CancelTask(timeout_id_);
713 timeout_id_ = MessageLoop::kTaskIdNull;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000714
Alex Deymo29b81532015-07-09 11:51:49 -0700715 for (size_t t = 0; t < arraysize(fd_task_maps_); ++t) {
716 for (const auto& fd_taks_pair : fd_task_maps_[t]) {
717 if (!MessageLoop::current()->CancelTask(fd_taks_pair.second)) {
718 LOG(WARNING) << "Error canceling the watch task "
719 << fd_taks_pair.second << " for "
720 << (t ? "writing" : "reading") << " the fd "
721 << fd_taks_pair.first;
722 }
Darin Petkov60e14152010-10-27 16:57:04 -0700723 }
Alex Deymo29b81532015-07-09 11:51:49 -0700724 fd_task_maps_[t].clear();
rspangler@google.com49fdf182009-10-10 00:57:34 +0000725 }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000726
Gilad Arnold9dd1e7c2012-02-16 12:13:36 -0800727 if (curl_http_headers_) {
728 curl_slist_free_all(curl_http_headers_);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700729 curl_http_headers_ = nullptr;
Gilad Arnold9dd1e7c2012-02-16 12:13:36 -0800730 }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000731 if (curl_handle_) {
732 if (curl_multi_handle_) {
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000733 CHECK_EQ(curl_multi_remove_handle(curl_multi_handle_, curl_handle_),
734 CURLM_OK);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000735 }
736 curl_easy_cleanup(curl_handle_);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700737 curl_handle_ = nullptr;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000738 }
739 if (curl_multi_handle_) {
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000740 CHECK_EQ(curl_multi_cleanup(curl_multi_handle_), CURLM_OK);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700741 curl_multi_handle_ = nullptr;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000742 }
743 transfer_in_progress_ = false;
Alex Deymof2858572016-02-25 11:20:13 -0800744 transfer_paused_ = false;
745 restart_transfer_on_unpause_ = false;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000746}
747
Andrew de los Reyes3fd5d302010-10-07 20:07:18 -0700748void LibcurlHttpFetcher::GetHttpResponseCode() {
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700749 long http_response_code = 0; // NOLINT(runtime/int) - curl needs long.
Alex Deymo56ccb072016-02-05 00:50:48 -0800750 if (base::StartsWith(url_, "file://", base::CompareCase::INSENSITIVE_ASCII)) {
751 // Fake out a valid response code for file:// URLs.
752 http_response_code_ = 299;
753 } else if (curl_easy_getinfo(curl_handle_,
754 CURLINFO_RESPONSE_CODE,
755 &http_response_code) == CURLE_OK) {
Andrew de los Reyes3fd5d302010-10-07 20:07:18 -0700756 http_response_code_ = static_cast<int>(http_response_code);
757 }
758}
759
rspangler@google.com49fdf182009-10-10 00:57:34 +0000760} // namespace chromeos_update_engine