xref: /aosp_15_r20/system/update_engine/libcurl_http_fetcher.h (revision 5a9231315b4521097b8dc3750bc806fcafe0c72f)
1*5a923131SAndroid Build Coastguard Worker //
2*5a923131SAndroid Build Coastguard Worker // Copyright (C) 2009 The Android Open Source Project
3*5a923131SAndroid Build Coastguard Worker //
4*5a923131SAndroid Build Coastguard Worker // Licensed under the Apache License, Version 2.0 (the "License");
5*5a923131SAndroid Build Coastguard Worker // you may not use this file except in compliance with the License.
6*5a923131SAndroid Build Coastguard Worker // You may obtain a copy of the License at
7*5a923131SAndroid Build Coastguard Worker //
8*5a923131SAndroid Build Coastguard Worker //      http://www.apache.org/licenses/LICENSE-2.0
9*5a923131SAndroid Build Coastguard Worker //
10*5a923131SAndroid Build Coastguard Worker // Unless required by applicable law or agreed to in writing, software
11*5a923131SAndroid Build Coastguard Worker // distributed under the License is distributed on an "AS IS" BASIS,
12*5a923131SAndroid Build Coastguard Worker // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*5a923131SAndroid Build Coastguard Worker // See the License for the specific language governing permissions and
14*5a923131SAndroid Build Coastguard Worker // limitations under the License.
15*5a923131SAndroid Build Coastguard Worker //
16*5a923131SAndroid Build Coastguard Worker 
17*5a923131SAndroid Build Coastguard Worker #ifndef UPDATE_ENGINE_LIBCURL_HTTP_FETCHER_H_
18*5a923131SAndroid Build Coastguard Worker #define UPDATE_ENGINE_LIBCURL_HTTP_FETCHER_H_
19*5a923131SAndroid Build Coastguard Worker 
20*5a923131SAndroid Build Coastguard Worker #include <map>
21*5a923131SAndroid Build Coastguard Worker #include <memory>
22*5a923131SAndroid Build Coastguard Worker #include <string>
23*5a923131SAndroid Build Coastguard Worker #include <utility>
24*5a923131SAndroid Build Coastguard Worker 
25*5a923131SAndroid Build Coastguard Worker #include <curl/curl.h>
26*5a923131SAndroid Build Coastguard Worker 
27*5a923131SAndroid Build Coastguard Worker #include <base/files/file_descriptor_watcher_posix.h>
28*5a923131SAndroid Build Coastguard Worker #include <base/logging.h>
29*5a923131SAndroid Build Coastguard Worker #include <android-base/macros.h>
30*5a923131SAndroid Build Coastguard Worker #include <brillo/message_loops/message_loop.h>
31*5a923131SAndroid Build Coastguard Worker 
32*5a923131SAndroid Build Coastguard Worker #include "update_engine/certificate_checker.h"
33*5a923131SAndroid Build Coastguard Worker #include "update_engine/common/hardware_interface.h"
34*5a923131SAndroid Build Coastguard Worker #include "update_engine/common/http_fetcher.h"
35*5a923131SAndroid Build Coastguard Worker 
36*5a923131SAndroid Build Coastguard Worker // This is a concrete implementation of HttpFetcher that uses libcurl to do the
37*5a923131SAndroid Build Coastguard Worker // http work.
38*5a923131SAndroid Build Coastguard Worker 
39*5a923131SAndroid Build Coastguard Worker namespace chromeos_update_engine {
40*5a923131SAndroid Build Coastguard Worker 
41*5a923131SAndroid Build Coastguard Worker // |UnresolvedHostStateMachine| is a representation of internal state machine of
42*5a923131SAndroid Build Coastguard Worker // |LibcurlHttpFetcher|.
43*5a923131SAndroid Build Coastguard Worker class UnresolvedHostStateMachine {
44*5a923131SAndroid Build Coastguard Worker  public:
45*5a923131SAndroid Build Coastguard Worker   UnresolvedHostStateMachine() = default;
46*5a923131SAndroid Build Coastguard Worker   enum class State {
47*5a923131SAndroid Build Coastguard Worker     kInit = 0,
48*5a923131SAndroid Build Coastguard Worker     kRetry = 1,
49*5a923131SAndroid Build Coastguard Worker     kRetriedSuccess = 2,
50*5a923131SAndroid Build Coastguard Worker     kNotRetry = 3,
51*5a923131SAndroid Build Coastguard Worker   };
52*5a923131SAndroid Build Coastguard Worker 
GetState()53*5a923131SAndroid Build Coastguard Worker   State GetState() { return state_; }
54*5a923131SAndroid Build Coastguard Worker 
55*5a923131SAndroid Build Coastguard Worker   // Updates the following internal state machine:
56*5a923131SAndroid Build Coastguard Worker   //
57*5a923131SAndroid Build Coastguard Worker   // |kInit|
58*5a923131SAndroid Build Coastguard Worker   //   |
59*5a923131SAndroid Build Coastguard Worker   //   |
60*5a923131SAndroid Build Coastguard Worker   //   \/
61*5a923131SAndroid Build Coastguard Worker   // (Try, host Unresolved)
62*5a923131SAndroid Build Coastguard Worker   //   |
63*5a923131SAndroid Build Coastguard Worker   //   |
64*5a923131SAndroid Build Coastguard Worker   //   \/
65*5a923131SAndroid Build Coastguard Worker   // |kRetry| --> (Retry, host resolved)
66*5a923131SAndroid Build Coastguard Worker   //   |                                  |
67*5a923131SAndroid Build Coastguard Worker   //   |                                  |
68*5a923131SAndroid Build Coastguard Worker   //   \/                                 \/
69*5a923131SAndroid Build Coastguard Worker   // (Retry, host Unresolved)    |kRetriedSuccess|
70*5a923131SAndroid Build Coastguard Worker   //   |
71*5a923131SAndroid Build Coastguard Worker   //   |
72*5a923131SAndroid Build Coastguard Worker   //   \/
73*5a923131SAndroid Build Coastguard Worker   // |kNotRetry|
74*5a923131SAndroid Build Coastguard Worker   //
75*5a923131SAndroid Build Coastguard Worker   void UpdateState(bool failed_to_resolve_host);
76*5a923131SAndroid Build Coastguard Worker 
77*5a923131SAndroid Build Coastguard Worker  private:
78*5a923131SAndroid Build Coastguard Worker   State state_ = {State::kInit};
79*5a923131SAndroid Build Coastguard Worker 
80*5a923131SAndroid Build Coastguard Worker   DISALLOW_COPY_AND_ASSIGN(UnresolvedHostStateMachine);
81*5a923131SAndroid Build Coastguard Worker };
82*5a923131SAndroid Build Coastguard Worker 
83*5a923131SAndroid Build Coastguard Worker class LibcurlHttpFetcher : public HttpFetcher {
84*5a923131SAndroid Build Coastguard Worker  public:
85*5a923131SAndroid Build Coastguard Worker   explicit LibcurlHttpFetcher(HardwareInterface* hardware);
86*5a923131SAndroid Build Coastguard Worker 
87*5a923131SAndroid Build Coastguard Worker   // Cleans up all internal state. Does not notify delegate
88*5a923131SAndroid Build Coastguard Worker   ~LibcurlHttpFetcher() override;
89*5a923131SAndroid Build Coastguard Worker 
SetOffset(off_t offset)90*5a923131SAndroid Build Coastguard Worker   void SetOffset(off_t offset) override { bytes_downloaded_ = offset; }
91*5a923131SAndroid Build Coastguard Worker 
SetLength(size_t length)92*5a923131SAndroid Build Coastguard Worker   void SetLength(size_t length) override { download_length_ = length; }
UnsetLength()93*5a923131SAndroid Build Coastguard Worker   void UnsetLength() override { SetLength(0); }
94*5a923131SAndroid Build Coastguard Worker 
95*5a923131SAndroid Build Coastguard Worker   // Begins the transfer if it hasn't already begun.
96*5a923131SAndroid Build Coastguard Worker   void BeginTransfer(const std::string& url) override;
97*5a923131SAndroid Build Coastguard Worker 
98*5a923131SAndroid Build Coastguard Worker   // If the transfer is in progress, aborts the transfer early. The transfer
99*5a923131SAndroid Build Coastguard Worker   // cannot be resumed.
100*5a923131SAndroid Build Coastguard Worker   void TerminateTransfer() override;
101*5a923131SAndroid Build Coastguard Worker 
102*5a923131SAndroid Build Coastguard Worker   // Pass the headers to libcurl.
103*5a923131SAndroid Build Coastguard Worker   void SetHeader(const std::string& header_name,
104*5a923131SAndroid Build Coastguard Worker                  const std::string& header_value) override;
105*5a923131SAndroid Build Coastguard Worker 
106*5a923131SAndroid Build Coastguard Worker   bool GetHeader(const std::string& header_name,
107*5a923131SAndroid Build Coastguard Worker                  std::string* header_value) const override;
108*5a923131SAndroid Build Coastguard Worker 
109*5a923131SAndroid Build Coastguard Worker   // Suspend the transfer by calling curl_easy_pause(CURLPAUSE_ALL).
110*5a923131SAndroid Build Coastguard Worker   void Pause() override;
111*5a923131SAndroid Build Coastguard Worker 
112*5a923131SAndroid Build Coastguard Worker   // Resume the transfer by calling curl_easy_pause(CURLPAUSE_CONT).
113*5a923131SAndroid Build Coastguard Worker   void Unpause() override;
114*5a923131SAndroid Build Coastguard Worker 
115*5a923131SAndroid Build Coastguard Worker   // Libcurl sometimes asks to be called back after some time while
116*5a923131SAndroid Build Coastguard Worker   // leaving that time unspecified. In that case, we pick a reasonable
117*5a923131SAndroid Build Coastguard Worker   // default of one second, but it can be overridden here. This is
118*5a923131SAndroid Build Coastguard Worker   // primarily useful for testing.
119*5a923131SAndroid Build Coastguard Worker   // From http://curl.haxx.se/libcurl/c/curl_multi_timeout.html:
120*5a923131SAndroid Build Coastguard Worker   //     if libcurl returns a -1 timeout here, it just means that libcurl
121*5a923131SAndroid Build Coastguard Worker   //     currently has no stored timeout value. You must not wait too long
122*5a923131SAndroid Build Coastguard Worker   //     (more than a few seconds perhaps) before you call
123*5a923131SAndroid Build Coastguard Worker   //     curl_multi_perform() again.
set_idle_seconds(int seconds)124*5a923131SAndroid Build Coastguard Worker   void set_idle_seconds(int seconds) override { idle_seconds_ = seconds; }
125*5a923131SAndroid Build Coastguard Worker 
126*5a923131SAndroid Build Coastguard Worker   // Sets the retry timeout. Useful for testing.
set_retry_seconds(int seconds)127*5a923131SAndroid Build Coastguard Worker   void set_retry_seconds(int seconds) override { retry_seconds_ = seconds; }
128*5a923131SAndroid Build Coastguard Worker 
set_no_network_max_retries(int retries)129*5a923131SAndroid Build Coastguard Worker   void set_no_network_max_retries(int retries) {
130*5a923131SAndroid Build Coastguard Worker     no_network_max_retries_ = retries;
131*5a923131SAndroid Build Coastguard Worker   }
132*5a923131SAndroid Build Coastguard Worker 
get_no_network_max_retries()133*5a923131SAndroid Build Coastguard Worker   int get_no_network_max_retries() { return no_network_max_retries_; }
134*5a923131SAndroid Build Coastguard Worker 
set_server_to_check(ServerToCheck server_to_check)135*5a923131SAndroid Build Coastguard Worker   void set_server_to_check(ServerToCheck server_to_check) {
136*5a923131SAndroid Build Coastguard Worker     server_to_check_ = server_to_check;
137*5a923131SAndroid Build Coastguard Worker   }
138*5a923131SAndroid Build Coastguard Worker 
GetBytesDownloaded()139*5a923131SAndroid Build Coastguard Worker   size_t GetBytesDownloaded() override {
140*5a923131SAndroid Build Coastguard Worker     return static_cast<size_t>(bytes_downloaded_);
141*5a923131SAndroid Build Coastguard Worker   }
142*5a923131SAndroid Build Coastguard Worker 
set_low_speed_limit(int low_speed_bps,int low_speed_sec)143*5a923131SAndroid Build Coastguard Worker   void set_low_speed_limit(int low_speed_bps, int low_speed_sec) override {
144*5a923131SAndroid Build Coastguard Worker     low_speed_limit_bps_ = low_speed_bps;
145*5a923131SAndroid Build Coastguard Worker     low_speed_time_seconds_ = low_speed_sec;
146*5a923131SAndroid Build Coastguard Worker   }
147*5a923131SAndroid Build Coastguard Worker 
set_connect_timeout(int connect_timeout_seconds)148*5a923131SAndroid Build Coastguard Worker   void set_connect_timeout(int connect_timeout_seconds) override {
149*5a923131SAndroid Build Coastguard Worker     connect_timeout_seconds_ = connect_timeout_seconds;
150*5a923131SAndroid Build Coastguard Worker   }
151*5a923131SAndroid Build Coastguard Worker 
set_max_retry_count(int max_retry_count)152*5a923131SAndroid Build Coastguard Worker   void set_max_retry_count(int max_retry_count) override {
153*5a923131SAndroid Build Coastguard Worker     max_retry_count_ = max_retry_count;
154*5a923131SAndroid Build Coastguard Worker   }
155*5a923131SAndroid Build Coastguard Worker 
set_is_update_check(bool is_update_check)156*5a923131SAndroid Build Coastguard Worker   void set_is_update_check(bool is_update_check) {
157*5a923131SAndroid Build Coastguard Worker     is_update_check_ = is_update_check;
158*5a923131SAndroid Build Coastguard Worker   }
159*5a923131SAndroid Build Coastguard Worker 
160*5a923131SAndroid Build Coastguard Worker  private:
161*5a923131SAndroid Build Coastguard Worker   FRIEND_TEST(LibcurlHttpFetcherTest, HostResolvedTest);
162*5a923131SAndroid Build Coastguard Worker 
163*5a923131SAndroid Build Coastguard Worker   // libcurl's CURLOPT_CLOSESOCKETFUNCTION callback function. Called when
164*5a923131SAndroid Build Coastguard Worker   // closing a socket created with the CURLOPT_OPENSOCKETFUNCTION callback.
165*5a923131SAndroid Build Coastguard Worker   static int LibcurlCloseSocketCallback(void* clientp, curl_socket_t item);
166*5a923131SAndroid Build Coastguard Worker 
167*5a923131SAndroid Build Coastguard Worker   // Asks libcurl for the http response code and stores it in the object.
168*5a923131SAndroid Build Coastguard Worker   virtual void GetHttpResponseCode();
169*5a923131SAndroid Build Coastguard Worker 
170*5a923131SAndroid Build Coastguard Worker   // Returns the last |CURLcode|.
171*5a923131SAndroid Build Coastguard Worker   CURLcode GetCurlCode();
172*5a923131SAndroid Build Coastguard Worker 
173*5a923131SAndroid Build Coastguard Worker   // Checks whether stored HTTP response is within the success range.
IsHttpResponseSuccess()174*5a923131SAndroid Build Coastguard Worker   inline bool IsHttpResponseSuccess() {
175*5a923131SAndroid Build Coastguard Worker     return (http_response_code_ >= 200 && http_response_code_ < 300);
176*5a923131SAndroid Build Coastguard Worker   }
177*5a923131SAndroid Build Coastguard Worker 
178*5a923131SAndroid Build Coastguard Worker   // Checks whether stored HTTP response is within the error range. This
179*5a923131SAndroid Build Coastguard Worker   // includes both errors with the request (4xx) and server errors (5xx).
IsHttpResponseError()180*5a923131SAndroid Build Coastguard Worker   inline bool IsHttpResponseError() {
181*5a923131SAndroid Build Coastguard Worker     return (http_response_code_ >= 400 && http_response_code_ < 600);
182*5a923131SAndroid Build Coastguard Worker   }
183*5a923131SAndroid Build Coastguard Worker 
184*5a923131SAndroid Build Coastguard Worker   // Resumes a transfer where it left off. This will use the
185*5a923131SAndroid Build Coastguard Worker   // HTTP Range: header to make a new connection from where the last
186*5a923131SAndroid Build Coastguard Worker   // left off.
187*5a923131SAndroid Build Coastguard Worker   virtual void ResumeTransfer(const std::string& url);
188*5a923131SAndroid Build Coastguard Worker 
189*5a923131SAndroid Build Coastguard Worker   void TimeoutCallback();
190*5a923131SAndroid Build Coastguard Worker   void RetryTimeoutCallback();
191*5a923131SAndroid Build Coastguard Worker 
192*5a923131SAndroid Build Coastguard Worker   // Calls into curl_multi_perform to let libcurl do its work. Returns after
193*5a923131SAndroid Build Coastguard Worker   // curl_multi_perform is finished, which may actually be after more than
194*5a923131SAndroid Build Coastguard Worker   // one call to curl_multi_perform. This method will set up the message
195*5a923131SAndroid Build Coastguard Worker   // loop with sources for future work that libcurl will do, if any, or complete
196*5a923131SAndroid Build Coastguard Worker   // the transfer and finish the action if no work left to do.
197*5a923131SAndroid Build Coastguard Worker   // This method will not block.
198*5a923131SAndroid Build Coastguard Worker   void CurlPerformOnce();
199*5a923131SAndroid Build Coastguard Worker 
200*5a923131SAndroid Build Coastguard Worker   // Sets up message loop sources as needed by libcurl. This is generally
201*5a923131SAndroid Build Coastguard Worker   // the file descriptor of the socket and a timer in case nothing happens
202*5a923131SAndroid Build Coastguard Worker   // on the fds.
203*5a923131SAndroid Build Coastguard Worker   void SetupMessageLoopSources();
204*5a923131SAndroid Build Coastguard Worker 
205*5a923131SAndroid Build Coastguard Worker   // Callback called by libcurl when new data has arrived on the transfer
206*5a923131SAndroid Build Coastguard Worker   size_t LibcurlWrite(void* ptr, size_t size, size_t nmemb);
StaticLibcurlWrite(void * ptr,size_t size,size_t nmemb,void * stream)207*5a923131SAndroid Build Coastguard Worker   static size_t StaticLibcurlWrite(void* ptr,
208*5a923131SAndroid Build Coastguard Worker                                    size_t size,
209*5a923131SAndroid Build Coastguard Worker                                    size_t nmemb,
210*5a923131SAndroid Build Coastguard Worker                                    void* stream) {
211*5a923131SAndroid Build Coastguard Worker     return reinterpret_cast<LibcurlHttpFetcher*>(stream)->LibcurlWrite(
212*5a923131SAndroid Build Coastguard Worker         ptr, size, nmemb);
213*5a923131SAndroid Build Coastguard Worker   }
214*5a923131SAndroid Build Coastguard Worker 
215*5a923131SAndroid Build Coastguard Worker   // Cleans up the following if they are non-null:
216*5a923131SAndroid Build Coastguard Worker   // curl(m) handles, fd_controller_maps_(fd_task_maps_), timeout_id_.
217*5a923131SAndroid Build Coastguard Worker   void CleanUp();
218*5a923131SAndroid Build Coastguard Worker 
219*5a923131SAndroid Build Coastguard Worker   // Force terminate the transfer. This will invoke the delegate's (if any)
220*5a923131SAndroid Build Coastguard Worker   // TransferTerminated callback so, after returning, this fetcher instance may
221*5a923131SAndroid Build Coastguard Worker   // be destroyed.
222*5a923131SAndroid Build Coastguard Worker   void ForceTransferTermination();
223*5a923131SAndroid Build Coastguard Worker 
224*5a923131SAndroid Build Coastguard Worker   // Sets the curl options for HTTP URL.
225*5a923131SAndroid Build Coastguard Worker   void SetCurlOptionsForHttp();
226*5a923131SAndroid Build Coastguard Worker 
227*5a923131SAndroid Build Coastguard Worker   // Sets the curl options for HTTPS URL.
228*5a923131SAndroid Build Coastguard Worker   void SetCurlOptionsForHttps();
229*5a923131SAndroid Build Coastguard Worker 
230*5a923131SAndroid Build Coastguard Worker   // Sets the curl options for file URI.
231*5a923131SAndroid Build Coastguard Worker   void SetCurlOptionsForFile();
232*5a923131SAndroid Build Coastguard Worker 
233*5a923131SAndroid Build Coastguard Worker   // Convert a proxy URL into a curl proxy type, if applicable. Returns true iff
234*5a923131SAndroid Build Coastguard Worker   // conversion was successful, false otherwise (in which case nothing is
235*5a923131SAndroid Build Coastguard Worker   // written to |out_type|).
236*5a923131SAndroid Build Coastguard Worker   bool GetProxyType(const std::string& proxy, curl_proxytype* out_type);
237*5a923131SAndroid Build Coastguard Worker 
238*5a923131SAndroid Build Coastguard Worker   // Hardware interface used to query dev-mode and official build settings.
239*5a923131SAndroid Build Coastguard Worker   HardwareInterface* hardware_;
240*5a923131SAndroid Build Coastguard Worker 
241*5a923131SAndroid Build Coastguard Worker   // Handles for the libcurl library
242*5a923131SAndroid Build Coastguard Worker   CURLM* curl_multi_handle_{nullptr};
243*5a923131SAndroid Build Coastguard Worker   CURL* curl_handle_{nullptr};
244*5a923131SAndroid Build Coastguard Worker   struct curl_slist* curl_http_headers_{nullptr};
245*5a923131SAndroid Build Coastguard Worker 
246*5a923131SAndroid Build Coastguard Worker   // The extra headers that will be sent on each request.
247*5a923131SAndroid Build Coastguard Worker   std::map<std::string, std::string> extra_headers_;
248*5a923131SAndroid Build Coastguard Worker 
249*5a923131SAndroid Build Coastguard Worker   // Lists of all read(0)/write(1) file descriptors that we're waiting on from
250*5a923131SAndroid Build Coastguard Worker   // the message loop. libcurl may open/close descriptors and switch their
251*5a923131SAndroid Build Coastguard Worker   // directions so maintain two separate lists so that watch conditions can be
252*5a923131SAndroid Build Coastguard Worker   // set appropriately.
253*5a923131SAndroid Build Coastguard Worker   std::map<int, std::unique_ptr<base::FileDescriptorWatcher::Controller>>
254*5a923131SAndroid Build Coastguard Worker       fd_controller_maps_[2];
255*5a923131SAndroid Build Coastguard Worker 
256*5a923131SAndroid Build Coastguard Worker   // The TaskId of the timer we're waiting on. kTaskIdNull if we are not waiting
257*5a923131SAndroid Build Coastguard Worker   // on it.
258*5a923131SAndroid Build Coastguard Worker   brillo::MessageLoop::TaskId timeout_id_{brillo::MessageLoop::kTaskIdNull};
259*5a923131SAndroid Build Coastguard Worker 
260*5a923131SAndroid Build Coastguard Worker   bool transfer_in_progress_{false};
261*5a923131SAndroid Build Coastguard Worker   bool transfer_paused_{false};
262*5a923131SAndroid Build Coastguard Worker 
263*5a923131SAndroid Build Coastguard Worker   // Whether it should ignore transfer failures for the purpose of retrying the
264*5a923131SAndroid Build Coastguard Worker   // connection.
265*5a923131SAndroid Build Coastguard Worker   bool ignore_failure_{false};
266*5a923131SAndroid Build Coastguard Worker 
267*5a923131SAndroid Build Coastguard Worker   // Whether we should restart the transfer once Unpause() is called. This can
268*5a923131SAndroid Build Coastguard Worker   // be caused because either the connection dropped while pause or the proxy
269*5a923131SAndroid Build Coastguard Worker   // was resolved and we never started the transfer in the first place.
270*5a923131SAndroid Build Coastguard Worker   bool restart_transfer_on_unpause_{false};
271*5a923131SAndroid Build Coastguard Worker 
272*5a923131SAndroid Build Coastguard Worker   // The transfer size. -1 if not known.
273*5a923131SAndroid Build Coastguard Worker   off_t transfer_size_{0};
274*5a923131SAndroid Build Coastguard Worker 
275*5a923131SAndroid Build Coastguard Worker   // How many bytes have been downloaded and sent to the delegate.
276*5a923131SAndroid Build Coastguard Worker   off_t bytes_downloaded_{0};
277*5a923131SAndroid Build Coastguard Worker 
278*5a923131SAndroid Build Coastguard Worker   // The remaining maximum number of bytes to download. Zero represents an
279*5a923131SAndroid Build Coastguard Worker   // unspecified length.
280*5a923131SAndroid Build Coastguard Worker   size_t download_length_{0};
281*5a923131SAndroid Build Coastguard Worker 
282*5a923131SAndroid Build Coastguard Worker   // If we resumed an earlier transfer, data offset that we used for the
283*5a923131SAndroid Build Coastguard Worker   // new connection.  0 otherwise.
284*5a923131SAndroid Build Coastguard Worker   // In this class, resume refers to resuming a dropped HTTP connection,
285*5a923131SAndroid Build Coastguard Worker   // not to resuming an interrupted download.
286*5a923131SAndroid Build Coastguard Worker   off_t resume_offset_{0};
287*5a923131SAndroid Build Coastguard Worker 
288*5a923131SAndroid Build Coastguard Worker   // Number of resumes performed so far and the max allowed.
289*5a923131SAndroid Build Coastguard Worker   int retry_count_{0};
290*5a923131SAndroid Build Coastguard Worker   int max_retry_count_{kDownloadMaxRetryCount};
291*5a923131SAndroid Build Coastguard Worker 
292*5a923131SAndroid Build Coastguard Worker   // Seconds to wait before retrying a resume.
293*5a923131SAndroid Build Coastguard Worker   int retry_seconds_{20};
294*5a923131SAndroid Build Coastguard Worker 
295*5a923131SAndroid Build Coastguard Worker   // When waiting for a retry, the task id of the retry callback.
296*5a923131SAndroid Build Coastguard Worker   brillo::MessageLoop::TaskId retry_task_id_{brillo::MessageLoop::kTaskIdNull};
297*5a923131SAndroid Build Coastguard Worker 
298*5a923131SAndroid Build Coastguard Worker   // Number of resumes due to no network (e.g., HTTP response code 0).
299*5a923131SAndroid Build Coastguard Worker   int no_network_retry_count_{0};
300*5a923131SAndroid Build Coastguard Worker   int no_network_max_retries_{0};
301*5a923131SAndroid Build Coastguard Worker 
302*5a923131SAndroid Build Coastguard Worker   // Seconds to wait before asking libcurl to "perform".
303*5a923131SAndroid Build Coastguard Worker   int idle_seconds_{1};
304*5a923131SAndroid Build Coastguard Worker 
305*5a923131SAndroid Build Coastguard Worker   // If true, we are currently performing a write callback on the delegate.
306*5a923131SAndroid Build Coastguard Worker   bool in_write_callback_{false};
307*5a923131SAndroid Build Coastguard Worker 
308*5a923131SAndroid Build Coastguard Worker   // If true, we have returned at least one byte in the write callback
309*5a923131SAndroid Build Coastguard Worker   // to the delegate.
310*5a923131SAndroid Build Coastguard Worker   bool sent_byte_{false};
311*5a923131SAndroid Build Coastguard Worker 
312*5a923131SAndroid Build Coastguard Worker   // We can't clean everything up while we're in a write callback, so
313*5a923131SAndroid Build Coastguard Worker   // if we get a terminate request, queue it until we can handle it.
314*5a923131SAndroid Build Coastguard Worker   bool terminate_requested_{false};
315*5a923131SAndroid Build Coastguard Worker 
316*5a923131SAndroid Build Coastguard Worker   // The ServerToCheck used when checking this connection's certificate. If no
317*5a923131SAndroid Build Coastguard Worker   // certificate check needs to be performed, this should be set to
318*5a923131SAndroid Build Coastguard Worker   // ServerToCheck::kNone.
319*5a923131SAndroid Build Coastguard Worker   ServerToCheck server_to_check_{ServerToCheck::kNone};
320*5a923131SAndroid Build Coastguard Worker 
321*5a923131SAndroid Build Coastguard Worker   // True if this object is for update check.
322*5a923131SAndroid Build Coastguard Worker   bool is_update_check_{false};
323*5a923131SAndroid Build Coastguard Worker 
324*5a923131SAndroid Build Coastguard Worker   // Internal state machine.
325*5a923131SAndroid Build Coastguard Worker   UnresolvedHostStateMachine unresolved_host_state_machine_;
326*5a923131SAndroid Build Coastguard Worker 
327*5a923131SAndroid Build Coastguard Worker   int low_speed_limit_bps_{kDownloadLowSpeedLimitBps};
328*5a923131SAndroid Build Coastguard Worker   int low_speed_time_seconds_{kDownloadLowSpeedTimeSeconds};
329*5a923131SAndroid Build Coastguard Worker   int connect_timeout_seconds_{kDownloadConnectTimeoutSeconds};
330*5a923131SAndroid Build Coastguard Worker 
331*5a923131SAndroid Build Coastguard Worker   DISALLOW_COPY_AND_ASSIGN(LibcurlHttpFetcher);
332*5a923131SAndroid Build Coastguard Worker };
333*5a923131SAndroid Build Coastguard Worker 
334*5a923131SAndroid Build Coastguard Worker }  // namespace chromeos_update_engine
335*5a923131SAndroid Build Coastguard Worker 
336*5a923131SAndroid Build Coastguard Worker #endif  // UPDATE_ENGINE_LIBCURL_HTTP_FETCHER_H_
337