blob: 9cacf869b96f1ba40b0253e333b5d5aedd045e07 [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
adlr@google.comc98a7ed2009-12-04 18:54:03 +000046void LibcurlHttpFetcher::ResumeTransfer(const std::string& url) {
Andrew de los Reyes3270f742010-07-15 22:28:14 -070047 LOG(INFO) << "Starting/Resuming transfer";
rspangler@google.com49fdf182009-10-10 00:57:34 +000048 CHECK(!transfer_in_progress_);
49 url_ = url;
50 curl_multi_handle_ = curl_multi_init();
51 CHECK(curl_multi_handle_);
52
53 curl_handle_ = curl_easy_init();
54 CHECK(curl_handle_);
55
56 if (post_data_set_) {
adlr@google.comc98a7ed2009-12-04 18:54:03 +000057 CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_POST, 1), CURLE_OK);
58 CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_POSTFIELDS,
59 &post_data_[0]),
60 CURLE_OK);
61 CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_POSTFIELDSIZE,
62 post_data_.size()),
63 CURLE_OK);
rspangler@google.com49fdf182009-10-10 00:57:34 +000064 }
65
adlr@google.comc98a7ed2009-12-04 18:54:03 +000066 if (bytes_downloaded_ > 0) {
67 // Resume from where we left off
68 resume_offset_ = bytes_downloaded_;
69 CHECK_EQ(curl_easy_setopt(curl_handle_,
70 CURLOPT_RESUME_FROM_LARGE,
71 bytes_downloaded_), CURLE_OK);
72 }
73
74 CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_WRITEDATA, this), CURLE_OK);
75 CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_WRITEFUNCTION,
76 StaticLibcurlWrite), CURLE_OK);
Andrew de los Reyesd57d1472010-10-21 13:34:08 -070077
78 string url_to_use(url_);
79 if (ConnectionIsExpensive()) {
80 LOG(INFO) << "Not initiating HTTP connection b/c we are on an expensive"
81 << " connection";
82 url_to_use = ""; // Sabotage the URL
83 }
84
85 CHECK_EQ(curl_easy_setopt(curl_handle_,
86 CURLOPT_URL,
87 url_to_use.c_str()),
88 CURLE_OK);
Andrew de los Reyes3270f742010-07-15 22:28:14 -070089
Darin Petkov192ced42010-07-23 16:20:24 -070090 // If the connection drops under 10 bytes/sec for 3 minutes, reconnect.
Andrew de los Reyes3270f742010-07-15 22:28:14 -070091 CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_LOW_SPEED_LIMIT, 10),
92 CURLE_OK);
Darin Petkov192ced42010-07-23 16:20:24 -070093 CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_LOW_SPEED_TIME, 3 * 60),
Andrew de los Reyes3270f742010-07-15 22:28:14 -070094 CURLE_OK);
95
Darin Petkov41c2fcf2010-08-25 13:14:48 -070096 // By default, libcurl doesn't follow redirections. Allow up to
97 // |kMaxRedirects| redirections.
Darin Petkov3a4016a2010-09-28 13:54:17 -070098 CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_FOLLOWLOCATION, 1), CURLE_OK);
Darin Petkov41c2fcf2010-08-25 13:14:48 -070099 CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_MAXREDIRS, kMaxRedirects),
100 CURLE_OK);
101
Darin Petkov3a4016a2010-09-28 13:54:17 -0700102 // Makes sure that peer certificate verification is enabled and restricts the
103 // set of trusted certificates.
104 CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_SSL_VERIFYPEER, 1), CURLE_OK);
105 CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_CAPATH, kCACertificatesPath),
106 CURLE_OK);
107
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000108 CHECK_EQ(curl_multi_add_handle(curl_multi_handle_, curl_handle_), CURLM_OK);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000109 transfer_in_progress_ = true;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000110}
111
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000112// Begins the transfer, which must not have already been started.
113void LibcurlHttpFetcher::BeginTransfer(const std::string& url) {
114 transfer_size_ = -1;
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000115 resume_offset_ = 0;
Andrew de los Reyes9bbd1872010-07-16 14:52:29 -0700116 retry_count_ = 0;
Darin Petkovcb466212010-08-26 09:40:11 -0700117 http_response_code_ = 0;
Andrew de los Reyes9bbd1872010-07-16 14:52:29 -0700118 ResumeTransfer(url);
119 CurlPerformOnce();
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000120}
121
rspangler@google.com49fdf182009-10-10 00:57:34 +0000122void LibcurlHttpFetcher::TerminateTransfer() {
Andrew de los Reyes3fd5d302010-10-07 20:07:18 -0700123 if (in_write_callback_)
124 terminate_requested_ = true;
125 else
126 CleanUp();
rspangler@google.com49fdf182009-10-10 00:57:34 +0000127}
128
Andrew de los Reyescb319332010-07-19 10:55:01 -0700129void LibcurlHttpFetcher::CurlPerformOnce() {
rspangler@google.com49fdf182009-10-10 00:57:34 +0000130 CHECK(transfer_in_progress_);
131 int running_handles = 0;
132 CURLMcode retcode = CURLM_CALL_MULTI_PERFORM;
133
134 // libcurl may request that we immediately call curl_multi_perform after it
135 // returns, so we do. libcurl promises that curl_multi_perform will not block.
136 while (CURLM_CALL_MULTI_PERFORM == retcode) {
137 retcode = curl_multi_perform(curl_multi_handle_, &running_handles);
Andrew de los Reyes3fd5d302010-10-07 20:07:18 -0700138 if (terminate_requested_) {
139 CleanUp();
140 return;
141 }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000142 }
143 if (0 == running_handles) {
Andrew de los Reyes3fd5d302010-10-07 20:07:18 -0700144 GetHttpResponseCode();
145 if (http_response_code_) {
146 LOG(INFO) << "HTTP response code: " << http_response_code_;
Andrew de los Reyes9bbd1872010-07-16 14:52:29 -0700147 } else {
148 LOG(ERROR) << "Unable to get http response code.";
149 }
Darin Petkov192ced42010-07-23 16:20:24 -0700150
rspangler@google.com49fdf182009-10-10 00:57:34 +0000151 // we're done!
152 CleanUp();
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000153
154 if ((transfer_size_ >= 0) && (bytes_downloaded_ < transfer_size_)) {
Andrew de los Reyes3270f742010-07-15 22:28:14 -0700155 // Need to restart transfer
Andrew de los Reyes9bbd1872010-07-16 14:52:29 -0700156 retry_count_++;
157 LOG(INFO) << "Restarting transfer b/c we finished, had downloaded "
158 << bytes_downloaded_ << " bytes, but transfer_size_ is "
159 << transfer_size_ << ". retry_count: " << retry_count_;
160 if (retry_count_ > kMaxRetriesCount) {
161 if (delegate_)
162 delegate_->TransferComplete(this, false); // success
163 } else {
Darin Petkovb83371f2010-08-17 09:34:49 -0700164 g_timeout_add_seconds(retry_seconds_,
Darin Petkov9b111652010-08-16 11:46:25 -0700165 &LibcurlHttpFetcher::StaticRetryTimeoutCallback,
166 this);
Andrew de los Reyes9bbd1872010-07-16 14:52:29 -0700167 }
Andrew de los Reyescb319332010-07-19 10:55:01 -0700168 return;
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000169 } else {
170 if (delegate_) {
Andrew de los Reyesfb4ad7d2010-07-19 10:43:46 -0700171 // success is when http_response_code is 2xx
Andrew de los Reyes3fd5d302010-10-07 20:07:18 -0700172 bool success = (http_response_code_ >= 200) &&
173 (http_response_code_ < 300);
Andrew de los Reyesfb4ad7d2010-07-19 10:43:46 -0700174 delegate_->TransferComplete(this, success);
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000175 }
176 }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000177 } else {
178 // set up callback
179 SetupMainloopSources();
180 }
181}
182
183size_t LibcurlHttpFetcher::LibcurlWrite(void *ptr, size_t size, size_t nmemb) {
Andrew de los Reyes3fd5d302010-10-07 20:07:18 -0700184 GetHttpResponseCode();
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000185 {
186 double transfer_size_double;
187 CHECK_EQ(curl_easy_getinfo(curl_handle_,
188 CURLINFO_CONTENT_LENGTH_DOWNLOAD,
189 &transfer_size_double), CURLE_OK);
190 off_t new_transfer_size = static_cast<off_t>(transfer_size_double);
191 if (new_transfer_size > 0) {
192 transfer_size_ = resume_offset_ + new_transfer_size;
193 }
194 }
195 bytes_downloaded_ += size * nmemb;
Andrew de los Reyes3fd5d302010-10-07 20:07:18 -0700196 in_write_callback_ = true;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000197 if (delegate_)
198 delegate_->ReceivedBytes(this, reinterpret_cast<char*>(ptr), size * nmemb);
Andrew de los Reyes3fd5d302010-10-07 20:07:18 -0700199 in_write_callback_ = false;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000200 return size * nmemb;
201}
202
203void LibcurlHttpFetcher::Pause() {
204 CHECK(curl_handle_);
205 CHECK(transfer_in_progress_);
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000206 CHECK_EQ(curl_easy_pause(curl_handle_, CURLPAUSE_ALL), CURLE_OK);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000207}
208
209void LibcurlHttpFetcher::Unpause() {
210 CHECK(curl_handle_);
211 CHECK(transfer_in_progress_);
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000212 CHECK_EQ(curl_easy_pause(curl_handle_, CURLPAUSE_CONT), CURLE_OK);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000213}
214
215// This method sets up callbacks with the glib main loop.
216void LibcurlHttpFetcher::SetupMainloopSources() {
217 fd_set fd_read;
218 fd_set fd_write;
219 fd_set fd_exec;
220
221 FD_ZERO(&fd_read);
222 FD_ZERO(&fd_write);
223 FD_ZERO(&fd_exec);
224
225 int fd_max = 0;
226
227 // Ask libcurl for the set of file descriptors we should track on its
228 // behalf.
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000229 CHECK_EQ(curl_multi_fdset(curl_multi_handle_, &fd_read, &fd_write,
230 &fd_exec, &fd_max), CURLM_OK);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000231
232 // We should iterate through all file descriptors up to libcurl's fd_max or
233 // the highest one we're tracking, whichever is larger
234 if (!io_channels_.empty())
235 fd_max = max(fd_max, io_channels_.rbegin()->first);
236
237 // For each fd, if we're not tracking it, track it. If we are tracking it,
238 // but libcurl doesn't care about it anymore, stop tracking it.
239 // After this loop, there should be exactly as many GIOChannel objects
240 // in io_channels_ as there are fds that we're tracking.
241 for (int i = 0; i <= fd_max; i++) {
242 if (!(FD_ISSET(i, &fd_read) || FD_ISSET(i, &fd_write) ||
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000243 FD_ISSET(i, &fd_exec))) {
rspangler@google.com49fdf182009-10-10 00:57:34 +0000244 // if we have an outstanding io_channel, remove it
245 if (io_channels_.find(i) != io_channels_.end()) {
246 g_source_remove(io_channels_[i].second);
247 g_io_channel_unref(io_channels_[i].first);
248 io_channels_.erase(io_channels_.find(i));
249 }
250 continue;
251 }
252 // If we are already tracking this fd, continue.
253 if (io_channels_.find(i) != io_channels_.end())
254 continue;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000255 // We must track a new fd
256 GIOChannel *io_channel = g_io_channel_unix_new(i);
257 guint tag = g_io_add_watch(
258 io_channel,
259 static_cast<GIOCondition>(G_IO_IN | G_IO_OUT | G_IO_PRI |
260 G_IO_ERR | G_IO_HUP),
261 &StaticFDCallback,
262 this);
263 io_channels_[i] = make_pair(io_channel, tag);
Andrew de los Reyes3270f742010-07-15 22:28:14 -0700264 static int io_counter = 0;
265 io_counter++;
266 if (io_counter % 50 == 0) {
267 LOG(INFO) << "io_counter = " << io_counter;
268 }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000269 }
270
Darin Petkovb83371f2010-08-17 09:34:49 -0700271 // Set up a timeout callback for libcurl.
Andrew de los Reyes3270f742010-07-15 22:28:14 -0700272 if (!timeout_source_) {
Darin Petkovb83371f2010-08-17 09:34:49 -0700273 LOG(INFO) << "Setting up timeout source: " << idle_seconds_ << " seconds.";
274 timeout_source_ = g_timeout_source_new_seconds(idle_seconds_);
275 g_source_set_callback(timeout_source_, StaticTimeoutCallback, this, NULL);
Andrew de los Reyes3270f742010-07-15 22:28:14 -0700276 g_source_attach(timeout_source_, NULL);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000277 }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000278}
279
280bool LibcurlHttpFetcher::FDCallback(GIOChannel *source,
281 GIOCondition condition) {
Andrew de los Reyes9bbd1872010-07-16 14:52:29 -0700282 CurlPerformOnce();
Andrew de los Reyes3270f742010-07-15 22:28:14 -0700283 // We handle removing of this source elsewhere, so we always return true.
284 // The docs say, "the function should return FALSE if the event source
285 // should be removed."
286 // http://www.gtk.org/api/2.6/glib/glib-IO-Channels.html#GIOFunc
287 return true;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000288}
289
Andrew de los Reyes9bbd1872010-07-16 14:52:29 -0700290gboolean LibcurlHttpFetcher::RetryTimeoutCallback() {
291 ResumeTransfer(url_);
292 CurlPerformOnce();
293 return FALSE; // Don't have glib auto call this callback again
294}
295
Andrew de los Reyes3270f742010-07-15 22:28:14 -0700296gboolean LibcurlHttpFetcher::TimeoutCallback() {
Andrew de los Reyescb319332010-07-19 10:55:01 -0700297 // We always return true, even if we don't want glib to call us back.
298 // We will remove the event source separately if we don't want to
299 // be called back.
Andrew de los Reyes3270f742010-07-15 22:28:14 -0700300 if (!transfer_in_progress_)
301 return TRUE;
Andrew de los Reyes9bbd1872010-07-16 14:52:29 -0700302 CurlPerformOnce();
Andrew de los Reyes3270f742010-07-15 22:28:14 -0700303 return TRUE;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000304}
305
306void LibcurlHttpFetcher::CleanUp() {
307 if (timeout_source_) {
308 g_source_destroy(timeout_source_);
309 timeout_source_ = NULL;
310 }
311
312 for (IOChannels::iterator it = io_channels_.begin();
313 it != io_channels_.end(); ++it) {
314 g_source_remove(it->second.second);
315 g_io_channel_unref(it->second.first);
316 }
317 io_channels_.clear();
318
319 if (curl_handle_) {
320 if (curl_multi_handle_) {
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000321 CHECK_EQ(curl_multi_remove_handle(curl_multi_handle_, curl_handle_),
322 CURLM_OK);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000323 }
324 curl_easy_cleanup(curl_handle_);
325 curl_handle_ = NULL;
326 }
327 if (curl_multi_handle_) {
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000328 CHECK_EQ(curl_multi_cleanup(curl_multi_handle_), CURLM_OK);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000329 curl_multi_handle_ = NULL;
330 }
331 transfer_in_progress_ = false;
332}
333
Andrew de los Reyes3fd5d302010-10-07 20:07:18 -0700334void LibcurlHttpFetcher::GetHttpResponseCode() {
335 long http_response_code = 0;
336 if (curl_easy_getinfo(curl_handle_,
337 CURLINFO_RESPONSE_CODE,
338 &http_response_code) == CURLE_OK) {
339 http_response_code_ = static_cast<int>(http_response_code);
340 }
341}
342
rspangler@google.com49fdf182009-10-10 00:57:34 +0000343} // namespace chromeos_update_engine