Repository navigation
Expand file tree
/
Copy pathssl_http_client_async_blocking.cpp
More file actions
238 lines (176 loc) · 5.93 KB
/
Copy pathssl_http_client_async_blocking.cpp
File metadata and controls
238 lines (176 loc) · 5.93 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
// HTTPs client sending a GET request.
// Based on Asio asynchronous APIs but run in blocking mode.
// Why not just use synchronous APIs?
// Because you might want to add a deadline timer for timeout control.
// See |ssl_http_client_async_blocking_timeout| for the details.
#include <iostream>
#include <string>
#include <vector>
#include "boost/asio.hpp"
#include "boost/asio/ssl.hpp"
#include "boost/lambda/bind.hpp"
#include "boost/lambda/lambda.hpp"
// -----------------------------------------------------------------------------
using boost::asio::ip::tcp;
namespace ssl = boost::asio::ssl;
typedef ssl::stream<tcp::socket> ssl_socket;
// Verify the certificate of the peer (remote host).
#define SSL_VERIFY 0
// -----------------------------------------------------------------------------
class Client {
public:
Client(const std::string& host, const std::string& path);
bool Request();
private:
bool Connect();
bool Handshake();
bool SendRequest();
bool ReadResponse();
boost::asio::io_context io_context_;
std::string host_;
std::string path_;
ssl::context ssl_context_;
ssl::stream<tcp::socket> ssl_socket_;
boost::asio::streambuf request_;
std::vector<char> buffer_;
};
// -----------------------------------------------------------------------------
Client::Client(const std::string& host, const std::string& path)
: host_(host), path_(path),
ssl_context_(ssl::context::sslv23),
ssl_socket_(io_context_, ssl_context_),
buffer_(1024) {
// Use the default paths for finding CA certificates.
ssl_context_.set_default_verify_paths();
}
bool Client::Request() {
if (!Connect()) {
return false;
}
if (!Handshake()) {
return false;
}
if (!SendRequest()) {
return false;
}
return ReadResponse();
}
bool Client::Connect() {
boost::system::error_code ec;
// Get a list of endpoints corresponding to the server name.
tcp::resolver resolver(io_context_);
auto endpoints = resolver.resolve(host_, "https", ec);
if (ec) {
std::cerr << "Resolve failed: " << ec.message() << std::endl;
return false;
}
ec = boost::asio::error::would_block;
// ConnectHandler: void (boost::system::error_code, tcp::endpoint)
// Using |boost::lambda::var()| is identical to:
// boost::asio::async_connect(
// ssl_socket_.lowest_layer(), endpoints,
// [this, &ec](boost::system::error_code inner_ec, tcp::endpoint) {
// ec = inner_ec;
// });
boost::asio::async_connect(ssl_socket_.lowest_layer(), endpoints,
boost::lambda::var(ec) = boost::lambda::_1);
// Block until the asynchronous operation has completed.
do {
io_context_.run_one();
} while (ec == boost::asio::error::would_block);
if (ec) {
std::cerr << "Connect failed: " << ec.message() << std::endl;
return false;
}
return true;
}
bool Client::Handshake() {
boost::system::error_code ec = boost::asio::error::would_block;
#if SSL_VERIFY
ssl_socket_.set_verify_mode(ssl::verify_peer);
#else
ssl_socket_.set_verify_mode(ssl::verify_none);
#endif // SSL_VERIFY
// ssl::host_name_verification has been added since Boost 1.73 to replace
// ssl::rfc2818_verification.
#if BOOST_VERSION < 107300
ssl_socket_.set_verify_callback(ssl::rfc2818_verification(host_));
#else
ssl_socket_.set_verify_callback(ssl::host_name_verification(host_));
#endif // BOOST_VERSION < 107300
// HandshakeHandler: void (boost::system::error_code)
ssl_socket_.async_handshake(ssl::stream_base::client,
boost::lambda::var(ec) = boost::lambda::_1);
// Block until the asynchronous operation has completed.
do {
io_context_.run_one();
} while (ec == boost::asio::error::would_block);
if (ec) {
std::cerr << "Handshake failed: " << ec.message() << std::endl;
return false;
}
return true;
}
bool Client::SendRequest() {
std::ostream request_stream(&request_);
request_stream << "GET " << path_ << " HTTP/1.1\r\n";
request_stream << "Host: " << host_ << "\r\n\r\n";
boost::system::error_code ec = boost::asio::error::would_block;
// WriteHandler: void (boost::system::error_code, std::size_t)
boost::asio::async_write(ssl_socket_, request_,
boost::lambda::var(ec) = boost::lambda::_1);
// Block until the asynchronous operation has completed.
do {
io_context_.run_one();
} while (ec == boost::asio::error::would_block);
if (ec) {
std::cerr << "Write failed: " << ec.message() << std::endl;
return false;
}
return true;
}
bool Client::ReadResponse() {
boost::system::error_code ec = boost::asio::error::would_block;
ssl_socket_.async_read_some(
boost::asio::buffer(buffer_),
[this, &ec](boost::system::error_code inner_ec, std::size_t length) {
ec = inner_ec;
if (inner_ec || length == 0) {
std::cout << "Socket read error." << std::endl;
return;
}
std::cout.write(buffer_.data(), length);
// TODO: Call ReadResponse() to read until the end.
});
// Block until the asynchronous operation has completed.
do {
io_context_.run_one();
} while (ec == boost::asio::error::would_block);
if (ec) {
std::cout << "Read failed: " << ec.message() << std::endl;
return false;
}
return true;
}
// -----------------------------------------------------------------------------
void Help(const char* argv0) {
std::cout << "Usage: " << argv0 << " <host> <path>" << std::endl;
std::cout << " E.g.," << std::endl;
std::cout << " " << argv0 << " www.boost.org /LICENSE_1_0.txt" << std::endl;
std::cout << " " << argv0 << " www.google.com /" << std::endl;
}
int main(int argc, char* argv[]) {
if (argc != 3) {
Help(argv[0]);
return 1;
}
std::string host = argv[1];
std::string path = argv[2];
try {
Client client(host, path);
client.Request();
} catch (const std::exception& e) {
std::cout << "Exception: " << e.what() << std::endl;
}
return 0;
}