blob: 0d19a9acbba2df67545af27dd5c3ab49e66d0b3c [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
Gilad Arnoldcf175a02014-07-10 16:48:47 -07005#ifndef UPDATE_ENGINE_HTTP_FETCHER_H_
6#define UPDATE_ENGINE_HTTP_FETCHER_H_
rspangler@google.com49fdf182009-10-10 00:57:34 +00007
Andrew de los Reyes45168102010-11-22 11:13:50 -08008#include <deque>
rspangler@google.com49fdf182009-10-10 00:57:34 +00009#include <string>
adlr@google.comc98a7ed2009-12-04 18:54:03 +000010#include <vector>
Andrew de los Reyes45168102010-11-22 11:13:50 -080011
Alex Vakulenko4906c1c2014-08-21 13:17:44 -070012#include <base/callback.h>
Andrew de los Reyes45168102010-11-22 11:13:50 -080013#include <base/logging.h>
Ben Chan05735a12014-09-03 07:48:22 -070014#include <base/macros.h>
Alex Deymo60ca1a72015-06-18 18:19:15 -070015#include <chromeos/message_loops/message_loop.h>
Andrew de los Reyes45168102010-11-22 11:13:50 -080016
Alex Deymo04f2b382014-03-21 15:45:17 -070017#include "update_engine/http_common.h"
Andrew de los Reyes45168102010-11-22 11:13:50 -080018#include "update_engine/proxy_resolver.h"
Jay Srinivasan43488792012-06-19 00:25:31 -070019#include "update_engine/system_state.h"
rspangler@google.com49fdf182009-10-10 00:57:34 +000020
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
Alex Deymo60ca1a72015-06-18 18:19:15 -070025// the MessageLoop to request callbacks when timers or file descriptors change.
rspangler@google.com49fdf182009-10-10 00:57:34 +000026
27namespace chromeos_update_engine {
28
29class HttpFetcherDelegate;
30
31class HttpFetcher {
32 public:
Andrew de los Reyes45168102010-11-22 11:13:50 -080033 // |proxy_resolver| is the resolver that will be consulted for proxy
34 // settings. It may be null, in which case direct connections will
35 // be used. Does not take ownership of the resolver.
Jay Srinivasan43488792012-06-19 00:25:31 -070036 HttpFetcher(ProxyResolver* proxy_resolver, SystemState* system_state)
Darin Petkovcb466212010-08-26 09:40:11 -070037 : post_data_set_(false),
38 http_response_code_(0),
Alex Vakulenko88b591f2014-08-28 16:48:57 -070039 delegate_(nullptr),
Andrew de los Reyes45168102010-11-22 11:13:50 -080040 proxies_(1, kNoProxy),
Andrew de los Reyesf3ed8e72011-02-16 10:35:46 -080041 proxy_resolver_(proxy_resolver),
Alex Vakulenko88b591f2014-08-28 16:48:57 -070042 callback_(nullptr),
Jay Srinivasan43488792012-06-19 00:25:31 -070043 system_state_(system_state) {}
Andrew de los Reyesf3ed8e72011-02-16 10:35:46 -080044 virtual ~HttpFetcher();
Darin Petkovcb466212010-08-26 09:40:11 -070045
46 void set_delegate(HttpFetcherDelegate* delegate) { delegate_ = delegate; }
47 HttpFetcherDelegate* delegate() const { return delegate_; }
48 int http_response_code() const { return http_response_code_; }
rspangler@google.com49fdf182009-10-10 00:57:34 +000049
50 // Optional: Post data to the server. The HttpFetcher should make a copy
Gilad Arnold9dd1e7c2012-02-16 12:13:36 -080051 // of this data and upload it via HTTP POST during the transfer. The type of
52 // the data is necessary for properly setting the Content-Type HTTP header.
53 void SetPostData(const void* data, size_t size, HttpContentType type);
54
55 // Same without a specified Content-Type.
Andrew de los Reyes45168102010-11-22 11:13:50 -080056 void SetPostData(const void* data, size_t size);
57
58 // Proxy methods to set the proxies, then to pop them off.
Andrew de los Reyesf3ed8e72011-02-16 10:35:46 -080059 // Returns true on success.
Alex Deymo60ca1a72015-06-18 18:19:15 -070060 bool ResolveProxiesForUrl(const std::string& url,
61 const base::Closure& callback);
Jay Srinivasan43488792012-06-19 00:25:31 -070062
Andrew de los Reyes45168102010-11-22 11:13:50 -080063 void SetProxies(const std::deque<std::string>& proxies) {
64 proxies_ = proxies;
rspangler@google.com49fdf182009-10-10 00:57:34 +000065 }
Andrew de los Reyes45168102010-11-22 11:13:50 -080066 const std::string& GetCurrentProxy() const {
67 return proxies_.front();
68 }
69 bool HasProxy() const { return !proxies_.empty(); }
70 void PopProxy() { proxies_.pop_front(); }
rspangler@google.com49fdf182009-10-10 00:57:34 +000071
Andrew de los Reyes3fd5d302010-10-07 20:07:18 -070072 // Downloading should resume from this offset
73 virtual void SetOffset(off_t offset) = 0;
74
Gilad Arnolde4ad2502011-12-29 17:08:54 -080075 // Set/unset the length of the range to be downloaded.
76 virtual void SetLength(size_t length) = 0;
77 virtual void UnsetLength() = 0;
78
Darin Petkov9ce452b2010-11-17 14:33:28 -080079 // Begins the transfer to the specified URL. This fetcher instance should not
80 // be destroyed until either TransferComplete, or TransferTerminated is
81 // called.
rspangler@google.com49fdf182009-10-10 00:57:34 +000082 virtual void BeginTransfer(const std::string& url) = 0;
83
Darin Petkov9ce452b2010-11-17 14:33:28 -080084 // Aborts the transfer. The transfer may not abort right away -- delegate's
85 // TransferTerminated() will be called when the transfer is actually done.
rspangler@google.com49fdf182009-10-10 00:57:34 +000086 virtual void TerminateTransfer() = 0;
87
88 // If data is coming in too quickly, you can call Pause() to pause the
89 // transfer. The delegate will not have ReceivedBytes() called while
90 // an HttpFetcher is paused.
91 virtual void Pause() = 0;
92
93 // Used to unpause an HttpFetcher and let the bytes stream in again.
94 // If a delegate is set, ReceivedBytes() may be called on it before
95 // Unpause() returns
96 virtual void Unpause() = 0;
97
Andrew de los Reyes3fd5d302010-10-07 20:07:18 -070098 // These two function are overloaded in LibcurlHttp fetcher to speed
99 // testing.
100 virtual void set_idle_seconds(int seconds) {}
101 virtual void set_retry_seconds(int seconds) {}
102
David Zeuthen34135a92013-08-06 11:16:16 -0700103 // Sets the values used to time out the connection if the transfer
104 // rate is less than |low_speed_bps| bytes/sec for more than
105 // |low_speed_sec| seconds.
106 virtual void set_low_speed_limit(int low_speed_bps, int low_speed_sec) = 0;
107
108 // Sets the connect timeout, e.g. the maximum amount of time willing
109 // to wait for establishing a connection to the server.
110 virtual void set_connect_timeout(int connect_timeout_seconds) = 0;
111
112 // Sets the number of allowed retries.
113 virtual void set_max_retry_count(int max_retry_count) = 0;
114
Gilad Arnold48085ba2011-11-16 09:36:08 -0800115 // Get the total number of bytes downloaded by fetcher.
116 virtual size_t GetBytesDownloaded() = 0;
117
Andrew de los Reyes819fef22010-12-17 11:33:58 -0800118 ProxyResolver* proxy_resolver() const { return proxy_resolver_; }
119
Alex Deymo7984bf02014-04-02 20:41:57 -0700120 // Returns the global SystemState.
Jay Srinivasan43488792012-06-19 00:25:31 -0700121 SystemState* GetSystemState() {
122 return system_state_;
123 }
124
rspangler@google.com49fdf182009-10-10 00:57:34 +0000125 protected:
126 // The URL we're actively fetching from
127 std::string url_;
128
129 // POST data for the transfer, and whether or not it was ever set
130 bool post_data_set_;
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800131 chromeos::Blob post_data_;
Gilad Arnold9dd1e7c2012-02-16 12:13:36 -0800132 HttpContentType post_content_type_;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000133
Darin Petkovcb466212010-08-26 09:40:11 -0700134 // The server's HTTP response code from the last transfer. This
135 // field should be set to 0 when a new transfer is initiated, and
136 // set to the response code when the transfer is complete.
137 int http_response_code_;
138
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700139 // The delegate; may be null.
rspangler@google.com49fdf182009-10-10 00:57:34 +0000140 HttpFetcherDelegate* delegate_;
Andrew de los Reyes45168102010-11-22 11:13:50 -0800141
142 // Proxy servers
143 std::deque<std::string> proxies_;
Andrew de los Reyesf3ed8e72011-02-16 10:35:46 -0800144
Andrew de los Reyes45168102010-11-22 11:13:50 -0800145 ProxyResolver* const proxy_resolver_;
146
Andrew de los Reyesf3ed8e72011-02-16 10:35:46 -0800147 // The ID of the idle callback, used when we have no proxy resolver.
Alex Deymo60ca1a72015-06-18 18:19:15 -0700148 chromeos::MessageLoop::TaskId no_resolver_idle_id_{
149 chromeos::MessageLoop::kTaskIdNull};
Andrew de los Reyesf3ed8e72011-02-16 10:35:46 -0800150
151 // Callback for when we are resolving proxies
Alex Deymo60ca1a72015-06-18 18:19:15 -0700152 std::unique_ptr<base::Closure> callback_;
Andrew de los Reyesf3ed8e72011-02-16 10:35:46 -0800153
Jay Srinivasan43488792012-06-19 00:25:31 -0700154 // Global system context.
155 SystemState* system_state_;
156
rspangler@google.com49fdf182009-10-10 00:57:34 +0000157 private:
Andrew de los Reyesf3ed8e72011-02-16 10:35:46 -0800158 // Callback from the proxy resolver
159 void ProxiesResolved(const std::deque<std::string>& proxies);
160 static void StaticProxiesResolved(const std::deque<std::string>& proxies,
161 void* data) {
162 reinterpret_cast<HttpFetcher*>(data)->ProxiesResolved(proxies);
163 }
Jay Srinivasan43488792012-06-19 00:25:31 -0700164
Alex Deymo60ca1a72015-06-18 18:19:15 -0700165 // Callback used to run the proxy resolver callback when there is no
166 // |proxy_resolver_|.
167 void NoProxyResolverCallback();
168
rspangler@google.com49fdf182009-10-10 00:57:34 +0000169 DISALLOW_COPY_AND_ASSIGN(HttpFetcher);
170};
171
172// Interface for delegates
173class HttpFetcherDelegate {
174 public:
Alex Deymoe8948702014-11-11 21:44:45 -0800175 virtual ~HttpFetcherDelegate() = default;
176
Andrew de los Reyes34e41a12010-10-26 20:07:58 -0700177 // Called every time bytes are received.
rspangler@google.com49fdf182009-10-10 00:57:34 +0000178 virtual void ReceivedBytes(HttpFetcher* fetcher,
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800179 const void* bytes,
180 size_t length) = 0;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000181
Andrew de los Reyes34e41a12010-10-26 20:07:58 -0700182 // Called if the fetcher seeks to a particular offset.
183 virtual void SeekToOffset(off_t offset) {}
184
Andrew de los Reyes819fef22010-12-17 11:33:58 -0800185 // When a transfer has completed, exactly one of these two methods will be
186 // called. TransferTerminated is called when the transfer has been aborted
187 // through TerminateTransfer. TransferComplete is called in all other
188 // situations. It's OK to destroy the |fetcher| object in this callback.
rspangler@google.com49fdf182009-10-10 00:57:34 +0000189 virtual void TransferComplete(HttpFetcher* fetcher, bool successful) = 0;
Darin Petkov9ce452b2010-11-17 14:33:28 -0800190 virtual void TransferTerminated(HttpFetcher* fetcher) {}
rspangler@google.com49fdf182009-10-10 00:57:34 +0000191};
192
193} // namespace chromeos_update_engine
194
Gilad Arnoldcf175a02014-07-10 16:48:47 -0700195#endif // UPDATE_ENGINE_HTTP_FETCHER_H_