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