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