blob: c8468125f042aabd5a67eaf765b3d02e12ed17c2 [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"
adlr@google.comc98a7ed2009-12-04 18:54:03 +00006#include <algorithm>
Chris Masone790e62e2010-08-12 10:41:18 -07007#include "base/logging.h"
adlr@google.comc98a7ed2009-12-04 18:54:03 +00008
9using std::max;
10using std::make_pair;
rspangler@google.com49fdf182009-10-10 00:57:34 +000011
12// This is a concrete implementation of HttpFetcher that uses libcurl to do the
13// http work.
14
15namespace chromeos_update_engine {
16
Andrew de los Reyes9bbd1872010-07-16 14:52:29 -070017namespace {
18const int kMaxRetriesCount = 20;
Darin Petkov3a4016a2010-09-28 13:54:17 -070019const char kCACertificatesPath[] = "/usr/share/update_engine/ca-certificates";
Andrew de los Reyes9bbd1872010-07-16 14:52:29 -070020}
21
rspangler@google.com49fdf182009-10-10 00:57:34 +000022LibcurlHttpFetcher::~LibcurlHttpFetcher() {
23 CleanUp();
24}
25
adlr@google.comc98a7ed2009-12-04 18:54:03 +000026void LibcurlHttpFetcher::ResumeTransfer(const std::string& url) {
Andrew de los Reyes3270f742010-07-15 22:28:14 -070027 LOG(INFO) << "Starting/Resuming transfer";
rspangler@google.com49fdf182009-10-10 00:57:34 +000028 CHECK(!transfer_in_progress_);
29 url_ = url;
30 curl_multi_handle_ = curl_multi_init();
31 CHECK(curl_multi_handle_);
32
33 curl_handle_ = curl_easy_init();
34 CHECK(curl_handle_);
35
36 if (post_data_set_) {
adlr@google.comc98a7ed2009-12-04 18:54:03 +000037 CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_POST, 1), CURLE_OK);
38 CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_POSTFIELDS,
39 &post_data_[0]),
40 CURLE_OK);
41 CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_POSTFIELDSIZE,
42 post_data_.size()),
43 CURLE_OK);
rspangler@google.com49fdf182009-10-10 00:57:34 +000044 }
45
adlr@google.comc98a7ed2009-12-04 18:54:03 +000046 if (bytes_downloaded_ > 0) {
47 // Resume from where we left off
48 resume_offset_ = bytes_downloaded_;
49 CHECK_EQ(curl_easy_setopt(curl_handle_,
50 CURLOPT_RESUME_FROM_LARGE,
51 bytes_downloaded_), CURLE_OK);
52 }
53
54 CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_WRITEDATA, this), CURLE_OK);
55 CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_WRITEFUNCTION,
56 StaticLibcurlWrite), CURLE_OK);
57 CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_URL, url_.c_str()), CURLE_OK);
Andrew de los Reyes3270f742010-07-15 22:28:14 -070058
Darin Petkov192ced42010-07-23 16:20:24 -070059 // If the connection drops under 10 bytes/sec for 3 minutes, reconnect.
Andrew de los Reyes3270f742010-07-15 22:28:14 -070060 CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_LOW_SPEED_LIMIT, 10),
61 CURLE_OK);
Darin Petkov192ced42010-07-23 16:20:24 -070062 CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_LOW_SPEED_TIME, 3 * 60),
Andrew de los Reyes3270f742010-07-15 22:28:14 -070063 CURLE_OK);
64
Darin Petkov41c2fcf2010-08-25 13:14:48 -070065 // By default, libcurl doesn't follow redirections. Allow up to
66 // |kMaxRedirects| redirections.
Darin Petkov3a4016a2010-09-28 13:54:17 -070067 CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_FOLLOWLOCATION, 1), CURLE_OK);
Darin Petkov41c2fcf2010-08-25 13:14:48 -070068 CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_MAXREDIRS, kMaxRedirects),
69 CURLE_OK);
70
Darin Petkov3a4016a2010-09-28 13:54:17 -070071 // Makes sure that peer certificate verification is enabled and restricts the
72 // set of trusted certificates.
73 CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_SSL_VERIFYPEER, 1), CURLE_OK);
74 CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_CAPATH, kCACertificatesPath),
75 CURLE_OK);
76
adlr@google.comc98a7ed2009-12-04 18:54:03 +000077 CHECK_EQ(curl_multi_add_handle(curl_multi_handle_, curl_handle_), CURLM_OK);
rspangler@google.com49fdf182009-10-10 00:57:34 +000078 transfer_in_progress_ = true;
rspangler@google.com49fdf182009-10-10 00:57:34 +000079}
80
adlr@google.comc98a7ed2009-12-04 18:54:03 +000081// Begins the transfer, which must not have already been started.
82void LibcurlHttpFetcher::BeginTransfer(const std::string& url) {
83 transfer_size_ = -1;
adlr@google.comc98a7ed2009-12-04 18:54:03 +000084 resume_offset_ = 0;
Andrew de los Reyes9bbd1872010-07-16 14:52:29 -070085 retry_count_ = 0;
Darin Petkovcb466212010-08-26 09:40:11 -070086 http_response_code_ = 0;
Andrew de los Reyes9bbd1872010-07-16 14:52:29 -070087 ResumeTransfer(url);
88 CurlPerformOnce();
adlr@google.comc98a7ed2009-12-04 18:54:03 +000089}
90
rspangler@google.com49fdf182009-10-10 00:57:34 +000091void LibcurlHttpFetcher::TerminateTransfer() {
Andrew de los Reyes3fd5d302010-10-07 20:07:18 -070092 if (in_write_callback_)
93 terminate_requested_ = true;
94 else
95 CleanUp();
rspangler@google.com49fdf182009-10-10 00:57:34 +000096}
97
Andrew de los Reyescb319332010-07-19 10:55:01 -070098void LibcurlHttpFetcher::CurlPerformOnce() {
rspangler@google.com49fdf182009-10-10 00:57:34 +000099 CHECK(transfer_in_progress_);
100 int running_handles = 0;
101 CURLMcode retcode = CURLM_CALL_MULTI_PERFORM;
102
103 // libcurl may request that we immediately call curl_multi_perform after it
104 // returns, so we do. libcurl promises that curl_multi_perform will not block.
105 while (CURLM_CALL_MULTI_PERFORM == retcode) {
106 retcode = curl_multi_perform(curl_multi_handle_, &running_handles);
Andrew de los Reyes3fd5d302010-10-07 20:07:18 -0700107 if (terminate_requested_) {
108 CleanUp();
109 return;
110 }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000111 }
112 if (0 == running_handles) {
Andrew de los Reyes3fd5d302010-10-07 20:07:18 -0700113 GetHttpResponseCode();
114 if (http_response_code_) {
115 LOG(INFO) << "HTTP response code: " << http_response_code_;
Andrew de los Reyes9bbd1872010-07-16 14:52:29 -0700116 } else {
117 LOG(ERROR) << "Unable to get http response code.";
118 }
Darin Petkov192ced42010-07-23 16:20:24 -0700119
rspangler@google.com49fdf182009-10-10 00:57:34 +0000120 // we're done!
121 CleanUp();
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000122
123 if ((transfer_size_ >= 0) && (bytes_downloaded_ < transfer_size_)) {
Andrew de los Reyes3270f742010-07-15 22:28:14 -0700124 // Need to restart transfer
Andrew de los Reyes9bbd1872010-07-16 14:52:29 -0700125 retry_count_++;
126 LOG(INFO) << "Restarting transfer b/c we finished, had downloaded "
127 << bytes_downloaded_ << " bytes, but transfer_size_ is "
128 << transfer_size_ << ". retry_count: " << retry_count_;
129 if (retry_count_ > kMaxRetriesCount) {
130 if (delegate_)
131 delegate_->TransferComplete(this, false); // success
132 } else {
Darin Petkovb83371f2010-08-17 09:34:49 -0700133 g_timeout_add_seconds(retry_seconds_,
Darin Petkov9b111652010-08-16 11:46:25 -0700134 &LibcurlHttpFetcher::StaticRetryTimeoutCallback,
135 this);
Andrew de los Reyes9bbd1872010-07-16 14:52:29 -0700136 }
Andrew de los Reyescb319332010-07-19 10:55:01 -0700137 return;
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000138 } else {
139 if (delegate_) {
Andrew de los Reyesfb4ad7d2010-07-19 10:43:46 -0700140 // success is when http_response_code is 2xx
Andrew de los Reyes3fd5d302010-10-07 20:07:18 -0700141 bool success = (http_response_code_ >= 200) &&
142 (http_response_code_ < 300);
Andrew de los Reyesfb4ad7d2010-07-19 10:43:46 -0700143 delegate_->TransferComplete(this, success);
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000144 }
145 }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000146 } else {
147 // set up callback
148 SetupMainloopSources();
149 }
150}
151
152size_t LibcurlHttpFetcher::LibcurlWrite(void *ptr, size_t size, size_t nmemb) {
Andrew de los Reyes3fd5d302010-10-07 20:07:18 -0700153 GetHttpResponseCode();
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000154 {
155 double transfer_size_double;
156 CHECK_EQ(curl_easy_getinfo(curl_handle_,
157 CURLINFO_CONTENT_LENGTH_DOWNLOAD,
158 &transfer_size_double), CURLE_OK);
159 off_t new_transfer_size = static_cast<off_t>(transfer_size_double);
160 if (new_transfer_size > 0) {
161 transfer_size_ = resume_offset_ + new_transfer_size;
162 }
163 }
164 bytes_downloaded_ += size * nmemb;
Andrew de los Reyes3fd5d302010-10-07 20:07:18 -0700165 in_write_callback_ = true;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000166 if (delegate_)
167 delegate_->ReceivedBytes(this, reinterpret_cast<char*>(ptr), size * nmemb);
Andrew de los Reyes3fd5d302010-10-07 20:07:18 -0700168 in_write_callback_ = false;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000169 return size * nmemb;
170}
171
172void LibcurlHttpFetcher::Pause() {
173 CHECK(curl_handle_);
174 CHECK(transfer_in_progress_);
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000175 CHECK_EQ(curl_easy_pause(curl_handle_, CURLPAUSE_ALL), CURLE_OK);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000176}
177
178void LibcurlHttpFetcher::Unpause() {
179 CHECK(curl_handle_);
180 CHECK(transfer_in_progress_);
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000181 CHECK_EQ(curl_easy_pause(curl_handle_, CURLPAUSE_CONT), CURLE_OK);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000182}
183
184// This method sets up callbacks with the glib main loop.
185void LibcurlHttpFetcher::SetupMainloopSources() {
186 fd_set fd_read;
187 fd_set fd_write;
188 fd_set fd_exec;
189
190 FD_ZERO(&fd_read);
191 FD_ZERO(&fd_write);
192 FD_ZERO(&fd_exec);
193
194 int fd_max = 0;
195
196 // Ask libcurl for the set of file descriptors we should track on its
197 // behalf.
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000198 CHECK_EQ(curl_multi_fdset(curl_multi_handle_, &fd_read, &fd_write,
199 &fd_exec, &fd_max), CURLM_OK);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000200
201 // We should iterate through all file descriptors up to libcurl's fd_max or
202 // the highest one we're tracking, whichever is larger
203 if (!io_channels_.empty())
204 fd_max = max(fd_max, io_channels_.rbegin()->first);
205
206 // For each fd, if we're not tracking it, track it. If we are tracking it,
207 // but libcurl doesn't care about it anymore, stop tracking it.
208 // After this loop, there should be exactly as many GIOChannel objects
209 // in io_channels_ as there are fds that we're tracking.
210 for (int i = 0; i <= fd_max; i++) {
211 if (!(FD_ISSET(i, &fd_read) || FD_ISSET(i, &fd_write) ||
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000212 FD_ISSET(i, &fd_exec))) {
rspangler@google.com49fdf182009-10-10 00:57:34 +0000213 // if we have an outstanding io_channel, remove it
214 if (io_channels_.find(i) != io_channels_.end()) {
215 g_source_remove(io_channels_[i].second);
216 g_io_channel_unref(io_channels_[i].first);
217 io_channels_.erase(io_channels_.find(i));
218 }
219 continue;
220 }
221 // If we are already tracking this fd, continue.
222 if (io_channels_.find(i) != io_channels_.end())
223 continue;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000224 // We must track a new fd
225 GIOChannel *io_channel = g_io_channel_unix_new(i);
226 guint tag = g_io_add_watch(
227 io_channel,
228 static_cast<GIOCondition>(G_IO_IN | G_IO_OUT | G_IO_PRI |
229 G_IO_ERR | G_IO_HUP),
230 &StaticFDCallback,
231 this);
232 io_channels_[i] = make_pair(io_channel, tag);
Andrew de los Reyes3270f742010-07-15 22:28:14 -0700233 static int io_counter = 0;
234 io_counter++;
235 if (io_counter % 50 == 0) {
236 LOG(INFO) << "io_counter = " << io_counter;
237 }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000238 }
239
Darin Petkovb83371f2010-08-17 09:34:49 -0700240 // Set up a timeout callback for libcurl.
Andrew de los Reyes3270f742010-07-15 22:28:14 -0700241 if (!timeout_source_) {
Darin Petkovb83371f2010-08-17 09:34:49 -0700242 LOG(INFO) << "Setting up timeout source: " << idle_seconds_ << " seconds.";
243 timeout_source_ = g_timeout_source_new_seconds(idle_seconds_);
244 g_source_set_callback(timeout_source_, StaticTimeoutCallback, this, NULL);
Andrew de los Reyes3270f742010-07-15 22:28:14 -0700245 g_source_attach(timeout_source_, NULL);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000246 }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000247}
248
249bool LibcurlHttpFetcher::FDCallback(GIOChannel *source,
250 GIOCondition condition) {
Andrew de los Reyes9bbd1872010-07-16 14:52:29 -0700251 CurlPerformOnce();
Andrew de los Reyes3270f742010-07-15 22:28:14 -0700252 // We handle removing of this source elsewhere, so we always return true.
253 // The docs say, "the function should return FALSE if the event source
254 // should be removed."
255 // http://www.gtk.org/api/2.6/glib/glib-IO-Channels.html#GIOFunc
256 return true;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000257}
258
Andrew de los Reyes9bbd1872010-07-16 14:52:29 -0700259gboolean LibcurlHttpFetcher::RetryTimeoutCallback() {
260 ResumeTransfer(url_);
261 CurlPerformOnce();
262 return FALSE; // Don't have glib auto call this callback again
263}
264
Andrew de los Reyes3270f742010-07-15 22:28:14 -0700265gboolean LibcurlHttpFetcher::TimeoutCallback() {
Andrew de los Reyescb319332010-07-19 10:55:01 -0700266 // We always return true, even if we don't want glib to call us back.
267 // We will remove the event source separately if we don't want to
268 // be called back.
Andrew de los Reyes3270f742010-07-15 22:28:14 -0700269 if (!transfer_in_progress_)
270 return TRUE;
Andrew de los Reyes9bbd1872010-07-16 14:52:29 -0700271 CurlPerformOnce();
Andrew de los Reyes3270f742010-07-15 22:28:14 -0700272 return TRUE;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000273}
274
275void LibcurlHttpFetcher::CleanUp() {
276 if (timeout_source_) {
277 g_source_destroy(timeout_source_);
278 timeout_source_ = NULL;
279 }
280
281 for (IOChannels::iterator it = io_channels_.begin();
282 it != io_channels_.end(); ++it) {
283 g_source_remove(it->second.second);
284 g_io_channel_unref(it->second.first);
285 }
286 io_channels_.clear();
287
288 if (curl_handle_) {
289 if (curl_multi_handle_) {
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000290 CHECK_EQ(curl_multi_remove_handle(curl_multi_handle_, curl_handle_),
291 CURLM_OK);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000292 }
293 curl_easy_cleanup(curl_handle_);
294 curl_handle_ = NULL;
295 }
296 if (curl_multi_handle_) {
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000297 CHECK_EQ(curl_multi_cleanup(curl_multi_handle_), CURLM_OK);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000298 curl_multi_handle_ = NULL;
299 }
300 transfer_in_progress_ = false;
301}
302
Andrew de los Reyes3fd5d302010-10-07 20:07:18 -0700303void LibcurlHttpFetcher::GetHttpResponseCode() {
304 long http_response_code = 0;
305 if (curl_easy_getinfo(curl_handle_,
306 CURLINFO_RESPONSE_CODE,
307 &http_response_code) == CURLE_OK) {
308 http_response_code_ = static_cast<int>(http_response_code);
309 }
310}
311
rspangler@google.com49fdf182009-10-10 00:57:34 +0000312} // namespace chromeos_update_engine