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