rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
Alex Deymo | 759c275 | 2014-03-17 21:09:36 -0700 | [diff] [blame^] | 5 | #ifndef CHROMEOS_PLATFORM_UPDATE_ENGINE_HTTP_FETCHER_H_ |
| 6 | #define CHROMEOS_PLATFORM_UPDATE_ENGINE_HTTP_FETCHER_H_ |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 7 | |
Andrew de los Reyes | 4516810 | 2010-11-22 11:13:50 -0800 | [diff] [blame] | 8 | #include <deque> |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 9 | #include <string> |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 10 | #include <vector> |
Andrew de los Reyes | 4516810 | 2010-11-22 11:13:50 -0800 | [diff] [blame] | 11 | |
| 12 | #include <base/basictypes.h> |
| 13 | #include <base/logging.h> |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 14 | #include <glib.h> |
Andrew de los Reyes | f3ed8e7 | 2011-02-16 10:35:46 -0800 | [diff] [blame] | 15 | #include <google/protobuf/stubs/common.h> |
Andrew de los Reyes | 4516810 | 2010-11-22 11:13:50 -0800 | [diff] [blame] | 16 | |
Gilad Arnold | 9dd1e7c | 2012-02-16 12:13:36 -0800 | [diff] [blame] | 17 | #include "http_common.h" |
Andrew de los Reyes | 4516810 | 2010-11-22 11:13:50 -0800 | [diff] [blame] | 18 | #include "update_engine/proxy_resolver.h" |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 19 | #include "update_engine/system_state.h" |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 20 | |
| 21 | // This class is a simple wrapper around an HTTP library (libcurl). We can |
| 22 | // easily mock out this interface for testing. |
| 23 | |
| 24 | // Implementations of this class should use asynchronous i/o. They can access |
| 25 | // the glib main loop to request callbacks when timers or file descriptors |
| 26 | // change. |
| 27 | |
| 28 | namespace chromeos_update_engine { |
| 29 | |
| 30 | class HttpFetcherDelegate; |
| 31 | |
| 32 | class HttpFetcher { |
| 33 | public: |
Andrew de los Reyes | 4516810 | 2010-11-22 11:13:50 -0800 | [diff] [blame] | 34 | // |proxy_resolver| is the resolver that will be consulted for proxy |
| 35 | // settings. It may be null, in which case direct connections will |
| 36 | // be used. Does not take ownership of the resolver. |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 37 | HttpFetcher(ProxyResolver* proxy_resolver, SystemState* system_state) |
Darin Petkov | cb46621 | 2010-08-26 09:40:11 -0700 | [diff] [blame] | 38 | : post_data_set_(false), |
| 39 | http_response_code_(0), |
Andrew de los Reyes | 4516810 | 2010-11-22 11:13:50 -0800 | [diff] [blame] | 40 | delegate_(NULL), |
| 41 | proxies_(1, kNoProxy), |
Andrew de los Reyes | f3ed8e7 | 2011-02-16 10:35:46 -0800 | [diff] [blame] | 42 | proxy_resolver_(proxy_resolver), |
| 43 | no_resolver_idle_id_(0), |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 44 | callback_(NULL), |
| 45 | system_state_(system_state) {} |
Andrew de los Reyes | f3ed8e7 | 2011-02-16 10:35:46 -0800 | [diff] [blame] | 46 | virtual ~HttpFetcher(); |
Darin Petkov | cb46621 | 2010-08-26 09:40:11 -0700 | [diff] [blame] | 47 | |
| 48 | void set_delegate(HttpFetcherDelegate* delegate) { delegate_ = delegate; } |
| 49 | HttpFetcherDelegate* delegate() const { return delegate_; } |
| 50 | int http_response_code() const { return http_response_code_; } |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 51 | |
| 52 | // Optional: Post data to the server. The HttpFetcher should make a copy |
Gilad Arnold | 9dd1e7c | 2012-02-16 12:13:36 -0800 | [diff] [blame] | 53 | // of this data and upload it via HTTP POST during the transfer. The type of |
| 54 | // the data is necessary for properly setting the Content-Type HTTP header. |
| 55 | void SetPostData(const void* data, size_t size, HttpContentType type); |
| 56 | |
| 57 | // Same without a specified Content-Type. |
Andrew de los Reyes | 4516810 | 2010-11-22 11:13:50 -0800 | [diff] [blame] | 58 | void SetPostData(const void* data, size_t size); |
| 59 | |
| 60 | // Proxy methods to set the proxies, then to pop them off. |
Andrew de los Reyes | f3ed8e7 | 2011-02-16 10:35:46 -0800 | [diff] [blame] | 61 | // Returns true on success. |
| 62 | bool ResolveProxiesForUrl(const std::string& url, |
| 63 | google::protobuf::Closure* callback); |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 64 | |
Andrew de los Reyes | 4516810 | 2010-11-22 11:13:50 -0800 | [diff] [blame] | 65 | void SetProxies(const std::deque<std::string>& proxies) { |
| 66 | proxies_ = proxies; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 67 | } |
Andrew de los Reyes | 4516810 | 2010-11-22 11:13:50 -0800 | [diff] [blame] | 68 | const std::string& GetCurrentProxy() const { |
| 69 | return proxies_.front(); |
| 70 | } |
| 71 | bool HasProxy() const { return !proxies_.empty(); } |
| 72 | void PopProxy() { proxies_.pop_front(); } |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 73 | |
Andrew de los Reyes | 3fd5d30 | 2010-10-07 20:07:18 -0700 | [diff] [blame] | 74 | // Downloading should resume from this offset |
| 75 | virtual void SetOffset(off_t offset) = 0; |
| 76 | |
Gilad Arnold | e4ad250 | 2011-12-29 17:08:54 -0800 | [diff] [blame] | 77 | // Set/unset the length of the range to be downloaded. |
| 78 | virtual void SetLength(size_t length) = 0; |
| 79 | virtual void UnsetLength() = 0; |
| 80 | |
Darin Petkov | 9ce452b | 2010-11-17 14:33:28 -0800 | [diff] [blame] | 81 | // Begins the transfer to the specified URL. This fetcher instance should not |
| 82 | // be destroyed until either TransferComplete, or TransferTerminated is |
| 83 | // called. |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 84 | virtual void BeginTransfer(const std::string& url) = 0; |
| 85 | |
Darin Petkov | 9ce452b | 2010-11-17 14:33:28 -0800 | [diff] [blame] | 86 | // Aborts the transfer. The transfer may not abort right away -- delegate's |
| 87 | // TransferTerminated() will be called when the transfer is actually done. |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 88 | virtual void TerminateTransfer() = 0; |
| 89 | |
| 90 | // If data is coming in too quickly, you can call Pause() to pause the |
| 91 | // transfer. The delegate will not have ReceivedBytes() called while |
| 92 | // an HttpFetcher is paused. |
| 93 | virtual void Pause() = 0; |
| 94 | |
| 95 | // Used to unpause an HttpFetcher and let the bytes stream in again. |
| 96 | // If a delegate is set, ReceivedBytes() may be called on it before |
| 97 | // Unpause() returns |
| 98 | virtual void Unpause() = 0; |
| 99 | |
Andrew de los Reyes | 3fd5d30 | 2010-10-07 20:07:18 -0700 | [diff] [blame] | 100 | // These two function are overloaded in LibcurlHttp fetcher to speed |
| 101 | // testing. |
| 102 | virtual void set_idle_seconds(int seconds) {} |
| 103 | virtual void set_retry_seconds(int seconds) {} |
| 104 | |
David Zeuthen | 34135a9 | 2013-08-06 11:16:16 -0700 | [diff] [blame] | 105 | // Sets the values used to time out the connection if the transfer |
| 106 | // rate is less than |low_speed_bps| bytes/sec for more than |
| 107 | // |low_speed_sec| seconds. |
| 108 | virtual void set_low_speed_limit(int low_speed_bps, int low_speed_sec) = 0; |
| 109 | |
| 110 | // Sets the connect timeout, e.g. the maximum amount of time willing |
| 111 | // to wait for establishing a connection to the server. |
| 112 | virtual void set_connect_timeout(int connect_timeout_seconds) = 0; |
| 113 | |
| 114 | // Sets the number of allowed retries. |
| 115 | virtual void set_max_retry_count(int max_retry_count) = 0; |
| 116 | |
Gilad Arnold | 48085ba | 2011-11-16 09:36:08 -0800 | [diff] [blame] | 117 | // Get the total number of bytes downloaded by fetcher. |
| 118 | virtual size_t GetBytesDownloaded() = 0; |
| 119 | |
Andrew de los Reyes | 819fef2 | 2010-12-17 11:33:58 -0800 | [diff] [blame] | 120 | ProxyResolver* proxy_resolver() const { return proxy_resolver_; } |
| 121 | |
| 122 | // These are used for testing: |
Andrew de los Reyes | 819fef2 | 2010-12-17 11:33:58 -0800 | [diff] [blame] | 123 | virtual void SetBuildType(bool is_official) {} |
| 124 | |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 125 | SystemState* GetSystemState() { |
| 126 | return system_state_; |
| 127 | } |
| 128 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 129 | protected: |
| 130 | // The URL we're actively fetching from |
| 131 | std::string url_; |
| 132 | |
| 133 | // POST data for the transfer, and whether or not it was ever set |
| 134 | bool post_data_set_; |
| 135 | std::vector<char> post_data_; |
Gilad Arnold | 9dd1e7c | 2012-02-16 12:13:36 -0800 | [diff] [blame] | 136 | HttpContentType post_content_type_; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 137 | |
Darin Petkov | cb46621 | 2010-08-26 09:40:11 -0700 | [diff] [blame] | 138 | // The server's HTTP response code from the last transfer. This |
| 139 | // field should be set to 0 when a new transfer is initiated, and |
| 140 | // set to the response code when the transfer is complete. |
| 141 | int http_response_code_; |
| 142 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 143 | // The delegate; may be NULL. |
| 144 | HttpFetcherDelegate* delegate_; |
Andrew de los Reyes | 4516810 | 2010-11-22 11:13:50 -0800 | [diff] [blame] | 145 | |
| 146 | // Proxy servers |
| 147 | std::deque<std::string> proxies_; |
Andrew de los Reyes | f3ed8e7 | 2011-02-16 10:35:46 -0800 | [diff] [blame] | 148 | |
Andrew de los Reyes | 4516810 | 2010-11-22 11:13:50 -0800 | [diff] [blame] | 149 | ProxyResolver* const proxy_resolver_; |
| 150 | |
Andrew de los Reyes | f3ed8e7 | 2011-02-16 10:35:46 -0800 | [diff] [blame] | 151 | // The ID of the idle callback, used when we have no proxy resolver. |
| 152 | guint no_resolver_idle_id_; |
| 153 | |
| 154 | // Callback for when we are resolving proxies |
| 155 | google::protobuf::Closure* callback_; |
| 156 | |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 157 | // Global system context. |
| 158 | SystemState* system_state_; |
| 159 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 160 | private: |
Andrew de los Reyes | f3ed8e7 | 2011-02-16 10:35:46 -0800 | [diff] [blame] | 161 | // Callback from the proxy resolver |
| 162 | void ProxiesResolved(const std::deque<std::string>& proxies); |
| 163 | static void StaticProxiesResolved(const std::deque<std::string>& proxies, |
| 164 | void* data) { |
| 165 | reinterpret_cast<HttpFetcher*>(data)->ProxiesResolved(proxies); |
| 166 | } |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 167 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 168 | DISALLOW_COPY_AND_ASSIGN(HttpFetcher); |
| 169 | }; |
| 170 | |
| 171 | // Interface for delegates |
| 172 | class HttpFetcherDelegate { |
| 173 | public: |
Andrew de los Reyes | 34e41a1 | 2010-10-26 20:07:58 -0700 | [diff] [blame] | 174 | // Called every time bytes are received. |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 175 | virtual void ReceivedBytes(HttpFetcher* fetcher, |
| 176 | const char* bytes, |
| 177 | int length) = 0; |
| 178 | |
Andrew de los Reyes | 34e41a1 | 2010-10-26 20:07:58 -0700 | [diff] [blame] | 179 | // Called if the fetcher seeks to a particular offset. |
| 180 | virtual void SeekToOffset(off_t offset) {} |
| 181 | |
Andrew de los Reyes | 819fef2 | 2010-12-17 11:33:58 -0800 | [diff] [blame] | 182 | // When a transfer has completed, exactly one of these two methods will be |
| 183 | // called. TransferTerminated is called when the transfer has been aborted |
| 184 | // through TerminateTransfer. TransferComplete is called in all other |
| 185 | // situations. It's OK to destroy the |fetcher| object in this callback. |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 186 | virtual void TransferComplete(HttpFetcher* fetcher, bool successful) = 0; |
Darin Petkov | 9ce452b | 2010-11-17 14:33:28 -0800 | [diff] [blame] | 187 | virtual void TransferTerminated(HttpFetcher* fetcher) {} |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 188 | }; |
| 189 | |
| 190 | } // namespace chromeos_update_engine |
| 191 | |
Alex Deymo | 759c275 | 2014-03-17 21:09:36 -0700 | [diff] [blame^] | 192 | #endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_HTTP_FETCHER_H_ |