rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 1 | // 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.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 5 | #include "update_engine/libcurl_http_fetcher.h" |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 6 | #include <algorithm> |
Chris Masone | 790e62e | 2010-08-12 10:41:18 -0700 | [diff] [blame] | 7 | #include "base/logging.h" |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 8 | |
| 9 | using std::max; |
| 10 | using std::make_pair; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 11 | |
| 12 | // This is a concrete implementation of HttpFetcher that uses libcurl to do the |
| 13 | // http work. |
| 14 | |
| 15 | namespace chromeos_update_engine { |
| 16 | |
Andrew de los Reyes | 9bbd187 | 2010-07-16 14:52:29 -0700 | [diff] [blame] | 17 | namespace { |
| 18 | const int kMaxRetriesCount = 20; |
Darin Petkov | 3a4016a | 2010-09-28 13:54:17 -0700 | [diff] [blame] | 19 | const char kCACertificatesPath[] = "/usr/share/update_engine/ca-certificates"; |
Andrew de los Reyes | 9bbd187 | 2010-07-16 14:52:29 -0700 | [diff] [blame] | 20 | } |
| 21 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 22 | LibcurlHttpFetcher::~LibcurlHttpFetcher() { |
| 23 | CleanUp(); |
| 24 | } |
| 25 | |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 26 | void LibcurlHttpFetcher::ResumeTransfer(const std::string& url) { |
Andrew de los Reyes | 3270f74 | 2010-07-15 22:28:14 -0700 | [diff] [blame] | 27 | LOG(INFO) << "Starting/Resuming transfer"; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 28 | 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.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 37 | 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.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 44 | } |
| 45 | |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 46 | 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 Reyes | 3270f74 | 2010-07-15 22:28:14 -0700 | [diff] [blame] | 58 | |
Darin Petkov | 192ced4 | 2010-07-23 16:20:24 -0700 | [diff] [blame] | 59 | // If the connection drops under 10 bytes/sec for 3 minutes, reconnect. |
Andrew de los Reyes | 3270f74 | 2010-07-15 22:28:14 -0700 | [diff] [blame] | 60 | CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_LOW_SPEED_LIMIT, 10), |
| 61 | CURLE_OK); |
Darin Petkov | 192ced4 | 2010-07-23 16:20:24 -0700 | [diff] [blame] | 62 | CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_LOW_SPEED_TIME, 3 * 60), |
Andrew de los Reyes | 3270f74 | 2010-07-15 22:28:14 -0700 | [diff] [blame] | 63 | CURLE_OK); |
| 64 | |
Darin Petkov | 41c2fcf | 2010-08-25 13:14:48 -0700 | [diff] [blame] | 65 | // By default, libcurl doesn't follow redirections. Allow up to |
| 66 | // |kMaxRedirects| redirections. |
Darin Petkov | 3a4016a | 2010-09-28 13:54:17 -0700 | [diff] [blame] | 67 | CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_FOLLOWLOCATION, 1), CURLE_OK); |
Darin Petkov | 41c2fcf | 2010-08-25 13:14:48 -0700 | [diff] [blame] | 68 | CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_MAXREDIRS, kMaxRedirects), |
| 69 | CURLE_OK); |
| 70 | |
Darin Petkov | 3a4016a | 2010-09-28 13:54:17 -0700 | [diff] [blame] | 71 | // 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.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 77 | CHECK_EQ(curl_multi_add_handle(curl_multi_handle_, curl_handle_), CURLM_OK); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 78 | transfer_in_progress_ = true; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 79 | } |
| 80 | |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 81 | // Begins the transfer, which must not have already been started. |
| 82 | void LibcurlHttpFetcher::BeginTransfer(const std::string& url) { |
| 83 | transfer_size_ = -1; |
| 84 | bytes_downloaded_ = 0; |
| 85 | resume_offset_ = 0; |
Andrew de los Reyes | 9bbd187 | 2010-07-16 14:52:29 -0700 | [diff] [blame] | 86 | retry_count_ = 0; |
Darin Petkov | cb46621 | 2010-08-26 09:40:11 -0700 | [diff] [blame] | 87 | http_response_code_ = 0; |
Andrew de los Reyes | 9bbd187 | 2010-07-16 14:52:29 -0700 | [diff] [blame] | 88 | ResumeTransfer(url); |
| 89 | CurlPerformOnce(); |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 90 | } |
| 91 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 92 | void LibcurlHttpFetcher::TerminateTransfer() { |
| 93 | CleanUp(); |
| 94 | } |
| 95 | |
Andrew de los Reyes | cb31933 | 2010-07-19 10:55:01 -0700 | [diff] [blame] | 96 | void LibcurlHttpFetcher::CurlPerformOnce() { |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 97 | 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 Reyes | 9bbd187 | 2010-07-16 14:52:29 -0700 | [diff] [blame] | 107 | 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 Petkov | cb46621 | 2010-08-26 09:40:11 -0700 | [diff] [blame] | 115 | http_response_code_ = static_cast<int>(http_response_code); |
Darin Petkov | 192ced4 | 2010-07-23 16:20:24 -0700 | [diff] [blame] | 116 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 117 | // we're done! |
| 118 | CleanUp(); |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 119 | |
| 120 | if ((transfer_size_ >= 0) && (bytes_downloaded_ < transfer_size_)) { |
Andrew de los Reyes | 3270f74 | 2010-07-15 22:28:14 -0700 | [diff] [blame] | 121 | // Need to restart transfer |
Andrew de los Reyes | 9bbd187 | 2010-07-16 14:52:29 -0700 | [diff] [blame] | 122 | 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 Petkov | b83371f | 2010-08-17 09:34:49 -0700 | [diff] [blame] | 130 | g_timeout_add_seconds(retry_seconds_, |
Darin Petkov | 9b11165 | 2010-08-16 11:46:25 -0700 | [diff] [blame] | 131 | &LibcurlHttpFetcher::StaticRetryTimeoutCallback, |
| 132 | this); |
Andrew de los Reyes | 9bbd187 | 2010-07-16 14:52:29 -0700 | [diff] [blame] | 133 | } |
Andrew de los Reyes | cb31933 | 2010-07-19 10:55:01 -0700 | [diff] [blame] | 134 | return; |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 135 | } else { |
| 136 | if (delegate_) { |
Andrew de los Reyes | fb4ad7d | 2010-07-19 10:43:46 -0700 | [diff] [blame] | 137 | // 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.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 141 | } |
| 142 | } |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 143 | } else { |
| 144 | // set up callback |
| 145 | SetupMainloopSources(); |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | size_t LibcurlHttpFetcher::LibcurlWrite(void *ptr, size_t size, size_t nmemb) { |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 150 | { |
| 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.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 161 | if (delegate_) |
| 162 | delegate_->ReceivedBytes(this, reinterpret_cast<char*>(ptr), size * nmemb); |
| 163 | return size * nmemb; |
| 164 | } |
| 165 | |
| 166 | void LibcurlHttpFetcher::Pause() { |
| 167 | CHECK(curl_handle_); |
| 168 | CHECK(transfer_in_progress_); |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 169 | CHECK_EQ(curl_easy_pause(curl_handle_, CURLPAUSE_ALL), CURLE_OK); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | void LibcurlHttpFetcher::Unpause() { |
| 173 | CHECK(curl_handle_); |
| 174 | CHECK(transfer_in_progress_); |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 175 | CHECK_EQ(curl_easy_pause(curl_handle_, CURLPAUSE_CONT), CURLE_OK); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | // This method sets up callbacks with the glib main loop. |
| 179 | void 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.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 192 | CHECK_EQ(curl_multi_fdset(curl_multi_handle_, &fd_read, &fd_write, |
| 193 | &fd_exec, &fd_max), CURLM_OK); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 194 | |
| 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.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 206 | FD_ISSET(i, &fd_exec))) { |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 207 | // 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.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 218 | // 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 Reyes | 3270f74 | 2010-07-15 22:28:14 -0700 | [diff] [blame] | 227 | static int io_counter = 0; |
| 228 | io_counter++; |
| 229 | if (io_counter % 50 == 0) { |
| 230 | LOG(INFO) << "io_counter = " << io_counter; |
| 231 | } |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 232 | } |
| 233 | |
Darin Petkov | b83371f | 2010-08-17 09:34:49 -0700 | [diff] [blame] | 234 | // Set up a timeout callback for libcurl. |
Andrew de los Reyes | 3270f74 | 2010-07-15 22:28:14 -0700 | [diff] [blame] | 235 | if (!timeout_source_) { |
Darin Petkov | b83371f | 2010-08-17 09:34:49 -0700 | [diff] [blame] | 236 | 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 Reyes | 3270f74 | 2010-07-15 22:28:14 -0700 | [diff] [blame] | 239 | g_source_attach(timeout_source_, NULL); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 240 | } |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | bool LibcurlHttpFetcher::FDCallback(GIOChannel *source, |
| 244 | GIOCondition condition) { |
Andrew de los Reyes | 9bbd187 | 2010-07-16 14:52:29 -0700 | [diff] [blame] | 245 | CurlPerformOnce(); |
Andrew de los Reyes | 3270f74 | 2010-07-15 22:28:14 -0700 | [diff] [blame] | 246 | // 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.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 251 | } |
| 252 | |
Andrew de los Reyes | 9bbd187 | 2010-07-16 14:52:29 -0700 | [diff] [blame] | 253 | gboolean LibcurlHttpFetcher::RetryTimeoutCallback() { |
| 254 | ResumeTransfer(url_); |
| 255 | CurlPerformOnce(); |
| 256 | return FALSE; // Don't have glib auto call this callback again |
| 257 | } |
| 258 | |
Andrew de los Reyes | 3270f74 | 2010-07-15 22:28:14 -0700 | [diff] [blame] | 259 | gboolean LibcurlHttpFetcher::TimeoutCallback() { |
Andrew de los Reyes | cb31933 | 2010-07-19 10:55:01 -0700 | [diff] [blame] | 260 | // 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 Reyes | 3270f74 | 2010-07-15 22:28:14 -0700 | [diff] [blame] | 263 | if (!transfer_in_progress_) |
| 264 | return TRUE; |
Andrew de los Reyes | 9bbd187 | 2010-07-16 14:52:29 -0700 | [diff] [blame] | 265 | CurlPerformOnce(); |
Andrew de los Reyes | 3270f74 | 2010-07-15 22:28:14 -0700 | [diff] [blame] | 266 | return TRUE; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | void 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.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 284 | CHECK_EQ(curl_multi_remove_handle(curl_multi_handle_, curl_handle_), |
| 285 | CURLM_OK); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 286 | } |
| 287 | curl_easy_cleanup(curl_handle_); |
| 288 | curl_handle_ = NULL; |
| 289 | } |
| 290 | if (curl_multi_handle_) { |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 291 | CHECK_EQ(curl_multi_cleanup(curl_multi_handle_), CURLM_OK); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 292 | curl_multi_handle_ = NULL; |
| 293 | } |
| 294 | transfer_in_progress_ = false; |
| 295 | } |
| 296 | |
| 297 | } // namespace chromeos_update_engine |