• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
22 #include <json/json.h>
23 
24 namespace cuttlefish {
25 
26 template <typename T>
27 struct CurlResponse {
HttpInfoCurlResponse28   bool HttpInfo() { return http_code >= 100 && http_code <= 199; }
HttpSuccessCurlResponse29   bool HttpSuccess() { return http_code >= 200 && http_code <= 299; }
HttpRedirectCurlResponse30   bool HttpRedirect() { return http_code >= 300 && http_code <= 399; }
HttpClientErrorCurlResponse31   bool HttpClientError() { return http_code >= 400 && http_code <= 499; }
HttpServerErrorCurlResponse32   bool HttpServerError() { return http_code >= 500 && http_code <= 599; }
33 
34   T data;
35   long http_code;
36 };
37 
38 class CurlWrapper {
39  public:
40   typedef std::function<bool(char*, size_t)> DataCallback;
41 
42   static std::unique_ptr<CurlWrapper> Create();
43   static std::unique_ptr<CurlWrapper> WithServerErrorRetry(
44       CurlWrapper&, int retry_attempts, std::chrono::milliseconds retry_delay);
45   virtual ~CurlWrapper();
46 
47   virtual CurlResponse<std::string> PostToString(
48       const std::string& url, const std::string& data,
49       const std::vector<std::string>& headers = {}) = 0;
50   virtual CurlResponse<Json::Value> PostToJson(
51       const std::string& url, const std::string& data,
52       const std::vector<std::string>& headers = {}) = 0;
53   virtual CurlResponse<Json::Value> PostToJson(
54       const std::string& url, const Json::Value& data,
55       const std::vector<std::string>& headers = {}) = 0;
56 
57   virtual CurlResponse<std::string> DownloadToFile(
58       const std::string& url, const std::string& path,
59       const std::vector<std::string>& headers = {}) = 0;
60   virtual CurlResponse<std::string> DownloadToString(
61       const std::string& url, const std::vector<std::string>& headers = {}) = 0;
62   virtual CurlResponse<Json::Value> DownloadToJson(
63       const std::string& url, const std::vector<std::string>& headers = {}) = 0;
64   virtual CurlResponse<bool> DownloadToCallback(
65       DataCallback callback, const std::string& url,
66       const std::vector<std::string>& headers = {}) = 0;
67 
68   virtual CurlResponse<Json::Value> DeleteToJson(
69       const std::string& url, const std::vector<std::string>& headers = {}) = 0;
70 
71   virtual std::string UrlEscape(const std::string&) = 0;
72 };
73 
74 }
75