1 //
2 // Copyright (C) 2019 The Android Open Source Project
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15
16 #include "curl_wrapper.h"
17
18 #include <sstream>
19 #include <string>
20 #include <stdio.h>
21
22 #include <android-base/logging.h>
23
24 #include <curl/curl.h>
25 #include <json/json.h>
26
27 namespace cuttlefish {
28 namespace {
29
file_write_callback(char * ptr,size_t,size_t nmemb,void * userdata)30 size_t file_write_callback(char *ptr, size_t, size_t nmemb, void *userdata) {
31 std::stringstream* stream = (std::stringstream*) userdata;
32 stream->write(ptr, nmemb);
33 return nmemb;
34 }
35
build_slist(const std::vector<std::string> & strings)36 curl_slist* build_slist(const std::vector<std::string>& strings) {
37 curl_slist* curl_headers = nullptr;
38 for (const auto& str : strings) {
39 curl_slist* temp = curl_slist_append(curl_headers, str.c_str());
40 if (temp == nullptr) {
41 LOG(ERROR) << "curl_slist_append failed to add " << str;
42 if (curl_headers) {
43 curl_slist_free_all(curl_headers);
44 return nullptr;
45 }
46 }
47 curl_headers = temp;
48 }
49 return curl_headers;
50 }
51
52 } // namespace
53
CurlWrapper()54 CurlWrapper::CurlWrapper() {
55 curl = curl_easy_init();
56 if (!curl) {
57 LOG(ERROR) << "failed to initialize curl";
58 return;
59 }
60 }
61
~CurlWrapper()62 CurlWrapper::~CurlWrapper() {
63 curl_easy_cleanup(curl);
64 }
65
DownloadToFile(const std::string & url,const std::string & path)66 bool CurlWrapper::DownloadToFile(const std::string& url, const std::string& path) {
67 return CurlWrapper::DownloadToFile(url, path, {});
68 }
69
DownloadToFile(const std::string & url,const std::string & path,const std::vector<std::string> & headers)70 bool CurlWrapper::DownloadToFile(const std::string& url, const std::string& path,
71 const std::vector<std::string>& headers) {
72 LOG(INFO) << "Attempting to save \"" << url << "\" to \"" << path << "\"";
73 if (!curl) {
74 LOG(ERROR) << "curl was not initialized\n";
75 return false;
76 }
77 curl_slist* curl_headers = build_slist(headers);
78 curl_easy_reset(curl);
79 curl_easy_setopt(curl, CURLOPT_CAINFO, "/etc/ssl/certs/ca-certificates.crt");
80 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, curl_headers);
81 curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
82 char error_buf[CURL_ERROR_SIZE];
83 curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, error_buf);
84 curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
85 FILE* file = fopen(path.c_str(), "w");
86 if (!file) {
87 LOG(ERROR) << "could not open file " << path;
88 return false;
89 }
90 curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void*) file);
91 CURLcode res = curl_easy_perform(curl);
92 if (curl_headers) {
93 curl_slist_free_all(curl_headers);
94 }
95 fclose(file);
96 if (res != CURLE_OK) {
97 LOG(ERROR) << "curl_easy_perform() failed. "
98 << "Code was \"" << res << "\". "
99 << "Strerror was \"" << curl_easy_strerror(res) << "\". "
100 << "Error buffer was \"" << error_buf << "\".";
101 return false;
102 }
103 return true;
104 }
105
DownloadToString(const std::string & url)106 std::string CurlWrapper::DownloadToString(const std::string& url) {
107 return DownloadToString(url, {});
108 }
109
DownloadToString(const std::string & url,const std::vector<std::string> & headers)110 std::string CurlWrapper::DownloadToString(const std::string& url,
111 const std::vector<std::string>& headers) {
112 LOG(INFO) << "Attempting to download \"" << url << "\"";
113 if (!curl) {
114 LOG(ERROR) << "curl was not initialized\n";
115 return "";
116 }
117 curl_slist* curl_headers = build_slist(headers);
118 curl_easy_reset(curl);
119 curl_easy_setopt(curl, CURLOPT_CAINFO, "/etc/ssl/certs/ca-certificates.crt");
120 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, curl_headers);
121 curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
122 std::stringstream data;
123 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, file_write_callback);
124 curl_easy_setopt(curl, CURLOPT_WRITEDATA, &data);
125 char error_buf[CURL_ERROR_SIZE];
126 curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, error_buf);
127 curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
128 CURLcode res = curl_easy_perform(curl);
129 if (curl_headers) {
130 curl_slist_free_all(curl_headers);
131 }
132 if (res != CURLE_OK) {
133 LOG(ERROR) << "curl_easy_perform() failed. "
134 << "Code was \"" << res << "\". "
135 << "Strerror was \"" << curl_easy_strerror(res) << "\". "
136 << "Error buffer was \"" << error_buf << "\".";
137 return "";
138 }
139 return data.str();
140 }
141
DownloadToJson(const std::string & url)142 Json::Value CurlWrapper::DownloadToJson(const std::string& url) {
143 return DownloadToJson(url, {});
144 }
145
DownloadToJson(const std::string & url,const std::vector<std::string> & headers)146 Json::Value CurlWrapper::DownloadToJson(const std::string& url,
147 const std::vector<std::string>& headers) {
148 std::string contents = DownloadToString(url, headers);
149 Json::Reader reader;
150 Json::Value json;
151 if (!reader.parse(contents, json)) {
152 LOG(ERROR) << "Could not parse json: " << reader.getFormattedErrorMessages();
153 json["error"] = "Failed to parse json.";
154 json["response"] = contents;
155 }
156 return json;
157 }
158
159 }
160