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 <glog/logging.h>
23
24 #include <curl/curl.h>
25 #include <json/json.h>
26
27 namespace {
28
file_write_callback(char * ptr,size_t,size_t nmemb,void * userdata)29 size_t file_write_callback(char *ptr, size_t, size_t nmemb, void *userdata) {
30 std::stringstream* stream = (std::stringstream*) userdata;
31 stream->write(ptr, nmemb);
32 return nmemb;
33 }
34
build_slist(const std::vector<std::string> & strings)35 curl_slist* build_slist(const std::vector<std::string>& strings) {
36 curl_slist* curl_headers = nullptr;
37 for (const auto& str : strings) {
38 curl_slist* temp = curl_slist_append(curl_headers, str.c_str());
39 if (temp == nullptr) {
40 LOG(ERROR) << "curl_slist_append failed to add " << str;
41 if (curl_headers) {
42 curl_slist_free_all(curl_headers);
43 return nullptr;
44 }
45 }
46 curl_headers = temp;
47 }
48 return curl_headers;
49 }
50
51 } // namespace
52
CurlWrapper()53 CurlWrapper::CurlWrapper() {
54 curl = curl_easy_init();
55 if (!curl) {
56 LOG(ERROR) << "failed to initialize curl";
57 return;
58 }
59 }
60
~CurlWrapper()61 CurlWrapper::~CurlWrapper() {
62 curl_easy_cleanup(curl);
63 }
64
DownloadToFile(const std::string & url,const std::string & path)65 bool CurlWrapper::DownloadToFile(const std::string& url, const std::string& path) {
66 return CurlWrapper::DownloadToFile(url, path, {});
67 }
68
DownloadToFile(const std::string & url,const std::string & path,const std::vector<std::string> & headers)69 bool CurlWrapper::DownloadToFile(const std::string& url, const std::string& path,
70 const std::vector<std::string>& headers) {
71 LOG(INFO) << "Attempting to save \"" << url << "\" to \"" << path << "\"";
72 if (!curl) {
73 LOG(ERROR) << "curl was not initialized\n";
74 return false;
75 }
76 curl_slist* curl_headers = build_slist(headers);
77 curl_easy_reset(curl);
78 curl_easy_setopt(curl, CURLOPT_CAINFO, "/etc/ssl/certs/ca-certificates.crt");
79 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, curl_headers);
80 curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
81 char error_buf[CURL_ERROR_SIZE];
82 curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, error_buf);
83 curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
84 FILE* file = fopen(path.c_str(), "w");
85 if (!file) {
86 LOG(ERROR) << "could not open file " << path;
87 return false;
88 }
89 curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void*) file);
90 CURLcode res = curl_easy_perform(curl);
91 if (curl_headers) {
92 curl_slist_free_all(curl_headers);
93 }
94 fclose(file);
95 if (res != CURLE_OK) {
96 LOG(ERROR) << "curl_easy_perform() failed. "
97 << "Code was \"" << res << "\". "
98 << "Strerror was \"" << curl_easy_strerror(res) << "\". "
99 << "Error buffer was \"" << error_buf << "\".";
100 return false;
101 }
102 return true;
103 }
104
DownloadToString(const std::string & url)105 std::string CurlWrapper::DownloadToString(const std::string& url) {
106 return DownloadToString(url, {});
107 }
108
DownloadToString(const std::string & url,const std::vector<std::string> & headers)109 std::string CurlWrapper::DownloadToString(const std::string& url,
110 const std::vector<std::string>& headers) {
111 LOG(INFO) << "Attempting to download \"" << url << "\"";
112 if (!curl) {
113 LOG(ERROR) << "curl was not initialized\n";
114 return "";
115 }
116 curl_slist* curl_headers = build_slist(headers);
117 curl_easy_reset(curl);
118 curl_easy_setopt(curl, CURLOPT_CAINFO, "/etc/ssl/certs/ca-certificates.crt");
119 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, curl_headers);
120 curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
121 std::stringstream data;
122 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, file_write_callback);
123 curl_easy_setopt(curl, CURLOPT_WRITEDATA, &data);
124 char error_buf[CURL_ERROR_SIZE];
125 curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, error_buf);
126 curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
127 CURLcode res = curl_easy_perform(curl);
128 if (curl_headers) {
129 curl_slist_free_all(curl_headers);
130 }
131 if (res != CURLE_OK) {
132 LOG(ERROR) << "curl_easy_perform() failed. "
133 << "Code was \"" << res << "\". "
134 << "Strerror was \"" << curl_easy_strerror(res) << "\". "
135 << "Error buffer was \"" << error_buf << "\".";
136 return "";
137 }
138 return data.str();
139 }
140
DownloadToJson(const std::string & url)141 Json::Value CurlWrapper::DownloadToJson(const std::string& url) {
142 return DownloadToJson(url, {});
143 }
144
DownloadToJson(const std::string & url,const std::vector<std::string> & headers)145 Json::Value CurlWrapper::DownloadToJson(const std::string& url,
146 const std::vector<std::string>& headers) {
147 std::string contents = DownloadToString(url, headers);
148 Json::Reader reader;
149 Json::Value json;
150 if (!reader.parse(contents, json)) {
151 LOG(ERROR) << "Could not parse json: " << reader.getFormattedErrorMessages();
152 json["error"] = "Failed to parse json.";
153 json["response"] = contents;
154 }
155 return json;
156 }
157