blob: 1dcea9ea1b55d073b170ff6a5125b9262da77775 [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;
84 bytes_downloaded_ = 0;
85 resume_offset_ = 0;
Andrew de los Reyes9bbd1872010-07-16 14:52:29 -070086 retry_count_ = 0;
Darin Petkovcb466212010-08-26 09:40:11 -070087 http_response_code_ = 0;
Andrew de los Reyes9bbd1872010-07-16 14:52:29 -070088 ResumeTransfer(url);
89 CurlPerformOnce();
adlr@google.comc98a7ed2009-12-04 18:54:03 +000090}
91
rspangler@google.com49fdf182009-10-10 00:57:34 +000092void LibcurlHttpFetcher::TerminateTransfer() {
93 CleanUp();
94}
95
Andrew de los Reyescb319332010-07-19 10:55:01 -070096void LibcurlHttpFetcher::CurlPerformOnce() {
rspangler@google.com49fdf182009-10-10 00:57:34 +000097 CHECK(transfer_in_progress_);
98 int running_handles = 0;
99 CURLMcode retcode = CURLM_CALL_MULTI_PERFORM;
100
101 // libcurl may request that we immediately call curl_multi_perform after it
102 // returns, so we do. libcurl promises that curl_multi_perform will not block.
103 while (CURLM_CALL_MULTI_PERFORM == retcode) {
104 retcode = curl_multi_perform(curl_multi_handle_, &running_handles);
105 }
106 if (0 == running_handles) {
Andrew de los Reyes9bbd1872010-07-16 14:52:29 -0700107 long http_response_code = 0;
108 if (curl_easy_getinfo(curl_handle_,
109 CURLINFO_RESPONSE_CODE,
110 &http_response_code) == CURLE_OK) {
111 LOG(INFO) << "HTTP response code: " << http_response_code;
112 } else {
113 LOG(ERROR) << "Unable to get http response code.";
114 }
Darin Petkovcb466212010-08-26 09:40:11 -0700115 http_response_code_ = static_cast<int>(http_response_code);
Darin Petkov192ced42010-07-23 16:20:24 -0700116
rspangler@google.com49fdf182009-10-10 00:57:34 +0000117 // we're done!
118 CleanUp();
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000119
120 if ((transfer_size_ >= 0) && (bytes_downloaded_ < transfer_size_)) {
Andrew de los Reyes3270f742010-07-15 22:28:14 -0700121 // Need to restart transfer
Andrew de los Reyes9bbd1872010-07-16 14:52:29 -0700122 retry_count_++;
123 LOG(INFO) << "Restarting transfer b/c we finished, had downloaded "
124 << bytes_downloaded_ << " bytes, but transfer_size_ is "
125 << transfer_size_ << ". retry_count: " << retry_count_;
126 if (retry_count_ > kMaxRetriesCount) {
127 if (delegate_)
128 delegate_->TransferComplete(this, false); // success
129 } else {
Darin Petkovb83371f2010-08-17 09:34:49 -0700130 g_timeout_add_seconds(retry_seconds_,
Darin Petkov9b111652010-08-16 11:46:25 -0700131 &LibcurlHttpFetcher::StaticRetryTimeoutCallback,
132 this);
Andrew de los Reyes9bbd1872010-07-16 14:52:29 -0700133 }
Andrew de los Reyescb319332010-07-19 10:55:01 -0700134 return;
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000135 } else {
136 if (delegate_) {
Andrew de los Reyesfb4ad7d2010-07-19 10:43:46 -0700137 // success is when http_response_code is 2xx
138 bool success = (http_response_code >= 200) &&
139 (http_response_code < 300);
140 delegate_->TransferComplete(this, success);
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000141 }
142 }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000143 } else {
144 // set up callback
145 SetupMainloopSources();
146 }
147}
148
149size_t LibcurlHttpFetcher::LibcurlWrite(void *ptr, size_t size, size_t nmemb) {
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000150 {
151 double transfer_size_double;
152 CHECK_EQ(curl_easy_getinfo(curl_handle_,
153 CURLINFO_CONTENT_LENGTH_DOWNLOAD,
154 &transfer_size_double), CURLE_OK);
155 off_t new_transfer_size = static_cast<off_t>(transfer_size_double);
156 if (new_transfer_size > 0) {
157 transfer_size_ = resume_offset_ + new_transfer_size;
158 }
159 }
160 bytes_downloaded_ += size * nmemb;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000161 if (delegate_)
162 delegate_->ReceivedBytes(this, reinterpret_cast<char*>(ptr), size * nmemb);
163 return size * nmemb;
164}
165
166void LibcurlHttpFetcher::Pause() {
167 CHECK(curl_handle_);
168 CHECK(transfer_in_progress_);
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000169 CHECK_EQ(curl_easy_pause(curl_handle_, CURLPAUSE_ALL), CURLE_OK);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000170}
171
172void LibcurlHttpFetcher::Unpause() {
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_CONT), CURLE_OK);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000176}
177
178// This method sets up callbacks with the glib main loop.
179void LibcurlHttpFetcher::SetupMainloopSources() {
180 fd_set fd_read;
181 fd_set fd_write;
182 fd_set fd_exec;
183
184 FD_ZERO(&fd_read);
185 FD_ZERO(&fd_write);
186 FD_ZERO(&fd_exec);
187
188 int fd_max = 0;
189
190 // Ask libcurl for the set of file descriptors we should track on its
191 // behalf.
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000192 CHECK_EQ(curl_multi_fdset(curl_multi_handle_, &fd_read, &fd_write,
193 &fd_exec, &fd_max), CURLM_OK);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000194
195 // We should iterate through all file descriptors up to libcurl's fd_max or
196 // the highest one we're tracking, whichever is larger
197 if (!io_channels_.empty())
198 fd_max = max(fd_max, io_channels_.rbegin()->first);
199
200 // For each fd, if we're not tracking it, track it. If we are tracking it,
201 // but libcurl doesn't care about it anymore, stop tracking it.
202 // After this loop, there should be exactly as many GIOChannel objects
203 // in io_channels_ as there are fds that we're tracking.
204 for (int i = 0; i <= fd_max; i++) {
205 if (!(FD_ISSET(i, &fd_read) || FD_ISSET(i, &fd_write) ||
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000206 FD_ISSET(i, &fd_exec))) {
rspangler@google.com49fdf182009-10-10 00:57:34 +0000207 // if we have an outstanding io_channel, remove it
208 if (io_channels_.find(i) != io_channels_.end()) {
209 g_source_remove(io_channels_[i].second);
210 g_io_channel_unref(io_channels_[i].first);
211 io_channels_.erase(io_channels_.find(i));
212 }
213 continue;
214 }
215 // If we are already tracking this fd, continue.
216 if (io_channels_.find(i) != io_channels_.end())
217 continue;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000218 // We must track a new fd
219 GIOChannel *io_channel = g_io_channel_unix_new(i);
220 guint tag = g_io_add_watch(
221 io_channel,
222 static_cast<GIOCondition>(G_IO_IN | G_IO_OUT | G_IO_PRI |
223 G_IO_ERR | G_IO_HUP),
224 &StaticFDCallback,
225 this);
226 io_channels_[i] = make_pair(io_channel, tag);
Andrew de los Reyes3270f742010-07-15 22:28:14 -0700227 static int io_counter = 0;
228 io_counter++;
229 if (io_counter % 50 == 0) {
230 LOG(INFO) << "io_counter = " << io_counter;
231 }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000232 }
233
Darin Petkovb83371f2010-08-17 09:34:49 -0700234 // Set up a timeout callback for libcurl.
Andrew de los Reyes3270f742010-07-15 22:28:14 -0700235 if (!timeout_source_) {
Darin Petkovb83371f2010-08-17 09:34:49 -0700236 LOG(INFO) << "Setting up timeout source: " << idle_seconds_ << " seconds.";
237 timeout_source_ = g_timeout_source_new_seconds(idle_seconds_);
238 g_source_set_callback(timeout_source_, StaticTimeoutCallback, this, NULL);
Andrew de los Reyes3270f742010-07-15 22:28:14 -0700239 g_source_attach(timeout_source_, NULL);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000240 }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000241}
242
243bool LibcurlHttpFetcher::FDCallback(GIOChannel *source,
244 GIOCondition condition) {
Andrew de los Reyes9bbd1872010-07-16 14:52:29 -0700245 CurlPerformOnce();
Andrew de los Reyes3270f742010-07-15 22:28:14 -0700246 // We handle removing of this source elsewhere, so we always return true.
247 // The docs say, "the function should return FALSE if the event source
248 // should be removed."
249 // http://www.gtk.org/api/2.6/glib/glib-IO-Channels.html#GIOFunc
250 return true;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000251}
252
Andrew de los Reyes9bbd1872010-07-16 14:52:29 -0700253gboolean LibcurlHttpFetcher::RetryTimeoutCallback() {
254 ResumeTransfer(url_);
255 CurlPerformOnce();
256 return FALSE; // Don't have glib auto call this callback again
257}
258
Andrew de los Reyes3270f742010-07-15 22:28:14 -0700259gboolean LibcurlHttpFetcher::TimeoutCallback() {
Andrew de los Reyescb319332010-07-19 10:55:01 -0700260 // We always return true, even if we don't want glib to call us back.
261 // We will remove the event source separately if we don't want to
262 // be called back.
Andrew de los Reyes3270f742010-07-15 22:28:14 -0700263 if (!transfer_in_progress_)
264 return TRUE;
Andrew de los Reyes9bbd1872010-07-16 14:52:29 -0700265 CurlPerformOnce();
Andrew de los Reyes3270f742010-07-15 22:28:14 -0700266 return TRUE;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000267}
268
269void LibcurlHttpFetcher::CleanUp() {
270 if (timeout_source_) {
271 g_source_destroy(timeout_source_);
272 timeout_source_ = NULL;
273 }
274
275 for (IOChannels::iterator it = io_channels_.begin();
276 it != io_channels_.end(); ++it) {
277 g_source_remove(it->second.second);
278 g_io_channel_unref(it->second.first);
279 }
280 io_channels_.clear();
281
282 if (curl_handle_) {
283 if (curl_multi_handle_) {
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000284 CHECK_EQ(curl_multi_remove_handle(curl_multi_handle_, curl_handle_),
285 CURLM_OK);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000286 }
287 curl_easy_cleanup(curl_handle_);
288 curl_handle_ = NULL;
289 }
290 if (curl_multi_handle_) {
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000291 CHECK_EQ(curl_multi_cleanup(curl_multi_handle_), CURLM_OK);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000292 curl_multi_handle_ = NULL;
293 }
294 transfer_in_progress_ = false;
295}
296
297} // namespace chromeos_update_engine