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 "host/libs/web/http_client/http_client.h"
17
18 #include <stdio.h>
19
20 #include <fstream>
21 #include <mutex>
22 #include <sstream>
23 #include <string>
24 #include <thread>
25
26 #include <android-base/logging.h>
27 #include <android-base/strings.h>
28 #include <curl/curl.h>
29 #include <json/json.h>
30
31 #include "common/libs/utils/json.h"
32 #include "common/libs/utils/subprocess.h"
33
34 namespace cuttlefish {
35 namespace {
36
37 enum class HttpMethod {
38 kGet,
39 kPost,
40 kDelete,
41 };
42
curl_to_function_cb(char * ptr,size_t,size_t nmemb,void * userdata)43 size_t curl_to_function_cb(char* ptr, size_t, size_t nmemb, void* userdata) {
44 HttpClient::DataCallback* callback = (HttpClient::DataCallback*)userdata;
45 if (!(*callback)(ptr, nmemb)) {
46 return 0; // Signals error to curl
47 }
48 return nmemb;
49 }
50
CurlUrlGet(CURLU * url,CURLUPart what,unsigned int flags)51 Result<std::string> CurlUrlGet(CURLU* url, CURLUPart what, unsigned int flags) {
52 char* str_ptr = nullptr;
53 CF_EXPECT(curl_url_get(url, what, &str_ptr, flags) == CURLUE_OK);
54 std::string str(str_ptr);
55 curl_free(str_ptr);
56 return str;
57 }
58
59 using ManagedCurlSlist =
60 std::unique_ptr<curl_slist, decltype(&curl_slist_free_all)>;
61
SlistFromStrings(const std::vector<std::string> & strings)62 Result<ManagedCurlSlist> SlistFromStrings(
63 const std::vector<std::string>& strings) {
64 ManagedCurlSlist curl_headers(nullptr, curl_slist_free_all);
65 for (const auto& str : strings) {
66 curl_slist* temp = curl_slist_append(curl_headers.get(), str.c_str());
67 CF_EXPECT(temp != nullptr,
68 "curl_slist_append failed to add \"" << str << "\"");
69 (void)curl_headers.release(); // Memory is now owned by `temp`
70 curl_headers.reset(temp);
71 }
72 return curl_headers;
73 }
74
75 class CurlClient : public HttpClient {
76 public:
CurlClient(NameResolver resolver)77 CurlClient(NameResolver resolver) : resolver_(std::move(resolver)) {
78 curl_ = curl_easy_init();
79 if (!curl_) {
80 LOG(ERROR) << "failed to initialize curl";
81 return;
82 }
83 }
~CurlClient()84 ~CurlClient() { curl_easy_cleanup(curl_); }
85
GetToString(const std::string & url,const std::vector<std::string> & headers)86 Result<HttpResponse<std::string>> GetToString(
87 const std::string& url,
88 const std::vector<std::string>& headers) override {
89 return DownloadToString(HttpMethod::kGet, url, headers);
90 }
91
PostToString(const std::string & url,const std::string & data_to_write,const std::vector<std::string> & headers)92 Result<HttpResponse<std::string>> PostToString(
93 const std::string& url, const std::string& data_to_write,
94 const std::vector<std::string>& headers) override {
95 return DownloadToString(HttpMethod::kPost, url, headers, data_to_write);
96 }
97
DeleteToString(const std::string & url,const std::vector<std::string> & headers)98 Result<HttpResponse<std::string>> DeleteToString(
99 const std::string& url,
100 const std::vector<std::string>& headers) override {
101 return DownloadToString(HttpMethod::kDelete, url, headers);
102 }
103
PostToJson(const std::string & url,const std::string & data_to_write,const std::vector<std::string> & headers)104 Result<HttpResponse<Json::Value>> PostToJson(
105 const std::string& url, const std::string& data_to_write,
106 const std::vector<std::string>& headers) override {
107 return DownloadToJson(HttpMethod::kPost, url, headers, data_to_write);
108 }
109
PostToJson(const std::string & url,const Json::Value & data_to_write,const std::vector<std::string> & headers)110 Result<HttpResponse<Json::Value>> PostToJson(
111 const std::string& url, const Json::Value& data_to_write,
112 const std::vector<std::string>& headers) override {
113 std::stringstream json_str;
114 json_str << data_to_write;
115 return DownloadToJson(HttpMethod::kPost, url, headers, json_str.str());
116 }
117
DownloadToCallback(DataCallback callback,const std::string & url,const std::vector<std::string> & headers)118 Result<HttpResponse<void>> DownloadToCallback(
119 DataCallback callback, const std::string& url,
120 const std::vector<std::string>& headers) {
121 return DownloadToCallback(HttpMethod::kGet, callback, url, headers);
122 }
123
DownloadToFile(const std::string & url,const std::string & path,const std::vector<std::string> & headers)124 Result<HttpResponse<std::string>> DownloadToFile(
125 const std::string& url, const std::string& path,
126 const std::vector<std::string>& headers) {
127 LOG(INFO) << "Attempting to save \"" << url << "\" to \"" << path << "\"";
128 std::fstream stream;
129 auto callback = [&stream, path](char* data, size_t size) -> bool {
130 if (data == nullptr) {
131 stream.open(path, std::ios::out | std::ios::binary | std::ios::trunc);
132 return !stream.fail();
133 }
134 stream.write(data, size);
135 return !stream.fail();
136 };
137 auto http_response = CF_EXPECT(DownloadToCallback(callback, url, headers));
138 return HttpResponse<std::string>{path, http_response.http_code};
139 }
140
DownloadToJson(const std::string & url,const std::vector<std::string> & headers)141 Result<HttpResponse<Json::Value>> DownloadToJson(
142 const std::string& url, const std::vector<std::string>& headers) {
143 return DownloadToJson(HttpMethod::kGet, url, headers);
144 }
145
DeleteToJson(const std::string & url,const std::vector<std::string> & headers)146 Result<HttpResponse<Json::Value>> DeleteToJson(
147 const std::string& url,
148 const std::vector<std::string>& headers) override {
149 return DownloadToJson(HttpMethod::kDelete, url, headers);
150 }
151
UrlEscape(const std::string & text)152 std::string UrlEscape(const std::string& text) override {
153 char* escaped_str = curl_easy_escape(curl_, text.c_str(), text.size());
154 std::string ret{escaped_str};
155 curl_free(escaped_str);
156 return ret;
157 }
158
159 private:
ManuallyResolveUrl(const std::string & url_str)160 Result<ManagedCurlSlist> ManuallyResolveUrl(const std::string& url_str) {
161 if (!resolver_) {
162 return ManagedCurlSlist(nullptr, curl_slist_free_all);
163 }
164 LOG(INFO) << "Manually resolving \"" << url_str << "\"";
165 std::stringstream resolve_line;
166 std::unique_ptr<CURLU, decltype(&curl_url_cleanup)> url(curl_url(),
167 curl_url_cleanup);
168 CF_EXPECT(curl_url_set(url.get(), CURLUPART_URL, url_str.c_str(), 0) ==
169 CURLUE_OK);
170 auto hostname = CF_EXPECT(CurlUrlGet(url.get(), CURLUPART_HOST, 0));
171 resolve_line << "+" << hostname;
172 auto port =
173 CF_EXPECT(CurlUrlGet(url.get(), CURLUPART_PORT, CURLU_DEFAULT_PORT));
174 resolve_line << ":" << port << ":";
175 resolve_line << android::base::Join(CF_EXPECT(resolver_(hostname)), ",");
176 auto slist = CF_EXPECT(SlistFromStrings({resolve_line.str()}));
177 return slist;
178 }
179
DownloadToJson(HttpMethod method,const std::string & url,const std::vector<std::string> & headers,const std::string & data_to_write="")180 Result<HttpResponse<Json::Value>> DownloadToJson(
181 HttpMethod method, const std::string& url,
182 const std::vector<std::string>& headers,
183 const std::string& data_to_write = "") {
184 auto response =
185 CF_EXPECT(DownloadToString(method, url, headers, data_to_write));
186 auto result = ParseJson(response.data);
187 if (!result.ok()) {
188 Json::Value error_json;
189 LOG(ERROR) << "Could not parse json: " << result.error().Message();
190 error_json["error"] = "Failed to parse json: " + result.error().Message();
191 error_json["response"] = response.data;
192 return HttpResponse<Json::Value>{error_json, response.http_code};
193 }
194 return HttpResponse<Json::Value>{*result, response.http_code};
195 }
196
DownloadToString(HttpMethod method,const std::string & url,const std::vector<std::string> & headers,const std::string & data_to_write="")197 Result<HttpResponse<std::string>> DownloadToString(
198 HttpMethod method, const std::string& url,
199 const std::vector<std::string>& headers,
200 const std::string& data_to_write = "") {
201 std::stringstream stream;
202 auto callback = [&stream](char* data, size_t size) -> bool {
203 if (data == nullptr) {
204 stream = std::stringstream();
205 return true;
206 }
207 stream.write(data, size);
208 return true;
209 };
210 auto http_response = CF_EXPECT(
211 DownloadToCallback(method, callback, url, headers, data_to_write));
212 return HttpResponse<std::string>{stream.str(), http_response.http_code};
213 }
214
DownloadToCallback(HttpMethod method,DataCallback callback,const std::string & url,const std::vector<std::string> & headers,const std::string & data_to_write="")215 Result<HttpResponse<void>> DownloadToCallback(
216 HttpMethod method, DataCallback callback, const std::string& url,
217 const std::vector<std::string>& headers,
218 const std::string& data_to_write = "") {
219 std::lock_guard<std::mutex> lock(mutex_);
220 auto extra_cache_entries = CF_EXPECT(ManuallyResolveUrl(url));
221 curl_easy_setopt(curl_, CURLOPT_RESOLVE, extra_cache_entries.get());
222 LOG(INFO) << "Attempting to download \"" << url << "\"";
223 CF_EXPECT(data_to_write.empty() || method == HttpMethod::kPost,
224 "data must be empty for non POST requests");
225 CF_EXPECT(curl_ != nullptr, "curl was not initialized");
226 CF_EXPECT(callback(nullptr, 0) /* Signal start of data */,
227 "callback failure");
228 auto curl_headers = CF_EXPECT(SlistFromStrings(headers));
229 curl_easy_reset(curl_);
230 if (method == HttpMethod::kDelete) {
231 curl_easy_setopt(curl_, CURLOPT_CUSTOMREQUEST, "DELETE");
232 }
233 curl_easy_setopt(curl_, CURLOPT_CAINFO,
234 "/etc/ssl/certs/ca-certificates.crt");
235 curl_easy_setopt(curl_, CURLOPT_HTTPHEADER, curl_headers.get());
236 curl_easy_setopt(curl_, CURLOPT_URL, url.c_str());
237 if (method == HttpMethod::kPost) {
238 curl_easy_setopt(curl_, CURLOPT_POSTFIELDSIZE, data_to_write.size());
239 curl_easy_setopt(curl_, CURLOPT_POSTFIELDS, data_to_write.c_str());
240 }
241 curl_easy_setopt(curl_, CURLOPT_WRITEFUNCTION, curl_to_function_cb);
242 curl_easy_setopt(curl_, CURLOPT_WRITEDATA, &callback);
243 char error_buf[CURL_ERROR_SIZE];
244 curl_easy_setopt(curl_, CURLOPT_ERRORBUFFER, error_buf);
245 curl_easy_setopt(curl_, CURLOPT_VERBOSE, 1L);
246 CURLcode res = curl_easy_perform(curl_);
247 CF_EXPECT(res == CURLE_OK,
248 "curl_easy_perform() failed. "
249 << "Code was \"" << res << "\". "
250 << "Strerror was \"" << curl_easy_strerror(res) << "\". "
251 << "Error buffer was \"" << error_buf << "\".");
252 long http_code = 0;
253 curl_easy_getinfo(curl_, CURLINFO_RESPONSE_CODE, &http_code);
254 return HttpResponse<void>{{}, http_code};
255 }
256
257 CURL* curl_;
258 NameResolver resolver_;
259 std::mutex mutex_;
260 };
261
262 class ServerErrorRetryClient : public HttpClient {
263 public:
ServerErrorRetryClient(HttpClient & inner,int retry_attempts,std::chrono::milliseconds retry_delay)264 ServerErrorRetryClient(HttpClient& inner, int retry_attempts,
265 std::chrono::milliseconds retry_delay)
266 : inner_client_(inner),
267 retry_attempts_(retry_attempts),
268 retry_delay_(retry_delay) {}
269
GetToString(const std::string & url,const std::vector<std::string> & headers)270 Result<HttpResponse<std::string>> GetToString(
271 const std::string& url, const std::vector<std::string>& headers) {
272 auto fn = [&, this]() { return inner_client_.GetToString(url, headers); };
273 return CF_EXPECT(RetryImpl<std::string>(fn));
274 }
275
PostToString(const std::string & url,const std::string & data,const std::vector<std::string> & headers)276 Result<HttpResponse<std::string>> PostToString(
277 const std::string& url, const std::string& data,
278 const std::vector<std::string>& headers) override {
279 auto fn = [&, this]() {
280 return inner_client_.PostToString(url, data, headers);
281 };
282 return CF_EXPECT(RetryImpl<std::string>(fn));
283 }
284
DeleteToString(const std::string & url,const std::vector<std::string> & headers)285 Result<HttpResponse<std::string>> DeleteToString(
286 const std::string& url, const std::vector<std::string>& headers) {
287 auto fn = [&, this]() {
288 return inner_client_.DeleteToString(url, headers);
289 };
290 return CF_EXPECT(RetryImpl<std::string>(fn));
291 }
292
PostToJson(const std::string & url,const Json::Value & data,const std::vector<std::string> & headers)293 Result<HttpResponse<Json::Value>> PostToJson(
294 const std::string& url, const Json::Value& data,
295 const std::vector<std::string>& headers) override {
296 auto fn = [&, this]() {
297 return inner_client_.PostToJson(url, data, headers);
298 };
299 return CF_EXPECT(RetryImpl<Json::Value>(fn));
300 }
301
PostToJson(const std::string & url,const std::string & data,const std::vector<std::string> & headers)302 Result<HttpResponse<Json::Value>> PostToJson(
303 const std::string& url, const std::string& data,
304 const std::vector<std::string>& headers) override {
305 auto fn = [&, this]() {
306 return inner_client_.PostToJson(url, data, headers);
307 };
308 return CF_EXPECT(RetryImpl<Json::Value>(fn));
309 }
310
DownloadToFile(const std::string & url,const std::string & path,const std::vector<std::string> & headers)311 Result<HttpResponse<std::string>> DownloadToFile(
312 const std::string& url, const std::string& path,
313 const std::vector<std::string>& headers) {
314 auto fn = [&, this]() {
315 return inner_client_.DownloadToFile(url, path, headers);
316 };
317 return CF_EXPECT(RetryImpl<std::string>(fn));
318 }
319
DownloadToJson(const std::string & url,const std::vector<std::string> & headers)320 Result<HttpResponse<Json::Value>> DownloadToJson(
321 const std::string& url, const std::vector<std::string>& headers) {
322 auto fn = [&, this]() {
323 return inner_client_.DownloadToJson(url, headers);
324 };
325 return CF_EXPECT(RetryImpl<Json::Value>(fn));
326 }
327
DownloadToCallback(DataCallback cb,const std::string & url,const std::vector<std::string> & hdrs)328 Result<HttpResponse<void>> DownloadToCallback(
329 DataCallback cb, const std::string& url,
330 const std::vector<std::string>& hdrs) override {
331 auto fn = [&, this]() {
332 return inner_client_.DownloadToCallback(cb, url, hdrs);
333 };
334 return CF_EXPECT(RetryImpl<void>(fn));
335 }
336
DeleteToJson(const std::string & url,const std::vector<std::string> & headers)337 Result<HttpResponse<Json::Value>> DeleteToJson(
338 const std::string& url,
339 const std::vector<std::string>& headers) override {
340 auto fn = [&, this]() { return inner_client_.DeleteToJson(url, headers); };
341 return CF_EXPECT(RetryImpl<Json::Value>(fn));
342 }
343
UrlEscape(const std::string & text)344 std::string UrlEscape(const std::string& text) override {
345 return inner_client_.UrlEscape(text);
346 }
347
348 private:
349 template <typename T>
RetryImpl(std::function<Result<HttpResponse<T>> ()> attempt_fn)350 Result<HttpResponse<T>> RetryImpl(
351 std::function<Result<HttpResponse<T>>()> attempt_fn) {
352 HttpResponse<T> response;
353 for (int attempt = 0; attempt != retry_attempts_; ++attempt) {
354 if (attempt != 0) {
355 std::this_thread::sleep_for(retry_delay_);
356 }
357 response = CF_EXPECT(attempt_fn());
358 if (!response.HttpServerError()) {
359 return response;
360 }
361 }
362 return response;
363 }
364
365 private:
366 HttpClient& inner_client_;
367 int retry_attempts_;
368 std::chrono::milliseconds retry_delay_;
369 };
370
371 } // namespace
372
GetEntDnsResolve(const std::string & host)373 Result<std::vector<std::string>> GetEntDnsResolve(const std::string& host) {
374 Command command("/bin/getent");
375 command.AddParameter("hosts");
376 command.AddParameter(host);
377
378 std::string out;
379 std::string err;
380 CF_EXPECT(RunWithManagedStdio(std::move(command), nullptr, &out, &err) == 0,
381 "`getent hosts " << host << "` failed: out = \"" << out
382 << "\", err = \"" << err << "\"");
383 auto lines = android::base::Tokenize(out, "\n");
384 for (auto& line : lines) {
385 auto line_split = android::base::Tokenize(line, " \t");
386 CF_EXPECT(line_split.size() == 2,
387 "unexpected line format: \"" << line << "\"");
388 line = line_split[0];
389 }
390 return lines;
391 }
392
CurlClient(NameResolver resolver)393 /* static */ std::unique_ptr<HttpClient> HttpClient::CurlClient(
394 NameResolver resolver) {
395 return std::unique_ptr<HttpClient>(new class CurlClient(std::move(resolver)));
396 }
397
ServerErrorRetryClient(HttpClient & inner,int retry_attempts,std::chrono::milliseconds retry_delay)398 /* static */ std::unique_ptr<HttpClient> HttpClient::ServerErrorRetryClient(
399 HttpClient& inner, int retry_attempts,
400 std::chrono::milliseconds retry_delay) {
401 return std::unique_ptr<HttpClient>(
402 new class ServerErrorRetryClient(inner, retry_attempts, retry_delay));
403 }
404
405 HttpClient::~HttpClient() = default;
406
407 } // namespace cuttlefish
408