blob: cdc7523f2d3c17e743d17f83e2812b5165367c50 [file] [log] [blame]
rspangler@google.com49fdf182009-10-10 00:57:34 +00001// Copyright (c) 2009 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
rspangler@google.com49fdf182009-10-10 00:57:34 +00005#include "update_engine/libcurl_http_fetcher.h"
Andrew de los Reyesd57d1472010-10-21 13:34:08 -07006
adlr@google.comc98a7ed2009-12-04 18:54:03 +00007#include <algorithm>
Andrew de los Reyesd57d1472010-10-21 13:34:08 -07008#include <string>
9
10#include <base/logging.h>
11
Andrew de los Reyes45168102010-11-22 11:13:50 -080012#include "update_engine/chrome_proxy_resolver.h"
Andrew de los Reyesd57d1472010-10-21 13:34:08 -070013#include "update_engine/dbus_interface.h"
14#include "update_engine/flimflam_proxy.h"
15#include "update_engine/utils.h"
adlr@google.comc98a7ed2009-12-04 18:54:03 +000016
17using std::max;
18using std::make_pair;
Andrew de los Reyesd57d1472010-10-21 13:34:08 -070019using std::string;
rspangler@google.com49fdf182009-10-10 00:57:34 +000020
21// This is a concrete implementation of HttpFetcher that uses libcurl to do the
22// http work.
23
24namespace chromeos_update_engine {
25
Andrew de los Reyes9bbd1872010-07-16 14:52:29 -070026namespace {
27const int kMaxRetriesCount = 20;
Andrew de los Reyes5d0783d2010-11-29 18:14:16 -080028const int kNoNetworkRetrySeconds = 10;
Ken Mixterb2bf1222010-11-18 17:29:38 -080029const char kCACertificatesPath[] = "/usr/share/chromeos-ca-certificates";
Andrew de los Reyesd57d1472010-10-21 13:34:08 -070030} // namespace {}
Andrew de los Reyes9bbd1872010-07-16 14:52:29 -070031
rspangler@google.com49fdf182009-10-10 00:57:34 +000032LibcurlHttpFetcher::~LibcurlHttpFetcher() {
Darin Petkov9ce452b2010-11-17 14:33:28 -080033 LOG_IF(ERROR, transfer_in_progress_)
34 << "Destroying the fetcher while a transfer is in progress.";
rspangler@google.com49fdf182009-10-10 00:57:34 +000035 CleanUp();
36}
37
Andrew de los Reyesd57d1472010-10-21 13:34:08 -070038// On error, returns false.
39bool LibcurlHttpFetcher::ConnectionIsExpensive() const {
40 if (force_connection_type_)
41 return forced_expensive_connection_;
42 NetworkConnectionType type;
43 ConcreteDbusGlib dbus_iface;
44 TEST_AND_RETURN_FALSE(FlimFlamProxy::GetConnectionType(&dbus_iface, &type));
45 LOG(INFO) << "We are connected via "
46 << FlimFlamProxy::StringForConnectionType(type);
47 return FlimFlamProxy::IsExpensiveConnectionType(type);
48}
49
Darin Petkovfc7a0ce2010-10-25 10:38:37 -070050bool LibcurlHttpFetcher::IsOfficialBuild() const {
51 return force_build_type_ ? forced_official_build_ : utils::IsOfficialBuild();
52}
53
adlr@google.comc98a7ed2009-12-04 18:54:03 +000054void LibcurlHttpFetcher::ResumeTransfer(const std::string& url) {
Andrew de los Reyes3270f742010-07-15 22:28:14 -070055 LOG(INFO) << "Starting/Resuming transfer";
rspangler@google.com49fdf182009-10-10 00:57:34 +000056 CHECK(!transfer_in_progress_);
57 url_ = url;
58 curl_multi_handle_ = curl_multi_init();
59 CHECK(curl_multi_handle_);
60
61 curl_handle_ = curl_easy_init();
62 CHECK(curl_handle_);
63
Andrew de los Reyes45168102010-11-22 11:13:50 -080064 CHECK(HasProxy());
65 LOG(INFO) << "Using proxy: " << GetCurrentProxy();
66 if (GetCurrentProxy() == kNoProxy) {
67 CHECK_EQ(curl_easy_setopt(curl_handle_,
68 CURLOPT_PROXY,
69 ""), CURLE_OK);
70 } else {
71 CHECK_EQ(curl_easy_setopt(curl_handle_,
72 CURLOPT_PROXY,
73 GetCurrentProxy().c_str()), CURLE_OK);
74 // Curl seems to require us to set the protocol
75 curl_proxytype type;
76 if (ChromeProxyResolver::GetProxyType(GetCurrentProxy(), &type)) {
77 CHECK_EQ(curl_easy_setopt(curl_handle_,
78 CURLOPT_PROXYTYPE,
79 type), CURLE_OK);
80 }
81 }
82
rspangler@google.com49fdf182009-10-10 00:57:34 +000083 if (post_data_set_) {
adlr@google.comc98a7ed2009-12-04 18:54:03 +000084 CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_POST, 1), CURLE_OK);
85 CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_POSTFIELDS,
86 &post_data_[0]),
87 CURLE_OK);
88 CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_POSTFIELDSIZE,
89 post_data_.size()),
90 CURLE_OK);
rspangler@google.com49fdf182009-10-10 00:57:34 +000091 }
92
adlr@google.comc98a7ed2009-12-04 18:54:03 +000093 if (bytes_downloaded_ > 0) {
94 // Resume from where we left off
95 resume_offset_ = bytes_downloaded_;
96 CHECK_EQ(curl_easy_setopt(curl_handle_,
97 CURLOPT_RESUME_FROM_LARGE,
98 bytes_downloaded_), CURLE_OK);
99 }
100
101 CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_WRITEDATA, this), CURLE_OK);
102 CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_WRITEFUNCTION,
103 StaticLibcurlWrite), CURLE_OK);
Andrew de los Reyesd57d1472010-10-21 13:34:08 -0700104
105 string url_to_use(url_);
106 if (ConnectionIsExpensive()) {
107 LOG(INFO) << "Not initiating HTTP connection b/c we are on an expensive"
108 << " connection";
109 url_to_use = ""; // Sabotage the URL
110 }
111
Darin Petkovfc7a0ce2010-10-25 10:38:37 -0700112 CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_URL, url_to_use.c_str()),
Andrew de los Reyesd57d1472010-10-21 13:34:08 -0700113 CURLE_OK);
Andrew de los Reyes3270f742010-07-15 22:28:14 -0700114
Darin Petkov192ced42010-07-23 16:20:24 -0700115 // If the connection drops under 10 bytes/sec for 3 minutes, reconnect.
Andrew de los Reyes3270f742010-07-15 22:28:14 -0700116 CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_LOW_SPEED_LIMIT, 10),
117 CURLE_OK);
Darin Petkov192ced42010-07-23 16:20:24 -0700118 CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_LOW_SPEED_TIME, 3 * 60),
Andrew de los Reyes3270f742010-07-15 22:28:14 -0700119 CURLE_OK);
120
Darin Petkov41c2fcf2010-08-25 13:14:48 -0700121 // By default, libcurl doesn't follow redirections. Allow up to
122 // |kMaxRedirects| redirections.
Darin Petkov3a4016a2010-09-28 13:54:17 -0700123 CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_FOLLOWLOCATION, 1), CURLE_OK);
Darin Petkov41c2fcf2010-08-25 13:14:48 -0700124 CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_MAXREDIRS, kMaxRedirects),
125 CURLE_OK);
126
Darin Petkove237d192010-11-16 10:26:08 -0800127 // Security lock-down in official builds: makes sure that peer certificate
128 // verification is enabled, restricts the set of trusted certificates,
129 // restricts protocols to HTTPS, restricts ciphers to HIGH.
Darin Petkovfc7a0ce2010-10-25 10:38:37 -0700130 if (IsOfficialBuild()) {
Darin Petkove237d192010-11-16 10:26:08 -0800131 CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_SSL_VERIFYPEER, 1),
132 CURLE_OK);
133 CHECK_EQ(curl_easy_setopt(curl_handle_,
134 CURLOPT_CAPATH,
135 kCACertificatesPath),
136 CURLE_OK);
Darin Petkovfc7a0ce2010-10-25 10:38:37 -0700137 CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_PROTOCOLS, CURLPROTO_HTTPS),
138 CURLE_OK);
139 CHECK_EQ(curl_easy_setopt(curl_handle_,
140 CURLOPT_REDIR_PROTOCOLS,
141 CURLPROTO_HTTPS),
142 CURLE_OK);
Andrew de los Reyes33676192010-11-29 19:41:39 -0800143 CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_SSL_CIPHER_LIST,
144 "HIGH:!ADH"),
Darin Petkove237d192010-11-16 10:26:08 -0800145 CURLE_OK);
Darin Petkovfc7a0ce2010-10-25 10:38:37 -0700146 }
147
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000148 CHECK_EQ(curl_multi_add_handle(curl_multi_handle_, curl_handle_), CURLM_OK);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000149 transfer_in_progress_ = true;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000150}
151
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000152// Begins the transfer, which must not have already been started.
153void LibcurlHttpFetcher::BeginTransfer(const std::string& url) {
154 transfer_size_ = -1;
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000155 resume_offset_ = 0;
Andrew de los Reyes9bbd1872010-07-16 14:52:29 -0700156 retry_count_ = 0;
Darin Petkova0929552010-11-29 14:19:06 -0800157 no_network_retry_count_ = 0;
Darin Petkovcb466212010-08-26 09:40:11 -0700158 http_response_code_ = 0;
Andrew de los Reyes45168102010-11-22 11:13:50 -0800159 ResolveProxiesForUrl(url);
Andrew de los Reyes9bbd1872010-07-16 14:52:29 -0700160 ResumeTransfer(url);
161 CurlPerformOnce();
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000162}
163
Darin Petkov9ce452b2010-11-17 14:33:28 -0800164void LibcurlHttpFetcher::ForceTransferTermination() {
165 CleanUp();
166 if (delegate_) {
167 // Note that after the callback returns this object may be destroyed.
168 delegate_->TransferTerminated(this);
169 }
170}
171
rspangler@google.com49fdf182009-10-10 00:57:34 +0000172void LibcurlHttpFetcher::TerminateTransfer() {
Darin Petkov9ce452b2010-11-17 14:33:28 -0800173 if (in_write_callback_) {
Andrew de los Reyes3fd5d302010-10-07 20:07:18 -0700174 terminate_requested_ = true;
Darin Petkov9ce452b2010-11-17 14:33:28 -0800175 } else {
176 ForceTransferTermination();
177 }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000178}
179
Andrew de los Reyescb319332010-07-19 10:55:01 -0700180void LibcurlHttpFetcher::CurlPerformOnce() {
rspangler@google.com49fdf182009-10-10 00:57:34 +0000181 CHECK(transfer_in_progress_);
182 int running_handles = 0;
183 CURLMcode retcode = CURLM_CALL_MULTI_PERFORM;
184
185 // libcurl may request that we immediately call curl_multi_perform after it
186 // returns, so we do. libcurl promises that curl_multi_perform will not block.
187 while (CURLM_CALL_MULTI_PERFORM == retcode) {
188 retcode = curl_multi_perform(curl_multi_handle_, &running_handles);
Andrew de los Reyes3fd5d302010-10-07 20:07:18 -0700189 if (terminate_requested_) {
Darin Petkov9ce452b2010-11-17 14:33:28 -0800190 ForceTransferTermination();
Andrew de los Reyes3fd5d302010-10-07 20:07:18 -0700191 return;
192 }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000193 }
194 if (0 == running_handles) {
Andrew de los Reyes3fd5d302010-10-07 20:07:18 -0700195 GetHttpResponseCode();
196 if (http_response_code_) {
197 LOG(INFO) << "HTTP response code: " << http_response_code_;
Darin Petkova0929552010-11-29 14:19:06 -0800198 no_network_retry_count_ = 0;
Andrew de los Reyes9bbd1872010-07-16 14:52:29 -0700199 } else {
200 LOG(ERROR) << "Unable to get http response code.";
201 }
Darin Petkov192ced42010-07-23 16:20:24 -0700202
rspangler@google.com49fdf182009-10-10 00:57:34 +0000203 // we're done!
204 CleanUp();
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000205
Darin Petkova0929552010-11-29 14:19:06 -0800206 // TODO(petkov): This temporary code tries to deal with the case where the
207 // update engine performs an update check while the network is not ready
208 // (e.g., right after resume). Longer term, we should check if the network
209 // is online/offline and return an appropriate error code.
210 if (!sent_byte_ &&
211 http_response_code_ == 0 &&
212 no_network_retry_count_ < no_network_max_retries_) {
213 no_network_retry_count_++;
214 g_timeout_add_seconds(kNoNetworkRetrySeconds,
215 &LibcurlHttpFetcher::StaticRetryTimeoutCallback,
216 this);
217 LOG(INFO) << "No HTTP response, retry " << no_network_retry_count_;
218 return;
219 }
220
Andrew de los Reyes45168102010-11-22 11:13:50 -0800221 if (!sent_byte_ &&
222 (http_response_code_ < 200 || http_response_code_ >= 300)) {
223 // The transfer completed w/ error and we didn't get any bytes.
224 // If we have another proxy to try, try that.
225
226 PopProxy(); // Delete the proxy we just gave up on.
227
228 if (HasProxy()) {
229 // We have another proxy. Retry immediately.
230 g_idle_add(&LibcurlHttpFetcher::StaticRetryTimeoutCallback, this);
231 } else {
232 // Out of proxies. Give up.
233 if (delegate_)
234 delegate_->TransferComplete(this, false); // success
235 }
236 return;
237 }
238
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000239 if ((transfer_size_ >= 0) && (bytes_downloaded_ < transfer_size_)) {
Andrew de los Reyes3270f742010-07-15 22:28:14 -0700240 // Need to restart transfer
Andrew de los Reyes9bbd1872010-07-16 14:52:29 -0700241 retry_count_++;
242 LOG(INFO) << "Restarting transfer b/c we finished, had downloaded "
243 << bytes_downloaded_ << " bytes, but transfer_size_ is "
244 << transfer_size_ << ". retry_count: " << retry_count_;
245 if (retry_count_ > kMaxRetriesCount) {
246 if (delegate_)
247 delegate_->TransferComplete(this, false); // success
248 } else {
Darin Petkovb83371f2010-08-17 09:34:49 -0700249 g_timeout_add_seconds(retry_seconds_,
Darin Petkov9b111652010-08-16 11:46:25 -0700250 &LibcurlHttpFetcher::StaticRetryTimeoutCallback,
251 this);
Andrew de los Reyes9bbd1872010-07-16 14:52:29 -0700252 }
Andrew de los Reyescb319332010-07-19 10:55:01 -0700253 return;
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000254 } else {
255 if (delegate_) {
Andrew de los Reyesfb4ad7d2010-07-19 10:43:46 -0700256 // success is when http_response_code is 2xx
Andrew de los Reyes3fd5d302010-10-07 20:07:18 -0700257 bool success = (http_response_code_ >= 200) &&
258 (http_response_code_ < 300);
Andrew de los Reyesfb4ad7d2010-07-19 10:43:46 -0700259 delegate_->TransferComplete(this, success);
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000260 }
261 }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000262 } else {
263 // set up callback
264 SetupMainloopSources();
265 }
266}
267
268size_t LibcurlHttpFetcher::LibcurlWrite(void *ptr, size_t size, size_t nmemb) {
Andrew de los Reyes45168102010-11-22 11:13:50 -0800269 if (size == 0)
270 return 0;
271 sent_byte_ = true;
Andrew de los Reyes3fd5d302010-10-07 20:07:18 -0700272 GetHttpResponseCode();
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000273 {
274 double transfer_size_double;
275 CHECK_EQ(curl_easy_getinfo(curl_handle_,
276 CURLINFO_CONTENT_LENGTH_DOWNLOAD,
277 &transfer_size_double), CURLE_OK);
278 off_t new_transfer_size = static_cast<off_t>(transfer_size_double);
279 if (new_transfer_size > 0) {
280 transfer_size_ = resume_offset_ + new_transfer_size;
281 }
282 }
283 bytes_downloaded_ += size * nmemb;
Andrew de los Reyes3fd5d302010-10-07 20:07:18 -0700284 in_write_callback_ = true;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000285 if (delegate_)
286 delegate_->ReceivedBytes(this, reinterpret_cast<char*>(ptr), size * nmemb);
Andrew de los Reyes3fd5d302010-10-07 20:07:18 -0700287 in_write_callback_ = false;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000288 return size * nmemb;
289}
290
291void LibcurlHttpFetcher::Pause() {
292 CHECK(curl_handle_);
293 CHECK(transfer_in_progress_);
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000294 CHECK_EQ(curl_easy_pause(curl_handle_, CURLPAUSE_ALL), CURLE_OK);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000295}
296
297void LibcurlHttpFetcher::Unpause() {
298 CHECK(curl_handle_);
299 CHECK(transfer_in_progress_);
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000300 CHECK_EQ(curl_easy_pause(curl_handle_, CURLPAUSE_CONT), CURLE_OK);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000301}
302
303// This method sets up callbacks with the glib main loop.
304void LibcurlHttpFetcher::SetupMainloopSources() {
305 fd_set fd_read;
306 fd_set fd_write;
Darin Petkov60e14152010-10-27 16:57:04 -0700307 fd_set fd_exc;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000308
309 FD_ZERO(&fd_read);
310 FD_ZERO(&fd_write);
Darin Petkov60e14152010-10-27 16:57:04 -0700311 FD_ZERO(&fd_exc);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000312
313 int fd_max = 0;
314
315 // Ask libcurl for the set of file descriptors we should track on its
316 // behalf.
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000317 CHECK_EQ(curl_multi_fdset(curl_multi_handle_, &fd_read, &fd_write,
Darin Petkov60e14152010-10-27 16:57:04 -0700318 &fd_exc, &fd_max), CURLM_OK);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000319
320 // We should iterate through all file descriptors up to libcurl's fd_max or
Darin Petkov60e14152010-10-27 16:57:04 -0700321 // the highest one we're tracking, whichever is larger.
322 for (size_t t = 0; t < arraysize(io_channels_); ++t) {
323 if (!io_channels_[t].empty())
324 fd_max = max(fd_max, io_channels_[t].rbegin()->first);
325 }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000326
Darin Petkov60e14152010-10-27 16:57:04 -0700327 // For each fd, if we're not tracking it, track it. If we are tracking it, but
328 // libcurl doesn't care about it anymore, stop tracking it. After this loop,
329 // there should be exactly as many GIOChannel objects in io_channels_[0|1] as
330 // there are read/write fds that we're tracking.
331 for (int fd = 0; fd <= fd_max; ++fd) {
332 // Note that fd_exc is unused in the current version of libcurl so is_exc
333 // should always be false.
334 bool is_exc = FD_ISSET(fd, &fd_exc) != 0;
335 bool must_track[2] = {
336 is_exc || (FD_ISSET(fd, &fd_read) != 0), // track 0 -- read
337 is_exc || (FD_ISSET(fd, &fd_write) != 0) // track 1 -- write
338 };
339
340 for (size_t t = 0; t < arraysize(io_channels_); ++t) {
341 bool tracked = io_channels_[t].find(fd) != io_channels_[t].end();
342
343 if (!must_track[t]) {
344 // If we have an outstanding io_channel, remove it.
345 if (tracked) {
346 g_source_remove(io_channels_[t][fd].second);
347 g_io_channel_unref(io_channels_[t][fd].first);
348 io_channels_[t].erase(io_channels_[t].find(fd));
349 }
350 continue;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000351 }
Darin Petkov60e14152010-10-27 16:57:04 -0700352
353 // If we are already tracking this fd, continue -- nothing to do.
354 if (tracked)
355 continue;
356
357 // Set conditions appropriately -- read for track 0, write for track 1.
358 GIOCondition condition = static_cast<GIOCondition>(
359 ((t == 0) ? (G_IO_IN | G_IO_PRI) : G_IO_OUT) | G_IO_ERR | G_IO_HUP);
360
361 // Track a new fd.
362 GIOChannel* io_channel = g_io_channel_unix_new(fd);
363 guint tag =
364 g_io_add_watch(io_channel, condition, &StaticFDCallback, this);
365
366 io_channels_[t][fd] = make_pair(io_channel, tag);
367 static int io_counter = 0;
368 io_counter++;
369 if (io_counter % 50 == 0) {
370 LOG(INFO) << "io_counter = " << io_counter;
371 }
Andrew de los Reyes3270f742010-07-15 22:28:14 -0700372 }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000373 }
374
Darin Petkovb83371f2010-08-17 09:34:49 -0700375 // Set up a timeout callback for libcurl.
Andrew de los Reyes3270f742010-07-15 22:28:14 -0700376 if (!timeout_source_) {
Darin Petkovb83371f2010-08-17 09:34:49 -0700377 LOG(INFO) << "Setting up timeout source: " << idle_seconds_ << " seconds.";
378 timeout_source_ = g_timeout_source_new_seconds(idle_seconds_);
379 g_source_set_callback(timeout_source_, StaticTimeoutCallback, this, NULL);
Andrew de los Reyes3270f742010-07-15 22:28:14 -0700380 g_source_attach(timeout_source_, NULL);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000381 }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000382}
383
384bool LibcurlHttpFetcher::FDCallback(GIOChannel *source,
385 GIOCondition condition) {
Andrew de los Reyes9bbd1872010-07-16 14:52:29 -0700386 CurlPerformOnce();
Andrew de los Reyes3270f742010-07-15 22:28:14 -0700387 // We handle removing of this source elsewhere, so we always return true.
388 // The docs say, "the function should return FALSE if the event source
389 // should be removed."
390 // http://www.gtk.org/api/2.6/glib/glib-IO-Channels.html#GIOFunc
391 return true;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000392}
393
Andrew de los Reyes9bbd1872010-07-16 14:52:29 -0700394gboolean LibcurlHttpFetcher::RetryTimeoutCallback() {
395 ResumeTransfer(url_);
396 CurlPerformOnce();
397 return FALSE; // Don't have glib auto call this callback again
398}
399
Andrew de los Reyes3270f742010-07-15 22:28:14 -0700400gboolean LibcurlHttpFetcher::TimeoutCallback() {
Andrew de los Reyescb319332010-07-19 10:55:01 -0700401 // We always return true, even if we don't want glib to call us back.
402 // We will remove the event source separately if we don't want to
403 // be called back.
Andrew de los Reyes3270f742010-07-15 22:28:14 -0700404 if (!transfer_in_progress_)
405 return TRUE;
Andrew de los Reyes9bbd1872010-07-16 14:52:29 -0700406 CurlPerformOnce();
Andrew de los Reyes3270f742010-07-15 22:28:14 -0700407 return TRUE;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000408}
409
410void LibcurlHttpFetcher::CleanUp() {
411 if (timeout_source_) {
412 g_source_destroy(timeout_source_);
413 timeout_source_ = NULL;
414 }
415
Darin Petkov60e14152010-10-27 16:57:04 -0700416 for (size_t t = 0; t < arraysize(io_channels_); ++t) {
417 for (IOChannels::iterator it = io_channels_[t].begin();
418 it != io_channels_[t].end(); ++it) {
419 g_source_remove(it->second.second);
420 g_io_channel_unref(it->second.first);
421 }
422 io_channels_[t].clear();
rspangler@google.com49fdf182009-10-10 00:57:34 +0000423 }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000424
425 if (curl_handle_) {
426 if (curl_multi_handle_) {
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000427 CHECK_EQ(curl_multi_remove_handle(curl_multi_handle_, curl_handle_),
428 CURLM_OK);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000429 }
430 curl_easy_cleanup(curl_handle_);
431 curl_handle_ = NULL;
432 }
433 if (curl_multi_handle_) {
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000434 CHECK_EQ(curl_multi_cleanup(curl_multi_handle_), CURLM_OK);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000435 curl_multi_handle_ = NULL;
436 }
437 transfer_in_progress_ = false;
438}
439
Andrew de los Reyes3fd5d302010-10-07 20:07:18 -0700440void LibcurlHttpFetcher::GetHttpResponseCode() {
441 long http_response_code = 0;
442 if (curl_easy_getinfo(curl_handle_,
443 CURLINFO_RESPONSE_CODE,
444 &http_response_code) == CURLE_OK) {
445 http_response_code_ = static_cast<int>(http_response_code);
446 }
447}
448
rspangler@google.com49fdf182009-10-10 00:57:34 +0000449} // namespace chromeos_update_engine