| 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 #ifndef CHROMEOS_PLATFORM_UPDATE_ENGINE_LIBCURL_HTTP_FETCHER_H__ | 5 #ifndef CHROMEOS_PLATFORM_UPDATE_ENGINE_LIBCURL_HTTP_FETCHER_H__ |
| 6 #define CHROMEOS_PLATFORM_UPDATE_ENGINE_LIBCURL_HTTP_FETCHER_H__ | 6 #define CHROMEOS_PLATFORM_UPDATE_ENGINE_LIBCURL_HTTP_FETCHER_H__ |
| 7 | 7 |
| 8 #include <map> | 8 #include <map> |
| 9 #include <string> | 9 #include <string> |
| 10 #include <curl/curl.h> | 10 #include <curl/curl.h> |
| 11 #include <glib.h> | 11 #include <glib.h> |
| 12 #include "base/basictypes.h" | 12 #include "base/basictypes.h" |
| 13 #include "base/logging.h" | 13 #include "base/logging.h" |
| 14 #include "update_engine/http_fetcher.h" | 14 #include "update_engine/http_fetcher.h" |
| 15 | 15 |
| 16 // This is a concrete implementation of HttpFetcher that uses libcurl to do the | 16 // This is a concrete implementation of HttpFetcher that uses libcurl to do the |
| 17 // http work. | 17 // http work. |
| 18 | 18 |
| 19 namespace chromeos_update_engine { | 19 namespace chromeos_update_engine { |
| 20 | 20 |
| 21 class LibcurlHttpFetcher : public HttpFetcher { | 21 class LibcurlHttpFetcher : public HttpFetcher { |
| 22 public: | 22 public: |
| 23 static const int kMaxRedirects = 10; | 23 static const int kMaxRedirects = 10; |
| 24 | 24 |
| 25 LibcurlHttpFetcher() | 25 LibcurlHttpFetcher() |
| 26 : curl_multi_handle_(NULL), | 26 : curl_multi_handle_(NULL), |
| 27 curl_handle_(NULL), | 27 curl_handle_(NULL), |
| 28 timeout_source_(NULL), | 28 timeout_source_(NULL), |
| 29 transfer_in_progress_(false), | 29 transfer_in_progress_(false), |
| 30 transfer_size_(0), |
| 31 bytes_downloaded_(0), |
| 32 resume_offset_(0), |
| 30 retry_count_(0), | 33 retry_count_(0), |
| 31 retry_seconds_(60), | 34 retry_seconds_(60), |
| 32 idle_seconds_(1) {} | 35 idle_seconds_(1), |
| 36 in_write_callback_(false), |
| 37 terminate_requested_(false) {} |
| 33 | 38 |
| 34 // Cleans up all internal state. Does not notify delegate | 39 // Cleans up all internal state. Does not notify delegate |
| 35 ~LibcurlHttpFetcher(); | 40 ~LibcurlHttpFetcher(); |
| 36 | 41 |
| 42 void SetOffset(off_t offset) { bytes_downloaded_ = offset; } |
| 43 |
| 37 // Begins the transfer if it hasn't already begun. | 44 // Begins the transfer if it hasn't already begun. |
| 38 virtual void BeginTransfer(const std::string& url); | 45 virtual void BeginTransfer(const std::string& url); |
| 39 | 46 |
| 40 // If the transfer is in progress, aborts the transfer early. | 47 // If the transfer is in progress, aborts the transfer early. |
| 41 // The transfer cannot be resumed. | 48 // The transfer cannot be resumed. |
| 42 virtual void TerminateTransfer(); | 49 virtual void TerminateTransfer(); |
| 43 | 50 |
| 44 // Suspend the transfer by calling curl_easy_pause(CURLPAUSE_ALL). | 51 // Suspend the transfer by calling curl_easy_pause(CURLPAUSE_ALL). |
| 45 virtual void Pause(); | 52 virtual void Pause(); |
| 46 | 53 |
| 47 // Resume the transfer by calling curl_easy_pause(CURLPAUSE_CONT). | 54 // Resume the transfer by calling curl_easy_pause(CURLPAUSE_CONT). |
| 48 virtual void Unpause(); | 55 virtual void Unpause(); |
| 49 | 56 |
| 50 // Libcurl sometimes asks to be called back after some time while | 57 // Libcurl sometimes asks to be called back after some time while |
| 51 // leaving that time unspecified. In that case, we pick a reasonable | 58 // leaving that time unspecified. In that case, we pick a reasonable |
| 52 // default of one second, but it can be overridden here. This is | 59 // default of one second, but it can be overridden here. This is |
| 53 // primarily useful for testing. | 60 // primarily useful for testing. |
| 54 // From http://curl.haxx.se/libcurl/c/curl_multi_timeout.html: | 61 // From http://curl.haxx.se/libcurl/c/curl_multi_timeout.html: |
| 55 // if libcurl returns a -1 timeout here, it just means that libcurl | 62 // if libcurl returns a -1 timeout here, it just means that libcurl |
| 56 // currently has no stored timeout value. You must not wait too long | 63 // currently has no stored timeout value. You must not wait too long |
| 57 // (more than a few seconds perhaps) before you call | 64 // (more than a few seconds perhaps) before you call |
| 58 // curl_multi_perform() again. | 65 // curl_multi_perform() again. |
| 59 void set_idle_seconds(int seconds) { idle_seconds_ = seconds; } | 66 void set_idle_seconds(int seconds) { idle_seconds_ = seconds; } |
| 60 | 67 |
| 61 // Sets the retry timeout. Useful for testing. | 68 // Sets the retry timeout. Useful for testing. |
| 62 void set_retry_seconds(int seconds) { retry_seconds_ = seconds; } | 69 void set_retry_seconds(int seconds) { retry_seconds_ = seconds; } |
| 63 | 70 |
| 64 private: | 71 private: |
| 72 // Asks libcurl for the http response code and stores it in the object. |
| 73 void GetHttpResponseCode(); |
| 74 |
| 65 // Resumes a transfer where it left off. This will use the | 75 // Resumes a transfer where it left off. This will use the |
| 66 // HTTP Range: header to make a new connection from where the last | 76 // HTTP Range: header to make a new connection from where the last |
| 67 // left off. | 77 // left off. |
| 68 virtual void ResumeTransfer(const std::string& url); | 78 virtual void ResumeTransfer(const std::string& url); |
| 69 | 79 |
| 70 // These two methods are for glib main loop callbacks. They are called | 80 // These two methods are for glib main loop callbacks. They are called |
| 71 // when either a file descriptor is ready for work or when a timer | 81 // when either a file descriptor is ready for work or when a timer |
| 72 // has fired. The static versions are shims for libcurl which has a C API. | 82 // has fired. The static versions are shims for libcurl which has a C API. |
| 73 bool FDCallback(GIOChannel *source, GIOCondition condition); | 83 bool FDCallback(GIOChannel *source, GIOCondition condition); |
| 74 static gboolean StaticFDCallback(GIOChannel *source, | 84 static gboolean StaticFDCallback(GIOChannel *source, |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 128 bool transfer_in_progress_; | 138 bool transfer_in_progress_; |
| 129 | 139 |
| 130 // The transfer size. -1 if not known. | 140 // The transfer size. -1 if not known. |
| 131 off_t transfer_size_; | 141 off_t transfer_size_; |
| 132 | 142 |
| 133 // How many bytes have been downloaded and sent to the delegate. | 143 // How many bytes have been downloaded and sent to the delegate. |
| 134 off_t bytes_downloaded_; | 144 off_t bytes_downloaded_; |
| 135 | 145 |
| 136 // If we resumed an earlier transfer, data offset that we used for the | 146 // If we resumed an earlier transfer, data offset that we used for the |
| 137 // new connection. 0 otherwise. | 147 // new connection. 0 otherwise. |
| 148 // In this class, resume refers to resuming a dropped HTTP connection, |
| 149 // not to resuming an interrupted download. |
| 138 off_t resume_offset_; | 150 off_t resume_offset_; |
| 139 | 151 |
| 140 // Number of resumes performed. | 152 // Number of resumes performed. |
| 141 int retry_count_; | 153 int retry_count_; |
| 142 | 154 |
| 143 // Seconds to wait before retrying a resume. | 155 // Seconds to wait before retrying a resume. |
| 144 int retry_seconds_; | 156 int retry_seconds_; |
| 145 | 157 |
| 146 // Seconds to wait before asking libcurl to "perform". | 158 // Seconds to wait before asking libcurl to "perform". |
| 147 int idle_seconds_; | 159 int idle_seconds_; |
| 148 | 160 |
| 161 // If true, we are currently performing a write callback on the delegate. |
| 162 bool in_write_callback_; |
| 163 |
| 164 // We can't clean everything up while we're in a write callback, so |
| 165 // if we get a terminate request, queue it until we can handle it. |
| 166 bool terminate_requested_; |
| 167 |
| 149 DISALLOW_COPY_AND_ASSIGN(LibcurlHttpFetcher); | 168 DISALLOW_COPY_AND_ASSIGN(LibcurlHttpFetcher); |
| 150 }; | 169 }; |
| 151 | 170 |
| 152 } // namespace chromeos_update_engine | 171 } // namespace chromeos_update_engine |
| 153 | 172 |
| 154 #endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_LIBCURL_HTTP_FETCHER_H__ | 173 #endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_LIBCURL_HTTP_FETCHER_H__ |
| OLD | NEW |