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