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 #pragma once
17 
18 #include <chrono>
19 #include <functional>
20 #include <mutex>
21 #include <string>
22 #include <type_traits>
23 #include <vector>
24 
25 #include <json/json.h>
26 
27 #include "common/libs/utils/result.h"
28 
29 namespace cuttlefish {
30 
IsHttpSuccess(int http_code)31 static inline bool IsHttpSuccess(int http_code) {
32   return http_code >= 200 && http_code <= 299;
33 };
34 
35 struct HttpVoidResponse {};
36 
37 template <typename T>
38 struct HttpResponse {
HttpInfoHttpResponse39   bool HttpInfo() { return http_code >= 100 && http_code <= 199; }
HttpSuccessHttpResponse40   bool HttpSuccess() { return IsHttpSuccess(http_code); }
HttpRedirectHttpResponse41   bool HttpRedirect() { return http_code >= 300 && http_code <= 399; }
HttpClientErrorHttpResponse42   bool HttpClientError() { return http_code >= 400 && http_code <= 499; }
HttpServerErrorHttpResponse43   bool HttpServerError() { return http_code >= 500 && http_code <= 599; }
44 
45   typename std::conditional<std::is_void_v<T>, HttpVoidResponse, T>::type data;
46   long http_code;
47 };
48 
49 using NameResolver =
50     std::function<Result<std::vector<std::string>>(const std::string&)>;
51 
52 Result<std::vector<std::string>> GetEntDnsResolve(const std::string& host);
53 
54 class HttpClient {
55  public:
56   typedef std::function<bool(char*, size_t)> DataCallback;
57 
58   static std::unique_ptr<HttpClient> CurlClient(
59       NameResolver resolver = NameResolver(),
60       const bool use_logging_debug_function = false);
61   static std::unique_ptr<HttpClient> ServerErrorRetryClient(
62       HttpClient&, int retry_attempts, std::chrono::milliseconds retry_delay);
63 
64   virtual ~HttpClient();
65 
66   virtual Result<HttpResponse<std::string>> GetToString(
67       const std::string& url, const std::vector<std::string>& headers = {}) = 0;
68   virtual Result<HttpResponse<std::string>> PostToString(
69       const std::string& url, const std::string& data,
70       const std::vector<std::string>& headers = {}) = 0;
71   virtual Result<HttpResponse<std::string>> DeleteToString(
72       const std::string& url, const std::vector<std::string>& headers = {}) = 0;
73 
74   // Returns the json object contained in the response's body.
75   //
76   // NOTE: In case of a parsing error a successful `result` will be returned
77   // with the relevant http status code and a json object with the next format:
78   // {
79   //   "error": "Failed to parse json",
80   //   "response: "<THE RESPONSE BODY>"
81   // }
82   virtual Result<HttpResponse<Json::Value>> PostToJson(
83       const std::string& url, const std::string& data,
84       const std::vector<std::string>& headers = {}) = 0;
85   virtual Result<HttpResponse<Json::Value>> PostToJson(
86       const std::string& url, const Json::Value& data,
87       const std::vector<std::string>& headers = {}) = 0;
88   virtual Result<HttpResponse<Json::Value>> DownloadToJson(
89       const std::string& url, const std::vector<std::string>& headers = {}) = 0;
90   virtual Result<HttpResponse<Json::Value>> DeleteToJson(
91       const std::string& url, const std::vector<std::string>& headers = {}) = 0;
92 
93   virtual Result<HttpResponse<std::string>> DownloadToFile(
94       const std::string& url, const std::string& path,
95       const std::vector<std::string>& headers = {}) = 0;
96 
97   // Returns response's status code.
98   virtual Result<HttpResponse<void>> DownloadToCallback(
99       DataCallback callback, const std::string& url,
100       const std::vector<std::string>& headers = {}) = 0;
101 
102   virtual std::string UrlEscape(const std::string&) = 0;
103 };
104 
105 }  // namespace cuttlefish
106