blob: 8c4b9464faa3fcdb28d8566bb2e6959423e83bc6 [file] [log] [blame]
Chris Sosa45d9f102014-03-24 11:18:54 -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#include "buffet/http_transport_curl.h"
6
Chris Sosa45d9f102014-03-24 11:18:54 -07007#include <base/logging.h>
8
Alex Vakulenkoa3062c52014-04-21 17:05:51 -07009#include "buffet/http_connection_curl.h"
10#include "buffet/http_request.h"
Chris Sosa45d9f102014-03-24 11:18:54 -070011
12using namespace chromeos;
13using namespace chromeos::http::curl;
14
Alex Vakulenkoa3062c52014-04-21 17:05:51 -070015Transport::Transport() {
16 VLOG(1) << "curl::Transport created";
Chris Sosa45d9f102014-03-24 11:18:54 -070017}
18
19Transport::~Transport() {
Alex Vakulenkoa3062c52014-04-21 17:05:51 -070020 VLOG(1) << "curl::Transport destroyed";
Chris Sosa45d9f102014-03-24 11:18:54 -070021}
22
Alex Vakulenkoa3062c52014-04-21 17:05:51 -070023std::unique_ptr<http::Connection> Transport::CreateConnection(
24 std::shared_ptr<http::Transport> transport,
25 const std::string& url,
26 const std::string& method,
27 const HeaderList& headers,
28 const std::string& user_agent,
29 const std::string& referer,
30 std::string* error_msg) {
31 CURL* curl_handle = curl_easy_init();
32 if (!curl_handle) {
Chris Sosa45d9f102014-03-24 11:18:54 -070033 LOG(ERROR) << "Failed to initialize CURL";
Alex Vakulenkoa3062c52014-04-21 17:05:51 -070034 if (error_msg)
35 *error_msg = "Failed to initialize CURL";
36 return std::unique_ptr<http::Connection>();
Chris Sosa45d9f102014-03-24 11:18:54 -070037 }
38
Alex Vakulenkoa3062c52014-04-21 17:05:51 -070039 LOG(INFO) << "Sending a " << method << " request to " << url;
40 curl_easy_setopt(curl_handle, CURLOPT_URL, url.c_str());
Chris Sosa45d9f102014-03-24 11:18:54 -070041
Alex Vakulenkoa3062c52014-04-21 17:05:51 -070042 if (!user_agent.empty()) {
43 curl_easy_setopt(curl_handle,
44 CURLOPT_USERAGENT, user_agent.c_str());
Chris Sosa45d9f102014-03-24 11:18:54 -070045 }
46
Alex Vakulenkoa3062c52014-04-21 17:05:51 -070047 if (!referer.empty()) {
48 curl_easy_setopt(curl_handle,
49 CURLOPT_REFERER, referer.c_str());
Chris Sosa45d9f102014-03-24 11:18:54 -070050 }
51
52 // Setup HTTP request method and optional request body.
Alex Vakulenkoa3062c52014-04-21 17:05:51 -070053 if (method == request_type::kGet) {
54 curl_easy_setopt(curl_handle, CURLOPT_HTTPGET, 1L);
55 } else if (method == request_type::kHead) {
56 curl_easy_setopt(curl_handle, CURLOPT_NOBODY, 1L);
57 } else if (method == request_type::kPut) {
58 curl_easy_setopt(curl_handle, CURLOPT_UPLOAD, 1L);
Alex Vakulenkob645cc92014-04-15 11:34:35 -070059 } else {
60 // POST and custom request methods
Alex Vakulenkoa3062c52014-04-21 17:05:51 -070061 curl_easy_setopt(curl_handle, CURLOPT_POST, 1L);
62 curl_easy_setopt(curl_handle, CURLOPT_POSTFIELDS, nullptr);
63 if (method != request_type::kPost)
64 curl_easy_setopt(curl_handle, CURLOPT_CUSTOMREQUEST, method.c_str());
Chris Sosa45d9f102014-03-24 11:18:54 -070065 }
66
Alex Vakulenkoa3062c52014-04-21 17:05:51 -070067 std::unique_ptr<http::Connection> connection(
68 new http::curl::Connection(curl_handle, method, transport));
69 CHECK(connection) << "Unable to create Connection object";
70 if (!connection->SendHeaders(headers)) {
71 connection.reset();
72 if (error_msg)
73 *error_msg = "Failed to send request headers";
Chris Sosa45d9f102014-03-24 11:18:54 -070074 }
Alex Vakulenkoa3062c52014-04-21 17:05:51 -070075 return connection;
Chris Sosa45d9f102014-03-24 11:18:54 -070076}