57 SocketClient(
const std::string &server_port_path,
bool verify_certificate =
true,
58 const std::string &certification_file = std::string(),
const std::string &private_key_file = std::string(),
59 const std::string &verify_file = std::string())
60 : SocketClientBase<
WSS>::SocketClientBase(server_port_path, 443),
61# if (defined(
ASIO_STANDALONE) && ASIO_VERSION >= 103300) || BOOST_ASIO_VERSION >= 103300
64 context.set_options(asio::ssl::context::no_tlsv1);
65 context.set_options(asio::ssl::context::no_tlsv1_1);
67 context(asio::ssl::context::tlsv12) {
70 if(certification_file.size() > 0 && private_key_file.size() > 0) {
71 context.use_certificate_chain_file(certification_file);
72 context.use_private_key_file(private_key_file, asio::ssl::context::pem);
75 if(verify_certificate) {
76# if (defined(ASIO_STANDALONE) && ASIO_VERSION >= 103300) || BOOST_ASIO_VERSION >= 103300
77 context.set_verify_callback(asio::ssl::host_name_verification(host));
79 context.set_verify_callback(asio::ssl::rfc2818_verification(host));
83 if(verify_file.size() > 0) {
84 context.load_verify_file(verify_file);
89 if(verify_certificate) {
90 context.set_verify_mode(asio::ssl::verify_peer);
92 context.set_verify_mode(asio::ssl::verify_none);
104 X509_STORE* store = ::SSL_CTX_get_cert_store(
context.native_handle());
105 HCERTSTORE hCertStore = CertOpenSystemStore(0LL,
"ROOT");
110 PCCERT_CONTEXT certContext =
nullptr;
112 certContext = CertEnumCertificatesInStore(hCertStore, certContext);
116 const unsigned char* encoded = certContext->pbCertEncoded;
117 X509* x509 = d2i_X509(
nullptr, &encoded, certContext->cbCertEncoded);
119 X509_STORE_add_cert(store, x509);
123 CertCloseStore(hCertStore, 0);
125 context.set_default_verify_paths();
138 LockGuard connection_lock(connection_mutex);
139 auto connection = this->connection = std::shared_ptr<Connection>(
new Connection(
140 handler_runner, config.timeout_idle, config.max_handshake_size, *io_service,
context));
141 connection_lock.unlock();
143 std::pair<std::string, std::string> host_port;
144 if(config.proxy_server.empty())
145 host_port = {host, std::to_string(port)};
147 auto proxy_host_port = parse_host_port(config.proxy_server, 8080);
148 host_port = {proxy_host_port.first, std::to_string(proxy_host_port.second)};
151 auto resolver = std::make_shared<asio::ip::tcp::resolver>(*io_service);
152 connection->set_timeout(config.timeout_request);
153 async_resolve(*resolver, host_port, [
this, connection, resolver](
const error_code &ec, resolver_results results) {
154 connection->cancel_timeout();
155 auto lock = connection->handler_runner->continue_lock();
159 connection->set_timeout(this->config.timeout_request);
160 asio::async_connect(connection->socket->lowest_layer(), results, [
this, connection, resolver](
const error_code &ec, async_connect_endpoint ) {
161 connection->cancel_timeout();
162 auto lock = connection->handler_runner->continue_lock();
166 asio::ip::tcp::no_delay option(true);
168 connection->socket->lowest_layer().set_option(option, ec);
170 if(!this->config.proxy_server.empty()) {
171 auto streambuf = std::make_shared<asio::streambuf>();
172 std::ostream ostream(streambuf.get());
173 auto host_port = this->host +
':' + std::to_string(this->port);
174 ostream <<
"CONNECT " + host_port +
" HTTP/1.1\r\n"
175 <<
"Host: " << host_port <<
"\r\n";
176 if(!this->config.proxy_auth.empty())
177 ostream <<
"Proxy-Authorization: Basic " << Crypto::Base64::encode(this->config.proxy_auth) <<
"\r\n";
179 connection->set_timeout(this->config.timeout_request);
180 asio::async_write(connection->socket->next_layer(), *streambuf, [this, connection, streambuf](const error_code &ec, std::size_t ) {
181 connection->cancel_timeout();
182 auto lock = connection->handler_runner->continue_lock();
186 connection->set_timeout(this->config.timeout_request);
187 connection->in_message = std::shared_ptr<InMessage>(new InMessage());
188 asio::async_read_until(connection->socket->next_layer(), connection->in_message->streambuf,
"\r\n\r\n", [this, connection](const error_code &ec, std::size_t ) {
189 connection->cancel_timeout();
190 auto lock = connection->handler_runner->continue_lock();
194 if(!ResponseMessage::parse(*connection->in_message, connection->http_version, connection->status_code, connection->header))
195 this->connection_error(connection, make_error_code::make_error_code(errc::protocol_error));
197 if(connection->status_code.compare(0, 3,
"200") != 0)
198 this->connection_error(connection, make_error_code::make_error_code(errc::permission_denied));
200 this->handshake(connection);
203 this->connection_error(connection, ec);
206 this->connection_error(connection, ec);
209 this->handshake(connection);
211 this->connection_error(connection, ec);
214 this->connection_error(connection, ec);