-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhttpasync.hpp
293 lines (247 loc) · 7.67 KB
/
httpasync.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
#ifndef HTTP_ASYNC_HPP
#define HTTP_ASYNC_HPP
//include certify for ssl
#include <boost/certify/extensions.hpp>
#include <boost/certify/https_verification.hpp>
//include session clients
#include "asyncSslSession.hpp"
#include "asyncSession.hpp"
//include other
#include <boost/lexical_cast.hpp>
#include <iostream>
#include <string>
#include <map>
#include <thread>
namespace beast = boost::beast;
namespace http = beast::http;
namespace asio = boost::asio;
namespace ssl = boost::asio::ssl;
using tcp = boost::asio::ip::tcp;
template<class requestType>
void execute_request(
const std::string type,
const std::string host,
const http::request<requestType> request,
std::function<void(std::string)> callback
){
//create io context
asio::io_context io;
if(type == "https"){
//create ssl context
ssl::context ssl{ssl::context::tls_client};
ssl.set_verify_mode(ssl::context::verify_peer | ssl::context::verify_fail_if_no_peer_cert);
ssl.set_default_verify_paths();
boost::certify::enable_native_https_server_verification(ssl);
//create a async ssl session and run the io
std::make_shared<AsyncSslSession>(asio::make_strand(io), ssl)->run(host.c_str(), "https", request, callback);
io.run();
}else if(type == "http"){
//create a async session and run the io
std::make_shared<AsyncSession>(io)->run(host.c_str(), "http", request, callback);
io.run();
}else{
std::cout << "ONLY HTTP/HTTPS SUPPORTED! \n";
std::terminate();
}
}
class AsyncHttpClient {
private:
template<class requestType>
static http::request<requestType> request_;
std::string request_type_;
std::string host_;
std::string type_;
auto parse_url(std::string& url, std::string& type, std::string& host, std::string& path);
public:
AsyncHttpClient(){}
auto get(std::string url, const std::map<std::string, std::string> &headers);
auto post(std::string url, const char* body, const std::map<std::string, std::string> &headers);
auto put(std::string url, const char* body, const std::map<std::string, std::string> &headers);
auto delete_(std::string url, const std::map<std::string, std::string> &headers);
void then(const std::function<void(std::string)>& lamda);
};
template<class requestType>
http::request<requestType> AsyncHttpClient::request_;
auto
AsyncHttpClient::parse_url(
std::string& url,
std::string& type,
std::string& host,
std::string& path
){
unsigned short column_index = url.find(':');
type = url.substr(0, column_index);
url = url.substr(column_index + 3);
try{
unsigned short slash_index = url.find('/');
host = url.substr(0, slash_index);
path = url.substr(slash_index);
}catch(std::out_of_range ex){ //user doesn't have to put "/" at the end of domain
path = "/";
host = url;
}
}
/*
Make a Http GET request.
@param url: The URL for the request
@param headers: Http request headers if any
@returns the client instance. You should call the .then()
to provide the callback to be invoked when the response recieved.
*/
auto
AsyncHttpClient::get(
std::string url,
const std::map<std::string, std::string> &headers = {}
){
//parse the url
std::string host;
std::string type;
std::string path;
parse_url(url, type, host, path);
//create a get request
http::request<http::empty_body> request(http::verb::get, path, 11);
//insert headers
request.set(http::field::host, host);
for(auto &pair : headers){
request.insert(pair.first, pair.second);
}
//save varibles to be used at .then()
request_type_ = "empty";
request_<http::empty_body> = request;
type_ = type;
host_ = host;
return *this;
}
/*
Make a Http POST request.
@param url: The URL for the request
@param body: Request body in plain text.
@param headers: Http request headers if any
@returns the client instance. You should call the .then()
to provide the callback to be invoked when the response recieved.
*/
auto
AsyncHttpClient::post(
std::string url,
const char* body,
const std::map<std::string, std::string> &headers = {}
){
//parse the url
std::string host;
std::string type;
std::string path;
parse_url(url, type, host, path);
//create a get request
http::request<http::string_body> request(http::verb::post, path, 11);
//insert headers
request.set(http::field::host, host);
for(auto &pair : headers){
request.insert(pair.first, pair.second);
}
//insert request body
request.body() = body;
request.prepare_payload();
request.set(http::field::content_length, boost::lexical_cast<std::string>(strlen(body)));
//save varibles to be used at .then()
request_type_ = "loaded";
request_<http::string_body> = request;
type_ = type;
host_ = host;
return *this;
}
/*
Make a Http PUT request.
@param url: The URL for the request
@param body: Request body in plain text.
@param headers: Http request headers if any
@returns the client instance. You should call the .then()
to provide the callback to be invoked when the response recieved.
*/
auto
AsyncHttpClient::put(
std::string url,
const char* body,
const std::map<std::string, std::string> &headers = {}
){
//parse the url
std::string host;
std::string type;
std::string path;
parse_url(url, type, host, path);
//create a get request
http::request<http::string_body> request(http::verb::put, path, 11);
//insert headers
request.set(http::field::host, host);
for(auto &pair : headers){
request.insert(pair.first, pair.second);
}
//insert request body
request.body() = body;
request.prepare_payload();
request.set(http::field::content_length, boost::lexical_cast<std::string>(strlen(body)));
//save varibles to be used at .then()
request_type_ = "loaded";
request_<http::string_body> = request;
type_ = type;
host_ = host;
return *this;
}
/*
Make a Http DELETE request.
@param url: The URL for the request
@param headers: Http request headers if any
@returns the client instance. You should call the .then()
to provide the callback to be invoked when the response recieved.
*/
auto
AsyncHttpClient::delete_(
std::string url,
const std::map<std::string, std::string> &headers = {}
){
//parse the url
std::string host;
std::string type;
std::string path;
parse_url(url, type, host, path);
//create a get request
http::request<http::empty_body> request(http::verb::delete_, path, 11);
//insert headers
request.set(http::field::host, host);
for(auto &pair : headers){
request.insert(pair.first, pair.second);
}
//save varibles to be used at .then()
request_type_ = "empty";
request_<http::empty_body> = request;
type_ = type;
host_ = host;
return *this;
}
/*
Function to be called to provide the callback function
that will be invoked when the response received.
@param callback: callback to be invoked when the response
received.
*/
void
AsyncHttpClient::then(const std::function<void(std::string)>& callback){
if(request_type_ == "empty"){
std::thread(
execute_request<http::empty_body>,
type_,
host_,
request_<http::empty_body>,
callback
).detach();
}
if(request_type_ == "loaded"){
std::thread(
execute_request<http::string_body>,
type_,
host_,
request_<http::string_body>,
callback
).detach();
}
}
#endif // HTTP_ASYNC_HPP