blob: 12b11a005206dc9ec7a2690067f0dbb47eef3b56 [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 Reyescb319332010-07-19 10:55:01 -070082void 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 }
Andrew de los Reyescb319332010-07-19 10:55:01 -0700119 return;
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 }
132}
133
134size_t LibcurlHttpFetcher::LibcurlWrite(void *ptr, size_t size, size_t nmemb) {
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000135 {
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.com49fdf182009-10-10 00:57:34 +0000146 if (delegate_)
147 delegate_->ReceivedBytes(this, reinterpret_cast<char*>(ptr), size * nmemb);
148 return size * nmemb;
149}
150
151void LibcurlHttpFetcher::Pause() {
152 CHECK(curl_handle_);
153 CHECK(transfer_in_progress_);
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000154 CHECK_EQ(curl_easy_pause(curl_handle_, CURLPAUSE_ALL), CURLE_OK);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000155}
156
157void LibcurlHttpFetcher::Unpause() {
158 CHECK(curl_handle_);
159 CHECK(transfer_in_progress_);
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000160 CHECK_EQ(curl_easy_pause(curl_handle_, CURLPAUSE_CONT), CURLE_OK);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000161}
162
163// This method sets up callbacks with the glib main loop.
164void 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.comc98a7ed2009-12-04 18:54:03 +0000177 CHECK_EQ(curl_multi_fdset(curl_multi_handle_, &fd_read, &fd_write,
178 &fd_exec, &fd_max), CURLM_OK);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000179
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.comc98a7ed2009-12-04 18:54:03 +0000191 FD_ISSET(i, &fd_exec))) {
rspangler@google.com49fdf182009-10-10 00:57:34 +0000192 // 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.com49fdf182009-10-10 00:57:34 +0000203 // 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 Reyes3270f742010-07-15 22:28:14 -0700212 static int io_counter = 0;
213 io_counter++;
214 if (io_counter % 50 == 0) {
215 LOG(INFO) << "io_counter = " << io_counter;
216 }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000217 }
218
219 // Wet up a timeout callback for libcurl
220 long ms = 0;
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000221 CHECK_EQ(curl_multi_timeout(curl_multi_handle_, &ms), CURLM_OK);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000222 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 Reyes3270f742010-07-15 22:28:14 -0700230 if (!timeout_source_) {
231 LOG(INFO) << "setting up timeout source:" << ms;
232 timeout_source_ = g_timeout_source_new(1000);
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.com49fdf182009-10-10 00:57:34 +0000242 }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000243}
244
245bool LibcurlHttpFetcher::FDCallback(GIOChannel *source,
246 GIOCondition condition) {
Andrew de los Reyes9bbd1872010-07-16 14:52:29 -0700247 CurlPerformOnce();
Andrew de los Reyes3270f742010-07-15 22:28:14 -0700248 // 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.com49fdf182009-10-10 00:57:34 +0000253}
254
Andrew de los Reyes9bbd1872010-07-16 14:52:29 -0700255gboolean LibcurlHttpFetcher::RetryTimeoutCallback() {
256 ResumeTransfer(url_);
257 CurlPerformOnce();
258 return FALSE; // Don't have glib auto call this callback again
259}
260
Andrew de los Reyes3270f742010-07-15 22:28:14 -0700261gboolean LibcurlHttpFetcher::TimeoutCallback() {
Andrew de los Reyescb319332010-07-19 10:55:01 -0700262 // 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 Reyes3270f742010-07-15 22:28:14 -0700265 if (!transfer_in_progress_)
266 return TRUE;
Andrew de los Reyes9bbd1872010-07-16 14:52:29 -0700267 CurlPerformOnce();
Andrew de los Reyes3270f742010-07-15 22:28:14 -0700268 return TRUE;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000269}
270
271void 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.comc98a7ed2009-12-04 18:54:03 +0000286 CHECK_EQ(curl_multi_remove_handle(curl_multi_handle_, curl_handle_),
287 CURLM_OK);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000288 }
289 curl_easy_cleanup(curl_handle_);
290 curl_handle_ = NULL;
291 }
292 if (curl_multi_handle_) {
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000293 CHECK_EQ(curl_multi_cleanup(curl_multi_handle_), CURLM_OK);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000294 curl_multi_handle_ = NULL;
295 }
296 transfer_in_progress_ = false;
297}
298
299} // namespace chromeos_update_engine