Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 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 "buffet/http_transport_curl.h" |
| 6 | |
Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 7 | #include <base/logging.h> |
| 8 | |
Alex Vakulenko | a3062c5 | 2014-04-21 17:05:51 -0700 | [diff] [blame^] | 9 | #include "buffet/http_connection_curl.h" |
| 10 | #include "buffet/http_request.h" |
Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 11 | |
| 12 | using namespace chromeos; |
| 13 | using namespace chromeos::http::curl; |
| 14 | |
Alex Vakulenko | a3062c5 | 2014-04-21 17:05:51 -0700 | [diff] [blame^] | 15 | Transport::Transport() { |
| 16 | VLOG(1) << "curl::Transport created"; |
Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 17 | } |
| 18 | |
| 19 | Transport::~Transport() { |
Alex Vakulenko | a3062c5 | 2014-04-21 17:05:51 -0700 | [diff] [blame^] | 20 | VLOG(1) << "curl::Transport destroyed"; |
Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 21 | } |
| 22 | |
Alex Vakulenko | a3062c5 | 2014-04-21 17:05:51 -0700 | [diff] [blame^] | 23 | std::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 Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 33 | LOG(ERROR) << "Failed to initialize CURL"; |
Alex Vakulenko | a3062c5 | 2014-04-21 17:05:51 -0700 | [diff] [blame^] | 34 | if (error_msg) |
| 35 | *error_msg = "Failed to initialize CURL"; |
| 36 | return std::unique_ptr<http::Connection>(); |
Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 37 | } |
| 38 | |
Alex Vakulenko | a3062c5 | 2014-04-21 17:05:51 -0700 | [diff] [blame^] | 39 | LOG(INFO) << "Sending a " << method << " request to " << url; |
| 40 | curl_easy_setopt(curl_handle, CURLOPT_URL, url.c_str()); |
Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 41 | |
Alex Vakulenko | a3062c5 | 2014-04-21 17:05:51 -0700 | [diff] [blame^] | 42 | if (!user_agent.empty()) { |
| 43 | curl_easy_setopt(curl_handle, |
| 44 | CURLOPT_USERAGENT, user_agent.c_str()); |
Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 45 | } |
| 46 | |
Alex Vakulenko | a3062c5 | 2014-04-21 17:05:51 -0700 | [diff] [blame^] | 47 | if (!referer.empty()) { |
| 48 | curl_easy_setopt(curl_handle, |
| 49 | CURLOPT_REFERER, referer.c_str()); |
Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | // Setup HTTP request method and optional request body. |
Alex Vakulenko | a3062c5 | 2014-04-21 17:05:51 -0700 | [diff] [blame^] | 53 | 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 Vakulenko | b645cc9 | 2014-04-15 11:34:35 -0700 | [diff] [blame] | 59 | } else { |
| 60 | // POST and custom request methods |
Alex Vakulenko | a3062c5 | 2014-04-21 17:05:51 -0700 | [diff] [blame^] | 61 | 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 Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 65 | } |
| 66 | |
Alex Vakulenko | a3062c5 | 2014-04-21 17:05:51 -0700 | [diff] [blame^] | 67 | 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 Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 74 | } |
Alex Vakulenko | a3062c5 | 2014-04-21 17:05:51 -0700 | [diff] [blame^] | 75 | return connection; |
Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 76 | } |