Repository navigation
Expand file tree
/
Copy pathssl_http_client_async.cpp
More file actions
185 lines (141 loc) · 5.26 KB
/
Copy pathssl_http_client_async.cpp
File metadata and controls
185 lines (141 loc) · 5.26 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
// HTTPs client sending a GET request.
// Based on Asio asynchronous APIs.
#include <iostream>
#include <string>
#include <vector>
#include "boost/asio.hpp"
#include "boost/asio/ssl.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(boost::asio::io_context& io_context,
const std::string& host, const std::string& path);
private:
void ConnectHandler(boost::system::error_code ec, tcp::endpoint);
void HandshakeHandler(boost::system::error_code ec);
void AsyncWrite();
void WriteHandler(boost::system::error_code ec, std::size_t length);
void AsyncReadSome();
void ReadHandler(boost::system::error_code ec, std::size_t length);
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(boost::asio::io_context& io_context,
const std::string& host, const std::string& path)
: io_context_(io_context),
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();
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;
}
// ConnectHandler: void (boost::system::error_code, tcp::endpoint)
boost::asio::async_connect(ssl_socket_.lowest_layer(), endpoints,
std::bind(&Client::ConnectHandler, this,
std::placeholders::_1,
std::placeholders::_2));
}
void Client::ConnectHandler(boost::system::error_code ec, tcp::endpoint) {
if (ec) {
std::cout << "Connect failed: " << ec.message() << std::endl;
} else {
#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,
std::bind(&Client::HandshakeHandler,
this,
std::placeholders::_1));
}
}
void Client::HandshakeHandler(boost::system::error_code ec) {
if (ec) {
std::cerr << "Handshake failed: " << ec.message() << std::endl;
} else {
AsyncWrite();
}
}
void Client::AsyncWrite() {
std::ostream request_stream(&request_);
request_stream << "GET " << path_ << " HTTP/1.1\r\n";
request_stream << "Host: " << host_ << "\r\n\r\n";
// WriteHandler: void (boost::system::error_code, std::size_t)
boost::asio::async_write(ssl_socket_, request_,
std::bind(&Client::WriteHandler, this,
std::placeholders::_1,
std::placeholders::_2));
}
void Client::WriteHandler(boost::system::error_code ec, std::size_t length) {
if (ec) {
std::cerr << "Write failed: " << ec.message() << std::endl;
} else {
AsyncReadSome();
}
}
void Client::AsyncReadSome() {
ssl_socket_.async_read_some(boost::asio::buffer(buffer_),
std::bind(&Client::ReadHandler, this,
std::placeholders::_1,
std::placeholders::_2));
}
void Client::ReadHandler(boost::system::error_code ec, std::size_t length) {
if (ec) {
std::cout << ec.message() << std::endl;
} else {
std::cout.write(buffer_.data(), length);
// TODO: Call AsyncReadSome() to read until the end.
}
}
// -----------------------------------------------------------------------------
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 {
boost::asio::io_context io_context;
Client client(io_context, host, path);
io_context.run();
} catch (const std::exception& e) {
std::cout << "Exception: " << e.what() << std::endl;
}
return 0;
}