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_CURL_H_ |
| 6 | #define BUFFET_HTTP_CONNECTION_CURL_H_ |
| 7 | |
| 8 | #include <map> |
| 9 | #include <string> |
| 10 | #include <vector> |
| 11 | |
| 12 | #include <base/basictypes.h> |
| 13 | #include <curl/curl.h> |
| 14 | |
| 15 | #include "buffet/http_connection.h" |
| 16 | |
Alex Vakulenko | af23b32 | 2014-05-08 16:25:45 -0700 | [diff] [blame] | 17 | namespace buffet { |
Alex Vakulenko | a3062c5 | 2014-04-21 17:05:51 -0700 | [diff] [blame] | 18 | namespace http { |
| 19 | namespace curl { |
| 20 | |
| 21 | // This is a libcurl-based implementation of http::Connection. |
Alex Vakulenko | af23b32 | 2014-05-08 16:25:45 -0700 | [diff] [blame] | 22 | class Connection : public http::Connection { |
Alex Vakulenko | a3062c5 | 2014-04-21 17:05:51 -0700 | [diff] [blame] | 23 | public: |
| 24 | Connection(CURL* curl_handle, const std::string& method, |
| 25 | std::shared_ptr<http::Transport> transport); |
| 26 | virtual ~Connection(); |
| 27 | |
| 28 | // Overrides from http::Connection. |
| 29 | // See http_connection.h for description of these methods. |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 30 | virtual bool SendHeaders(const HeaderList& headers, ErrorPtr* error) override; |
| 31 | virtual bool WriteRequestData(const void* data, size_t size, |
| 32 | ErrorPtr* error) override; |
| 33 | virtual bool FinishRequest(ErrorPtr* error) override; |
Alex Vakulenko | a3062c5 | 2014-04-21 17:05:51 -0700 | [diff] [blame] | 34 | |
| 35 | virtual int GetResponseStatusCode() const override; |
| 36 | virtual std::string GetResponseStatusText() const override; |
| 37 | virtual std::string GetProtocolVersion() const override; |
| 38 | virtual std::string GetResponseHeader( |
| 39 | const std::string& header_name) const override; |
| 40 | virtual uint64_t GetResponseDataSize() const override; |
| 41 | virtual bool ReadResponseData(void* data, size_t buffer_size, |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 42 | size_t* size_read, ErrorPtr* error) override; |
Alex Vakulenko | a3062c5 | 2014-04-21 17:05:51 -0700 | [diff] [blame] | 43 | |
| 44 | protected: |
| 45 | // Write data callback. Used by CURL when receiving response data. |
| 46 | static size_t write_callback(char* ptr, size_t size, size_t num, void* data); |
| 47 | // Read data callback. Used by CURL when sending request body data. |
| 48 | static size_t read_callback(char* ptr, size_t size, size_t num, void* data); |
| 49 | // Write header data callback. Used by CURL when receiving response headers. |
| 50 | static size_t header_callback(char* ptr, size_t size, size_t num, void* data); |
| 51 | |
| 52 | // HTTP request verb, such as "GET", "POST", "PUT", ... |
| 53 | std::string method_; |
| 54 | |
| 55 | // Binary data for request body. |
| 56 | std::vector<unsigned char> request_data_; |
| 57 | // Read pointer for request data. Used when streaming data to the server. |
| 58 | size_t request_data_ptr_ = 0; |
| 59 | |
| 60 | // Received response data. |
| 61 | std::vector<unsigned char> response_data_; |
| 62 | size_t response_data_ptr_ = 0; |
| 63 | |
| 64 | // List of optional request headers provided by the caller. |
| 65 | // After request has been sent, contains the received response headers. |
| 66 | std::map<std::string, std::string> headers_; |
| 67 | |
Alex Vakulenko | a3062c5 | 2014-04-21 17:05:51 -0700 | [diff] [blame] | 68 | // HTTP protocol version, such as HTTP/1.1 |
| 69 | std::string protocol_version_; |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 70 | // Response status text, such as "OK" for 200, or "Forbidden" for 403 |
Alex Vakulenko | a3062c5 | 2014-04-21 17:05:51 -0700 | [diff] [blame] | 71 | std::string status_text_; |
| 72 | // Flag used when parsing response headers to separate the response status |
| 73 | // from the rest of response headers. |
| 74 | bool status_text_set_ = false; |
| 75 | |
| 76 | CURL* curl_handle_ = nullptr; |
| 77 | |
| 78 | private: |
| 79 | DISALLOW_COPY_AND_ASSIGN(Connection); |
| 80 | }; |
| 81 | |
| 82 | } // namespace curl |
| 83 | } // namespace http |
Alex Vakulenko | af23b32 | 2014-05-08 16:25:45 -0700 | [diff] [blame] | 84 | } // namespace buffet |
Alex Vakulenko | a3062c5 | 2014-04-21 17:05:51 -0700 | [diff] [blame] | 85 | |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 86 | #endif // BUFFET_HTTP_CONNECTION_CURL_H_ |