forked from VestigeJ/dataminer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
httpreader.cpp
213 lines (173 loc) · 6.25 KB
/
httpreader.cpp
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
#include <boost/asio/connect.hpp>
#include <boost/asio/ip/tcp.hpp>
#include <boost/asio.hpp>
#include <boost/algorithm/string.hpp>
#include <cstdlib>
#include <iostream>
#include <cctype>
#include <regex>
#include "httpreader.h"
namespace beast = boost::beast; // from <boost/beast.hpp>
namespace http = beast::http; // from <boost/beast/http.hpp>
HttpReader::HttpReader()
{
m_stream = nullptr;
m_isSSL = false;
m_lastConnectionTime = 0;
}
HttpReader::~HttpReader()
{
close();
}
bool HttpReader::connect()
{
try {
if (m_scheme.compare("https") == 0)
connectSSL();
else {
// These objects perform our I/O
tcp::resolver resolver(m_ioc);
m_stream = new tcp::socket(m_ioc);
// Look up the domain name
auto const results = resolver.resolve(m_host, m_port);
boost::asio::connect(*m_stream, results.begin(), results.end());
}
}
catch (exception &e)
{
std::cerr << "[" << m_host << ":" << m_port << "] " << e.what() << endl;
return false;
}
boost::system::error_code ec;
boost::asio::socket_base::keep_alive option(true);
m_stream->set_option(option, ec);
m_lastConnectionTime = time(nullptr);
return true;
}
bool HttpReader::connectSSL()
{
// The SSL context is required, and holds certificates
ssl::context ctx(ssl::context::tlsv12_client);
// Verify the remote server's certificate
ctx.set_verify_mode(ssl::verify_none);
// These objects perform our I/O
tcp::resolver resolver(m_ioc);
ssl::stream<tcp::socket> *stream = new ssl::stream<tcp::socket>(m_ioc, ctx);
// Set SNI Hostname (many hosts need this to handshake successfully)
if(!SSL_set_tlsext_host_name(stream->native_handle(), m_host.c_str()))
{
beast::error_code ec{static_cast<int>(::ERR_get_error()), net::error::get_ssl_category()};
throw beast::system_error{ec};
}
// Look up the domain name
auto const results = resolver.resolve(m_host, m_port);
// Make the connection on the IP address we get from a lookup
boost::asio::connect(stream->next_layer(), results.begin(), results.end());
// Perform the SSL handshake
stream->handshake(ssl::stream_base::client);
m_stream = (tcp::socket *)stream;
return true;
}
void HttpReader::close()
{
if (m_stream != nullptr)
{
try {
beast::error_code ec;
m_stream->shutdown(tcp::socket::shutdown_both, ec);
m_buffer.consume(m_buffer.size());
delete m_stream;
}
catch (exception &e)
{
cerr << e.what() << endl;
}
m_stream = nullptr;
}
m_lastConnectionTime = 0;
}
string HttpReader::read()
{
if (m_stream == nullptr)
{
// try to reconnect
if (!connect())
return "";
}
// Declare a container to hold the response
http::response<http::string_body> res;
try {
// Set up an HTTP GET request message
http::request<http::string_body> req{http::verb::get, "/"+m_path + m_query, 11};
req.set(http::field::host, m_host);
req.set(http::field::user_agent, BOOST_BEAST_VERSION_STRING);
if (m_isSSL)
http::write(*(ssl::stream<tcp::socket> *)m_stream, req);
else
http::write(*m_stream, req);
// Receive the HTTP response
if (m_isSSL)
http::read(*(ssl::stream<tcp::socket> *)m_stream, m_buffer, res);
else
http::read(*m_stream, m_buffer, res);
return res.body().data();
}
catch(std::exception const& e)
{
std::cerr << "[" << m_host << ":" << m_port << "] Error: " << e.what() << std::endl;
string respdata = res.body().data();
// try reconnect
close();
time_t last = m_lastConnectionTime;
if (!connect())
std::cerr <<
"[" << m_host << ":" << m_port << "] Fail to reconnect to MT agent!" << std::endl;
else {
std::cerr <<
"[" << m_host << ":" << m_port << "] Reconnect to MT agent!" << std::endl;
// avoid repeat read errors if reconnect happens within 5 seconds
if (m_lastConnectionTime - last > 5)
return read();
}
}
return "";
}
static const char* SCHEME_REGEX = "((http[s]?)://)?"; // match http or https before the ://
static const char* USER_REGEX = "(([^@/:\\s]+)@)?"; // match anything other than @ / : or whitespace before the ending @
static const char* HOST_REGEX = "([^@/:\\s]+)"; // mandatory. match anything other than @ / : or whitespace
static const char* PORT_REGEX = "(:([0-9]{1,5}))?"; // after the : match 1 to 5 digits
static const char* PATH_REGEX = "(/[^:#?\\s]*)?"; // after the / match anything other than : # ? or whitespace
static const char* QUERY_REGEX = "(\\?(([^?;&#=]+=[^?;&#=]+)([;|&]([^?;&#=]+=[^?;&#=]+))*))?"; // after the ? match any number of x=y pairs, seperated by & or ;
static const char* FRAGMENT_REGEX = "(#([^#\\s]*))?"; // after the # match anything other than # or whitespace
static const std::regex regExpr(std::string("^")
+ SCHEME_REGEX + USER_REGEX
+ HOST_REGEX + PORT_REGEX
+ PATH_REGEX + QUERY_REGEX
+ FRAGMENT_REGEX + "$");
bool HttpReader::parseUri(const string &uri)
{
std::smatch matchResults;
if (std::regex_match(uri.cbegin(), uri.cend(), matchResults, regExpr))
{
m_scheme.assign(matchResults[2].first, matchResults[2].second);
boost::algorithm::to_lower(m_scheme);
m_user.assign(matchResults[4].first, matchResults[4].second);
m_host.assign(matchResults[5].first, matchResults[5].second);
m_port.assign(matchResults[7].first, matchResults[7].second);
m_path.assign(matchResults[8].first, matchResults[8].second);
m_query.assign(matchResults[10].first, matchResults[10].second);
m_fragment.assign(matchResults[15].first, matchResults[15].second);
if (m_port.length() == 0)
{
if (m_scheme.compare("https") == 0)
{
m_port = "443";
m_isSSL = true;
}
else
m_port = "80";
}
return true;
}
return false;
}