sorin | 395c2ac | 2014-09-16 21:31:07 | [diff] [blame] | 1 | // Copyright 2014 The Chromium 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 | |
sorin | 52ac088 | 2015-01-24 01:15:00 | [diff] [blame] | 5 | #ifndef COMPONENTS_UPDATE_CLIENT_REQUEST_SENDER_H_ |
| 6 | #define COMPONENTS_UPDATE_CLIENT_REQUEST_SENDER_H_ |
sorin | 395c2ac | 2014-09-16 21:31:07 | [diff] [blame] | 7 | |
sorin | fccbf2d | 2016-04-04 20:34:34 | [diff] [blame] | 8 | #include <stdint.h> |
| 9 | |
dcheng | d0fc6aa9 | 2016-04-22 18:03:12 | [diff] [blame] | 10 | #include <memory> |
sorin | 395c2ac | 2014-09-16 21:31:07 | [diff] [blame] | 11 | #include <string> |
| 12 | #include <vector> |
| 13 | |
| 14 | #include "base/callback.h" |
sorin | 395c2ac | 2014-09-16 21:31:07 | [diff] [blame] | 15 | #include "base/macros.h" |
sorin | 958b5d3 | 2016-01-09 02:00:20 | [diff] [blame] | 16 | #include "base/memory/ref_counted.h" |
waffles | 720946a | 2014-11-15 00:07:15 | [diff] [blame] | 17 | #include "base/threading/thread_checker.h" |
sorin | 395c2ac | 2014-09-16 21:31:07 | [diff] [blame] | 18 | #include "net/url_request/url_fetcher_delegate.h" |
| 19 | #include "url/gurl.h" |
| 20 | |
mab | bb61b52a | 2016-03-17 23:37:23 | [diff] [blame] | 21 | namespace client_update_protocol { |
| 22 | class Ecdsa; |
| 23 | } |
| 24 | |
sorin | 395c2ac | 2014-09-16 21:31:07 | [diff] [blame] | 25 | namespace net { |
| 26 | class URLFetcher; |
| 27 | } |
| 28 | |
sorin | 52ac088 | 2015-01-24 01:15:00 | [diff] [blame] | 29 | namespace update_client { |
sorin | 395c2ac | 2014-09-16 21:31:07 | [diff] [blame] | 30 | |
| 31 | class Configurator; |
| 32 | |
| 33 | // Sends a request to one of the urls provided. The class implements a chain |
| 34 | // of responsibility design pattern, where the urls are tried in the order they |
| 35 | // are specified, until the request to one of them succeeds or all have failed. |
sorin | 1bc5eff | 2016-02-17 18:45:17 | [diff] [blame] | 36 | // CUP signing is optional. |
sorin | 395c2ac | 2014-09-16 21:31:07 | [diff] [blame] | 37 | class RequestSender : public net::URLFetcherDelegate { |
| 38 | public: |
sorin | 1bc5eff | 2016-02-17 18:45:17 | [diff] [blame] | 39 | // If |error| is 0, then the response is provided in the |response| parameter. |
sorin | fccbf2d | 2016-04-04 20:34:34 | [diff] [blame] | 40 | // |retry_after_sec| contains the value of the X-Retry-After response header, |
| 41 | // when the response was received from a cryptographically secure URL. The |
| 42 | // range for this value is [-1, 86400]. If |retry_after_sec| is -1 it means |
| 43 | // that the header could not be found, or trusted, or had an invalid value. |
| 44 | // The upper bound represents a delay of one day. |
| 45 | using RequestSenderCallback = base::Callback< |
| 46 | void(int error, const std::string& response, int retry_after_sec)>; |
sorin | 1bc5eff | 2016-02-17 18:45:17 | [diff] [blame] | 47 | |
| 48 | static int kErrorResponseNotTrusted; |
sorin | 395c2ac | 2014-09-16 21:31:07 | [diff] [blame] | 49 | |
sorin | 958b5d3 | 2016-01-09 02:00:20 | [diff] [blame] | 50 | explicit RequestSender(const scoped_refptr<Configurator>& config); |
dcheng | 00ea022b | 2014-10-21 11:24:56 | [diff] [blame] | 51 | ~RequestSender() override; |
sorin | 395c2ac | 2014-09-16 21:31:07 | [diff] [blame] | 52 | |
sorin | 1bc5eff | 2016-02-17 18:45:17 | [diff] [blame] | 53 | // |use_signing| enables CUP signing of protocol messages exchanged using |
| 54 | // this class. |
| 55 | void Send(bool use_signing, |
| 56 | const std::string& request_body, |
sorin | 395c2ac | 2014-09-16 21:31:07 | [diff] [blame] | 57 | const std::vector<GURL>& urls, |
| 58 | const RequestSenderCallback& request_sender_callback); |
| 59 | |
| 60 | private: |
sorin | 1bc5eff | 2016-02-17 18:45:17 | [diff] [blame] | 61 | // Combines the |url| and |query_params| parameters. |
| 62 | static GURL BuildUpdateUrl(const GURL& url, const std::string& query_params); |
| 63 | |
| 64 | // Decodes and returns the public key used by CUP. |
| 65 | static std::string GetKey(const char* key_bytes_base64); |
| 66 | |
sorin | fccbf2d | 2016-04-04 20:34:34 | [diff] [blame] | 67 | // Returns the string value of a header of the server response or an empty |
| 68 | // string if the header is not available. |
| 69 | static std::string GetStringHeaderValue(const net::URLFetcher* source, |
| 70 | const char* header_name); |
| 71 | |
| 72 | // Returns the integral value of a header of the server response or -1 if |
| 73 | // if the header is not available or a conversion error has occured. |
| 74 | static int64_t GetInt64HeaderValue(const net::URLFetcher* source, |
| 75 | const char* header_name); |
sorin | 395c2ac | 2014-09-16 21:31:07 | [diff] [blame] | 76 | |
| 77 | // Overrides for URLFetcherDelegate. |
dcheng | 00ea022b | 2014-10-21 11:24:56 | [diff] [blame] | 78 | void OnURLFetchComplete(const net::URLFetcher* source) override; |
sorin | 395c2ac | 2014-09-16 21:31:07 | [diff] [blame] | 79 | |
sorin | 1bc5eff | 2016-02-17 18:45:17 | [diff] [blame] | 80 | // Implements the error handling and url fallback mechanism. |
| 81 | void SendInternal(); |
| 82 | |
sorin | fccbf2d | 2016-04-04 20:34:34 | [diff] [blame] | 83 | // Called when SendInternal completes. |response_body| and |response_etag| |
sorin | 1bc5eff | 2016-02-17 18:45:17 | [diff] [blame] | 84 | // contain the body and the etag associated with the HTTP response. |
| 85 | void SendInternalComplete(int error, |
| 86 | const std::string& response_body, |
sorin | fccbf2d | 2016-04-04 20:34:34 | [diff] [blame] | 87 | const std::string& response_etag, |
| 88 | int retry_after_sec); |
sorin | 1bc5eff | 2016-02-17 18:45:17 | [diff] [blame] | 89 | |
| 90 | // Helper function to handle a non-continuable error in Send. |
sorin | fccbf2d | 2016-04-04 20:34:34 | [diff] [blame] | 91 | void HandleSendError(int error, int retry_after_sec); |
sorin | 395c2ac | 2014-09-16 21:31:07 | [diff] [blame] | 92 | |
waffles | 720946a | 2014-11-15 00:07:15 | [diff] [blame] | 93 | base::ThreadChecker thread_checker_; |
| 94 | |
sorin | 1bc5eff | 2016-02-17 18:45:17 | [diff] [blame] | 95 | const scoped_refptr<Configurator> config_; |
sorin | fccbf2d | 2016-04-04 20:34:34 | [diff] [blame] | 96 | bool use_signing_; // True if CUP signing is used. |
sorin | 1bc5eff | 2016-02-17 18:45:17 | [diff] [blame] | 97 | std::vector<GURL> urls_; |
| 98 | std::string request_body_; |
| 99 | RequestSenderCallback request_sender_callback_; |
| 100 | |
| 101 | std::string public_key_; |
| 102 | std::vector<GURL>::const_iterator cur_url_; |
dcheng | d0fc6aa9 | 2016-04-22 18:03:12 | [diff] [blame] | 103 | std::unique_ptr<net::URLFetcher> url_fetcher_; |
| 104 | std::unique_ptr<client_update_protocol::Ecdsa> signer_; |
sorin | 1bc5eff | 2016-02-17 18:45:17 | [diff] [blame] | 105 | |
sorin | 395c2ac | 2014-09-16 21:31:07 | [diff] [blame] | 106 | DISALLOW_COPY_AND_ASSIGN(RequestSender); |
| 107 | }; |
| 108 | |
sorin | 52ac088 | 2015-01-24 01:15:00 | [diff] [blame] | 109 | } // namespace update_client |
sorin | 395c2ac | 2014-09-16 21:31:07 | [diff] [blame] | 110 | |
sorin | 52ac088 | 2015-01-24 01:15:00 | [diff] [blame] | 111 | #endif // COMPONENTS_UPDATE_CLIENT_REQUEST_SENDER_H_ |