blob: 9989ba266bde273075359f2fd529fe4b11493be6 [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>
Chris Masone790e62e2010-08-12 10:41:18 -07007#include "base/logging.h"
adlr@google.comc98a7ed2009-12-04 18:54:03 +00008
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
Darin Petkov192ced42010-07-23 16:20:24 -070058 // If the connection drops under 10 bytes/sec for 3 minutes, reconnect.
Andrew de los Reyes3270f742010-07-15 22:28:14 -070059 CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_LOW_SPEED_LIMIT, 10),
60 CURLE_OK);
Darin Petkov192ced42010-07-23 16:20:24 -070061 CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_LOW_SPEED_TIME, 3 * 60),
Andrew de los Reyes3270f742010-07-15 22:28:14 -070062 CURLE_OK);
63
Darin Petkov41c2fcf2010-08-25 13:14:48 -070064 // By default, libcurl doesn't follow redirections. Allow up to
65 // |kMaxRedirects| redirections.
66 CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_FOLLOWLOCATION, 1),
67 CURLE_OK);
68 CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_MAXREDIRS, kMaxRedirects),
69 CURLE_OK);
70
adlr@google.comc98a7ed2009-12-04 18:54:03 +000071 CHECK_EQ(curl_multi_add_handle(curl_multi_handle_, curl_handle_), CURLM_OK);
rspangler@google.com49fdf182009-10-10 00:57:34 +000072 transfer_in_progress_ = true;
rspangler@google.com49fdf182009-10-10 00:57:34 +000073}
74
adlr@google.comc98a7ed2009-12-04 18:54:03 +000075// Begins the transfer, which must not have already been started.
76void LibcurlHttpFetcher::BeginTransfer(const std::string& url) {
77 transfer_size_ = -1;
78 bytes_downloaded_ = 0;
79 resume_offset_ = 0;
Andrew de los Reyes9bbd1872010-07-16 14:52:29 -070080 retry_count_ = 0;
Darin Petkovcb466212010-08-26 09:40:11 -070081 http_response_code_ = 0;
Andrew de los Reyes9bbd1872010-07-16 14:52:29 -070082 ResumeTransfer(url);
83 CurlPerformOnce();
adlr@google.comc98a7ed2009-12-04 18:54:03 +000084}
85
rspangler@google.com49fdf182009-10-10 00:57:34 +000086void LibcurlHttpFetcher::TerminateTransfer() {
87 CleanUp();
88}
89
Andrew de los Reyescb319332010-07-19 10:55:01 -070090void LibcurlHttpFetcher::CurlPerformOnce() {
rspangler@google.com49fdf182009-10-10 00:57:34 +000091 CHECK(transfer_in_progress_);
92 int running_handles = 0;
93 CURLMcode retcode = CURLM_CALL_MULTI_PERFORM;
94
95 // libcurl may request that we immediately call curl_multi_perform after it
96 // returns, so we do. libcurl promises that curl_multi_perform will not block.
97 while (CURLM_CALL_MULTI_PERFORM == retcode) {
98 retcode = curl_multi_perform(curl_multi_handle_, &running_handles);
99 }
100 if (0 == running_handles) {
Andrew de los Reyes9bbd1872010-07-16 14:52:29 -0700101 long http_response_code = 0;
102 if (curl_easy_getinfo(curl_handle_,
103 CURLINFO_RESPONSE_CODE,
104 &http_response_code) == CURLE_OK) {
105 LOG(INFO) << "HTTP response code: " << http_response_code;
106 } else {
107 LOG(ERROR) << "Unable to get http response code.";
108 }
Darin Petkovcb466212010-08-26 09:40:11 -0700109 http_response_code_ = static_cast<int>(http_response_code);
Darin Petkov192ced42010-07-23 16:20:24 -0700110
rspangler@google.com49fdf182009-10-10 00:57:34 +0000111 // we're done!
112 CleanUp();
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000113
114 if ((transfer_size_ >= 0) && (bytes_downloaded_ < transfer_size_)) {
Andrew de los Reyes3270f742010-07-15 22:28:14 -0700115 // Need to restart transfer
Andrew de los Reyes9bbd1872010-07-16 14:52:29 -0700116 retry_count_++;
117 LOG(INFO) << "Restarting transfer b/c we finished, had downloaded "
118 << bytes_downloaded_ << " bytes, but transfer_size_ is "
119 << transfer_size_ << ". retry_count: " << retry_count_;
120 if (retry_count_ > kMaxRetriesCount) {
121 if (delegate_)
122 delegate_->TransferComplete(this, false); // success
123 } else {
Darin Petkovb83371f2010-08-17 09:34:49 -0700124 g_timeout_add_seconds(retry_seconds_,
Darin Petkov9b111652010-08-16 11:46:25 -0700125 &LibcurlHttpFetcher::StaticRetryTimeoutCallback,
126 this);
Andrew de los Reyes9bbd1872010-07-16 14:52:29 -0700127 }
Andrew de los Reyescb319332010-07-19 10:55:01 -0700128 return;
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000129 } else {
130 if (delegate_) {
Andrew de los Reyesfb4ad7d2010-07-19 10:43:46 -0700131 // success is when http_response_code is 2xx
132 bool success = (http_response_code >= 200) &&
133 (http_response_code < 300);
134 delegate_->TransferComplete(this, success);
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000135 }
136 }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000137 } else {
138 // set up callback
139 SetupMainloopSources();
140 }
141}
142
143size_t LibcurlHttpFetcher::LibcurlWrite(void *ptr, size_t size, size_t nmemb) {
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000144 {
145 double transfer_size_double;
146 CHECK_EQ(curl_easy_getinfo(curl_handle_,
147 CURLINFO_CONTENT_LENGTH_DOWNLOAD,
148 &transfer_size_double), CURLE_OK);
149 off_t new_transfer_size = static_cast<off_t>(transfer_size_double);
150 if (new_transfer_size > 0) {
151 transfer_size_ = resume_offset_ + new_transfer_size;
152 }
153 }
154 bytes_downloaded_ += size * nmemb;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000155 if (delegate_)
156 delegate_->ReceivedBytes(this, reinterpret_cast<char*>(ptr), size * nmemb);
157 return size * nmemb;
158}
159
160void LibcurlHttpFetcher::Pause() {
161 CHECK(curl_handle_);
162 CHECK(transfer_in_progress_);
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000163 CHECK_EQ(curl_easy_pause(curl_handle_, CURLPAUSE_ALL), CURLE_OK);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000164}
165
166void LibcurlHttpFetcher::Unpause() {
167 CHECK(curl_handle_);
168 CHECK(transfer_in_progress_);
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000169 CHECK_EQ(curl_easy_pause(curl_handle_, CURLPAUSE_CONT), CURLE_OK);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000170}
171
172// This method sets up callbacks with the glib main loop.
173void LibcurlHttpFetcher::SetupMainloopSources() {
174 fd_set fd_read;
175 fd_set fd_write;
176 fd_set fd_exec;
177
178 FD_ZERO(&fd_read);
179 FD_ZERO(&fd_write);
180 FD_ZERO(&fd_exec);
181
182 int fd_max = 0;
183
184 // Ask libcurl for the set of file descriptors we should track on its
185 // behalf.
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000186 CHECK_EQ(curl_multi_fdset(curl_multi_handle_, &fd_read, &fd_write,
187 &fd_exec, &fd_max), CURLM_OK);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000188
189 // We should iterate through all file descriptors up to libcurl's fd_max or
190 // the highest one we're tracking, whichever is larger
191 if (!io_channels_.empty())
192 fd_max = max(fd_max, io_channels_.rbegin()->first);
193
194 // For each fd, if we're not tracking it, track it. If we are tracking it,
195 // but libcurl doesn't care about it anymore, stop tracking it.
196 // After this loop, there should be exactly as many GIOChannel objects
197 // in io_channels_ as there are fds that we're tracking.
198 for (int i = 0; i <= fd_max; i++) {
199 if (!(FD_ISSET(i, &fd_read) || FD_ISSET(i, &fd_write) ||
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000200 FD_ISSET(i, &fd_exec))) {
rspangler@google.com49fdf182009-10-10 00:57:34 +0000201 // if we have an outstanding io_channel, remove it
202 if (io_channels_.find(i) != io_channels_.end()) {
203 g_source_remove(io_channels_[i].second);
204 g_io_channel_unref(io_channels_[i].first);
205 io_channels_.erase(io_channels_.find(i));
206 }
207 continue;
208 }
209 // If we are already tracking this fd, continue.
210 if (io_channels_.find(i) != io_channels_.end())
211 continue;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000212 // We must track a new fd
213 GIOChannel *io_channel = g_io_channel_unix_new(i);
214 guint tag = g_io_add_watch(
215 io_channel,
216 static_cast<GIOCondition>(G_IO_IN | G_IO_OUT | G_IO_PRI |
217 G_IO_ERR | G_IO_HUP),
218 &StaticFDCallback,
219 this);
220 io_channels_[i] = make_pair(io_channel, tag);
Andrew de los Reyes3270f742010-07-15 22:28:14 -0700221 static int io_counter = 0;
222 io_counter++;
223 if (io_counter % 50 == 0) {
224 LOG(INFO) << "io_counter = " << io_counter;
225 }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000226 }
227
Darin Petkovb83371f2010-08-17 09:34:49 -0700228 // Set up a timeout callback for libcurl.
Andrew de los Reyes3270f742010-07-15 22:28:14 -0700229 if (!timeout_source_) {
Darin Petkovb83371f2010-08-17 09:34:49 -0700230 LOG(INFO) << "Setting up timeout source: " << idle_seconds_ << " seconds.";
231 timeout_source_ = g_timeout_source_new_seconds(idle_seconds_);
232 g_source_set_callback(timeout_source_, StaticTimeoutCallback, this, NULL);
Andrew de los Reyes3270f742010-07-15 22:28:14 -0700233 g_source_attach(timeout_source_, NULL);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000234 }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000235}
236
237bool LibcurlHttpFetcher::FDCallback(GIOChannel *source,
238 GIOCondition condition) {
Andrew de los Reyes9bbd1872010-07-16 14:52:29 -0700239 CurlPerformOnce();
Andrew de los Reyes3270f742010-07-15 22:28:14 -0700240 // We handle removing of this source elsewhere, so we always return true.
241 // The docs say, "the function should return FALSE if the event source
242 // should be removed."
243 // http://www.gtk.org/api/2.6/glib/glib-IO-Channels.html#GIOFunc
244 return true;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000245}
246
Andrew de los Reyes9bbd1872010-07-16 14:52:29 -0700247gboolean LibcurlHttpFetcher::RetryTimeoutCallback() {
248 ResumeTransfer(url_);
249 CurlPerformOnce();
250 return FALSE; // Don't have glib auto call this callback again
251}
252
Andrew de los Reyes3270f742010-07-15 22:28:14 -0700253gboolean LibcurlHttpFetcher::TimeoutCallback() {
Andrew de los Reyescb319332010-07-19 10:55:01 -0700254 // We always return true, even if we don't want glib to call us back.
255 // We will remove the event source separately if we don't want to
256 // be called back.
Andrew de los Reyes3270f742010-07-15 22:28:14 -0700257 if (!transfer_in_progress_)
258 return TRUE;
Andrew de los Reyes9bbd1872010-07-16 14:52:29 -0700259 CurlPerformOnce();
Andrew de los Reyes3270f742010-07-15 22:28:14 -0700260 return TRUE;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000261}
262
263void LibcurlHttpFetcher::CleanUp() {
264 if (timeout_source_) {
265 g_source_destroy(timeout_source_);
266 timeout_source_ = NULL;
267 }
268
269 for (IOChannels::iterator it = io_channels_.begin();
270 it != io_channels_.end(); ++it) {
271 g_source_remove(it->second.second);
272 g_io_channel_unref(it->second.first);
273 }
274 io_channels_.clear();
275
276 if (curl_handle_) {
277 if (curl_multi_handle_) {
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000278 CHECK_EQ(curl_multi_remove_handle(curl_multi_handle_, curl_handle_),
279 CURLM_OK);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000280 }
281 curl_easy_cleanup(curl_handle_);
282 curl_handle_ = NULL;
283 }
284 if (curl_multi_handle_) {
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000285 CHECK_EQ(curl_multi_cleanup(curl_multi_handle_), CURLM_OK);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000286 curl_multi_handle_ = NULL;
287 }
288 transfer_in_progress_ = false;
289}
290
291} // namespace chromeos_update_engine