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 LIBBRILLO_BRILLO_HTTP_HTTP_CONNECTION_CURL_H_ 6 #define LIBBRILLO_BRILLO_HTTP_HTTP_CONNECTION_CURL_H_ 7 8 #include <map> 9 #include <memory> 10 #include <string> 11 #include <vector> 12 13 #include <base/macros.h> 14 #include <brillo/brillo_export.h> 15 #include <brillo/http/http_connection.h> 16 #include <brillo/http/http_transport_curl.h> 17 #include <curl/curl.h> 18 19 namespace brillo { 20 namespace http { 21 namespace curl { 22 23 // This is a libcurl-based implementation of http::Connection. 24 class BRILLO_EXPORT Connection : public http::Connection { 25 public: 26 Connection(CURL* curl_handle, 27 const std::string& method, 28 const std::shared_ptr<CurlInterface>& curl_interface, 29 const std::shared_ptr<http::Transport>& transport); 30 ~Connection() override; 31 32 // Overrides from http::Connection. 33 // See http_connection.h for description of these methods. 34 bool SendHeaders(const HeaderList& headers, brillo::ErrorPtr* error) override; 35 bool SetRequestData(StreamPtr stream, brillo::ErrorPtr* error) override; 36 void SetResponseData(StreamPtr stream) override; 37 bool FinishRequest(brillo::ErrorPtr* error) override; 38 RequestID FinishRequestAsync( 39 const SuccessCallback& success_callback, 40 const ErrorCallback& error_callback) override; 41 42 int GetResponseStatusCode() const override; 43 std::string GetResponseStatusText() const override; 44 std::string GetProtocolVersion() const override; 45 std::string GetResponseHeader(const std::string& header_name) const override; 46 StreamPtr ExtractDataStream(brillo::ErrorPtr* error) override; 47 48 protected: 49 // Write data callback. Used by CURL when receiving response data. 50 BRILLO_PRIVATE static size_t write_callback(char* ptr, 51 size_t size, 52 size_t num, 53 void* data); 54 // Read data callback. Used by CURL when sending request body data. 55 BRILLO_PRIVATE static size_t read_callback(char* ptr, 56 size_t size, 57 size_t num, 58 void* data); 59 // Write header data callback. Used by CURL when receiving response headers. 60 BRILLO_PRIVATE static size_t header_callback(char* ptr, 61 size_t size, 62 size_t num, 63 void* data); 64 65 // Helper method to set up the |curl_handle_| with all the parameters 66 // pertaining to the current connection. 67 BRILLO_PRIVATE void PrepareRequest(); 68 69 // HTTP request verb, such as "GET", "POST", "PUT", ... 70 std::string method_; 71 72 // Binary data for request body. 73 StreamPtr request_data_stream_; 74 75 // Received response data. 76 StreamPtr response_data_stream_; 77 78 // List of optional request headers provided by the caller. 79 // After request has been sent, contains the received response headers. 80 std::multimap<std::string, std::string> headers_; 81 82 // HTTP protocol version, such as HTTP/1.1 83 std::string protocol_version_; 84 // Response status text, such as "OK" for 200, or "Forbidden" for 403 85 std::string status_text_; 86 // Flag used when parsing response headers to separate the response status 87 // from the rest of response headers. 88 bool status_text_set_{false}; 89 90 CURL* curl_handle_{nullptr}; 91 curl_slist* header_list_{nullptr}; 92 93 std::shared_ptr<CurlInterface> curl_interface_; 94 95 private: 96 friend class http::curl::Transport; 97 DISALLOW_COPY_AND_ASSIGN(Connection); 98 }; 99 100 } // namespace curl 101 } // namespace http 102 } // namespace brillo 103 104 #endif // LIBBRILLO_BRILLO_HTTP_HTTP_CONNECTION_CURL_H_ 105