blob: a92172544d7c499ef519173c994559047b431209 [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
12#include "update_engine/dbus_interface.h"
13#include "update_engine/flimflam_proxy.h"
14#include "update_engine/utils.h"
adlr@google.comc98a7ed2009-12-04 18:54:03 +000015
16using std::max;
17using std::make_pair;
Andrew de los Reyesd57d1472010-10-21 13:34:08 -070018using std::string;
rspangler@google.com49fdf182009-10-10 00:57:34 +000019
20// This is a concrete implementation of HttpFetcher that uses libcurl to do the
21// http work.
22
23namespace chromeos_update_engine {
24
Andrew de los Reyes9bbd1872010-07-16 14:52:29 -070025namespace {
26const int kMaxRetriesCount = 20;
Darin Petkov3a4016a2010-09-28 13:54:17 -070027const char kCACertificatesPath[] = "/usr/share/update_engine/ca-certificates";
Andrew de los Reyesd57d1472010-10-21 13:34:08 -070028} // namespace {}
Andrew de los Reyes9bbd1872010-07-16 14:52:29 -070029
rspangler@google.com49fdf182009-10-10 00:57:34 +000030LibcurlHttpFetcher::~LibcurlHttpFetcher() {
31 CleanUp();
32}
33
Andrew de los Reyesd57d1472010-10-21 13:34:08 -070034// On error, returns false.
35bool LibcurlHttpFetcher::ConnectionIsExpensive() const {
36 if (force_connection_type_)
37 return forced_expensive_connection_;
38 NetworkConnectionType type;
39 ConcreteDbusGlib dbus_iface;
40 TEST_AND_RETURN_FALSE(FlimFlamProxy::GetConnectionType(&dbus_iface, &type));
41 LOG(INFO) << "We are connected via "
42 << FlimFlamProxy::StringForConnectionType(type);
43 return FlimFlamProxy::IsExpensiveConnectionType(type);
44}
45
Darin Petkovfc7a0ce2010-10-25 10:38:37 -070046bool LibcurlHttpFetcher::IsOfficialBuild() const {
47 return force_build_type_ ? forced_official_build_ : utils::IsOfficialBuild();
48}
49
adlr@google.comc98a7ed2009-12-04 18:54:03 +000050void LibcurlHttpFetcher::ResumeTransfer(const std::string& url) {
Andrew de los Reyes3270f742010-07-15 22:28:14 -070051 LOG(INFO) << "Starting/Resuming transfer";
rspangler@google.com49fdf182009-10-10 00:57:34 +000052 CHECK(!transfer_in_progress_);
53 url_ = url;
54 curl_multi_handle_ = curl_multi_init();
55 CHECK(curl_multi_handle_);
56
57 curl_handle_ = curl_easy_init();
58 CHECK(curl_handle_);
59
60 if (post_data_set_) {
adlr@google.comc98a7ed2009-12-04 18:54:03 +000061 CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_POST, 1), CURLE_OK);
62 CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_POSTFIELDS,
63 &post_data_[0]),
64 CURLE_OK);
65 CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_POSTFIELDSIZE,
66 post_data_.size()),
67 CURLE_OK);
rspangler@google.com49fdf182009-10-10 00:57:34 +000068 }
69
adlr@google.comc98a7ed2009-12-04 18:54:03 +000070 if (bytes_downloaded_ > 0) {
71 // Resume from where we left off
72 resume_offset_ = bytes_downloaded_;
73 CHECK_EQ(curl_easy_setopt(curl_handle_,
74 CURLOPT_RESUME_FROM_LARGE,
75 bytes_downloaded_), CURLE_OK);
76 }
77
78 CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_WRITEDATA, this), CURLE_OK);
79 CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_WRITEFUNCTION,
80 StaticLibcurlWrite), CURLE_OK);
Andrew de los Reyesd57d1472010-10-21 13:34:08 -070081
82 string url_to_use(url_);
83 if (ConnectionIsExpensive()) {
84 LOG(INFO) << "Not initiating HTTP connection b/c we are on an expensive"
85 << " connection";
86 url_to_use = ""; // Sabotage the URL
87 }
88
Darin Petkovfc7a0ce2010-10-25 10:38:37 -070089 CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_URL, url_to_use.c_str()),
Andrew de los Reyesd57d1472010-10-21 13:34:08 -070090 CURLE_OK);
Andrew de los Reyes3270f742010-07-15 22:28:14 -070091
Darin Petkov192ced42010-07-23 16:20:24 -070092 // If the connection drops under 10 bytes/sec for 3 minutes, reconnect.
Andrew de los Reyes3270f742010-07-15 22:28:14 -070093 CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_LOW_SPEED_LIMIT, 10),
94 CURLE_OK);
Darin Petkov192ced42010-07-23 16:20:24 -070095 CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_LOW_SPEED_TIME, 3 * 60),
Andrew de los Reyes3270f742010-07-15 22:28:14 -070096 CURLE_OK);
97
Darin Petkov41c2fcf2010-08-25 13:14:48 -070098 // By default, libcurl doesn't follow redirections. Allow up to
99 // |kMaxRedirects| redirections.
Darin Petkov3a4016a2010-09-28 13:54:17 -0700100 CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_FOLLOWLOCATION, 1), CURLE_OK);
Darin Petkov41c2fcf2010-08-25 13:14:48 -0700101 CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_MAXREDIRS, kMaxRedirects),
102 CURLE_OK);
103
Darin Petkov3a4016a2010-09-28 13:54:17 -0700104 // Makes sure that peer certificate verification is enabled and restricts the
105 // set of trusted certificates.
106 CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_SSL_VERIFYPEER, 1), CURLE_OK);
107 CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_CAPATH, kCACertificatesPath),
108 CURLE_OK);
109
Darin Petkovfc7a0ce2010-10-25 10:38:37 -0700110 // Restrict protocols to HTTPS in official builds.
111 if (IsOfficialBuild()) {
112 CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_PROTOCOLS, CURLPROTO_HTTPS),
113 CURLE_OK);
114 CHECK_EQ(curl_easy_setopt(curl_handle_,
115 CURLOPT_REDIR_PROTOCOLS,
116 CURLPROTO_HTTPS),
117 CURLE_OK);
118 }
119
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000120 CHECK_EQ(curl_multi_add_handle(curl_multi_handle_, curl_handle_), CURLM_OK);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000121 transfer_in_progress_ = true;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000122}
123
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000124// Begins the transfer, which must not have already been started.
125void LibcurlHttpFetcher::BeginTransfer(const std::string& url) {
126 transfer_size_ = -1;
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000127 resume_offset_ = 0;
Andrew de los Reyes9bbd1872010-07-16 14:52:29 -0700128 retry_count_ = 0;
Darin Petkovcb466212010-08-26 09:40:11 -0700129 http_response_code_ = 0;
Andrew de los Reyes9bbd1872010-07-16 14:52:29 -0700130 ResumeTransfer(url);
131 CurlPerformOnce();
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000132}
133
rspangler@google.com49fdf182009-10-10 00:57:34 +0000134void LibcurlHttpFetcher::TerminateTransfer() {
Andrew de los Reyes3fd5d302010-10-07 20:07:18 -0700135 if (in_write_callback_)
136 terminate_requested_ = true;
137 else
138 CleanUp();
rspangler@google.com49fdf182009-10-10 00:57:34 +0000139}
140
Andrew de los Reyescb319332010-07-19 10:55:01 -0700141void LibcurlHttpFetcher::CurlPerformOnce() {
rspangler@google.com49fdf182009-10-10 00:57:34 +0000142 CHECK(transfer_in_progress_);
143 int running_handles = 0;
144 CURLMcode retcode = CURLM_CALL_MULTI_PERFORM;
145
146 // libcurl may request that we immediately call curl_multi_perform after it
147 // returns, so we do. libcurl promises that curl_multi_perform will not block.
148 while (CURLM_CALL_MULTI_PERFORM == retcode) {
149 retcode = curl_multi_perform(curl_multi_handle_, &running_handles);
Andrew de los Reyes3fd5d302010-10-07 20:07:18 -0700150 if (terminate_requested_) {
151 CleanUp();
152 return;
153 }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000154 }
155 if (0 == running_handles) {
Andrew de los Reyes3fd5d302010-10-07 20:07:18 -0700156 GetHttpResponseCode();
157 if (http_response_code_) {
158 LOG(INFO) << "HTTP response code: " << http_response_code_;
Andrew de los Reyes9bbd1872010-07-16 14:52:29 -0700159 } else {
160 LOG(ERROR) << "Unable to get http response code.";
161 }
Darin Petkov192ced42010-07-23 16:20:24 -0700162
rspangler@google.com49fdf182009-10-10 00:57:34 +0000163 // we're done!
164 CleanUp();
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000165
166 if ((transfer_size_ >= 0) && (bytes_downloaded_ < transfer_size_)) {
Andrew de los Reyes3270f742010-07-15 22:28:14 -0700167 // Need to restart transfer
Andrew de los Reyes9bbd1872010-07-16 14:52:29 -0700168 retry_count_++;
169 LOG(INFO) << "Restarting transfer b/c we finished, had downloaded "
170 << bytes_downloaded_ << " bytes, but transfer_size_ is "
171 << transfer_size_ << ". retry_count: " << retry_count_;
172 if (retry_count_ > kMaxRetriesCount) {
173 if (delegate_)
174 delegate_->TransferComplete(this, false); // success
175 } else {
Darin Petkovb83371f2010-08-17 09:34:49 -0700176 g_timeout_add_seconds(retry_seconds_,
Darin Petkov9b111652010-08-16 11:46:25 -0700177 &LibcurlHttpFetcher::StaticRetryTimeoutCallback,
178 this);
Andrew de los Reyes9bbd1872010-07-16 14:52:29 -0700179 }
Andrew de los Reyescb319332010-07-19 10:55:01 -0700180 return;
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000181 } else {
182 if (delegate_) {
Andrew de los Reyesfb4ad7d2010-07-19 10:43:46 -0700183 // success is when http_response_code is 2xx
Andrew de los Reyes3fd5d302010-10-07 20:07:18 -0700184 bool success = (http_response_code_ >= 200) &&
185 (http_response_code_ < 300);
Andrew de los Reyesfb4ad7d2010-07-19 10:43:46 -0700186 delegate_->TransferComplete(this, success);
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000187 }
188 }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000189 } else {
190 // set up callback
191 SetupMainloopSources();
192 }
193}
194
195size_t LibcurlHttpFetcher::LibcurlWrite(void *ptr, size_t size, size_t nmemb) {
Andrew de los Reyes3fd5d302010-10-07 20:07:18 -0700196 GetHttpResponseCode();
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000197 {
198 double transfer_size_double;
199 CHECK_EQ(curl_easy_getinfo(curl_handle_,
200 CURLINFO_CONTENT_LENGTH_DOWNLOAD,
201 &transfer_size_double), CURLE_OK);
202 off_t new_transfer_size = static_cast<off_t>(transfer_size_double);
203 if (new_transfer_size > 0) {
204 transfer_size_ = resume_offset_ + new_transfer_size;
205 }
206 }
207 bytes_downloaded_ += size * nmemb;
Andrew de los Reyes3fd5d302010-10-07 20:07:18 -0700208 in_write_callback_ = true;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000209 if (delegate_)
210 delegate_->ReceivedBytes(this, reinterpret_cast<char*>(ptr), size * nmemb);
Andrew de los Reyes3fd5d302010-10-07 20:07:18 -0700211 in_write_callback_ = false;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000212 return size * nmemb;
213}
214
215void LibcurlHttpFetcher::Pause() {
216 CHECK(curl_handle_);
217 CHECK(transfer_in_progress_);
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000218 CHECK_EQ(curl_easy_pause(curl_handle_, CURLPAUSE_ALL), CURLE_OK);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000219}
220
221void LibcurlHttpFetcher::Unpause() {
222 CHECK(curl_handle_);
223 CHECK(transfer_in_progress_);
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000224 CHECK_EQ(curl_easy_pause(curl_handle_, CURLPAUSE_CONT), CURLE_OK);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000225}
226
227// This method sets up callbacks with the glib main loop.
228void LibcurlHttpFetcher::SetupMainloopSources() {
229 fd_set fd_read;
230 fd_set fd_write;
231 fd_set fd_exec;
232
233 FD_ZERO(&fd_read);
234 FD_ZERO(&fd_write);
235 FD_ZERO(&fd_exec);
236
237 int fd_max = 0;
238
239 // Ask libcurl for the set of file descriptors we should track on its
240 // behalf.
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000241 CHECK_EQ(curl_multi_fdset(curl_multi_handle_, &fd_read, &fd_write,
242 &fd_exec, &fd_max), CURLM_OK);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000243
244 // We should iterate through all file descriptors up to libcurl's fd_max or
245 // the highest one we're tracking, whichever is larger
246 if (!io_channels_.empty())
247 fd_max = max(fd_max, io_channels_.rbegin()->first);
248
249 // For each fd, if we're not tracking it, track it. If we are tracking it,
250 // but libcurl doesn't care about it anymore, stop tracking it.
251 // After this loop, there should be exactly as many GIOChannel objects
252 // in io_channels_ as there are fds that we're tracking.
253 for (int i = 0; i <= fd_max; i++) {
254 if (!(FD_ISSET(i, &fd_read) || FD_ISSET(i, &fd_write) ||
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000255 FD_ISSET(i, &fd_exec))) {
rspangler@google.com49fdf182009-10-10 00:57:34 +0000256 // if we have an outstanding io_channel, remove it
257 if (io_channels_.find(i) != io_channels_.end()) {
258 g_source_remove(io_channels_[i].second);
259 g_io_channel_unref(io_channels_[i].first);
260 io_channels_.erase(io_channels_.find(i));
261 }
262 continue;
263 }
264 // If we are already tracking this fd, continue.
265 if (io_channels_.find(i) != io_channels_.end())
266 continue;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000267 // We must track a new fd
268 GIOChannel *io_channel = g_io_channel_unix_new(i);
269 guint tag = g_io_add_watch(
270 io_channel,
271 static_cast<GIOCondition>(G_IO_IN | G_IO_OUT | G_IO_PRI |
272 G_IO_ERR | G_IO_HUP),
273 &StaticFDCallback,
274 this);
275 io_channels_[i] = make_pair(io_channel, tag);
Andrew de los Reyes3270f742010-07-15 22:28:14 -0700276 static int io_counter = 0;
277 io_counter++;
278 if (io_counter % 50 == 0) {
279 LOG(INFO) << "io_counter = " << io_counter;
280 }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000281 }
282
Darin Petkovb83371f2010-08-17 09:34:49 -0700283 // Set up a timeout callback for libcurl.
Andrew de los Reyes3270f742010-07-15 22:28:14 -0700284 if (!timeout_source_) {
Darin Petkovb83371f2010-08-17 09:34:49 -0700285 LOG(INFO) << "Setting up timeout source: " << idle_seconds_ << " seconds.";
286 timeout_source_ = g_timeout_source_new_seconds(idle_seconds_);
287 g_source_set_callback(timeout_source_, StaticTimeoutCallback, this, NULL);
Andrew de los Reyes3270f742010-07-15 22:28:14 -0700288 g_source_attach(timeout_source_, NULL);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000289 }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000290}
291
292bool LibcurlHttpFetcher::FDCallback(GIOChannel *source,
293 GIOCondition condition) {
Andrew de los Reyes9bbd1872010-07-16 14:52:29 -0700294 CurlPerformOnce();
Andrew de los Reyes3270f742010-07-15 22:28:14 -0700295 // We handle removing of this source elsewhere, so we always return true.
296 // The docs say, "the function should return FALSE if the event source
297 // should be removed."
298 // http://www.gtk.org/api/2.6/glib/glib-IO-Channels.html#GIOFunc
299 return true;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000300}
301
Andrew de los Reyes9bbd1872010-07-16 14:52:29 -0700302gboolean LibcurlHttpFetcher::RetryTimeoutCallback() {
303 ResumeTransfer(url_);
304 CurlPerformOnce();
305 return FALSE; // Don't have glib auto call this callback again
306}
307
Andrew de los Reyes3270f742010-07-15 22:28:14 -0700308gboolean LibcurlHttpFetcher::TimeoutCallback() {
Andrew de los Reyescb319332010-07-19 10:55:01 -0700309 // We always return true, even if we don't want glib to call us back.
310 // We will remove the event source separately if we don't want to
311 // be called back.
Andrew de los Reyes3270f742010-07-15 22:28:14 -0700312 if (!transfer_in_progress_)
313 return TRUE;
Andrew de los Reyes9bbd1872010-07-16 14:52:29 -0700314 CurlPerformOnce();
Andrew de los Reyes3270f742010-07-15 22:28:14 -0700315 return TRUE;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000316}
317
318void LibcurlHttpFetcher::CleanUp() {
319 if (timeout_source_) {
320 g_source_destroy(timeout_source_);
321 timeout_source_ = NULL;
322 }
323
324 for (IOChannels::iterator it = io_channels_.begin();
325 it != io_channels_.end(); ++it) {
326 g_source_remove(it->second.second);
327 g_io_channel_unref(it->second.first);
328 }
329 io_channels_.clear();
330
331 if (curl_handle_) {
332 if (curl_multi_handle_) {
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000333 CHECK_EQ(curl_multi_remove_handle(curl_multi_handle_, curl_handle_),
334 CURLM_OK);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000335 }
336 curl_easy_cleanup(curl_handle_);
337 curl_handle_ = NULL;
338 }
339 if (curl_multi_handle_) {
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000340 CHECK_EQ(curl_multi_cleanup(curl_multi_handle_), CURLM_OK);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000341 curl_multi_handle_ = NULL;
342 }
343 transfer_in_progress_ = false;
344}
345
Andrew de los Reyes3fd5d302010-10-07 20:07:18 -0700346void LibcurlHttpFetcher::GetHttpResponseCode() {
347 long http_response_code = 0;
348 if (curl_easy_getinfo(curl_handle_,
349 CURLINFO_RESPONSE_CODE,
350 &http_response_code) == CURLE_OK) {
351 http_response_code_ = static_cast<int>(http_response_code);
352 }
353}
354
rspangler@google.com49fdf182009-10-10 00:57:34 +0000355} // namespace chromeos_update_engine