Alex Vakulenko | a3062c5 | 2014-04-21 17:05:51 -0700 | [diff] [blame] | 1 | // Copyright 2014 The Chromium OS 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 | |
| 5 | #ifndef BUFFET_HTTP_CONNECTION_H_ |
| 6 | #define BUFFET_HTTP_CONNECTION_H_ |
| 7 | |
| 8 | #include <string> |
| 9 | #include <vector> |
| 10 | |
| 11 | #include <base/basictypes.h> |
| 12 | |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 13 | #include "buffet/error.h" |
Alex Vakulenko | a3062c5 | 2014-04-21 17:05:51 -0700 | [diff] [blame] | 14 | #include "buffet/http_transport.h" |
| 15 | |
Alex Vakulenko | af23b32 | 2014-05-08 16:25:45 -0700 | [diff] [blame] | 16 | namespace buffet { |
Alex Vakulenko | a3062c5 | 2014-04-21 17:05:51 -0700 | [diff] [blame] | 17 | namespace http { |
| 18 | |
| 19 | /////////////////////////////////////////////////////////////////////////////// |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 20 | // Connection class is the base class for HTTP communication session. |
Alex Vakulenko | a3062c5 | 2014-04-21 17:05:51 -0700 | [diff] [blame] | 21 | // It abstracts the implementation of underlying transport library (ex libcurl). |
| 22 | // When the Connection-derived class is constructed, it is pre-set up with |
| 23 | // basic initialization information necessary to initiate the server request |
| 24 | // connection (such as the URL, request method, etc - see |
| 25 | // Transport::CreateConnection() for more details). But most implementations |
| 26 | // would not probably initiate the physical connection until SendHeaders |
| 27 | // is called. |
| 28 | // You normally shouldn't worry about using this class directly. |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 29 | // http::Request and http::Response classes use it for communication. |
Alex Vakulenko | a3062c5 | 2014-04-21 17:05:51 -0700 | [diff] [blame] | 30 | /////////////////////////////////////////////////////////////////////////////// |
| 31 | class Connection { |
| 32 | public: |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 33 | explicit Connection(std::shared_ptr<Transport> transport) |
| 34 | : transport_(transport) {} |
Alex Vakulenko | a3062c5 | 2014-04-21 17:05:51 -0700 | [diff] [blame] | 35 | virtual ~Connection() = default; |
| 36 | |
| 37 | // Called by http::Request to initiate the connection with the server. |
| 38 | // This normally opens the socket and sends the request headers. |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 39 | virtual bool SendHeaders(const HeaderList& headers, ErrorPtr* error) = 0; |
Alex Vakulenko | a3062c5 | 2014-04-21 17:05:51 -0700 | [diff] [blame] | 40 | // If needed, this function can be called to send the request body data. |
| 41 | // This function can be called repeatedly until all data is sent. |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 42 | virtual bool WriteRequestData(const void* data, size_t size, |
| 43 | ErrorPtr* error) = 0; |
Alex Vakulenko | a3062c5 | 2014-04-21 17:05:51 -0700 | [diff] [blame] | 44 | // This function is called when all the data is sent off and it's time |
| 45 | // to receive the response data. |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 46 | virtual bool FinishRequest(ErrorPtr* error) = 0; |
Alex Vakulenko | a3062c5 | 2014-04-21 17:05:51 -0700 | [diff] [blame] | 47 | |
| 48 | // Returns the HTTP status code (e.g. 200 for success). |
| 49 | virtual int GetResponseStatusCode() const = 0; |
| 50 | // Returns the status text (e.g. for error 403 it could be "NOT AUTHORIZED"). |
| 51 | virtual std::string GetResponseStatusText() const = 0; |
| 52 | // Returns the HTTP protocol version (e.g. "HTTP/1.1"). |
| 53 | virtual std::string GetProtocolVersion() const = 0; |
| 54 | // Returns the value of particular response header, or empty string if the |
| 55 | // headers wasn't received. |
| 56 | virtual std::string GetResponseHeader( |
| 57 | const std::string& header_name) const = 0; |
| 58 | // Returns the response data size, if known. For chunked (streaming) |
| 59 | // transmission this might not be known until all the data is sent. |
| 60 | // In this case GetResponseDataSize() will return 0. |
| 61 | virtual uint64_t GetResponseDataSize() const = 0; |
| 62 | // This function is called to read a block of response data. |
| 63 | // It needs to be called repeatedly until it returns false or |size_read| is |
| 64 | // set to 0. |data| is the destination buffer to read the data into. |
| 65 | // |buffer_size| is the size of the buffer (amount of data to read). |
| 66 | // |read_size| is the amount of data actually read, which could be less than |
| 67 | // the size requested or 0 if there is no more data available. |
| 68 | virtual bool ReadResponseData(void* data, size_t buffer_size, |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 69 | size_t* size_read, ErrorPtr* error) = 0; |
Alex Vakulenko | a3062c5 | 2014-04-21 17:05:51 -0700 | [diff] [blame] | 70 | |
| 71 | protected: |
| 72 | // |transport_| is mainly used to keep the object alive as long as the |
| 73 | // connection exists. But some implementations of Connection could use |
| 74 | // the Transport-derived class for their own needs as well. |
| 75 | std::shared_ptr<Transport> transport_; |
| 76 | |
| 77 | private: |
| 78 | DISALLOW_COPY_AND_ASSIGN(Connection); |
| 79 | }; |
| 80 | |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 81 | } // namespace http |
Alex Vakulenko | af23b32 | 2014-05-08 16:25:45 -0700 | [diff] [blame] | 82 | } // namespace buffet |
Alex Vakulenko | a3062c5 | 2014-04-21 17:05:51 -0700 | [diff] [blame] | 83 | |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 84 | #endif // BUFFET_HTTP_CONNECTION_H_ |