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_CONNECTION_H_ 6*1a96fba6SXin Li #define LIBBRILLO_BRILLO_HTTP_HTTP_CONNECTION_H_ 7*1a96fba6SXin Li 8*1a96fba6SXin Li #include <memory> 9*1a96fba6SXin Li #include <string> 10*1a96fba6SXin Li #include <vector> 11*1a96fba6SXin Li 12*1a96fba6SXin Li #include <base/callback_forward.h> 13*1a96fba6SXin Li #include <base/macros.h> 14*1a96fba6SXin Li #include <brillo/brillo_export.h> 15*1a96fba6SXin Li #include <brillo/errors/error.h> 16*1a96fba6SXin Li #include <brillo/http/http_transport.h> 17*1a96fba6SXin Li #include <brillo/streams/stream.h> 18*1a96fba6SXin Li 19*1a96fba6SXin Li namespace brillo { 20*1a96fba6SXin Li namespace http { 21*1a96fba6SXin Li 22*1a96fba6SXin Li class Response; 23*1a96fba6SXin Li 24*1a96fba6SXin Li /////////////////////////////////////////////////////////////////////////////// 25*1a96fba6SXin Li // Connection class is the base class for HTTP communication session. 26*1a96fba6SXin Li // It abstracts the implementation of underlying transport library (ex libcurl). 27*1a96fba6SXin Li // When the Connection-derived class is constructed, it is pre-set up with 28*1a96fba6SXin Li // basic initialization information necessary to initiate the server request 29*1a96fba6SXin Li // connection (such as the URL, request method, etc - see 30*1a96fba6SXin Li // Transport::CreateConnection() for more details). But most implementations 31*1a96fba6SXin Li // would not probably initiate the physical connection until SendHeaders 32*1a96fba6SXin Li // is called. 33*1a96fba6SXin Li // You normally shouldn't worry about using this class directly. 34*1a96fba6SXin Li // http::Request and http::Response classes use it for communication. 35*1a96fba6SXin Li // Effectively this class is the interface for the request/response objects to 36*1a96fba6SXin Li // the transport-specific instance of the communication channel with the 37*1a96fba6SXin Li // destination server. It is created by Transport as part of initiating 38*1a96fba6SXin Li // the connection to the destination URI and is shared between the request and 39*1a96fba6SXin Li // response object until all the data is sent to the server and the response 40*1a96fba6SXin Li // is received. The class does NOT represent a persistent TCP connection though 41*1a96fba6SXin Li // (e.g. in keep-alive scenarios). 42*1a96fba6SXin Li /////////////////////////////////////////////////////////////////////////////// 43*1a96fba6SXin Li class BRILLO_EXPORT Connection 44*1a96fba6SXin Li : public std::enable_shared_from_this<Connection> { 45*1a96fba6SXin Li public: Connection(const std::shared_ptr<Transport> & transport)46*1a96fba6SXin Li explicit Connection(const std::shared_ptr<Transport>& transport) 47*1a96fba6SXin Li : transport_(transport) {} 48*1a96fba6SXin Li virtual ~Connection() = default; 49*1a96fba6SXin Li 50*1a96fba6SXin Li // The following methods are used by http::Request object to initiate the 51*1a96fba6SXin Li // communication with the server, send the request data and receive the 52*1a96fba6SXin Li // response. 53*1a96fba6SXin Li 54*1a96fba6SXin Li // Called by http::Request to initiate the connection with the server. 55*1a96fba6SXin Li // This normally opens the socket and sends the request headers. 56*1a96fba6SXin Li virtual bool SendHeaders(const HeaderList& headers, 57*1a96fba6SXin Li brillo::ErrorPtr* error) = 0; 58*1a96fba6SXin Li // If needed, this function can be called to send the request body data. 59*1a96fba6SXin Li virtual bool SetRequestData(StreamPtr stream, brillo::ErrorPtr* error) = 0; 60*1a96fba6SXin Li // If needed, this function can be called to customize where the response 61*1a96fba6SXin Li // data is streamed to. 62*1a96fba6SXin Li virtual void SetResponseData(StreamPtr stream) = 0; 63*1a96fba6SXin Li // This function is called when all the data is sent off and it's time 64*1a96fba6SXin Li // to receive the response data. The method will block until the whole 65*1a96fba6SXin Li // response message is received, or if an error occur in which case the 66*1a96fba6SXin Li // function will return false and fill the error details in |error| object. 67*1a96fba6SXin Li virtual bool FinishRequest(brillo::ErrorPtr* error) = 0; 68*1a96fba6SXin Li // Send the request asynchronously and invoke the callback with the response 69*1a96fba6SXin Li // received. Returns the ID of the pending async request. 70*1a96fba6SXin Li virtual RequestID FinishRequestAsync(const SuccessCallback& success_callback, 71*1a96fba6SXin Li const ErrorCallback& error_callback) = 0; 72*1a96fba6SXin Li 73*1a96fba6SXin Li // The following methods are used by http::Response object to obtain the 74*1a96fba6SXin Li // response data. They are used only after the response data has been received 75*1a96fba6SXin Li // since the http::Response object is not constructed until 76*1a96fba6SXin Li // Request::GetResponse()/Request::GetResponseAndBlock() methods are called. 77*1a96fba6SXin Li 78*1a96fba6SXin Li // Returns the HTTP status code (e.g. 200 for success). 79*1a96fba6SXin Li virtual int GetResponseStatusCode() const = 0; 80*1a96fba6SXin Li // Returns the status text (e.g. for error 403 it could be "NOT AUTHORIZED"). 81*1a96fba6SXin Li virtual std::string GetResponseStatusText() const = 0; 82*1a96fba6SXin Li // Returns the HTTP protocol version (e.g. "HTTP/1.1"). 83*1a96fba6SXin Li virtual std::string GetProtocolVersion() const = 0; 84*1a96fba6SXin Li // Returns the value of particular response header, or empty string if the 85*1a96fba6SXin Li // headers wasn't received. 86*1a96fba6SXin Li virtual std::string GetResponseHeader( 87*1a96fba6SXin Li const std::string& header_name) const = 0; 88*1a96fba6SXin Li // Returns the response data stream. This function can be called only once 89*1a96fba6SXin Li // as it transfers ownership of the data stream to the caller. Subsequent 90*1a96fba6SXin Li // calls will fail with "Stream closed" error. 91*1a96fba6SXin Li // Returns empty stream on failure and fills in the error information in 92*1a96fba6SXin Li // |error| object. 93*1a96fba6SXin Li virtual StreamPtr ExtractDataStream(brillo::ErrorPtr* error) = 0; 94*1a96fba6SXin Li 95*1a96fba6SXin Li protected: 96*1a96fba6SXin Li // |transport_| is mainly used to keep the object alive as long as the 97*1a96fba6SXin Li // connection exists. But some implementations of Connection could use 98*1a96fba6SXin Li // the Transport-derived class for their own needs as well. 99*1a96fba6SXin Li std::shared_ptr<Transport> transport_; 100*1a96fba6SXin Li 101*1a96fba6SXin Li private: 102*1a96fba6SXin Li DISALLOW_COPY_AND_ASSIGN(Connection); 103*1a96fba6SXin Li }; 104*1a96fba6SXin Li 105*1a96fba6SXin Li } // namespace http 106*1a96fba6SXin Li } // namespace brillo 107*1a96fba6SXin Li 108*1a96fba6SXin Li #endif // LIBBRILLO_BRILLO_HTTP_HTTP_CONNECTION_H_ 109