blob: 9a82e26e8f52c2655803d4513e03099e3131bc43 [file] [log] [blame]
Alex Vakulenkoa3062c52014-04-21 17:05:51 -07001// 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 Vakulenkoaf23b322014-05-08 16:25:45 -070017namespace buffet {
Alex Vakulenkoa3062c52014-04-21 17:05:51 -070018namespace http {
19namespace curl {
20
21// This is a libcurl-based implementation of http::Connection.
Alex Vakulenkoaf23b322014-05-08 16:25:45 -070022class Connection : public http::Connection {
Alex Vakulenkoa3062c52014-04-21 17:05:51 -070023 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 Vakulenkob3aac252014-05-07 17:35:24 -070030 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 Vakulenkoa3062c52014-04-21 17:05:51 -070034
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 Vakulenkob3aac252014-05-07 17:35:24 -070042 size_t* size_read, ErrorPtr* error) override;
Alex Vakulenkoa3062c52014-04-21 17:05:51 -070043
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 Vakulenkoa3062c52014-04-21 17:05:51 -070068 // HTTP protocol version, such as HTTP/1.1
69 std::string protocol_version_;
Alex Vakulenkob3aac252014-05-07 17:35:24 -070070 // Response status text, such as "OK" for 200, or "Forbidden" for 403
Alex Vakulenkoa3062c52014-04-21 17:05:51 -070071 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 Vakulenkoaf23b322014-05-08 16:25:45 -070084} // namespace buffet
Alex Vakulenkoa3062c52014-04-21 17:05:51 -070085
Alex Vakulenkob3aac252014-05-07 17:35:24 -070086#endif // BUFFET_HTTP_CONNECTION_CURL_H_