Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2009 The Chromium OS Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "update_engine/libcurl_http_fetcher.h" | 5 #include "update_engine/libcurl_http_fetcher.h" |
| 6 | |
| 6 #include <algorithm> | 7 #include <algorithm> |
| 7 #include "base/logging.h" | 8 #include <string> |
| 9 | |
| 10 #include <base/logging.h> | |
| 11 | |
| 12 #include "update_engine/dbus_interface.h" | |
| 13 #include "update_engine/flimflam_proxy.h" | |
| 14 #include "update_engine/utils.h" | |
| 8 | 15 |
| 9 using std::max; | 16 using std::max; |
| 10 using std::make_pair; | 17 using std::make_pair; |
| 18 using std::string; | |
| 11 | 19 |
| 12 // This is a concrete implementation of HttpFetcher that uses libcurl to do the | 20 // This is a concrete implementation of HttpFetcher that uses libcurl to do the |
| 13 // http work. | 21 // http work. |
| 14 | 22 |
| 15 namespace chromeos_update_engine { | 23 namespace chromeos_update_engine { |
| 16 | 24 |
| 17 namespace { | 25 namespace { |
| 18 const int kMaxRetriesCount = 20; | 26 const int kMaxRetriesCount = 20; |
| 19 const char kCACertificatesPath[] = "/usr/share/update_engine/ca-certificates"; | 27 const char kCACertificatesPath[] = "/usr/share/update_engine/ca-certificates"; |
| 20 } | 28 } // namespace {} |
| 21 | 29 |
| 22 LibcurlHttpFetcher::~LibcurlHttpFetcher() { | 30 LibcurlHttpFetcher::~LibcurlHttpFetcher() { |
| 23 CleanUp(); | 31 CleanUp(); |
| 24 } | 32 } |
| 25 | 33 |
| 34 // On error, returns false. | |
| 35 bool LibcurlHttpFetcher::ConnectionIsExpensive() const { | |
| 36 if (force_connection_type_) | |
| 37 return forced_expensive_connection_; | |
| 38 NetworkConnectionType type; | |
| 39 ConcreteDbusGlib dbus_iface; | |
| 40 TEST_AND_RETURN_FALSE(FlimFlamProxy::GetConnectionType(&dbus_iface, &type)); | |
| 41 LOG(INFO) << "We are connected via " | |
| 42 << FlimFlamProxy::StringForConnectionType(type); | |
| 43 return FlimFlamProxy::IsExpensiveConnectionType(type); | |
| 44 } | |
| 45 | |
| 26 void LibcurlHttpFetcher::ResumeTransfer(const std::string& url) { | 46 void LibcurlHttpFetcher::ResumeTransfer(const std::string& url) { |
| 27 LOG(INFO) << "Starting/Resuming transfer"; | 47 LOG(INFO) << "Starting/Resuming transfer"; |
| 28 CHECK(!transfer_in_progress_); | 48 CHECK(!transfer_in_progress_); |
| 29 url_ = url; | 49 url_ = url; |
| 30 curl_multi_handle_ = curl_multi_init(); | 50 curl_multi_handle_ = curl_multi_init(); |
| 31 CHECK(curl_multi_handle_); | 51 CHECK(curl_multi_handle_); |
| 32 | 52 |
| 33 curl_handle_ = curl_easy_init(); | 53 curl_handle_ = curl_easy_init(); |
| 34 CHECK(curl_handle_); | 54 CHECK(curl_handle_); |
| 35 | 55 |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 47 // Resume from where we left off | 67 // Resume from where we left off |
| 48 resume_offset_ = bytes_downloaded_; | 68 resume_offset_ = bytes_downloaded_; |
| 49 CHECK_EQ(curl_easy_setopt(curl_handle_, | 69 CHECK_EQ(curl_easy_setopt(curl_handle_, |
| 50 CURLOPT_RESUME_FROM_LARGE, | 70 CURLOPT_RESUME_FROM_LARGE, |
| 51 bytes_downloaded_), CURLE_OK); | 71 bytes_downloaded_), CURLE_OK); |
| 52 } | 72 } |
| 53 | 73 |
| 54 CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_WRITEDATA, this), CURLE_OK); | 74 CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_WRITEDATA, this), CURLE_OK); |
| 55 CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_WRITEFUNCTION, | 75 CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_WRITEFUNCTION, |
| 56 StaticLibcurlWrite), CURLE_OK); | 76 StaticLibcurlWrite), CURLE_OK); |
| 57 CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_URL, url_.c_str()), CURLE_OK); | 77 |
| 78 string url_to_use(url_); | |
| 79 if (ConnectionIsExpensive()) { | |
|
petkov
2010/10/21 05:15:19
If the device switches from WiFi to 3G in the midd
adlr
2010/10/21 19:41:14
This code path is called for every connection or r
| |
| 80 LOG(INFO) << "Not initiating HTTP connection b/c we are on an expensive" | |
| 81 << " connection"; | |
| 82 url_to_use = ""; // Sabotage the URL | |
| 83 } | |
| 84 | |
| 85 CHECK_EQ(curl_easy_setopt(curl_handle_, | |
| 86 CURLOPT_URL, | |
| 87 url_to_use.c_str()), | |
| 88 CURLE_OK); | |
| 58 | 89 |
| 59 // If the connection drops under 10 bytes/sec for 3 minutes, reconnect. | 90 // If the connection drops under 10 bytes/sec for 3 minutes, reconnect. |
| 60 CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_LOW_SPEED_LIMIT, 10), | 91 CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_LOW_SPEED_LIMIT, 10), |
| 61 CURLE_OK); | 92 CURLE_OK); |
| 62 CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_LOW_SPEED_TIME, 3 * 60), | 93 CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_LOW_SPEED_TIME, 3 * 60), |
| 63 CURLE_OK); | 94 CURLE_OK); |
| 64 | 95 |
| 65 // By default, libcurl doesn't follow redirections. Allow up to | 96 // By default, libcurl doesn't follow redirections. Allow up to |
| 66 // |kMaxRedirects| redirections. | 97 // |kMaxRedirects| redirections. |
| 67 CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_FOLLOWLOCATION, 1), CURLE_OK); | 98 CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_FOLLOWLOCATION, 1), CURLE_OK); |
| (...skipping 235 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 303 void LibcurlHttpFetcher::GetHttpResponseCode() { | 334 void LibcurlHttpFetcher::GetHttpResponseCode() { |
| 304 long http_response_code = 0; | 335 long http_response_code = 0; |
| 305 if (curl_easy_getinfo(curl_handle_, | 336 if (curl_easy_getinfo(curl_handle_, |
| 306 CURLINFO_RESPONSE_CODE, | 337 CURLINFO_RESPONSE_CODE, |
| 307 &http_response_code) == CURLE_OK) { | 338 &http_response_code) == CURLE_OK) { |
| 308 http_response_code_ = static_cast<int>(http_response_code); | 339 http_response_code_ = static_cast<int>(http_response_code); |
| 309 } | 340 } |
| 310 } | 341 } |
| 311 | 342 |
| 312 } // namespace chromeos_update_engine | 343 } // namespace chromeos_update_engine |
| OLD | NEW |