blob: 84f09726e1e1a027f5452be9d8a1b87e8262ab2f [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>
7#include "chromeos/obsolete_logging.h"
8
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;
19}
20
rspangler@google.com49fdf182009-10-10 00:57:34 +000021LibcurlHttpFetcher::~LibcurlHttpFetcher() {
22 CleanUp();
23}
24
adlr@google.comc98a7ed2009-12-04 18:54:03 +000025void LibcurlHttpFetcher::ResumeTransfer(const std::string& url) {
Andrew de los Reyes3270f742010-07-15 22:28:14 -070026 LOG(INFO) << "Starting/Resuming transfer";
rspangler@google.com49fdf182009-10-10 00:57:34 +000027 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.comc98a7ed2009-12-04 18:54:03 +000036 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.com49fdf182009-10-10 00:57:34 +000043 }
44
adlr@google.comc98a7ed2009-12-04 18:54:03 +000045 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 Reyes3270f742010-07-15 22:28:14 -070057
58 // If the connection drops under 10 bytes/sec for 90 seconds, reconnect.
59 CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_LOW_SPEED_LIMIT, 10),
60 CURLE_OK);
61 CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_LOW_SPEED_TIME, 90),
62 CURLE_OK);
63
adlr@google.comc98a7ed2009-12-04 18:54:03 +000064 CHECK_EQ(curl_multi_add_handle(curl_multi_handle_, curl_handle_), CURLM_OK);
rspangler@google.com49fdf182009-10-10 00:57:34 +000065 transfer_in_progress_ = true;
rspangler@google.com49fdf182009-10-10 00:57:34 +000066}
67
adlr@google.comc98a7ed2009-12-04 18:54:03 +000068// Begins the transfer, which must not have already been started.
69void LibcurlHttpFetcher::BeginTransfer(const std::string& url) {
70 transfer_size_ = -1;
71 bytes_downloaded_ = 0;
72 resume_offset_ = 0;
Andrew de los Reyes9bbd1872010-07-16 14:52:29 -070073 retry_count_ = 0;
74 ResumeTransfer(url);
75 CurlPerformOnce();
adlr@google.comc98a7ed2009-12-04 18:54:03 +000076}
77
rspangler@google.com49fdf182009-10-10 00:57:34 +000078void LibcurlHttpFetcher::TerminateTransfer() {
79 CleanUp();
80}
81
Andrew de los Reyes3270f742010-07-15 22:28:14 -070082bool LibcurlHttpFetcher::CurlPerformOnce() {
rspangler@google.com49fdf182009-10-10 00:57:34 +000083 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 Reyes9bbd1872010-07-16 14:52:29 -070093 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 }
101
rspangler@google.com49fdf182009-10-10 00:57:34 +0000102 // we're done!
103 CleanUp();
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000104
105 if ((transfer_size_ >= 0) && (bytes_downloaded_ < transfer_size_)) {
Andrew de los Reyes3270f742010-07-15 22:28:14 -0700106 // Need to restart transfer
Andrew de los Reyes9bbd1872010-07-16 14:52:29 -0700107 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 {
115 g_timeout_add(5 * 1000,
116 &LibcurlHttpFetcher::StaticRetryTimeoutCallback,
117 this);
118 }
119 return false;
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000120 } else {
121 if (delegate_) {
Andrew de los Reyesfb4ad7d2010-07-19 10:43:46 -0700122 // 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.comc98a7ed2009-12-04 18:54:03 +0000126 }
127 }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000128 } else {
129 // set up callback
130 SetupMainloopSources();
131 }
Andrew de los Reyes3270f742010-07-15 22:28:14 -0700132 return false;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000133}
134
135size_t LibcurlHttpFetcher::LibcurlWrite(void *ptr, size_t size, size_t nmemb) {
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000136 {
137 double transfer_size_double;
138 CHECK_EQ(curl_easy_getinfo(curl_handle_,
139 CURLINFO_CONTENT_LENGTH_DOWNLOAD,
140 &transfer_size_double), CURLE_OK);
141 off_t new_transfer_size = static_cast<off_t>(transfer_size_double);
142 if (new_transfer_size > 0) {
143 transfer_size_ = resume_offset_ + new_transfer_size;
144 }
145 }
146 bytes_downloaded_ += size * nmemb;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000147 if (delegate_)
148 delegate_->ReceivedBytes(this, reinterpret_cast<char*>(ptr), size * nmemb);
149 return size * nmemb;
150}
151
152void LibcurlHttpFetcher::Pause() {
153 CHECK(curl_handle_);
154 CHECK(transfer_in_progress_);
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000155 CHECK_EQ(curl_easy_pause(curl_handle_, CURLPAUSE_ALL), CURLE_OK);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000156}
157
158void LibcurlHttpFetcher::Unpause() {
159 CHECK(curl_handle_);
160 CHECK(transfer_in_progress_);
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000161 CHECK_EQ(curl_easy_pause(curl_handle_, CURLPAUSE_CONT), CURLE_OK);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000162}
163
164// This method sets up callbacks with the glib main loop.
165void LibcurlHttpFetcher::SetupMainloopSources() {
166 fd_set fd_read;
167 fd_set fd_write;
168 fd_set fd_exec;
169
170 FD_ZERO(&fd_read);
171 FD_ZERO(&fd_write);
172 FD_ZERO(&fd_exec);
173
174 int fd_max = 0;
175
176 // Ask libcurl for the set of file descriptors we should track on its
177 // behalf.
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000178 CHECK_EQ(curl_multi_fdset(curl_multi_handle_, &fd_read, &fd_write,
179 &fd_exec, &fd_max), CURLM_OK);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000180
181 // We should iterate through all file descriptors up to libcurl's fd_max or
182 // the highest one we're tracking, whichever is larger
183 if (!io_channels_.empty())
184 fd_max = max(fd_max, io_channels_.rbegin()->first);
185
186 // For each fd, if we're not tracking it, track it. If we are tracking it,
187 // but libcurl doesn't care about it anymore, stop tracking it.
188 // After this loop, there should be exactly as many GIOChannel objects
189 // in io_channels_ as there are fds that we're tracking.
190 for (int i = 0; i <= fd_max; i++) {
191 if (!(FD_ISSET(i, &fd_read) || FD_ISSET(i, &fd_write) ||
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000192 FD_ISSET(i, &fd_exec))) {
rspangler@google.com49fdf182009-10-10 00:57:34 +0000193 // if we have an outstanding io_channel, remove it
194 if (io_channels_.find(i) != io_channels_.end()) {
195 g_source_remove(io_channels_[i].second);
196 g_io_channel_unref(io_channels_[i].first);
197 io_channels_.erase(io_channels_.find(i));
198 }
199 continue;
200 }
201 // If we are already tracking this fd, continue.
202 if (io_channels_.find(i) != io_channels_.end())
203 continue;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000204 // We must track a new fd
205 GIOChannel *io_channel = g_io_channel_unix_new(i);
206 guint tag = g_io_add_watch(
207 io_channel,
208 static_cast<GIOCondition>(G_IO_IN | G_IO_OUT | G_IO_PRI |
209 G_IO_ERR | G_IO_HUP),
210 &StaticFDCallback,
211 this);
212 io_channels_[i] = make_pair(io_channel, tag);
Andrew de los Reyes3270f742010-07-15 22:28:14 -0700213 static int io_counter = 0;
214 io_counter++;
215 if (io_counter % 50 == 0) {
216 LOG(INFO) << "io_counter = " << io_counter;
217 }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000218 }
219
220 // Wet up a timeout callback for libcurl
221 long ms = 0;
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000222 CHECK_EQ(curl_multi_timeout(curl_multi_handle_, &ms), CURLM_OK);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000223 if (ms < 0) {
224 // From http://curl.haxx.se/libcurl/c/curl_multi_timeout.html:
225 // if libcurl returns a -1 timeout here, it just means that libcurl
226 // currently has no stored timeout value. You must not wait too long
227 // (more than a few seconds perhaps) before you call
228 // curl_multi_perform() again.
229 ms = idle_ms_;
230 }
Andrew de los Reyes3270f742010-07-15 22:28:14 -0700231 if (!timeout_source_) {
232 LOG(INFO) << "setting up timeout source:" << ms;
233 timeout_source_ = g_timeout_source_new(1000);
234 CHECK(timeout_source_);
235 g_source_set_callback(timeout_source_, StaticTimeoutCallback, this,
236 NULL);
237 g_source_attach(timeout_source_, NULL);
238 static int counter = 0;
239 counter++;
240 if (counter % 50 == 0) {
241 LOG(INFO) << "counter = " << counter;
242 }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000243 }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000244}
245
246bool LibcurlHttpFetcher::FDCallback(GIOChannel *source,
247 GIOCondition condition) {
Andrew de los Reyes9bbd1872010-07-16 14:52:29 -0700248 CurlPerformOnce();
Andrew de los Reyes3270f742010-07-15 22:28:14 -0700249 // We handle removing of this source elsewhere, so we always return true.
250 // The docs say, "the function should return FALSE if the event source
251 // should be removed."
252 // http://www.gtk.org/api/2.6/glib/glib-IO-Channels.html#GIOFunc
253 return true;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000254}
255
Andrew de los Reyes9bbd1872010-07-16 14:52:29 -0700256gboolean LibcurlHttpFetcher::RetryTimeoutCallback() {
257 ResumeTransfer(url_);
258 CurlPerformOnce();
259 return FALSE; // Don't have glib auto call this callback again
260}
261
Andrew de los Reyes3270f742010-07-15 22:28:14 -0700262gboolean LibcurlHttpFetcher::TimeoutCallback() {
263 if (!transfer_in_progress_)
264 return TRUE;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000265 // Since we will return false from this function, which tells glib to
266 // destroy the timeout callback, we must NULL it out here. This way, when
267 // setting up callback sources again, we won't try to delete this (doomed)
268 // timeout callback then.
269 // TODO(adlr): optimize by checking if we can keep this timeout callback.
Andrew de los Reyes3270f742010-07-15 22:28:14 -0700270 //timeout_source_ = NULL;
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
303} // namespace chromeos_update_engine