forked from zhllxt/asio2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttps_client.hpp
More file actions
423 lines (363 loc) · 14.4 KB
/
https_client.hpp
File metadata and controls
423 lines (363 loc) · 14.4 KB
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
/*
* COPYRIGHT (C) 2017-2021, zhllxt
*
* author : zhllxt
* email : 37792738@qq.com
*
* Distributed under the GNU GENERAL PUBLIC LICENSE Version 3, 29 June 2007
* (See accompanying file LICENSE or see <http://www.gnu.org/licenses/>)
*/
#if defined(ASIO2_USE_SSL)
#ifndef __ASIO2_HTTPS_CLIENT_HPP__
#define __ASIO2_HTTPS_CLIENT_HPP__
#if defined(_MSC_VER) && (_MSC_VER >= 1200)
#pragma once
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
#include <asio2/base/detail/push_options.hpp>
#include <asio2/http/http_client.hpp>
#include <asio2/tcp/component/ssl_stream_cp.hpp>
#include <asio2/tcp/component/ssl_context_cp.hpp>
namespace asio2::detail
{
ASIO2_CLASS_FORWARD_DECLARE_BASE;
ASIO2_CLASS_FORWARD_DECLARE_TCP_BASE;
ASIO2_CLASS_FORWARD_DECLARE_TCP_CLIENT;
template<class derived_t, class args_t>
class https_client_impl_t
: public ssl_context_cp <derived_t, args_t>
, public http_client_impl_t<derived_t, args_t>
, public ssl_stream_cp <derived_t, args_t>
{
ASIO2_CLASS_FRIEND_DECLARE_BASE;
ASIO2_CLASS_FRIEND_DECLARE_TCP_BASE;
ASIO2_CLASS_FRIEND_DECLARE_TCP_CLIENT;
public:
using super = http_client_impl_t <derived_t, args_t>;
using self = https_client_impl_t<derived_t, args_t>;
using body_type = typename args_t::body_t;
using buffer_type = typename args_t::buffer_t;
using ssl_context_comp = ssl_context_cp<derived_t, args_t>;
using ssl_stream_comp = ssl_stream_cp <derived_t, args_t>;
using super::send;
using super::async_send;
public:
/**
* @constructor
*/
template<class... Args>
explicit https_client_impl_t(
asio::ssl::context::method method = asio::ssl::context::sslv23,
Args&&... args
)
: ssl_context_comp(method)
, super(std::forward<Args>(args)...)
, ssl_stream_comp(this->io_, *this, asio::ssl::stream_base::client)
{
}
/**
* @destructor
*/
~https_client_impl_t()
{
this->stop();
}
/**
* @function : get the stream object refrence
*/
inline typename ssl_stream_comp::stream_type & stream()
{
ASIO2_ASSERT(bool(this->ssl_stream_));
return (*(this->ssl_stream_));
}
public:
template<typename String, typename StrOrInt, class Rep, class Period,
class Body = http::string_body, class Fields = http::fields, class Buffer = beast::flat_buffer>
static inline http::response_t<Body, Fields> execute(const asio::ssl::context& ctx, String&& host, StrOrInt&& port,
http::request_t<Body, Fields>& req, std::chrono::duration<Rep, Period> timeout, error_code& ec)
{
http::parser<false, Body, typename Fields::allocator_type> parser;
try
{
// set default result to unknown
parser.get().result(http::status::unknown);
parser.eager(true);
// First assign default value timed_out to ec
ec = asio::error::timed_out;
// The io_context is required for all I/O
asio::io_context ioc;
// These objects perform our I/O
asio::ip::tcp::resolver resolver{ ioc };
asio::ip::tcp::socket socket{ ioc };
asio::ssl::stream<asio::ip::tcp::socket&> stream(socket, const_cast<asio::ssl::context&>(ctx));
// This buffer is used for reading and must be persisted
Buffer buffer;
// Look up the domain name
resolver.async_resolve(std::forward<String>(host), to_string(std::forward<StrOrInt>(port)),
[&](const error_code& ec1, const asio::ip::tcp::resolver::results_type& endpoints) mutable
{
if (ec1) { ec = ec1; return; }
// Make the connection on the IP address we get from a lookup
asio::async_connect(socket, endpoints,
[&](const error_code & ec2, const asio::ip::tcp::endpoint&) mutable
{
if (ec2) { ec = ec2; return; }
stream.async_handshake(asio::ssl::stream_base::client,
[&](const error_code& ec3) mutable
{
if (ec3) { ec = ec3; return; }
http::async_write(stream, req, [&](const error_code& ec4, std::size_t) mutable
{
// can't use stream.shutdown(),in some case the shutdowm will blocking forever.
if (ec4) { ec = ec4; stream.async_shutdown([](const error_code&) {}); return; }
// Then start asynchronous reading
http::async_read(stream, buffer, parser,
[&](const error_code& ec5, std::size_t) mutable
{
// Reading completed, assign the read the result to ec
// If the code does not execute into here, the ec value
// is the default value timed_out.
ec = ec5;
stream.async_shutdown([](const error_code&) mutable {});
});
});
});
});
});
// timedout run
ioc.run_for(timeout);
error_code ec_ignore{};
// Gracefully close the socket
socket.shutdown(asio::ip::tcp::socket::shutdown_both, ec_ignore);
socket.close(ec_ignore);
}
catch (system_error & e)
{
ec = e.code();
}
return parser.release();
}
template<typename String, typename StrOrInt, class Rep, class Period,
class Body = http::string_body, class Fields = http::fields, class Buffer = beast::flat_buffer>
static inline http::response_t<Body, Fields> execute(const asio::ssl::context& ctx, String&& host, StrOrInt&& port,
http::request_t<Body, Fields>& req, std::chrono::duration<Rep, Period> timeout)
{
error_code ec;
http::response_t<Body, Fields> rep = execute(ctx,
std::forward<String>(host), std::forward<StrOrInt>(port), req, timeout, ec);
asio::detail::throw_error(ec);
return rep;
}
template<typename String, typename StrOrInt, class Body = http::string_body, class Fields = http::fields>
static inline http::response_t<Body, Fields> execute(String&& host, StrOrInt&& port,
http::request_t<Body, Fields>& req, error_code& ec)
{
ec.clear();
return execute(asio::ssl::context{ asio::ssl::context::sslv23 },
std::forward<String>(host), std::forward<StrOrInt>(port),
req, std::chrono::milliseconds(http_execute_timeout), ec);
}
template<typename String, typename StrOrInt, class Body = http::string_body, class Fields = http::fields>
static inline http::response_t<Body, Fields> execute(String&& host, StrOrInt&& port,
http::request_t<Body, Fields>& req)
{
error_code ec;
http::response_t<Body, Fields> rep = execute(
std::forward<String>(host), std::forward<StrOrInt>(port), req, ec);
asio::detail::throw_error(ec);
return rep;
}
/**
* @function : blocking execute the http request until it is returned on success or failure
* You need to encode the "url"(by url_encode) before calling this function
*/
template<class Body = http::string_body, class Fields = http::fields>
static inline http::response_t<Body, Fields> execute(std::string_view url, error_code& ec)
{
return execute(url, std::chrono::milliseconds(http_execute_timeout), ec);
}
/**
* @function : blocking execute the http request until it is returned on success or failure
* You need to encode the "url"(by url_encode) before calling this function
*/
template<class Body = http::string_body, class Fields = http::fields>
static inline http::response_t<Body, Fields> execute(std::string_view url)
{
error_code ec;
http::response_t<Body, Fields> rep = execute(url, ec);
asio::detail::throw_error(ec);
return rep;
}
/**
* @function : blocking execute the http request until it is returned on success or failure
* You need to encode the "url"(by url_encode) before calling this function
*/
template<class Rep, class Period, class Body = http::string_body, class Fields = http::fields>
static inline http::response_t<Body, Fields> execute(std::string_view url,
std::chrono::duration<Rep, Period> timeout, error_code& ec)
{
return execute(asio::ssl::context{ asio::ssl::context::sslv23 },
url, timeout, ec);
}
/**
* @function : blocking execute the http request until it is returned on success or failure
* You need to encode the "url"(by url_encode) before calling this function
*/
template<class Rep, class Period, class Body = http::string_body, class Fields = http::fields>
static inline http::response_t<Body, Fields> execute(std::string_view url,
std::chrono::duration<Rep, Period> timeout)
{
error_code ec;
http::response_t<Body, Fields> rep = execute(url, timeout, ec);
asio::detail::throw_error(ec);
return rep;
}
/**
* @function : blocking execute the http request until it is returned on success or failure
* You need to encode the "url"(by url_encode) before calling this function
*/
template<class Rep, class Period, class Body = http::string_body, class Fields = http::fields>
static inline http::response_t<Body, Fields> execute(const asio::ssl::context& ctx, std::string_view url,
std::chrono::duration<Rep, Period> timeout, error_code& ec)
{
ec.clear();
http::request_t<Body, Fields> req = http::make_request<Body, Fields>(url, ec);
if (ec) return http::response_t<Body, Fields>{ http::status::unknown, 11};
std::string_view host = http::url_to_host(url);
std::string_view port = http::url_to_port(url);
return execute(ctx,
host, port, req, timeout, ec);
}
/**
* @function : blocking execute the http request until it is returned on success or failure
* You need to encode the "url"(by url_encode) before calling this function
*/
template<class Rep, class Period, class Body = http::string_body, class Fields = http::fields>
static inline http::response_t<Body, Fields> execute(const asio::ssl::context& ctx, std::string_view url,
std::chrono::duration<Rep, Period> timeout)
{
error_code ec;
http::response_t<Body, Fields> rep = execute(ctx, url, timeout, ec);
asio::detail::throw_error(ec);
return rep;
}
/**
* @function : blocking execute the http request until it is returned on success or failure
* You need to encode the "target"(by url_encode) before calling this function
*/
template<typename String, typename StrOrInt, class Body = http::string_body, class Fields = http::fields>
static inline http::response_t<Body, Fields> execute(String&& host, StrOrInt&& port,
std::string_view target, error_code& ec)
{
return execute(
std::forward<String>(host), std::forward<StrOrInt>(port),
target, std::chrono::milliseconds(http_execute_timeout), ec);
}
/**
* @function : blocking execute the http request until it is returned on success or failure
* You need to encode the "target"(by url_encode) before calling this function
*/
template<typename String, typename StrOrInt, class Body = http::string_body, class Fields = http::fields>
static inline http::response_t<Body, Fields> execute(String&& host, StrOrInt&& port,
std::string_view target)
{
error_code ec;
http::response_t<Body, Fields> rep = execute(
std::forward<String>(host), std::forward<StrOrInt>(port), target, ec);
asio::detail::throw_error(ec);
return rep;
}
/**
* @function : blocking execute the http request until it is returned on success or failure
* You need to encode the "target"(by url_encode) before calling this function
*/
template<typename String, typename StrOrInt, class Rep, class Period,
class Body = http::string_body, class Fields = http::fields>
static inline http::response_t<Body, Fields> execute(String&& host, StrOrInt&& port,
std::string_view target, std::chrono::duration<Rep, Period> timeout, error_code& ec)
{
using host_type = std::remove_cv_t<std::remove_reference_t<String>>;
using port_type = std::remove_cv_t<std::remove_reference_t<StrOrInt>>;
ec.clear();
http::request_t<Body, Fields> req = http::make_request<
const host_type&, const port_type&, Body, Fields>(
const_cast<const host_type&>(host), const_cast<const port_type&>(port),
target);
return execute(asio::ssl::context{ asio::ssl::context::sslv23 },
std::forward<String>(host), std::forward<StrOrInt>(port),
req, timeout, ec);
}
/**
* @function : blocking execute the http request until it is returned on success or failure
* You need to encode the "target"(by url_encode) before calling this function
*/
template<typename String, typename StrOrInt, class Rep, class Period,
class Body = http::string_body, class Fields = http::fields>
static inline http::response_t<Body, Fields> execute(String&& host, StrOrInt&& port,
std::string_view target, std::chrono::duration<Rep, Period> timeout)
{
error_code ec;
http::response_t<Body, Fields> rep = execute(
std::forward<String>(host), std::forward<StrOrInt>(port), target, timeout, ec);
asio::detail::throw_error(ec);
return rep;
}
// ----------------------------------------------------------------------------------------
public:
/**
* @function : bind ssl handshake listener
* @param : fun - a user defined callback function
* Function signature : void(asio2::error_code ec)
*/
template<class F, class ...C>
inline derived_t & bind_handshake(F&& fun, C&&... obj)
{
this->listener_.bind(event_type::handshake,
observer_t<error_code>(std::forward<F>(fun), std::forward<C>(obj)...));
return (this->derived());
}
protected:
template<typename MatchCondition>
inline void _do_init(condition_wrap<MatchCondition> condition)
{
super::_do_init(condition);
this->derived()._ssl_init(condition, this->socket_, *this);
}
inline void _handle_disconnect(const error_code& ec, std::shared_ptr<derived_t> this_ptr)
{
this->derived()._rdc_stop();
this->derived()._ssl_stop(this_ptr, [this, ec, this_ptr]() mutable
{
super::_handle_disconnect(ec, std::move(this_ptr));
});
}
template<typename MatchCondition>
inline void _handle_connect(const error_code & ec, std::shared_ptr<derived_t> this_ptr,
condition_wrap<MatchCondition> condition)
{
set_last_error(ec);
if (ec)
return this->derived()._done_connect(ec, std::move(this_ptr), std::move(condition));
this->derived()._ssl_start(this_ptr, condition, this->socket_, *this);
this->derived()._post_handshake(std::move(this_ptr), std::move(condition));
}
inline void _fire_handshake(std::shared_ptr<derived_t>& this_ptr, error_code ec)
{
// the _fire_handshake must be executed in the thread 0.
ASIO2_ASSERT(this->derived().io().strand().running_in_this_thread());
detail::ignore_unused(this_ptr);
this->listener_.notify(event_type::handshake, ec);
}
protected:
};
}
namespace asio2
{
class https_client : public detail::https_client_impl_t<https_client, detail::template_args_http_client>
{
public:
using https_client_impl_t<https_client, detail::template_args_http_client>::https_client_impl_t;
};
}
#include <asio2/base/detail/pop_options.hpp>
#endif // !__ASIO2_HTTPS_CLIENT_HPP__
#endif