blob: 7ef914b935d5540400ecedf2099d4d7911a99c44 [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
Alex Vakulenkoaf23b322014-05-08 16:25:45 -070012namespace buffet {
13namespace http {
14namespace curl {
Alex Vakulenkob3aac252014-05-07 17:35:24 -070015
Alex Vakulenkoaf23b322014-05-08 16:25:45 -070016const char kErrorDomain[] = "http_transport";
Chris Sosa45d9f102014-03-24 11:18:54 -070017
Alex Vakulenkoa3062c52014-04-21 17:05:51 -070018Transport::Transport() {
19 VLOG(1) << "curl::Transport created";
Chris Sosa45d9f102014-03-24 11:18:54 -070020}
21
22Transport::~Transport() {
Alex Vakulenkoa3062c52014-04-21 17:05:51 -070023 VLOG(1) << "curl::Transport destroyed";
Chris Sosa45d9f102014-03-24 11:18:54 -070024}
25
Alex Vakulenkoa3062c52014-04-21 17:05:51 -070026std::unique_ptr<http::Connection> Transport::CreateConnection(
27 std::shared_ptr<http::Transport> transport,
28 const std::string& url,
29 const std::string& method,
30 const HeaderList& headers,
31 const std::string& user_agent,
32 const std::string& referer,
Alex Vakulenkob3aac252014-05-07 17:35:24 -070033 ErrorPtr* error) {
Alex Vakulenkoa3062c52014-04-21 17:05:51 -070034 CURL* curl_handle = curl_easy_init();
35 if (!curl_handle) {
Chris Sosa45d9f102014-03-24 11:18:54 -070036 LOG(ERROR) << "Failed to initialize CURL";
Alex Vakulenkob3aac252014-05-07 17:35:24 -070037 Error::AddTo(error, http::curl::kErrorDomain, "curl_init_failed",
38 "Failed to initialize CURL");
Alex Vakulenkoa3062c52014-04-21 17:05:51 -070039 return std::unique_ptr<http::Connection>();
Chris Sosa45d9f102014-03-24 11:18:54 -070040 }
41
Alex Vakulenkoa3062c52014-04-21 17:05:51 -070042 LOG(INFO) << "Sending a " << method << " request to " << url;
43 curl_easy_setopt(curl_handle, CURLOPT_URL, url.c_str());
Chris Sosa45d9f102014-03-24 11:18:54 -070044
Alex Vakulenkoa3062c52014-04-21 17:05:51 -070045 if (!user_agent.empty()) {
46 curl_easy_setopt(curl_handle,
47 CURLOPT_USERAGENT, user_agent.c_str());
Chris Sosa45d9f102014-03-24 11:18:54 -070048 }
49
Alex Vakulenkoa3062c52014-04-21 17:05:51 -070050 if (!referer.empty()) {
51 curl_easy_setopt(curl_handle,
52 CURLOPT_REFERER, referer.c_str());
Chris Sosa45d9f102014-03-24 11:18:54 -070053 }
54
55 // Setup HTTP request method and optional request body.
Alex Vakulenkoa3062c52014-04-21 17:05:51 -070056 if (method == request_type::kGet) {
57 curl_easy_setopt(curl_handle, CURLOPT_HTTPGET, 1L);
58 } else if (method == request_type::kHead) {
59 curl_easy_setopt(curl_handle, CURLOPT_NOBODY, 1L);
60 } else if (method == request_type::kPut) {
61 curl_easy_setopt(curl_handle, CURLOPT_UPLOAD, 1L);
Alex Vakulenkob645cc92014-04-15 11:34:35 -070062 } else {
63 // POST and custom request methods
Alex Vakulenkoa3062c52014-04-21 17:05:51 -070064 curl_easy_setopt(curl_handle, CURLOPT_POST, 1L);
65 curl_easy_setopt(curl_handle, CURLOPT_POSTFIELDS, nullptr);
66 if (method != request_type::kPost)
67 curl_easy_setopt(curl_handle, CURLOPT_CUSTOMREQUEST, method.c_str());
Chris Sosa45d9f102014-03-24 11:18:54 -070068 }
69
Alex Vakulenkoa3062c52014-04-21 17:05:51 -070070 std::unique_ptr<http::Connection> connection(
71 new http::curl::Connection(curl_handle, method, transport));
72 CHECK(connection) << "Unable to create Connection object";
Alex Vakulenkob3aac252014-05-07 17:35:24 -070073 if (!connection->SendHeaders(headers, error)) {
Alex Vakulenkoa3062c52014-04-21 17:05:51 -070074 connection.reset();
Chris Sosa45d9f102014-03-24 11:18:54 -070075 }
Alex Vakulenkoa3062c52014-04-21 17:05:51 -070076 return connection;
Chris Sosa45d9f102014-03-24 11:18:54 -070077}
Alex Vakulenkob3aac252014-05-07 17:35:24 -070078
Alex Vakulenkoaf23b322014-05-08 16:25:45 -070079} // namespace curl
80} // namespace http
81} // namespace buffet