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