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