Android-cuttlefish cvd tool
http_client.h
Go to the documentation of this file.
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 <functional>
19#include <optional>
20#include <string>
21#include <type_traits>
22#include <vector>
23
24#include "absl/strings/string_view.h"
25#include "fmt/format.h"
26
28
29namespace cuttlefish {
30
31static constexpr int kTooManyRequestsCode = 429;
32static constexpr absl::string_view kRetryAfterHeaderName = "Retry-After";
33
35
36struct HttpHeader {
37 std::string name;
38 std::string value;
39};
40
41template <typename T>
43 bool HttpInfo() const { return http_code >= 100 && http_code <= 199; }
44 bool HttpSuccess() const { return http_code >= 200 && http_code <= 299; }
45 bool HttpRedirect() const { return http_code >= 300 && http_code <= 399; }
46 bool HttpClientError() const { return http_code >= 400 && http_code <= 499; }
47 bool HttpServerError() const { return http_code >= 500 && http_code <= 599; }
48
49 std::string StatusDescription() const {
50 switch (http_code) {
51 case 200:
52 return "OK";
53 case 201:
54 return "Created";
55 case 204:
56 return "No Content";
57 case 400:
58 return "Bad Request";
59 case 401:
60 return "Unauthorized";
61 case 403:
62 return "Forbidden";
63 case 404:
64 return "File Not Found";
65 case 429:
66 return "Too Many Requests";
67 case 500:
68 return "Internal Server Error";
69 case 502:
70 return "Bad Gateway";
71 case 503:
72 return "Service Unavailable";
73 default:
74 return fmt::format("Status Code: {}", http_code);
75 }
76 }
77
78 typename std::conditional<std::is_void_v<T>, HttpVoidResponse, T>::type data;
80 std::vector<HttpHeader> headers;
81};
82
83std::optional<absl::string_view> HeaderValue(
84 const std::vector<HttpHeader>& headers, absl::string_view header_name);
85
86enum class HttpMethod {
87 kGet,
88 kPost,
89 kDelete,
90 kHead,
91};
92
95 std::string url;
96 std::vector<std::string> headers;
97 std::string data_to_write;
98};
99
101 public:
102 typedef std::function<bool(char*, size_t)> BoolDataCallback;
103 typedef std::function<Result<void>(char*, size_t)> ResultDataCallback;
104
105 virtual ~HttpClient();
106
107 // Returns response's status code.
109 HttpRequest, ResultDataCallback callback) = 0;
110};
111
112} // namespace cuttlefish
Definition: http_client.h:100
std::function< Result< void >(char *, size_t)> ResultDataCallback
Definition: http_client.h:103
virtual Result< HttpResponse< void > > DownloadToCallback(HttpRequest, ResultDataCallback callback)=0
std::function< bool(char *, size_t)> BoolDataCallback
Definition: http_client.h:102
EventFormat format
Definition: kernel_log_server.cc:61
Definition: alloc_driver.h:21
std::optional< absl::string_view > HeaderValue(const std::vector< HttpHeader > &headers, absl::string_view header_name)
Definition: http_client.cc:46
static constexpr absl::string_view kRetryAfterHeaderName
Definition: http_client.h:32
HttpMethod
Definition: http_client.h:86
tl::expected< T, StackTraceError< E > > Result
Definition: result_type.h:30
static constexpr int kTooManyRequestsCode
Definition: http_client.h:31
uint8_t type
Definition: pairing_connection.h:0
Definition: http_client.h:36
std::string name
Definition: http_client.h:37
std::string value
Definition: http_client.h:38
Definition: http_client.h:93
HttpMethod method
Definition: http_client.h:94
std::string url
Definition: http_client.h:95
std::vector< std::string > headers
Definition: http_client.h:96
std::string data_to_write
Definition: http_client.h:97
Definition: http_client.h:42
bool HttpRedirect() const
Definition: http_client.h:45
bool HttpSuccess() const
Definition: http_client.h:44
std::conditional< std::is_void_v< T >, HttpVoidResponse, T >::type data
Definition: http_client.h:78
bool HttpInfo() const
Definition: http_client.h:43
std::vector< HttpHeader > headers
Definition: http_client.h:80
bool HttpServerError() const
Definition: http_client.h:47
long http_code
Definition: http_client.h:79
bool HttpClientError() const
Definition: http_client.h:46
std::string StatusDescription() const
Definition: http_client.h:49
Definition: http_client.h:34