request_sender.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. #ifndef COMPONENTS_UPDATE_CLIENT_REQUEST_SENDER_H_
  5. #define COMPONENTS_UPDATE_CLIENT_REQUEST_SENDER_H_
  6. #include <stdint.h>
  7. #include <memory>
  8. #include <string>
  9. #include <vector>
  10. #include "base/callback.h"
  11. #include "base/containers/flat_map.h"
  12. #include "base/memory/ref_counted.h"
  13. #include "base/threading/thread_checker.h"
  14. #include "url/gurl.h"
  15. namespace client_update_protocol {
  16. class Ecdsa;
  17. }
  18. namespace update_client {
  19. class Configurator;
  20. class NetworkFetcher;
  21. // Sends a request to one of the urls provided. The class implements a chain
  22. // of responsibility design pattern, where the urls are tried in the order they
  23. // are specified, until the request to one of them succeeds or all have failed.
  24. // CUP signing is optional.
  25. class RequestSender {
  26. public:
  27. // If |error| is 0, then the response is provided in the |response| parameter.
  28. // |retry_after_sec| contains the value of the X-Retry-After response header,
  29. // when the response was received from a cryptographically secure URL. The
  30. // range for this value is [-1, 86400]. If |retry_after_sec| is -1 it means
  31. // that the header could not be found, or trusted, or had an invalid value.
  32. // The upper bound represents a delay of one day.
  33. using RequestSenderCallback = base::OnceCallback<
  34. void(int error, const std::string& response, int retry_after_sec)>;
  35. explicit RequestSender(scoped_refptr<Configurator> config);
  36. RequestSender(const RequestSender&) = delete;
  37. RequestSender& operator=(const RequestSender&) = delete;
  38. ~RequestSender();
  39. // |use_signing| enables CUP signing of protocol messages exchanged using
  40. // this class. |is_foreground| controls the presence and the value for the
  41. // X-GoogleUpdate-Interactvity header serialized in the protocol request.
  42. // If this optional parameter is set, the values of "fg" or "bg" are sent
  43. // for true or false values of this parameter. Otherwise the header is not
  44. // sent at all.
  45. void Send(
  46. const std::vector<GURL>& urls,
  47. const base::flat_map<std::string, std::string>& request_extra_headers,
  48. const std::string& request_body,
  49. bool use_signing,
  50. RequestSenderCallback request_sender_callback);
  51. private:
  52. // Combines the |url| and |query_params| parameters.
  53. static GURL BuildUpdateUrl(const GURL& url, const std::string& query_params);
  54. // Decodes and returns the public key used by CUP.
  55. static std::string GetKey(const char* key_bytes_base64);
  56. void OnResponseStarted(int response_code, int64_t content_length);
  57. void OnNetworkFetcherComplete(const GURL& original_url,
  58. std::unique_ptr<std::string> response_body,
  59. int net_error,
  60. const std::string& header_etag,
  61. const std::string& xheader_cup_server_proof,
  62. int64_t xheader_retry_after_sec);
  63. // Implements the error handling and url fallback mechanism.
  64. void SendInternal();
  65. // Called when SendInternal completes. |response_body| and |response_etag|
  66. // contain the body and the etag associated with the HTTP response.
  67. void SendInternalComplete(int error,
  68. const std::string& response_body,
  69. const std::string& response_etag,
  70. const std::string& response_cup_server_proof,
  71. int retry_after_sec);
  72. // Helper function to handle a non-continuable error in Send.
  73. void HandleSendError(int error, int retry_after_sec);
  74. base::ThreadChecker thread_checker_;
  75. const scoped_refptr<Configurator> config_;
  76. std::vector<GURL> urls_;
  77. base::flat_map<std::string, std::string> request_extra_headers_;
  78. std::string request_body_;
  79. bool use_signing_ = false; // True if CUP signing is used.
  80. RequestSenderCallback request_sender_callback_;
  81. std::string public_key_;
  82. std::vector<GURL>::const_iterator cur_url_;
  83. std::unique_ptr<NetworkFetcher> network_fetcher_;
  84. std::unique_ptr<client_update_protocol::Ecdsa> signer_;
  85. int response_code_ = -1;
  86. };
  87. } // namespace update_client
  88. #endif // COMPONENTS_UPDATE_CLIENT_REQUEST_SENDER_H_