blob: 11e8e9f7c4b236bd15988dec25e368ba72f504b1 [file] [log] [blame]
Alex Deymoaea4c1c2015-08-19 20:24:43 -07001//
2// Copyright (C) 2009 The Android Open Source Project
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15//
rspangler@google.com49fdf182009-10-10 00:57:34 +000016
Alex Deymo39910dc2015-11-09 17:04:30 -080017#ifndef UPDATE_ENGINE_COMMON_HTTP_FETCHER_H_
18#define UPDATE_ENGINE_COMMON_HTTP_FETCHER_H_
rspangler@google.com49fdf182009-10-10 00:57:34 +000019
Andrew de los Reyes45168102010-11-22 11:13:50 -080020#include <deque>
rspangler@google.com49fdf182009-10-10 00:57:34 +000021#include <string>
adlr@google.comc98a7ed2009-12-04 18:54:03 +000022#include <vector>
Andrew de los Reyes45168102010-11-22 11:13:50 -080023
Alex Vakulenko4906c1c2014-08-21 13:17:44 -070024#include <base/callback.h>
Andrew de los Reyes45168102010-11-22 11:13:50 -080025#include <base/logging.h>
Ben Chan05735a12014-09-03 07:48:22 -070026#include <base/macros.h>
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070027#include <brillo/message_loops/message_loop.h>
Andrew de los Reyes45168102010-11-22 11:13:50 -080028
Alex Deymo39910dc2015-11-09 17:04:30 -080029#include "update_engine/common/http_common.h"
Andrew de los Reyes45168102010-11-22 11:13:50 -080030#include "update_engine/proxy_resolver.h"
rspangler@google.com49fdf182009-10-10 00:57:34 +000031
32// This class is a simple wrapper around an HTTP library (libcurl). We can
33// easily mock out this interface for testing.
34
35// Implementations of this class should use asynchronous i/o. They can access
Alex Deymo60ca1a72015-06-18 18:19:15 -070036// the MessageLoop to request callbacks when timers or file descriptors change.
rspangler@google.com49fdf182009-10-10 00:57:34 +000037
38namespace chromeos_update_engine {
39
40class HttpFetcherDelegate;
41
42class HttpFetcher {
43 public:
Andrew de los Reyes45168102010-11-22 11:13:50 -080044 // |proxy_resolver| is the resolver that will be consulted for proxy
45 // settings. It may be null, in which case direct connections will
46 // be used. Does not take ownership of the resolver.
Alex Deymoc1c17b42015-11-23 03:53:15 -030047 HttpFetcher(ProxyResolver* proxy_resolver)
Darin Petkovcb466212010-08-26 09:40:11 -070048 : post_data_set_(false),
49 http_response_code_(0),
Alex Vakulenko88b591f2014-08-28 16:48:57 -070050 delegate_(nullptr),
Andrew de los Reyes45168102010-11-22 11:13:50 -080051 proxies_(1, kNoProxy),
Andrew de los Reyesf3ed8e72011-02-16 10:35:46 -080052 proxy_resolver_(proxy_resolver),
Alex Deymoc1c17b42015-11-23 03:53:15 -030053 callback_(nullptr) {}
Andrew de los Reyesf3ed8e72011-02-16 10:35:46 -080054 virtual ~HttpFetcher();
Darin Petkovcb466212010-08-26 09:40:11 -070055
56 void set_delegate(HttpFetcherDelegate* delegate) { delegate_ = delegate; }
57 HttpFetcherDelegate* delegate() const { return delegate_; }
58 int http_response_code() const { return http_response_code_; }
rspangler@google.com49fdf182009-10-10 00:57:34 +000059
60 // Optional: Post data to the server. The HttpFetcher should make a copy
Gilad Arnold9dd1e7c2012-02-16 12:13:36 -080061 // of this data and upload it via HTTP POST during the transfer. The type of
62 // the data is necessary for properly setting the Content-Type HTTP header.
63 void SetPostData(const void* data, size_t size, HttpContentType type);
64
65 // Same without a specified Content-Type.
Andrew de los Reyes45168102010-11-22 11:13:50 -080066 void SetPostData(const void* data, size_t size);
67
68 // Proxy methods to set the proxies, then to pop them off.
Andrew de los Reyesf3ed8e72011-02-16 10:35:46 -080069 // Returns true on success.
Alex Deymo60ca1a72015-06-18 18:19:15 -070070 bool ResolveProxiesForUrl(const std::string& url,
71 const base::Closure& callback);
Jay Srinivasan43488792012-06-19 00:25:31 -070072
Andrew de los Reyes45168102010-11-22 11:13:50 -080073 void SetProxies(const std::deque<std::string>& proxies) {
74 proxies_ = proxies;
rspangler@google.com49fdf182009-10-10 00:57:34 +000075 }
Andrew de los Reyes45168102010-11-22 11:13:50 -080076 const std::string& GetCurrentProxy() const {
77 return proxies_.front();
78 }
79 bool HasProxy() const { return !proxies_.empty(); }
80 void PopProxy() { proxies_.pop_front(); }
rspangler@google.com49fdf182009-10-10 00:57:34 +000081
Andrew de los Reyes3fd5d302010-10-07 20:07:18 -070082 // Downloading should resume from this offset
83 virtual void SetOffset(off_t offset) = 0;
84
Gilad Arnolde4ad2502011-12-29 17:08:54 -080085 // Set/unset the length of the range to be downloaded.
86 virtual void SetLength(size_t length) = 0;
87 virtual void UnsetLength() = 0;
88
Darin Petkov9ce452b2010-11-17 14:33:28 -080089 // Begins the transfer to the specified URL. This fetcher instance should not
90 // be destroyed until either TransferComplete, or TransferTerminated is
91 // called.
rspangler@google.com49fdf182009-10-10 00:57:34 +000092 virtual void BeginTransfer(const std::string& url) = 0;
93
Darin Petkov9ce452b2010-11-17 14:33:28 -080094 // Aborts the transfer. The transfer may not abort right away -- delegate's
95 // TransferTerminated() will be called when the transfer is actually done.
rspangler@google.com49fdf182009-10-10 00:57:34 +000096 virtual void TerminateTransfer() = 0;
97
98 // If data is coming in too quickly, you can call Pause() to pause the
99 // transfer. The delegate will not have ReceivedBytes() called while
100 // an HttpFetcher is paused.
101 virtual void Pause() = 0;
102
103 // Used to unpause an HttpFetcher and let the bytes stream in again.
104 // If a delegate is set, ReceivedBytes() may be called on it before
105 // Unpause() returns
106 virtual void Unpause() = 0;
107
Andrew de los Reyes3fd5d302010-10-07 20:07:18 -0700108 // These two function are overloaded in LibcurlHttp fetcher to speed
109 // testing.
110 virtual void set_idle_seconds(int seconds) {}
111 virtual void set_retry_seconds(int seconds) {}
112
David Zeuthen34135a92013-08-06 11:16:16 -0700113 // Sets the values used to time out the connection if the transfer
114 // rate is less than |low_speed_bps| bytes/sec for more than
115 // |low_speed_sec| seconds.
116 virtual void set_low_speed_limit(int low_speed_bps, int low_speed_sec) = 0;
117
118 // Sets the connect timeout, e.g. the maximum amount of time willing
119 // to wait for establishing a connection to the server.
120 virtual void set_connect_timeout(int connect_timeout_seconds) = 0;
121
122 // Sets the number of allowed retries.
123 virtual void set_max_retry_count(int max_retry_count) = 0;
124
Gilad Arnold48085ba2011-11-16 09:36:08 -0800125 // Get the total number of bytes downloaded by fetcher.
126 virtual size_t GetBytesDownloaded() = 0;
127
Andrew de los Reyes819fef22010-12-17 11:33:58 -0800128 ProxyResolver* proxy_resolver() const { return proxy_resolver_; }
129
rspangler@google.com49fdf182009-10-10 00:57:34 +0000130 protected:
131 // The URL we're actively fetching from
132 std::string url_;
133
134 // POST data for the transfer, and whether or not it was ever set
135 bool post_data_set_;
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700136 brillo::Blob post_data_;
Gilad Arnold9dd1e7c2012-02-16 12:13:36 -0800137 HttpContentType post_content_type_;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000138
Darin Petkovcb466212010-08-26 09:40:11 -0700139 // The server's HTTP response code from the last transfer. This
140 // field should be set to 0 when a new transfer is initiated, and
141 // set to the response code when the transfer is complete.
142 int http_response_code_;
143
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700144 // The delegate; may be null.
rspangler@google.com49fdf182009-10-10 00:57:34 +0000145 HttpFetcherDelegate* delegate_;
Andrew de los Reyes45168102010-11-22 11:13:50 -0800146
147 // Proxy servers
148 std::deque<std::string> proxies_;
Andrew de los Reyesf3ed8e72011-02-16 10:35:46 -0800149
Andrew de los Reyes45168102010-11-22 11:13:50 -0800150 ProxyResolver* const proxy_resolver_;
151
Andrew de los Reyesf3ed8e72011-02-16 10:35:46 -0800152 // The ID of the idle callback, used when we have no proxy resolver.
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700153 brillo::MessageLoop::TaskId no_resolver_idle_id_{
154 brillo::MessageLoop::kTaskIdNull};
Andrew de los Reyesf3ed8e72011-02-16 10:35:46 -0800155
156 // Callback for when we are resolving proxies
Alex Deymo60ca1a72015-06-18 18:19:15 -0700157 std::unique_ptr<base::Closure> callback_;
Andrew de los Reyesf3ed8e72011-02-16 10:35:46 -0800158
rspangler@google.com49fdf182009-10-10 00:57:34 +0000159 private:
Andrew de los Reyesf3ed8e72011-02-16 10:35:46 -0800160 // Callback from the proxy resolver
161 void ProxiesResolved(const std::deque<std::string>& proxies);
162 static void StaticProxiesResolved(const std::deque<std::string>& proxies,
163 void* data) {
164 reinterpret_cast<HttpFetcher*>(data)->ProxiesResolved(proxies);
165 }
Jay Srinivasan43488792012-06-19 00:25:31 -0700166
Alex Deymo60ca1a72015-06-18 18:19:15 -0700167 // Callback used to run the proxy resolver callback when there is no
168 // |proxy_resolver_|.
169 void NoProxyResolverCallback();
170
rspangler@google.com49fdf182009-10-10 00:57:34 +0000171 DISALLOW_COPY_AND_ASSIGN(HttpFetcher);
172};
173
174// Interface for delegates
175class HttpFetcherDelegate {
176 public:
Alex Deymoe8948702014-11-11 21:44:45 -0800177 virtual ~HttpFetcherDelegate() = default;
178
Andrew de los Reyes34e41a12010-10-26 20:07:58 -0700179 // Called every time bytes are received.
rspangler@google.com49fdf182009-10-10 00:57:34 +0000180 virtual void ReceivedBytes(HttpFetcher* fetcher,
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800181 const void* bytes,
182 size_t length) = 0;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000183
Andrew de los Reyes34e41a12010-10-26 20:07:58 -0700184 // Called if the fetcher seeks to a particular offset.
185 virtual void SeekToOffset(off_t offset) {}
186
Andrew de los Reyes819fef22010-12-17 11:33:58 -0800187 // When a transfer has completed, exactly one of these two methods will be
188 // called. TransferTerminated is called when the transfer has been aborted
189 // through TerminateTransfer. TransferComplete is called in all other
190 // situations. It's OK to destroy the |fetcher| object in this callback.
rspangler@google.com49fdf182009-10-10 00:57:34 +0000191 virtual void TransferComplete(HttpFetcher* fetcher, bool successful) = 0;
Darin Petkov9ce452b2010-11-17 14:33:28 -0800192 virtual void TransferTerminated(HttpFetcher* fetcher) {}
rspangler@google.com49fdf182009-10-10 00:57:34 +0000193};
194
195} // namespace chromeos_update_engine
196
Alex Deymo39910dc2015-11-09 17:04:30 -0800197#endif // UPDATE_ENGINE_COMMON_HTTP_FETCHER_H_