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: return "OK";
49 case 201: return "Created";
50 case 204: return "No Content";
51 case 400: return "Bad Request";
52 case 401: return "Unauthorized";
53 case 403: return "Forbidden";
54 case 404: return "File Not Found";
55 case 500: return "Internal Server Error";
56 case 502: return "Bad Gateway";
57 case 503: return "Service Unavailable";
58 default: return fmt::format("Status Code: {}", http_code);
59 }
60 }
61
62 typename std::conditional<std::is_void_v<T>, HttpVoidResponse, T>::type data;
64 std::vector<HttpHeader> headers;
65};
66
67std::optional<std::string_view> HeaderValue(
68 const std::vector<HttpHeader>& headers, std::string_view header_name);
69
70enum class HttpMethod {
71 kGet,
72 kPost,
73 kDelete,
74 kHead,
75};
76
79 std::string url;
80 std::vector<std::string> headers;
81 std::string data_to_write;
82};
83
85 public:
86 typedef std::function<bool(char*, size_t)> DataCallback;
87
88 virtual ~HttpClient();
89
90 // Returns response's status code.
92 HttpRequest, DataCallback callback) = 0;
93};
94
95} // namespace cuttlefish
Definition: expected.h:86
Definition: http_client.h:84
std::function< bool(char *, size_t)> DataCallback
Definition: http_client.h:86
virtual Result< HttpResponse< void > > DownloadToCallback(HttpRequest, DataCallback callback)=0
EventFormat format
Definition: kernel_log_server.cc:57
Definition: alloc_utils.cpp:23
std::optional< std::string_view > HeaderValue(const std::vector< HttpHeader > &headers, std::string_view header_name)
Definition: http_client.cc:44
HttpMethod
Definition: http_client.h:70
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:77
HttpMethod method
Definition: http_client.h:78
std::string url
Definition: http_client.h:79
std::vector< std::string > headers
Definition: http_client.h:80
std::string data_to_write
Definition: http_client.h:81
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:62
bool HttpInfo() const
Definition: http_client.h:40
std::vector< HttpHeader > headers
Definition: http_client.h:64
bool HttpServerError() const
Definition: http_client.h:44
long http_code
Definition: http_client.h:63
bool HttpClientError() const
Definition: http_client.h:43
std::string StatusDescription() const
Definition: http_client.h:46
Definition: http_client.h:31