| // Copyright 2014 The Chromium OS Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| |
| #ifndef BUFFET_HTTP_CONNECTION_H_ |
| #define BUFFET_HTTP_CONNECTION_H_ |
| |
| #include <string> |
| #include <vector> |
| |
| #include <base/basictypes.h> |
| |
| #include "buffet/error.h" |
| #include "buffet/http_transport.h" |
| |
| namespace chromeos { |
| namespace http { |
| |
| /////////////////////////////////////////////////////////////////////////////// |
| // Connection class is the base class for HTTP communication session. |
| // It abstracts the implementation of underlying transport library (ex libcurl). |
| // When the Connection-derived class is constructed, it is pre-set up with |
| // basic initialization information necessary to initiate the server request |
| // connection (such as the URL, request method, etc - see |
| // Transport::CreateConnection() for more details). But most implementations |
| // would not probably initiate the physical connection until SendHeaders |
| // is called. |
| // You normally shouldn't worry about using this class directly. |
| // http::Request and http::Response classes use it for communication. |
| /////////////////////////////////////////////////////////////////////////////// |
| class Connection { |
| public: |
| explicit Connection(std::shared_ptr<Transport> transport) |
| : transport_(transport) {} |
| virtual ~Connection() = default; |
| |
| // Called by http::Request to initiate the connection with the server. |
| // This normally opens the socket and sends the request headers. |
| virtual bool SendHeaders(const HeaderList& headers, ErrorPtr* error) = 0; |
| // If needed, this function can be called to send the request body data. |
| // This function can be called repeatedly until all data is sent. |
| virtual bool WriteRequestData(const void* data, size_t size, |
| ErrorPtr* error) = 0; |
| // This function is called when all the data is sent off and it's time |
| // to receive the response data. |
| virtual bool FinishRequest(ErrorPtr* error) = 0; |
| |
| // Returns the HTTP status code (e.g. 200 for success). |
| virtual int GetResponseStatusCode() const = 0; |
| // Returns the status text (e.g. for error 403 it could be "NOT AUTHORIZED"). |
| virtual std::string GetResponseStatusText() const = 0; |
| // Returns the HTTP protocol version (e.g. "HTTP/1.1"). |
| virtual std::string GetProtocolVersion() const = 0; |
| // Returns the value of particular response header, or empty string if the |
| // headers wasn't received. |
| virtual std::string GetResponseHeader( |
| const std::string& header_name) const = 0; |
| // Returns the response data size, if known. For chunked (streaming) |
| // transmission this might not be known until all the data is sent. |
| // In this case GetResponseDataSize() will return 0. |
| virtual uint64_t GetResponseDataSize() const = 0; |
| // This function is called to read a block of response data. |
| // It needs to be called repeatedly until it returns false or |size_read| is |
| // set to 0. |data| is the destination buffer to read the data into. |
| // |buffer_size| is the size of the buffer (amount of data to read). |
| // |read_size| is the amount of data actually read, which could be less than |
| // the size requested or 0 if there is no more data available. |
| virtual bool ReadResponseData(void* data, size_t buffer_size, |
| size_t* size_read, ErrorPtr* error) = 0; |
| |
| protected: |
| // |transport_| is mainly used to keep the object alive as long as the |
| // connection exists. But some implementations of Connection could use |
| // the Transport-derived class for their own needs as well. |
| std::shared_ptr<Transport> transport_; |
| |
| private: |
| DISALLOW_COPY_AND_ASSIGN(Connection); |
| }; |
| |
| } // namespace http |
| } // namespace chromeos |
| |
| #endif // BUFFET_HTTP_CONNECTION_H_ |