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 #include <brillo/http/http_connection_curl.h>
6
7 #include <utility>
8
9 #include <base/logging.h>
10 #include <brillo/http/http_request.h>
11 #include <brillo/http/http_transport_curl.h>
12 #include <brillo/streams/memory_stream.h>
13 #include <brillo/streams/stream_utils.h>
14 #include <brillo/strings/string_utils.h>
15
16 namespace brillo {
17 namespace http {
18 namespace curl {
19
curl_trace(CURL *,curl_infotype type,char * data,size_t size,void *)20 static int curl_trace(CURL* /* handle */,
21 curl_infotype type,
22 char* data,
23 size_t size,
24 void* /* userp */) {
25 std::string msg(data, size);
26
27 switch (type) {
28 case CURLINFO_TEXT:
29 VLOG(3) << "== Info: " << msg;
30 break;
31 case CURLINFO_HEADER_OUT:
32 VLOG(3) << "=> Send headers:\n" << msg;
33 break;
34 case CURLINFO_DATA_OUT:
35 VLOG(3) << "=> Send data:\n" << msg;
36 break;
37 case CURLINFO_SSL_DATA_OUT:
38 VLOG(3) << "=> Send SSL data" << msg;
39 break;
40 case CURLINFO_HEADER_IN:
41 VLOG(3) << "<= Recv header: " << msg;
42 break;
43 case CURLINFO_DATA_IN:
44 VLOG(3) << "<= Recv data:\n" << msg;
45 break;
46 case CURLINFO_SSL_DATA_IN:
47 VLOG(3) << "<= Recv SSL data" << msg;
48 break;
49 default:
50 break;
51 }
52 return 0;
53 }
54
Connection(CURL * curl_handle,const std::string & method,const std::shared_ptr<CurlInterface> & curl_interface,const std::shared_ptr<http::Transport> & transport)55 Connection::Connection(CURL* curl_handle,
56 const std::string& method,
57 const std::shared_ptr<CurlInterface>& curl_interface,
58 const std::shared_ptr<http::Transport>& transport)
59 : http::Connection(transport),
60 method_(method),
61 curl_handle_(curl_handle),
62 curl_interface_(curl_interface) {
63 // Store the connection pointer inside the CURL handle so we can easily
64 // retrieve it when doing asynchronous I/O.
65 curl_interface_->EasySetOptPtr(curl_handle_, CURLOPT_PRIVATE, this);
66 VLOG(2) << "curl::Connection created: " << method_;
67 }
68
~Connection()69 Connection::~Connection() {
70 if (header_list_)
71 curl_slist_free_all(header_list_);
72 curl_interface_->EasyCleanup(curl_handle_);
73 VLOG(2) << "curl::Connection destroyed";
74 }
75
SendHeaders(const HeaderList & headers,brillo::ErrorPtr *)76 bool Connection::SendHeaders(const HeaderList& headers,
77 brillo::ErrorPtr* /* error */) {
78 headers_.insert(headers.begin(), headers.end());
79 return true;
80 }
81
SetRequestData(StreamPtr stream,brillo::ErrorPtr *)82 bool Connection::SetRequestData(StreamPtr stream,
83 brillo::ErrorPtr* /* error */) {
84 request_data_stream_ = std::move(stream);
85 return true;
86 }
87
SetResponseData(StreamPtr stream)88 void Connection::SetResponseData(StreamPtr stream) {
89 response_data_stream_ = std::move(stream);
90 }
91
PrepareRequest()92 void Connection::PrepareRequest() {
93 if (VLOG_IS_ON(3)) {
94 curl_interface_->EasySetOptCallback(
95 curl_handle_, CURLOPT_DEBUGFUNCTION, &curl_trace);
96 curl_interface_->EasySetOptInt(curl_handle_, CURLOPT_VERBOSE, 1);
97 }
98
99 if (method_ != request_type::kGet) {
100 // Set up HTTP request data.
101 uint64_t data_size = 0;
102 if (request_data_stream_ && request_data_stream_->CanGetSize())
103 data_size = request_data_stream_->GetRemainingSize();
104
105 if (!request_data_stream_ || request_data_stream_->CanGetSize()) {
106 // Data size is known (either no data, or data size is available).
107 if (method_ == request_type::kPut) {
108 curl_interface_->EasySetOptOffT(
109 curl_handle_, CURLOPT_INFILESIZE_LARGE, data_size);
110 } else {
111 curl_interface_->EasySetOptOffT(
112 curl_handle_, CURLOPT_POSTFIELDSIZE_LARGE, data_size);
113 }
114 } else {
115 // Data size is unknown, so use chunked upload.
116 headers_.emplace(http::request_header::kTransferEncoding, "chunked");
117 }
118
119 if (request_data_stream_) {
120 curl_interface_->EasySetOptCallback(
121 curl_handle_, CURLOPT_READFUNCTION, &Connection::read_callback);
122 curl_interface_->EasySetOptPtr(curl_handle_, CURLOPT_READDATA, this);
123 }
124 }
125
126 if (!headers_.empty()) {
127 CHECK(header_list_ == nullptr);
128 for (auto pair : headers_) {
129 std::string header =
130 brillo::string_utils::Join(": ", pair.first, pair.second);
131 VLOG(2) << "Request header: " << header;
132 header_list_ = curl_slist_append(header_list_, header.c_str());
133 }
134 curl_interface_->EasySetOptPtr(
135 curl_handle_, CURLOPT_HTTPHEADER, header_list_);
136 }
137
138 headers_.clear();
139
140 // Set up HTTP response data.
141 if (!response_data_stream_)
142 response_data_stream_ = MemoryStream::Create(nullptr);
143 if (method_ != request_type::kHead) {
144 curl_interface_->EasySetOptCallback(
145 curl_handle_, CURLOPT_WRITEFUNCTION, &Connection::write_callback);
146 curl_interface_->EasySetOptPtr(curl_handle_, CURLOPT_WRITEDATA, this);
147 }
148
149 // HTTP response headers
150 curl_interface_->EasySetOptCallback(
151 curl_handle_, CURLOPT_HEADERFUNCTION, &Connection::header_callback);
152 curl_interface_->EasySetOptPtr(curl_handle_, CURLOPT_HEADERDATA, this);
153 }
154
FinishRequest(brillo::ErrorPtr * error)155 bool Connection::FinishRequest(brillo::ErrorPtr* error) {
156 PrepareRequest();
157 CURLcode ret = curl_interface_->EasyPerform(curl_handle_);
158 if (ret != CURLE_OK) {
159 Transport::AddEasyCurlError(error, FROM_HERE, ret, curl_interface_.get());
160 } else {
161 // Rewind our data stream to the beginning so that it can be read back.
162 if (response_data_stream_->CanSeek() &&
163 !response_data_stream_->SetPosition(0, error))
164 return false;
165 LOG(INFO) << "Response: " << GetResponseStatusCode() << " ("
166 << GetResponseStatusText() << ")";
167 }
168 return (ret == CURLE_OK);
169 }
170
FinishRequestAsync(const SuccessCallback & success_callback,const ErrorCallback & error_callback)171 RequestID Connection::FinishRequestAsync(
172 const SuccessCallback& success_callback,
173 const ErrorCallback& error_callback) {
174 PrepareRequest();
175 return transport_->StartAsyncTransfer(this, success_callback, error_callback);
176 }
177
GetResponseStatusCode() const178 int Connection::GetResponseStatusCode() const {
179 int status_code = 0;
180 curl_interface_->EasyGetInfoInt(
181 curl_handle_, CURLINFO_RESPONSE_CODE, &status_code);
182 return status_code;
183 }
184
GetResponseStatusText() const185 std::string Connection::GetResponseStatusText() const {
186 return status_text_;
187 }
188
GetProtocolVersion() const189 std::string Connection::GetProtocolVersion() const {
190 return protocol_version_;
191 }
192
GetResponseHeader(const std::string & header_name) const193 std::string Connection::GetResponseHeader(
194 const std::string& header_name) const {
195 auto p = headers_.find(header_name);
196 return p != headers_.end() ? p->second : std::string();
197 }
198
ExtractDataStream(brillo::ErrorPtr * error)199 StreamPtr Connection::ExtractDataStream(brillo::ErrorPtr* error) {
200 if (!response_data_stream_) {
201 stream_utils::ErrorStreamClosed(FROM_HERE, error);
202 }
203 return std::move(response_data_stream_);
204 }
205
write_callback(char * ptr,size_t size,size_t num,void * data)206 size_t Connection::write_callback(char* ptr,
207 size_t size,
208 size_t num,
209 void* data) {
210 Connection* me = reinterpret_cast<Connection*>(data);
211 size_t data_len = size * num;
212 VLOG(1) << "Response data (" << data_len << "): "
213 << std::string{ptr, data_len};
214 // TODO(nathanbullock): Currently we are relying on the stream not blocking,
215 // but if the stream is representing a pipe or some other construct that might
216 // block then this code will behave badly.
217 if (!me->response_data_stream_->WriteAllBlocking(ptr, data_len, nullptr)) {
218 LOG(ERROR) << "Failed to write response data";
219 data_len = 0;
220 }
221 return data_len;
222 }
223
read_callback(char * ptr,size_t size,size_t num,void * data)224 size_t Connection::read_callback(char* ptr,
225 size_t size,
226 size_t num,
227 void* data) {
228 Connection* me = reinterpret_cast<Connection*>(data);
229 size_t data_len = size * num;
230
231 size_t read_size = 0;
232 bool success = me->request_data_stream_->ReadBlocking(ptr, data_len,
233 &read_size, nullptr);
234 VLOG_IF(3, success) << "Sending data: " << std::string{ptr, read_size};
235 return success ? read_size : CURL_READFUNC_ABORT;
236 }
237
header_callback(char * ptr,size_t size,size_t num,void * data)238 size_t Connection::header_callback(char* ptr,
239 size_t size,
240 size_t num,
241 void* data) {
242 using brillo::string_utils::SplitAtFirst;
243 Connection* me = reinterpret_cast<Connection*>(data);
244 size_t hdr_len = size * num;
245 std::string header(ptr, hdr_len);
246 // Remove newlines at the end of header line.
247 while (!header.empty() && (header.back() == '\r' || header.back() == '\n')) {
248 header.pop_back();
249 }
250
251 VLOG(2) << "Response header: " << header;
252
253 if (!me->status_text_set_) {
254 // First header - response code as "HTTP/1.1 200 OK".
255 // Need to extract the OK part
256 auto pair = SplitAtFirst(header, " ");
257 me->protocol_version_ = pair.first;
258 me->status_text_ = SplitAtFirst(pair.second, " ").second;
259 me->status_text_set_ = true;
260 } else {
261 auto pair = SplitAtFirst(header, ":");
262 if (!pair.second.empty())
263 me->headers_.insert(pair);
264 }
265 return hdr_len;
266 }
267
268 } // namespace curl
269 } // namespace http
270 } // namespace brillo
271