Safeheron-SGX-Native-Development-Framework v1.2.0
Loading...
Searching...
No Matches
ssgx_http_t_client.h
Go to the documentation of this file.
1#ifndef SAFEHERON_SGX_TRUSTED_HTTP_CLIENT_H
2#define SAFEHERON_SGX_TRUSTED_HTTP_CLIENT_H
3
4#include <map>
5#include <string>
6
8
9namespace ssgx {
10namespace http_t {
11
12/* Time out for connection, in seconds */
13constexpr int SSGX_HTTP_CLIENT_TIMEOUT = 5;
14
18enum class ErrorCode {
19 Success = 0,
20 InvalidParam = 1,
21 InvalidCall = 2,
22 InvalidUrl = 3,
23 SetSeedFailed = 4,
24 ServerCAError = 5,
25 SSLConfigFailed = 6,
26 SSLSetupFailed = 7,
27 SSLHostWrong = 8,
29 VerifyCertFailed = 10,
30 ConnectFailed = 11,
31 WriteFailed = 12,
32 ReadFailed = 13,
33 MallocFailed = 14,
35 Unknown = 9999,
36};
37
41class Error {
42 public:
43 Error() = default;
44 Error(const Error& other) = default;
45 Error(ErrorCode error_code, std::string error_msg, std::string internal_err)
46 : error_code_(error_code), error_msg_(std::move(error_msg)), internal_err_(std::move(internal_err)) {
47 }
48
49 [[nodiscard]] ErrorCode code() const {
50 return error_code_;
51 }
52 [[nodiscard]] const std::string& message() const {
53 return error_msg_;
54 }
55 [[nodiscard]] const std::string& internal_error() const {
56 return internal_err_;
57 }
58
59 private:
60 ErrorCode error_code_ = ErrorCode::Unknown;
61 std::string error_msg_;
62 std::string internal_err_;
63};
64
68class Result {
69 public:
70 Result() = default;
71 Result(std::unique_ptr<Response>&& res, const Error& err) : res_(std::move(res)), err_(err) {
72 }
73 // Response
74 explicit operator bool() const {
75 return res_ != nullptr;
76 }
77 bool operator==(std::nullptr_t) const {
78 return res_ == nullptr;
79 }
80 bool operator!=(std::nullptr_t) const {
81 return res_ != nullptr;
82 }
83 [[nodiscard]] const Response& value() const {
84 return *res_;
85 }
87 return *res_;
88 }
89 const Response& operator*() const {
90 return *res_;
91 }
93 return *res_;
94 }
95 const Response* operator->() const {
96 return res_.get();
97 }
99 return res_.get();
100 }
101
102 // Error
103 [[nodiscard]] Error error() const {
104 return err_;
105 }
106
107 private:
108 std::unique_ptr<Response> res_;
109 Error err_ = Error(ErrorCode::Unknown, "Invalid", "Unknown");
110};
111
122class Client {
123 public:
133 explicit Client(const std::string& host);
134
144 explicit Client(const std::string& host, int port);
145
156 explicit Client(const std::string& host, int port, const std::string& server_ca);
157
161 virtual ~Client() = default;
162
170 Result Get(const std::string& path, int time_out_sec = SSGX_HTTP_CLIENT_TIMEOUT);
171
180 Result Get(const std::string& path, const TypeHeaders & headers, int time_out_sec = SSGX_HTTP_CLIENT_TIMEOUT);
181
190 Result Get(const std::string& path, const TypeParams& params, int time_out_sec = SSGX_HTTP_CLIENT_TIMEOUT);
191
201 Result Get(const std::string& path, const TypeHeaders& headers, const TypeParams& params,
202 int time_out_sec = SSGX_HTTP_CLIENT_TIMEOUT);
203
211 Result Post(const std::string& path, int time_out_sec = SSGX_HTTP_CLIENT_TIMEOUT);
212
221 Result Post(const std::string& path, const TypeHeaders& headers, int time_out_sec = SSGX_HTTP_CLIENT_TIMEOUT);
222
233 Result Post(const std::string& path, const char* body, size_t content_length, const std::string& content_type,
234 int time_out_sec = SSGX_HTTP_CLIENT_TIMEOUT);
235
247 Result Post(const std::string& path, const TypeHeaders& headers, const char* body, size_t content_length,
248 const std::string& content_type, int time_out_sec = SSGX_HTTP_CLIENT_TIMEOUT);
249
259 Result Post(const std::string& path, const std::string& body, const std::string& content_type,
260 int time_out_sec = SSGX_HTTP_CLIENT_TIMEOUT);
261
272 Result Post(const std::string& path, const TypeHeaders& headers, const std::string& body,
273 const std::string& content_type, int time_out_sec = SSGX_HTTP_CLIENT_TIMEOUT);
274
275 private:
276 [[nodiscard]] Result SendRequest(const std::string& method, const std::string& path, const TypeHeaders& headers,
277 const TypeParams& params, const std::string& body, const std::string& content_type,
278 int time_out_sec) const;
279
280 int port_ = -1;
281 std::string host_;
282 std::string server_ca_;
283};
284
285} // namespace http_t
286} // namespace ssgx
287
288#endif // SAFEHERON_SGX_TRUSTED_HTTP_CLIENT_H
Client class for making HTTP and HTTPS requests.
Definition ssgx_http_t_client.h:122
Client(const std::string &host, int port, const std::string &server_ca)
Constructor to initialize the client with a hostname, port, and server CA certificate.
Result Post(const std::string &path, const std::string &body, const std::string &content_type, int time_out_sec=SSGX_HTTP_CLIENT_TIMEOUT)
Sends a POST request with a body.
Result Post(const std::string &path, const char *body, size_t content_length, const std::string &content_type, int time_out_sec=SSGX_HTTP_CLIENT_TIMEOUT)
Sends a POST request with a body.
Result Get(const std::string &path, const TypeHeaders &headers, const TypeParams &params, int time_out_sec=SSGX_HTTP_CLIENT_TIMEOUT)
Sends a GET request with HTTP headers and URL parameters.
Result Get(const std::string &path, const TypeParams &params, int time_out_sec=SSGX_HTTP_CLIENT_TIMEOUT)
Sends a GET request with URL parameters.
Result Get(const std::string &path, const TypeHeaders &headers, int time_out_sec=SSGX_HTTP_CLIENT_TIMEOUT)
Sends a GET request with HTTP headers.
virtual ~Client()=default
Destructor to clean up resources.
Result Post(const std::string &path, int time_out_sec=SSGX_HTTP_CLIENT_TIMEOUT)
Sends a POST request.
Client(const std::string &host, int port)
Constructor to initialize the client with a hostname and port.
Client(const std::string &host)
Constructor to initialize the client with a hostname.
Result Post(const std::string &path, const TypeHeaders &headers, const char *body, size_t content_length, const std::string &content_type, int time_out_sec=SSGX_HTTP_CLIENT_TIMEOUT)
Sends a POST request with HTTP headers and a body.
Result Post(const std::string &path, const TypeHeaders &headers, int time_out_sec=SSGX_HTTP_CLIENT_TIMEOUT)
Sends a POST request with HTTP headers.
Result Get(const std::string &path, int time_out_sec=SSGX_HTTP_CLIENT_TIMEOUT)
Sends a GET request.
Result Post(const std::string &path, const TypeHeaders &headers, const std::string &body, const std::string &content_type, int time_out_sec=SSGX_HTTP_CLIENT_TIMEOUT)
Sends a POST request with HTTP headers and a body.
Represents detailed error information, typically from client operations.
Definition ssgx_http_t_client.h:41
const std::string & internal_error() const
Definition ssgx_http_t_client.h:55
Error(const Error &other)=default
ErrorCode code() const
Definition ssgx_http_t_client.h:49
const std::string & message() const
Definition ssgx_http_t_client.h:52
Error(ErrorCode error_code, std::string error_msg, std::string internal_err)
Definition ssgx_http_t_client.h:45
HTTP response class to represent an outgoing HTTP response.
Definition ssgx_http_t_structs.h:424
The result class of the client operation stores the response data of the request or the error informa...
Definition ssgx_http_t_client.h:68
Error error() const
Definition ssgx_http_t_client.h:103
Response & value()
Definition ssgx_http_t_client.h:86
bool operator==(std::nullptr_t) const
Definition ssgx_http_t_client.h:77
bool operator!=(std::nullptr_t) const
Definition ssgx_http_t_client.h:80
Result(std::unique_ptr< Response > &&res, const Error &err)
Definition ssgx_http_t_client.h:71
Response & operator*()
Definition ssgx_http_t_client.h:92
Response * operator->()
Definition ssgx_http_t_client.h:98
const Response * operator->() const
Definition ssgx_http_t_client.h:95
const Response & value() const
Definition ssgx_http_t_client.h:83
const Response & operator*() const
Definition ssgx_http_t_client.h:89
std::map< std::string, std::string, ci > TypeHeaders
Definition ssgx_http_t_structs.h:103
ErrorCode
Error code of the client operation.
Definition ssgx_http_t_client.h:18
std::map< std::string, std::string > TypeParams
Definition ssgx_http_t_structs.h:104
constexpr int SSGX_HTTP_CLIENT_TIMEOUT
Definition ssgx_http_t_client.h:13
Definition ssgx_attestation_t.h:6