1*1a96fba6SXin Li // Copyright 2014 The Chromium OS Authors. All rights reserved. 2*1a96fba6SXin Li // Use of this source code is governed by a BSD-style license that can be 3*1a96fba6SXin Li // found in the LICENSE file. 4*1a96fba6SXin Li 5*1a96fba6SXin Li #ifndef LIBBRILLO_BRILLO_HTTP_HTTP_TRANSPORT_H_ 6*1a96fba6SXin Li #define LIBBRILLO_BRILLO_HTTP_HTTP_TRANSPORT_H_ 7*1a96fba6SXin Li 8*1a96fba6SXin Li #include <memory> 9*1a96fba6SXin Li #include <string> 10*1a96fba6SXin Li #include <utility> 11*1a96fba6SXin Li #include <vector> 12*1a96fba6SXin Li 13*1a96fba6SXin Li #include <base/callback_forward.h> 14*1a96fba6SXin Li #include <base/files/file_path.h> 15*1a96fba6SXin Li #include <base/location.h> 16*1a96fba6SXin Li #include <base/macros.h> 17*1a96fba6SXin Li #include <base/time/time.h> 18*1a96fba6SXin Li #include <brillo/brillo_export.h> 19*1a96fba6SXin Li #include <brillo/errors/error.h> 20*1a96fba6SXin Li 21*1a96fba6SXin Li namespace brillo { 22*1a96fba6SXin Li namespace http { 23*1a96fba6SXin Li 24*1a96fba6SXin Li BRILLO_EXPORT extern const char kErrorDomain[]; 25*1a96fba6SXin Li // Constant referring to 'direct' proxy which implies no proxy server. 26*1a96fba6SXin Li BRILLO_EXPORT extern const char kDirectProxy[]; // direct:// 27*1a96fba6SXin Li 28*1a96fba6SXin Li class Request; 29*1a96fba6SXin Li class Response; 30*1a96fba6SXin Li class Connection; 31*1a96fba6SXin Li 32*1a96fba6SXin Li using RequestID = int; 33*1a96fba6SXin Li 34*1a96fba6SXin Li using HeaderList = std::vector<std::pair<std::string, std::string>>; 35*1a96fba6SXin Li using SuccessCallback = 36*1a96fba6SXin Li base::Callback<void(RequestID, std::unique_ptr<Response>)>; 37*1a96fba6SXin Li using ErrorCallback = base::Callback<void(RequestID, const brillo::Error*)>; 38*1a96fba6SXin Li 39*1a96fba6SXin Li /////////////////////////////////////////////////////////////////////////////// 40*1a96fba6SXin Li // Transport is a base class for specific implementation of HTTP communication. 41*1a96fba6SXin Li // This class (and its underlying implementation) is used by http::Request and 42*1a96fba6SXin Li // http::Response classes to provide HTTP functionality to the clients. By 43*1a96fba6SXin Li // default, this interface will use CA certificates that only allow secure 44*1a96fba6SXin Li // (HTTPS) communication with Google services. 45*1a96fba6SXin Li /////////////////////////////////////////////////////////////////////////////// 46*1a96fba6SXin Li class BRILLO_EXPORT Transport : public std::enable_shared_from_this<Transport> { 47*1a96fba6SXin Li public: 48*1a96fba6SXin Li enum class Certificate { 49*1a96fba6SXin Li // Default certificate; only allows communication with Google services. 50*1a96fba6SXin Li kDefault, 51*1a96fba6SXin Li // Certificates for communicating only with production SM-DP+ and SM-DS 52*1a96fba6SXin Li // servers. 53*1a96fba6SXin Li kHermesProd, 54*1a96fba6SXin Li // Certificates for communicating only with test SM-DP+ and SM-DS servers. 55*1a96fba6SXin Li kHermesTest, 56*1a96fba6SXin Li // The NSS certificate store, which the curl command-line tool and libcurl 57*1a96fba6SXin Li // library use by default. This set of certificates does not restrict 58*1a96fba6SXin Li // secure communication to only Google services. 59*1a96fba6SXin Li kNss, 60*1a96fba6SXin Li }; 61*1a96fba6SXin Li 62*1a96fba6SXin Li Transport() = default; 63*1a96fba6SXin Li virtual ~Transport() = default; 64*1a96fba6SXin Li 65*1a96fba6SXin Li // Creates a connection object and initializes it with the specified data. 66*1a96fba6SXin Li // |transport| is a shared pointer to this transport object instance, 67*1a96fba6SXin Li // used to maintain the object alive as long as the connection exists. 68*1a96fba6SXin Li // The |url| here is the full URL specified in the request. It is passed 69*1a96fba6SXin Li // to the underlying transport (e.g. CURL) to establish the connection. 70*1a96fba6SXin Li virtual std::shared_ptr<Connection> CreateConnection( 71*1a96fba6SXin Li const std::string& url, 72*1a96fba6SXin Li const std::string& method, 73*1a96fba6SXin Li const HeaderList& headers, 74*1a96fba6SXin Li const std::string& user_agent, 75*1a96fba6SXin Li const std::string& referer, 76*1a96fba6SXin Li brillo::ErrorPtr* error) = 0; 77*1a96fba6SXin Li 78*1a96fba6SXin Li // Runs |callback| on the task runner (message loop) associated with the 79*1a96fba6SXin Li // transport. For transports that do not contain references to real message 80*1a96fba6SXin Li // loops (e.g. a fake transport), calls the callback immediately. 81*1a96fba6SXin Li virtual void RunCallbackAsync(const base::Location& from_here, 82*1a96fba6SXin Li const base::Closure& callback) = 0; 83*1a96fba6SXin Li 84*1a96fba6SXin Li // Initiates an asynchronous transfer on the given |connection|. 85*1a96fba6SXin Li // The actual implementation of an async I/O is transport-specific. 86*1a96fba6SXin Li // Returns a request ID which can be used to cancel the request. 87*1a96fba6SXin Li virtual RequestID StartAsyncTransfer( 88*1a96fba6SXin Li Connection* connection, 89*1a96fba6SXin Li const SuccessCallback& success_callback, 90*1a96fba6SXin Li const ErrorCallback& error_callback) = 0; 91*1a96fba6SXin Li 92*1a96fba6SXin Li // Cancels a pending asynchronous request. This will cancel a pending request 93*1a96fba6SXin Li // scheduled by the transport while the I/O operations are still in progress. 94*1a96fba6SXin Li // As soon as all I/O completes for the request/response, or when an error 95*1a96fba6SXin Li // occurs, the success/error callbacks are invoked and the request is 96*1a96fba6SXin Li // considered complete and can no longer be canceled. 97*1a96fba6SXin Li // Returns false if pending request with |request_id| is not found (e.g. it 98*1a96fba6SXin Li // has already completed/its callbacks are dispatched). 99*1a96fba6SXin Li virtual bool CancelRequest(RequestID request_id) = 0; 100*1a96fba6SXin Li 101*1a96fba6SXin Li // Set the default timeout of requests made. 102*1a96fba6SXin Li virtual void SetDefaultTimeout(base::TimeDelta timeout) = 0; 103*1a96fba6SXin Li 104*1a96fba6SXin Li // Set the local IP address of requests 105*1a96fba6SXin Li virtual void SetLocalIpAddress(const std::string& ip_address) = 0; 106*1a96fba6SXin Li 107*1a96fba6SXin Li // Use the default CA certificate for certificate verification. This 108*1a96fba6SXin Li // means that clients are only allowed to communicate with Google services. UseDefaultCertificate()109*1a96fba6SXin Li virtual void UseDefaultCertificate() {} 110*1a96fba6SXin Li 111*1a96fba6SXin Li // Set the CA certificate to use for certificate verification. 112*1a96fba6SXin Li // 113*1a96fba6SXin Li // This call can allow a client to securly communicate with a different subset 114*1a96fba6SXin Li // of services than it can otherwise. However, setting a custom certificate 115*1a96fba6SXin Li // should be done only when necessary, and should be done with careful control 116*1a96fba6SXin Li // over the certificates that are contained in the relevant path. See 117*1a96fba6SXin Li // https://chromium.googlesource.com/chromiumos/docs/+/master/ca_certs.md for 118*1a96fba6SXin Li // more information on certificates in Chrome OS. UseCustomCertificate(Transport::Certificate cert)119*1a96fba6SXin Li virtual void UseCustomCertificate(Transport::Certificate cert) {} 120*1a96fba6SXin Li 121*1a96fba6SXin Li // Appends host entry to DNS cache. curl can only do HTTPS request to a custom 122*1a96fba6SXin Li // IP if it resolves an HTTPS hostname to that IP. This is useful in 123*1a96fba6SXin Li // forcing a particular mapping for an HTTPS host. See CURLOPT_RESOLVE for 124*1a96fba6SXin Li // more details. ResolveHostToIp(const std::string & host,uint16_t port,const std::string & ip_address)125*1a96fba6SXin Li virtual void ResolveHostToIp(const std::string& host, 126*1a96fba6SXin Li uint16_t port, 127*1a96fba6SXin Li const std::string& ip_address) {} 128*1a96fba6SXin Li 129*1a96fba6SXin Li // Creates a default http::Transport (currently, using http::curl::Transport). 130*1a96fba6SXin Li static std::shared_ptr<Transport> CreateDefault(); 131*1a96fba6SXin Li 132*1a96fba6SXin Li // Creates a default http::Transport that will utilize the passed in proxy 133*1a96fba6SXin Li // server (currently, using a http::curl::Transport). |proxy| should be of the 134*1a96fba6SXin Li // form scheme://[user:pass@]host:port or may be the empty string or the 135*1a96fba6SXin Li // string kDirectProxy (i.e. direct://) to indicate no proxy. 136*1a96fba6SXin Li static std::shared_ptr<Transport> CreateDefaultWithProxy( 137*1a96fba6SXin Li const std::string& proxy); 138*1a96fba6SXin Li 139*1a96fba6SXin Li protected: 140*1a96fba6SXin Li // Clears the forced DNS mappings created by ResolveHostToIp. ClearHost()141*1a96fba6SXin Li virtual void ClearHost() {} 142*1a96fba6SXin Li 143*1a96fba6SXin Li static base::FilePath CertificateToPath(Certificate cert); 144*1a96fba6SXin Li 145*1a96fba6SXin Li private: 146*1a96fba6SXin Li DISALLOW_COPY_AND_ASSIGN(Transport); 147*1a96fba6SXin Li }; 148*1a96fba6SXin Li 149*1a96fba6SXin Li } // namespace http 150*1a96fba6SXin Li } // namespace brillo 151*1a96fba6SXin Li 152*1a96fba6SXin Li #endif // LIBBRILLO_BRILLO_HTTP_HTTP_TRANSPORT_H_ 153