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