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