forked from Skewjo/SysLat_Software
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHTTP_Client_Async_SSL.cpp
More file actions
138 lines (109 loc) · 3.97 KB
/
HTTP_Client_Async_SSL.cpp
File metadata and controls
138 lines (109 loc) · 3.97 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
#include "stdafx.h"
#include "HTTP_Client_Async_SSL.h"
//#include <wincrypt.h> //not necessary when using a "pragma lib" directive??
//#pragma comment (lib, "crypt32")
http::response<http::string_body>* SSL_session::run(Json::Value dataToSend, char const* host, char const* port, char const* target, int version)
{
if (!SSL_set_tlsext_host_name(stream_.native_handle(), host))
{
beast::error_code ec{ static_cast<int>(::ERR_get_error()), net::error::get_ssl_category() };
std::cerr << ec.message() << "\n";
return &res_;
}
// Set up an HTTP POST request message
req_.version(version);
req_.method(http::verb::post);
req_.target(target);
req_.set(http::field::host, host);
req_.set(http::field::user_agent, BOOST_BEAST_VERSION_STRING);
req_.set(beast::http::field::content_type, "application/json");
Json::StreamWriterBuilder builder;
string output = Json::writeString(builder, dataToSend);
req_.body() = output;
std::ostringstream debugOut;
debugOut << req_ << std::endl;
DEBUG_PRINT(debugOut.str().c_str())
req_.prepare_payload();
// Look up the domain name
resolver_.async_resolve(
host,
port,
beast::bind_front_handler(
&SSL_session::on_resolve,
shared_from_this()));
return &res_;
}
void SSL_session::on_resolve(beast::error_code ec, tcp::resolver::results_type results)
{
if (ec)
return boostFail_secure(ec, "resolve");
// Set a timeout on the operation
beast::get_lowest_layer(stream_).expires_after(std::chrono::seconds(30));
// Make the connection on the IP address we get from a lookup
beast::get_lowest_layer(stream_).async_connect(
results,
beast::bind_front_handler(
&SSL_session::on_connect,
shared_from_this()));
}
void SSL_session::on_connect(beast::error_code ec, tcp::resolver::results_type::endpoint_type)
{
if (ec)
return boostFail_secure(ec, "connect");
// Perform the SSL handshake
stream_.async_handshake(
ssl::stream_base::client,
beast::bind_front_handler(
&SSL_session::on_handshake,
shared_from_this()));
}
void SSL_session::on_handshake(beast::error_code ec)
{
if (ec)
return boostFail_secure(ec, "handshake");
// Set a timeout on the operation
beast::get_lowest_layer(stream_).expires_after(std::chrono::seconds(30));
// Send the HTTP request to the remote host
http::async_write(stream_, req_,
beast::bind_front_handler(
&SSL_session::on_write,
shared_from_this()));
}
void SSL_session::on_write(beast::error_code ec, std::size_t bytes_transferred) {
boost::ignore_unused(bytes_transferred);
if (ec)
return boostFail_secure(ec, "write");
// Receive the HTTP response
http::async_read(stream_, buffer_, res_,
beast::bind_front_handler(
&SSL_session::on_read,
shared_from_this()));
}
void SSL_session::on_read(beast::error_code ec, std::size_t bytes_transferred) {
boost::ignore_unused(bytes_transferred);
if (ec)
return boostFail_secure(ec, "read");
// Write the message to standard out - DEBUG_PRINT for Windows...
std::ostringstream debugOut;
debugOut << res_ << std::endl;
DEBUG_PRINT(debugOut.str().c_str())
// Set a timeout on the operation
beast::get_lowest_layer(stream_).expires_after(std::chrono::seconds(30));
// Gracefully close the stream
stream_.async_shutdown(
beast::bind_front_handler(
&SSL_session::on_shutdown,
shared_from_this()));
}
void SSL_session::on_shutdown(beast::error_code ec)
{
if (ec == net::error::eof)
{
// Rationale:
// http://stackoverflow.com/questions/25587403/boost-asio-ssl-async-shutdown-always-finishes-with-an-error
ec = {};
}
if (ec)
return boostFail_secure(ec, "shutdown");
// If we get here then the connection is closed gracefully
}