blob: af7e8453a32fa7227be60ff928e2b97d25875b50 [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
Andrew de los Reyesf3ed8e72011-02-16 10:35:46 -080017using google::protobuf::NewCallback;
adlr@google.comc98a7ed2009-12-04 18:54:03 +000018using std::max;
19using std::make_pair;
Andrew de los Reyesd57d1472010-10-21 13:34:08 -070020using std::string;
rspangler@google.com49fdf182009-10-10 00:57:34 +000021
22// This is a concrete implementation of HttpFetcher that uses libcurl to do the
23// http work.
24
25namespace chromeos_update_engine {
26
Andrew de los Reyes9bbd1872010-07-16 14:52:29 -070027namespace {
28const int kMaxRetriesCount = 20;
Andrew de los Reyes5d0783d2010-11-29 18:14:16 -080029const int kNoNetworkRetrySeconds = 10;
Ken Mixterb2bf1222010-11-18 17:29:38 -080030const char kCACertificatesPath[] = "/usr/share/chromeos-ca-certificates";
Andrew de los Reyesd57d1472010-10-21 13:34:08 -070031} // namespace {}
Andrew de los Reyes9bbd1872010-07-16 14:52:29 -070032
rspangler@google.com49fdf182009-10-10 00:57:34 +000033LibcurlHttpFetcher::~LibcurlHttpFetcher() {
Darin Petkov9ce452b2010-11-17 14:33:28 -080034 LOG_IF(ERROR, transfer_in_progress_)
35 << "Destroying the fetcher while a transfer is in progress.";
rspangler@google.com49fdf182009-10-10 00:57:34 +000036 CleanUp();
37}
38
Andrew de los Reyesd57d1472010-10-21 13:34:08 -070039// On error, returns false.
40bool LibcurlHttpFetcher::ConnectionIsExpensive() const {
41 if (force_connection_type_)
42 return forced_expensive_connection_;
43 NetworkConnectionType type;
44 ConcreteDbusGlib dbus_iface;
45 TEST_AND_RETURN_FALSE(FlimFlamProxy::GetConnectionType(&dbus_iface, &type));
46 LOG(INFO) << "We are connected via "
47 << FlimFlamProxy::StringForConnectionType(type);
48 return FlimFlamProxy::IsExpensiveConnectionType(type);
49}
50
Darin Petkovfc7a0ce2010-10-25 10:38:37 -070051bool LibcurlHttpFetcher::IsOfficialBuild() const {
52 return force_build_type_ ? forced_official_build_ : utils::IsOfficialBuild();
53}
54
adlr@google.comc98a7ed2009-12-04 18:54:03 +000055void LibcurlHttpFetcher::ResumeTransfer(const std::string& url) {
Andrew de los Reyes3270f742010-07-15 22:28:14 -070056 LOG(INFO) << "Starting/Resuming transfer";
rspangler@google.com49fdf182009-10-10 00:57:34 +000057 CHECK(!transfer_in_progress_);
58 url_ = url;
59 curl_multi_handle_ = curl_multi_init();
60 CHECK(curl_multi_handle_);
61
62 curl_handle_ = curl_easy_init();
63 CHECK(curl_handle_);
64
Andrew de los Reyes45168102010-11-22 11:13:50 -080065 CHECK(HasProxy());
66 LOG(INFO) << "Using proxy: " << GetCurrentProxy();
67 if (GetCurrentProxy() == kNoProxy) {
68 CHECK_EQ(curl_easy_setopt(curl_handle_,
69 CURLOPT_PROXY,
70 ""), CURLE_OK);
71 } else {
72 CHECK_EQ(curl_easy_setopt(curl_handle_,
73 CURLOPT_PROXY,
74 GetCurrentProxy().c_str()), CURLE_OK);
75 // Curl seems to require us to set the protocol
76 curl_proxytype type;
77 if (ChromeProxyResolver::GetProxyType(GetCurrentProxy(), &type)) {
78 CHECK_EQ(curl_easy_setopt(curl_handle_,
79 CURLOPT_PROXYTYPE,
80 type), CURLE_OK);
81 }
82 }
83
rspangler@google.com49fdf182009-10-10 00:57:34 +000084 if (post_data_set_) {
adlr@google.comc98a7ed2009-12-04 18:54:03 +000085 CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_POST, 1), CURLE_OK);
86 CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_POSTFIELDS,
87 &post_data_[0]),
88 CURLE_OK);
89 CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_POSTFIELDSIZE,
90 post_data_.size()),
91 CURLE_OK);
rspangler@google.com49fdf182009-10-10 00:57:34 +000092 }
93
adlr@google.comc98a7ed2009-12-04 18:54:03 +000094 if (bytes_downloaded_ > 0) {
95 // Resume from where we left off
96 resume_offset_ = bytes_downloaded_;
97 CHECK_EQ(curl_easy_setopt(curl_handle_,
98 CURLOPT_RESUME_FROM_LARGE,
99 bytes_downloaded_), CURLE_OK);
100 }
101
102 CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_WRITEDATA, this), CURLE_OK);
103 CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_WRITEFUNCTION,
104 StaticLibcurlWrite), CURLE_OK);
Andrew de los Reyesd57d1472010-10-21 13:34:08 -0700105
106 string url_to_use(url_);
107 if (ConnectionIsExpensive()) {
108 LOG(INFO) << "Not initiating HTTP connection b/c we are on an expensive"
109 << " connection";
110 url_to_use = ""; // Sabotage the URL
111 }
112
Darin Petkovfc7a0ce2010-10-25 10:38:37 -0700113 CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_URL, url_to_use.c_str()),
Andrew de los Reyesd57d1472010-10-21 13:34:08 -0700114 CURLE_OK);
Andrew de los Reyes3270f742010-07-15 22:28:14 -0700115
Darin Petkov192ced42010-07-23 16:20:24 -0700116 // If the connection drops under 10 bytes/sec for 3 minutes, reconnect.
Andrew de los Reyes3270f742010-07-15 22:28:14 -0700117 CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_LOW_SPEED_LIMIT, 10),
118 CURLE_OK);
Andrew de los Reyes2a0fd742011-04-06 11:04:53 -0700119 // Use a smaller timeout on official builds, larger for dev. Dev users
120 // want a longer timeout b/c they may be waiting on the dev server to
121 // build an image.
122 const int kTimeout = IsOfficialBuild() ? 90 : 3 * 60;
123 CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_LOW_SPEED_TIME, kTimeout),
Andrew de los Reyes3270f742010-07-15 22:28:14 -0700124 CURLE_OK);
125
Darin Petkov41c2fcf2010-08-25 13:14:48 -0700126 // By default, libcurl doesn't follow redirections. Allow up to
127 // |kMaxRedirects| redirections.
Darin Petkov3a4016a2010-09-28 13:54:17 -0700128 CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_FOLLOWLOCATION, 1), CURLE_OK);
Darin Petkov41c2fcf2010-08-25 13:14:48 -0700129 CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_MAXREDIRS, kMaxRedirects),
130 CURLE_OK);
131
Darin Petkove237d192010-11-16 10:26:08 -0800132 // Security lock-down in official builds: makes sure that peer certificate
133 // verification is enabled, restricts the set of trusted certificates,
134 // restricts protocols to HTTPS, restricts ciphers to HIGH.
Darin Petkovfc7a0ce2010-10-25 10:38:37 -0700135 if (IsOfficialBuild()) {
Darin Petkove237d192010-11-16 10:26:08 -0800136 CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_SSL_VERIFYPEER, 1),
137 CURLE_OK);
138 CHECK_EQ(curl_easy_setopt(curl_handle_,
139 CURLOPT_CAPATH,
140 kCACertificatesPath),
141 CURLE_OK);
Darin Petkovfc7a0ce2010-10-25 10:38:37 -0700142 CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_PROTOCOLS, CURLPROTO_HTTPS),
143 CURLE_OK);
144 CHECK_EQ(curl_easy_setopt(curl_handle_,
145 CURLOPT_REDIR_PROTOCOLS,
146 CURLPROTO_HTTPS),
147 CURLE_OK);
Andrew de los Reyes33676192010-11-29 19:41:39 -0800148 CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_SSL_CIPHER_LIST,
149 "HIGH:!ADH"),
Darin Petkove237d192010-11-16 10:26:08 -0800150 CURLE_OK);
Darin Petkovfc7a0ce2010-10-25 10:38:37 -0700151 }
152
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000153 CHECK_EQ(curl_multi_add_handle(curl_multi_handle_, curl_handle_), CURLM_OK);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000154 transfer_in_progress_ = true;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000155}
156
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000157// Begins the transfer, which must not have already been started.
158void LibcurlHttpFetcher::BeginTransfer(const std::string& url) {
Andrew de los Reyesf3ed8e72011-02-16 10:35:46 -0800159 CHECK(!transfer_in_progress_);
160 url_ = url;
161 if (!ResolveProxiesForUrl(
162 url_,
163 NewCallback(this, &LibcurlHttpFetcher::ProxiesResolved))) {
164 LOG(ERROR) << "Couldn't resolve proxies";
165 if (delegate_)
166 delegate_->TransferComplete(this, false);
167 }
168}
169
170void LibcurlHttpFetcher::ProxiesResolved() {
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000171 transfer_size_ = -1;
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000172 resume_offset_ = 0;
Andrew de los Reyes9bbd1872010-07-16 14:52:29 -0700173 retry_count_ = 0;
Darin Petkova0929552010-11-29 14:19:06 -0800174 no_network_retry_count_ = 0;
Darin Petkovcb466212010-08-26 09:40:11 -0700175 http_response_code_ = 0;
Andrew de los Reyes819fef22010-12-17 11:33:58 -0800176 terminate_requested_ = false;
Andrew de los Reyesf3ed8e72011-02-16 10:35:46 -0800177 ResumeTransfer(url_);
Andrew de los Reyes9bbd1872010-07-16 14:52:29 -0700178 CurlPerformOnce();
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000179}
180
Darin Petkov9ce452b2010-11-17 14:33:28 -0800181void LibcurlHttpFetcher::ForceTransferTermination() {
182 CleanUp();
183 if (delegate_) {
184 // Note that after the callback returns this object may be destroyed.
185 delegate_->TransferTerminated(this);
186 }
187}
188
rspangler@google.com49fdf182009-10-10 00:57:34 +0000189void LibcurlHttpFetcher::TerminateTransfer() {
Darin Petkov9ce452b2010-11-17 14:33:28 -0800190 if (in_write_callback_) {
Andrew de los Reyes3fd5d302010-10-07 20:07:18 -0700191 terminate_requested_ = true;
Darin Petkov9ce452b2010-11-17 14:33:28 -0800192 } else {
193 ForceTransferTermination();
194 }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000195}
196
Andrew de los Reyescb319332010-07-19 10:55:01 -0700197void LibcurlHttpFetcher::CurlPerformOnce() {
rspangler@google.com49fdf182009-10-10 00:57:34 +0000198 CHECK(transfer_in_progress_);
199 int running_handles = 0;
200 CURLMcode retcode = CURLM_CALL_MULTI_PERFORM;
201
202 // libcurl may request that we immediately call curl_multi_perform after it
203 // returns, so we do. libcurl promises that curl_multi_perform will not block.
204 while (CURLM_CALL_MULTI_PERFORM == retcode) {
205 retcode = curl_multi_perform(curl_multi_handle_, &running_handles);
Andrew de los Reyes3fd5d302010-10-07 20:07:18 -0700206 if (terminate_requested_) {
Darin Petkov9ce452b2010-11-17 14:33:28 -0800207 ForceTransferTermination();
Andrew de los Reyes3fd5d302010-10-07 20:07:18 -0700208 return;
209 }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000210 }
211 if (0 == running_handles) {
Andrew de los Reyes3fd5d302010-10-07 20:07:18 -0700212 GetHttpResponseCode();
213 if (http_response_code_) {
214 LOG(INFO) << "HTTP response code: " << http_response_code_;
Darin Petkova0929552010-11-29 14:19:06 -0800215 no_network_retry_count_ = 0;
Andrew de los Reyes9bbd1872010-07-16 14:52:29 -0700216 } else {
217 LOG(ERROR) << "Unable to get http response code.";
218 }
Darin Petkov192ced42010-07-23 16:20:24 -0700219
rspangler@google.com49fdf182009-10-10 00:57:34 +0000220 // we're done!
221 CleanUp();
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000222
Darin Petkova0929552010-11-29 14:19:06 -0800223 // TODO(petkov): This temporary code tries to deal with the case where the
224 // update engine performs an update check while the network is not ready
225 // (e.g., right after resume). Longer term, we should check if the network
226 // is online/offline and return an appropriate error code.
227 if (!sent_byte_ &&
228 http_response_code_ == 0 &&
229 no_network_retry_count_ < no_network_max_retries_) {
230 no_network_retry_count_++;
231 g_timeout_add_seconds(kNoNetworkRetrySeconds,
232 &LibcurlHttpFetcher::StaticRetryTimeoutCallback,
233 this);
234 LOG(INFO) << "No HTTP response, retry " << no_network_retry_count_;
235 return;
236 }
237
Andrew de los Reyes45168102010-11-22 11:13:50 -0800238 if (!sent_byte_ &&
239 (http_response_code_ < 200 || http_response_code_ >= 300)) {
240 // The transfer completed w/ error and we didn't get any bytes.
241 // If we have another proxy to try, try that.
242
243 PopProxy(); // Delete the proxy we just gave up on.
244
245 if (HasProxy()) {
246 // We have another proxy. Retry immediately.
247 g_idle_add(&LibcurlHttpFetcher::StaticRetryTimeoutCallback, this);
248 } else {
249 // Out of proxies. Give up.
250 if (delegate_)
251 delegate_->TransferComplete(this, false); // success
252 }
253 return;
254 }
255
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000256 if ((transfer_size_ >= 0) && (bytes_downloaded_ < transfer_size_)) {
Andrew de los Reyes3270f742010-07-15 22:28:14 -0700257 // Need to restart transfer
Andrew de los Reyes9bbd1872010-07-16 14:52:29 -0700258 retry_count_++;
259 LOG(INFO) << "Restarting transfer b/c we finished, had downloaded "
260 << bytes_downloaded_ << " bytes, but transfer_size_ is "
261 << transfer_size_ << ". retry_count: " << retry_count_;
262 if (retry_count_ > kMaxRetriesCount) {
263 if (delegate_)
264 delegate_->TransferComplete(this, false); // success
265 } else {
Darin Petkovb83371f2010-08-17 09:34:49 -0700266 g_timeout_add_seconds(retry_seconds_,
Darin Petkov9b111652010-08-16 11:46:25 -0700267 &LibcurlHttpFetcher::StaticRetryTimeoutCallback,
268 this);
Andrew de los Reyes9bbd1872010-07-16 14:52:29 -0700269 }
Andrew de los Reyescb319332010-07-19 10:55:01 -0700270 return;
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000271 } else {
272 if (delegate_) {
Andrew de los Reyesfb4ad7d2010-07-19 10:43:46 -0700273 // success is when http_response_code is 2xx
Andrew de los Reyes3fd5d302010-10-07 20:07:18 -0700274 bool success = (http_response_code_ >= 200) &&
275 (http_response_code_ < 300);
Andrew de los Reyesfb4ad7d2010-07-19 10:43:46 -0700276 delegate_->TransferComplete(this, success);
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000277 }
278 }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000279 } else {
280 // set up callback
281 SetupMainloopSources();
282 }
283}
284
285size_t LibcurlHttpFetcher::LibcurlWrite(void *ptr, size_t size, size_t nmemb) {
Andrew de los Reyes45168102010-11-22 11:13:50 -0800286 if (size == 0)
287 return 0;
288 sent_byte_ = true;
Andrew de los Reyes3fd5d302010-10-07 20:07:18 -0700289 GetHttpResponseCode();
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000290 {
291 double transfer_size_double;
292 CHECK_EQ(curl_easy_getinfo(curl_handle_,
293 CURLINFO_CONTENT_LENGTH_DOWNLOAD,
294 &transfer_size_double), CURLE_OK);
295 off_t new_transfer_size = static_cast<off_t>(transfer_size_double);
296 if (new_transfer_size > 0) {
297 transfer_size_ = resume_offset_ + new_transfer_size;
298 }
299 }
300 bytes_downloaded_ += size * nmemb;
Andrew de los Reyes3fd5d302010-10-07 20:07:18 -0700301 in_write_callback_ = true;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000302 if (delegate_)
303 delegate_->ReceivedBytes(this, reinterpret_cast<char*>(ptr), size * nmemb);
Andrew de los Reyes3fd5d302010-10-07 20:07:18 -0700304 in_write_callback_ = false;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000305 return size * nmemb;
306}
307
308void LibcurlHttpFetcher::Pause() {
309 CHECK(curl_handle_);
310 CHECK(transfer_in_progress_);
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000311 CHECK_EQ(curl_easy_pause(curl_handle_, CURLPAUSE_ALL), CURLE_OK);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000312}
313
314void LibcurlHttpFetcher::Unpause() {
315 CHECK(curl_handle_);
316 CHECK(transfer_in_progress_);
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000317 CHECK_EQ(curl_easy_pause(curl_handle_, CURLPAUSE_CONT), CURLE_OK);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000318}
319
320// This method sets up callbacks with the glib main loop.
321void LibcurlHttpFetcher::SetupMainloopSources() {
322 fd_set fd_read;
323 fd_set fd_write;
Darin Petkov60e14152010-10-27 16:57:04 -0700324 fd_set fd_exc;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000325
326 FD_ZERO(&fd_read);
327 FD_ZERO(&fd_write);
Darin Petkov60e14152010-10-27 16:57:04 -0700328 FD_ZERO(&fd_exc);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000329
330 int fd_max = 0;
331
332 // Ask libcurl for the set of file descriptors we should track on its
333 // behalf.
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000334 CHECK_EQ(curl_multi_fdset(curl_multi_handle_, &fd_read, &fd_write,
Darin Petkov60e14152010-10-27 16:57:04 -0700335 &fd_exc, &fd_max), CURLM_OK);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000336
337 // We should iterate through all file descriptors up to libcurl's fd_max or
Darin Petkov60e14152010-10-27 16:57:04 -0700338 // the highest one we're tracking, whichever is larger.
339 for (size_t t = 0; t < arraysize(io_channels_); ++t) {
340 if (!io_channels_[t].empty())
341 fd_max = max(fd_max, io_channels_[t].rbegin()->first);
342 }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000343
Darin Petkov60e14152010-10-27 16:57:04 -0700344 // For each fd, if we're not tracking it, track it. If we are tracking it, but
345 // libcurl doesn't care about it anymore, stop tracking it. After this loop,
346 // there should be exactly as many GIOChannel objects in io_channels_[0|1] as
347 // there are read/write fds that we're tracking.
348 for (int fd = 0; fd <= fd_max; ++fd) {
349 // Note that fd_exc is unused in the current version of libcurl so is_exc
350 // should always be false.
351 bool is_exc = FD_ISSET(fd, &fd_exc) != 0;
352 bool must_track[2] = {
353 is_exc || (FD_ISSET(fd, &fd_read) != 0), // track 0 -- read
354 is_exc || (FD_ISSET(fd, &fd_write) != 0) // track 1 -- write
355 };
356
357 for (size_t t = 0; t < arraysize(io_channels_); ++t) {
358 bool tracked = io_channels_[t].find(fd) != io_channels_[t].end();
359
360 if (!must_track[t]) {
361 // If we have an outstanding io_channel, remove it.
362 if (tracked) {
363 g_source_remove(io_channels_[t][fd].second);
364 g_io_channel_unref(io_channels_[t][fd].first);
365 io_channels_[t].erase(io_channels_[t].find(fd));
366 }
367 continue;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000368 }
Darin Petkov60e14152010-10-27 16:57:04 -0700369
370 // If we are already tracking this fd, continue -- nothing to do.
371 if (tracked)
372 continue;
373
374 // Set conditions appropriately -- read for track 0, write for track 1.
375 GIOCondition condition = static_cast<GIOCondition>(
376 ((t == 0) ? (G_IO_IN | G_IO_PRI) : G_IO_OUT) | G_IO_ERR | G_IO_HUP);
377
378 // Track a new fd.
379 GIOChannel* io_channel = g_io_channel_unix_new(fd);
380 guint tag =
381 g_io_add_watch(io_channel, condition, &StaticFDCallback, this);
382
383 io_channels_[t][fd] = make_pair(io_channel, tag);
384 static int io_counter = 0;
385 io_counter++;
386 if (io_counter % 50 == 0) {
387 LOG(INFO) << "io_counter = " << io_counter;
388 }
Andrew de los Reyes3270f742010-07-15 22:28:14 -0700389 }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000390 }
391
Darin Petkovb83371f2010-08-17 09:34:49 -0700392 // Set up a timeout callback for libcurl.
Andrew de los Reyes3270f742010-07-15 22:28:14 -0700393 if (!timeout_source_) {
Darin Petkovb83371f2010-08-17 09:34:49 -0700394 LOG(INFO) << "Setting up timeout source: " << idle_seconds_ << " seconds.";
395 timeout_source_ = g_timeout_source_new_seconds(idle_seconds_);
396 g_source_set_callback(timeout_source_, StaticTimeoutCallback, this, NULL);
Andrew de los Reyes3270f742010-07-15 22:28:14 -0700397 g_source_attach(timeout_source_, NULL);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000398 }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000399}
400
401bool LibcurlHttpFetcher::FDCallback(GIOChannel *source,
402 GIOCondition condition) {
Andrew de los Reyes9bbd1872010-07-16 14:52:29 -0700403 CurlPerformOnce();
Andrew de los Reyes3270f742010-07-15 22:28:14 -0700404 // We handle removing of this source elsewhere, so we always return true.
405 // The docs say, "the function should return FALSE if the event source
406 // should be removed."
407 // http://www.gtk.org/api/2.6/glib/glib-IO-Channels.html#GIOFunc
408 return true;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000409}
410
Andrew de los Reyes9bbd1872010-07-16 14:52:29 -0700411gboolean LibcurlHttpFetcher::RetryTimeoutCallback() {
412 ResumeTransfer(url_);
413 CurlPerformOnce();
414 return FALSE; // Don't have glib auto call this callback again
415}
416
Andrew de los Reyes3270f742010-07-15 22:28:14 -0700417gboolean LibcurlHttpFetcher::TimeoutCallback() {
Andrew de los Reyescb319332010-07-19 10:55:01 -0700418 // We always return true, even if we don't want glib to call us back.
419 // We will remove the event source separately if we don't want to
420 // be called back.
Andrew de los Reyes3270f742010-07-15 22:28:14 -0700421 if (!transfer_in_progress_)
422 return TRUE;
Andrew de los Reyes9bbd1872010-07-16 14:52:29 -0700423 CurlPerformOnce();
Andrew de los Reyes3270f742010-07-15 22:28:14 -0700424 return TRUE;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000425}
426
427void LibcurlHttpFetcher::CleanUp() {
428 if (timeout_source_) {
429 g_source_destroy(timeout_source_);
430 timeout_source_ = NULL;
431 }
432
Darin Petkov60e14152010-10-27 16:57:04 -0700433 for (size_t t = 0; t < arraysize(io_channels_); ++t) {
434 for (IOChannels::iterator it = io_channels_[t].begin();
435 it != io_channels_[t].end(); ++it) {
436 g_source_remove(it->second.second);
437 g_io_channel_unref(it->second.first);
438 }
439 io_channels_[t].clear();
rspangler@google.com49fdf182009-10-10 00:57:34 +0000440 }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000441
442 if (curl_handle_) {
443 if (curl_multi_handle_) {
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000444 CHECK_EQ(curl_multi_remove_handle(curl_multi_handle_, curl_handle_),
445 CURLM_OK);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000446 }
447 curl_easy_cleanup(curl_handle_);
448 curl_handle_ = NULL;
449 }
450 if (curl_multi_handle_) {
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000451 CHECK_EQ(curl_multi_cleanup(curl_multi_handle_), CURLM_OK);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000452 curl_multi_handle_ = NULL;
453 }
454 transfer_in_progress_ = false;
455}
456
Andrew de los Reyes3fd5d302010-10-07 20:07:18 -0700457void LibcurlHttpFetcher::GetHttpResponseCode() {
458 long http_response_code = 0;
459 if (curl_easy_getinfo(curl_handle_,
460 CURLINFO_RESPONSE_CODE,
461 &http_response_code) == CURLE_OK) {
462 http_response_code_ = static_cast<int>(http_response_code);
463 }
464}
465
rspangler@google.com49fdf182009-10-10 00:57:34 +0000466} // namespace chromeos_update_engine