blob: f1ec16e95d4ecc16e2954ce9fea9cf2eedacef28 [file] [log] [blame]
sorin395c2ac2014-09-16 21:31:071// 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
sorin52ac0882015-01-24 01:15:005#ifndef COMPONENTS_UPDATE_CLIENT_REQUEST_SENDER_H_
6#define COMPONENTS_UPDATE_CLIENT_REQUEST_SENDER_H_
sorin395c2ac2014-09-16 21:31:077
sorinfccbf2d2016-04-04 20:34:348#include <stdint.h>
9
dchengd0fc6aa92016-04-22 18:03:1210#include <memory>
sorin395c2ac2014-09-16 21:31:0711#include <string>
12#include <vector>
13
14#include "base/callback.h"
sorin395c2ac2014-09-16 21:31:0715#include "base/macros.h"
sorin958b5d32016-01-09 02:00:2016#include "base/memory/ref_counted.h"
waffles720946a2014-11-15 00:07:1517#include "base/threading/thread_checker.h"
sorin395c2ac2014-09-16 21:31:0718#include "net/url_request/url_fetcher_delegate.h"
19#include "url/gurl.h"
20
mabbb61b52a2016-03-17 23:37:2321namespace client_update_protocol {
22class Ecdsa;
23}
24
sorin395c2ac2014-09-16 21:31:0725namespace net {
26class URLFetcher;
27}
28
sorin52ac0882015-01-24 01:15:0029namespace update_client {
sorin395c2ac2014-09-16 21:31:0730
31class 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.
sorin1bc5eff2016-02-17 18:45:1736// CUP signing is optional.
sorin395c2ac2014-09-16 21:31:0737class RequestSender : public net::URLFetcherDelegate {
38 public:
sorin1bc5eff2016-02-17 18:45:1739 // If |error| is 0, then the response is provided in the |response| parameter.
sorinfccbf2d2016-04-04 20:34:3440 // |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)>;
sorin1bc5eff2016-02-17 18:45:1747
48 static int kErrorResponseNotTrusted;
sorin395c2ac2014-09-16 21:31:0749
sorin958b5d32016-01-09 02:00:2050 explicit RequestSender(const scoped_refptr<Configurator>& config);
dcheng00ea022b2014-10-21 11:24:5651 ~RequestSender() override;
sorin395c2ac2014-09-16 21:31:0752
sorin1bc5eff2016-02-17 18:45:1753 // |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,
sorin395c2ac2014-09-16 21:31:0757 const std::vector<GURL>& urls,
58 const RequestSenderCallback& request_sender_callback);
59
60 private:
sorin1bc5eff2016-02-17 18:45:1761 // 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
sorinfccbf2d2016-04-04 20:34:3467 // 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);
sorin395c2ac2014-09-16 21:31:0776
77 // Overrides for URLFetcherDelegate.
dcheng00ea022b2014-10-21 11:24:5678 void OnURLFetchComplete(const net::URLFetcher* source) override;
sorin395c2ac2014-09-16 21:31:0779
sorin1bc5eff2016-02-17 18:45:1780 // Implements the error handling and url fallback mechanism.
81 void SendInternal();
82
sorinfccbf2d2016-04-04 20:34:3483 // Called when SendInternal completes. |response_body| and |response_etag|
sorin1bc5eff2016-02-17 18:45:1784 // contain the body and the etag associated with the HTTP response.
85 void SendInternalComplete(int error,
86 const std::string& response_body,
sorinfccbf2d2016-04-04 20:34:3487 const std::string& response_etag,
88 int retry_after_sec);
sorin1bc5eff2016-02-17 18:45:1789
90 // Helper function to handle a non-continuable error in Send.
sorinfccbf2d2016-04-04 20:34:3491 void HandleSendError(int error, int retry_after_sec);
sorin395c2ac2014-09-16 21:31:0792
waffles720946a2014-11-15 00:07:1593 base::ThreadChecker thread_checker_;
94
sorin1bc5eff2016-02-17 18:45:1795 const scoped_refptr<Configurator> config_;
sorinfccbf2d2016-04-04 20:34:3496 bool use_signing_; // True if CUP signing is used.
sorin1bc5eff2016-02-17 18:45:1797 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_;
dchengd0fc6aa92016-04-22 18:03:12103 std::unique_ptr<net::URLFetcher> url_fetcher_;
104 std::unique_ptr<client_update_protocol::Ecdsa> signer_;
sorin1bc5eff2016-02-17 18:45:17105
sorin395c2ac2014-09-16 21:31:07106 DISALLOW_COPY_AND_ASSIGN(RequestSender);
107};
108
sorin52ac0882015-01-24 01:15:00109} // namespace update_client
sorin395c2ac2014-09-16 21:31:07110
sorin52ac0882015-01-24 01:15:00111#endif // COMPONENTS_UPDATE_CLIENT_REQUEST_SENDER_H_