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; |
| 19 | } |
| 20 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 21 | LibcurlHttpFetcher::~LibcurlHttpFetcher() { |
| 22 | CleanUp(); |
| 23 | } |
| 24 | |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 25 | void LibcurlHttpFetcher::ResumeTransfer(const std::string& url) { |
Andrew de los Reyes | 3270f74 | 2010-07-15 22:28:14 -0700 | [diff] [blame] | 26 | LOG(INFO) << "Starting/Resuming transfer"; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 27 | CHECK(!transfer_in_progress_); |
| 28 | url_ = url; |
| 29 | curl_multi_handle_ = curl_multi_init(); |
| 30 | CHECK(curl_multi_handle_); |
| 31 | |
| 32 | curl_handle_ = curl_easy_init(); |
| 33 | CHECK(curl_handle_); |
| 34 | |
| 35 | if (post_data_set_) { |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 36 | CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_POST, 1), CURLE_OK); |
| 37 | CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_POSTFIELDS, |
| 38 | &post_data_[0]), |
| 39 | CURLE_OK); |
| 40 | CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_POSTFIELDSIZE, |
| 41 | post_data_.size()), |
| 42 | CURLE_OK); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 43 | } |
| 44 | |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 45 | if (bytes_downloaded_ > 0) { |
| 46 | // Resume from where we left off |
| 47 | resume_offset_ = bytes_downloaded_; |
| 48 | CHECK_EQ(curl_easy_setopt(curl_handle_, |
| 49 | CURLOPT_RESUME_FROM_LARGE, |
| 50 | bytes_downloaded_), CURLE_OK); |
| 51 | } |
| 52 | |
| 53 | CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_WRITEDATA, this), CURLE_OK); |
| 54 | CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_WRITEFUNCTION, |
| 55 | StaticLibcurlWrite), CURLE_OK); |
| 56 | 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] | 57 | |
Darin Petkov | 192ced4 | 2010-07-23 16:20:24 -0700 | [diff] [blame] | 58 | // 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] | 59 | CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_LOW_SPEED_LIMIT, 10), |
| 60 | CURLE_OK); |
Darin Petkov | 192ced4 | 2010-07-23 16:20:24 -0700 | [diff] [blame] | 61 | 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] | 62 | CURLE_OK); |
| 63 | |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 64 | 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] | 65 | transfer_in_progress_ = true; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 66 | } |
| 67 | |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 68 | // Begins the transfer, which must not have already been started. |
| 69 | void LibcurlHttpFetcher::BeginTransfer(const std::string& url) { |
| 70 | transfer_size_ = -1; |
| 71 | bytes_downloaded_ = 0; |
| 72 | resume_offset_ = 0; |
Andrew de los Reyes | 9bbd187 | 2010-07-16 14:52:29 -0700 | [diff] [blame] | 73 | retry_count_ = 0; |
| 74 | ResumeTransfer(url); |
| 75 | CurlPerformOnce(); |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 76 | } |
| 77 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 78 | void LibcurlHttpFetcher::TerminateTransfer() { |
| 79 | CleanUp(); |
| 80 | } |
| 81 | |
Andrew de los Reyes | cb31933 | 2010-07-19 10:55:01 -0700 | [diff] [blame] | 82 | void LibcurlHttpFetcher::CurlPerformOnce() { |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 83 | CHECK(transfer_in_progress_); |
| 84 | int running_handles = 0; |
| 85 | CURLMcode retcode = CURLM_CALL_MULTI_PERFORM; |
| 86 | |
| 87 | // libcurl may request that we immediately call curl_multi_perform after it |
| 88 | // returns, so we do. libcurl promises that curl_multi_perform will not block. |
| 89 | while (CURLM_CALL_MULTI_PERFORM == retcode) { |
| 90 | retcode = curl_multi_perform(curl_multi_handle_, &running_handles); |
| 91 | } |
| 92 | if (0 == running_handles) { |
Andrew de los Reyes | 9bbd187 | 2010-07-16 14:52:29 -0700 | [diff] [blame] | 93 | long http_response_code = 0; |
| 94 | if (curl_easy_getinfo(curl_handle_, |
| 95 | CURLINFO_RESPONSE_CODE, |
| 96 | &http_response_code) == CURLE_OK) { |
| 97 | LOG(INFO) << "HTTP response code: " << http_response_code; |
| 98 | } else { |
| 99 | LOG(ERROR) << "Unable to get http response code."; |
| 100 | } |
Darin Petkov | 192ced4 | 2010-07-23 16:20:24 -0700 | [diff] [blame] | 101 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 102 | // we're done! |
| 103 | CleanUp(); |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 104 | |
| 105 | if ((transfer_size_ >= 0) && (bytes_downloaded_ < transfer_size_)) { |
Andrew de los Reyes | 3270f74 | 2010-07-15 22:28:14 -0700 | [diff] [blame] | 106 | // Need to restart transfer |
Andrew de los Reyes | 9bbd187 | 2010-07-16 14:52:29 -0700 | [diff] [blame] | 107 | retry_count_++; |
| 108 | LOG(INFO) << "Restarting transfer b/c we finished, had downloaded " |
| 109 | << bytes_downloaded_ << " bytes, but transfer_size_ is " |
| 110 | << transfer_size_ << ". retry_count: " << retry_count_; |
| 111 | if (retry_count_ > kMaxRetriesCount) { |
| 112 | if (delegate_) |
| 113 | delegate_->TransferComplete(this, false); // success |
| 114 | } else { |
Darin Petkov | 9b11165 | 2010-08-16 11:46:25 -0700 | [diff] [blame^] | 115 | g_timeout_add_seconds(5, |
| 116 | &LibcurlHttpFetcher::StaticRetryTimeoutCallback, |
| 117 | this); |
Andrew de los Reyes | 9bbd187 | 2010-07-16 14:52:29 -0700 | [diff] [blame] | 118 | } |
Andrew de los Reyes | cb31933 | 2010-07-19 10:55:01 -0700 | [diff] [blame] | 119 | return; |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 120 | } else { |
| 121 | if (delegate_) { |
Andrew de los Reyes | fb4ad7d | 2010-07-19 10:43:46 -0700 | [diff] [blame] | 122 | // success is when http_response_code is 2xx |
| 123 | bool success = (http_response_code >= 200) && |
| 124 | (http_response_code < 300); |
| 125 | delegate_->TransferComplete(this, success); |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 126 | } |
| 127 | } |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 128 | } else { |
| 129 | // set up callback |
| 130 | SetupMainloopSources(); |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | 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] | 135 | { |
| 136 | double transfer_size_double; |
| 137 | CHECK_EQ(curl_easy_getinfo(curl_handle_, |
| 138 | CURLINFO_CONTENT_LENGTH_DOWNLOAD, |
| 139 | &transfer_size_double), CURLE_OK); |
| 140 | off_t new_transfer_size = static_cast<off_t>(transfer_size_double); |
| 141 | if (new_transfer_size > 0) { |
| 142 | transfer_size_ = resume_offset_ + new_transfer_size; |
| 143 | } |
| 144 | } |
| 145 | bytes_downloaded_ += size * nmemb; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 146 | if (delegate_) |
| 147 | delegate_->ReceivedBytes(this, reinterpret_cast<char*>(ptr), size * nmemb); |
| 148 | return size * nmemb; |
| 149 | } |
| 150 | |
| 151 | void LibcurlHttpFetcher::Pause() { |
| 152 | CHECK(curl_handle_); |
| 153 | CHECK(transfer_in_progress_); |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 154 | CHECK_EQ(curl_easy_pause(curl_handle_, CURLPAUSE_ALL), CURLE_OK); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | void LibcurlHttpFetcher::Unpause() { |
| 158 | CHECK(curl_handle_); |
| 159 | CHECK(transfer_in_progress_); |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 160 | CHECK_EQ(curl_easy_pause(curl_handle_, CURLPAUSE_CONT), CURLE_OK); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | // This method sets up callbacks with the glib main loop. |
| 164 | void LibcurlHttpFetcher::SetupMainloopSources() { |
| 165 | fd_set fd_read; |
| 166 | fd_set fd_write; |
| 167 | fd_set fd_exec; |
| 168 | |
| 169 | FD_ZERO(&fd_read); |
| 170 | FD_ZERO(&fd_write); |
| 171 | FD_ZERO(&fd_exec); |
| 172 | |
| 173 | int fd_max = 0; |
| 174 | |
| 175 | // Ask libcurl for the set of file descriptors we should track on its |
| 176 | // behalf. |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 177 | CHECK_EQ(curl_multi_fdset(curl_multi_handle_, &fd_read, &fd_write, |
| 178 | &fd_exec, &fd_max), CURLM_OK); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 179 | |
| 180 | // We should iterate through all file descriptors up to libcurl's fd_max or |
| 181 | // the highest one we're tracking, whichever is larger |
| 182 | if (!io_channels_.empty()) |
| 183 | fd_max = max(fd_max, io_channels_.rbegin()->first); |
| 184 | |
| 185 | // For each fd, if we're not tracking it, track it. If we are tracking it, |
| 186 | // but libcurl doesn't care about it anymore, stop tracking it. |
| 187 | // After this loop, there should be exactly as many GIOChannel objects |
| 188 | // in io_channels_ as there are fds that we're tracking. |
| 189 | for (int i = 0; i <= fd_max; i++) { |
| 190 | if (!(FD_ISSET(i, &fd_read) || FD_ISSET(i, &fd_write) || |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 191 | FD_ISSET(i, &fd_exec))) { |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 192 | // if we have an outstanding io_channel, remove it |
| 193 | if (io_channels_.find(i) != io_channels_.end()) { |
| 194 | g_source_remove(io_channels_[i].second); |
| 195 | g_io_channel_unref(io_channels_[i].first); |
| 196 | io_channels_.erase(io_channels_.find(i)); |
| 197 | } |
| 198 | continue; |
| 199 | } |
| 200 | // If we are already tracking this fd, continue. |
| 201 | if (io_channels_.find(i) != io_channels_.end()) |
| 202 | continue; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 203 | // We must track a new fd |
| 204 | GIOChannel *io_channel = g_io_channel_unix_new(i); |
| 205 | guint tag = g_io_add_watch( |
| 206 | io_channel, |
| 207 | static_cast<GIOCondition>(G_IO_IN | G_IO_OUT | G_IO_PRI | |
| 208 | G_IO_ERR | G_IO_HUP), |
| 209 | &StaticFDCallback, |
| 210 | this); |
| 211 | io_channels_[i] = make_pair(io_channel, tag); |
Andrew de los Reyes | 3270f74 | 2010-07-15 22:28:14 -0700 | [diff] [blame] | 212 | static int io_counter = 0; |
| 213 | io_counter++; |
| 214 | if (io_counter % 50 == 0) { |
| 215 | LOG(INFO) << "io_counter = " << io_counter; |
| 216 | } |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | // Wet up a timeout callback for libcurl |
| 220 | long ms = 0; |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 221 | CHECK_EQ(curl_multi_timeout(curl_multi_handle_, &ms), CURLM_OK); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 222 | if (ms < 0) { |
| 223 | // From http://curl.haxx.se/libcurl/c/curl_multi_timeout.html: |
| 224 | // if libcurl returns a -1 timeout here, it just means that libcurl |
| 225 | // currently has no stored timeout value. You must not wait too long |
| 226 | // (more than a few seconds perhaps) before you call |
| 227 | // curl_multi_perform() again. |
| 228 | ms = idle_ms_; |
| 229 | } |
Andrew de los Reyes | 3270f74 | 2010-07-15 22:28:14 -0700 | [diff] [blame] | 230 | if (!timeout_source_) { |
| 231 | LOG(INFO) << "setting up timeout source:" << ms; |
Darin Petkov | 9b11165 | 2010-08-16 11:46:25 -0700 | [diff] [blame^] | 232 | timeout_source_ = g_timeout_source_new_seconds(1); |
Andrew de los Reyes | 3270f74 | 2010-07-15 22:28:14 -0700 | [diff] [blame] | 233 | CHECK(timeout_source_); |
| 234 | g_source_set_callback(timeout_source_, StaticTimeoutCallback, this, |
| 235 | NULL); |
| 236 | g_source_attach(timeout_source_, NULL); |
| 237 | static int counter = 0; |
| 238 | counter++; |
| 239 | if (counter % 50 == 0) { |
| 240 | LOG(INFO) << "counter = " << counter; |
| 241 | } |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 242 | } |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | bool LibcurlHttpFetcher::FDCallback(GIOChannel *source, |
| 246 | GIOCondition condition) { |
Andrew de los Reyes | 9bbd187 | 2010-07-16 14:52:29 -0700 | [diff] [blame] | 247 | CurlPerformOnce(); |
Andrew de los Reyes | 3270f74 | 2010-07-15 22:28:14 -0700 | [diff] [blame] | 248 | // We handle removing of this source elsewhere, so we always return true. |
| 249 | // The docs say, "the function should return FALSE if the event source |
| 250 | // should be removed." |
| 251 | // http://www.gtk.org/api/2.6/glib/glib-IO-Channels.html#GIOFunc |
| 252 | return true; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 253 | } |
| 254 | |
Andrew de los Reyes | 9bbd187 | 2010-07-16 14:52:29 -0700 | [diff] [blame] | 255 | gboolean LibcurlHttpFetcher::RetryTimeoutCallback() { |
| 256 | ResumeTransfer(url_); |
| 257 | CurlPerformOnce(); |
| 258 | return FALSE; // Don't have glib auto call this callback again |
| 259 | } |
| 260 | |
Andrew de los Reyes | 3270f74 | 2010-07-15 22:28:14 -0700 | [diff] [blame] | 261 | gboolean LibcurlHttpFetcher::TimeoutCallback() { |
Andrew de los Reyes | cb31933 | 2010-07-19 10:55:01 -0700 | [diff] [blame] | 262 | // We always return true, even if we don't want glib to call us back. |
| 263 | // We will remove the event source separately if we don't want to |
| 264 | // be called back. |
Andrew de los Reyes | 3270f74 | 2010-07-15 22:28:14 -0700 | [diff] [blame] | 265 | if (!transfer_in_progress_) |
| 266 | return TRUE; |
Andrew de los Reyes | 9bbd187 | 2010-07-16 14:52:29 -0700 | [diff] [blame] | 267 | CurlPerformOnce(); |
Andrew de los Reyes | 3270f74 | 2010-07-15 22:28:14 -0700 | [diff] [blame] | 268 | return TRUE; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | void LibcurlHttpFetcher::CleanUp() { |
| 272 | if (timeout_source_) { |
| 273 | g_source_destroy(timeout_source_); |
| 274 | timeout_source_ = NULL; |
| 275 | } |
| 276 | |
| 277 | for (IOChannels::iterator it = io_channels_.begin(); |
| 278 | it != io_channels_.end(); ++it) { |
| 279 | g_source_remove(it->second.second); |
| 280 | g_io_channel_unref(it->second.first); |
| 281 | } |
| 282 | io_channels_.clear(); |
| 283 | |
| 284 | if (curl_handle_) { |
| 285 | if (curl_multi_handle_) { |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 286 | CHECK_EQ(curl_multi_remove_handle(curl_multi_handle_, curl_handle_), |
| 287 | CURLM_OK); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 288 | } |
| 289 | curl_easy_cleanup(curl_handle_); |
| 290 | curl_handle_ = NULL; |
| 291 | } |
| 292 | if (curl_multi_handle_) { |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 293 | CHECK_EQ(curl_multi_cleanup(curl_multi_handle_), CURLM_OK); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 294 | curl_multi_handle_ = NULL; |
| 295 | } |
| 296 | transfer_in_progress_ = false; |
| 297 | } |
| 298 | |
| 299 | } // namespace chromeos_update_engine |