49 SocketClient(
const std::string &server_port_path,
bool verify_certificate =
true,
50 const std::string &certification_file = std::string(),
const std::string &private_key_file = std::string(),
51 const std::string &verify_file = std::string())
52 : SocketClientBase<
WSS>::SocketClientBase(server_port_path, 443),
53# if (defined(ASIO_STANDALONE) && ASIO_VERSION >= 103300) || BOOST_ASIO_VERSION >= 103300
56 context.set_options(asio::ssl::context::no_tlsv1);
57 context.set_options(asio::ssl::context::no_tlsv1_1);
59 context(asio::ssl::context::tlsv12) {
62 if(certification_file.size() > 0 && private_key_file.size() > 0) {
63 context.use_certificate_chain_file(certification_file);
64 context.use_private_key_file(private_key_file, asio::ssl::context::pem);
67 if(verify_certificate) {
68# if (defined(ASIO_STANDALONE) && ASIO_VERSION >= 103300) || BOOST_ASIO_VERSION >= 103300
69 context.set_verify_callback(asio::ssl::host_name_verification(host));
71 context.set_verify_callback(asio::ssl::rfc2818_verification(host));
75 if(verify_file.size() > 0) {
76 context.load_verify_file(verify_file);
81 if(verify_certificate) {
82 context.set_verify_mode(asio::ssl::verify_peer);
84 context.set_verify_mode(asio::ssl::verify_none);
96 X509_STORE* store = ::SSL_CTX_get_cert_store(
context.native_handle());
97 HCERTSTORE hCertStore = CertOpenSystemStore(0LL,
"ROOT");
102 PCCERT_CONTEXT certContext =
nullptr;
104 certContext = CertEnumCertificatesInStore(hCertStore, certContext);
108 const unsigned char* encoded = certContext->pbCertEncoded;
109 X509* x509 = d2i_X509(
nullptr, &encoded, certContext->cbCertEncoded);
111 X509_STORE_add_cert(store, x509);
115 CertCloseStore(hCertStore, 0);
117 context.set_default_verify_paths();
130 LockGuard connection_lock(connection_mutex);
131 auto connection = this->connection = std::shared_ptr<Connection>(
new Connection(handler_runner, config.timeout_idle, *io_service,
context));
132 connection_lock.unlock();
134 std::pair<std::string, std::string> host_port;
135 if(config.proxy_server.empty())
136 host_port = {host, std::to_string(port)};
138 auto proxy_host_port = parse_host_port(config.proxy_server, 8080);
139 host_port = {proxy_host_port.first, std::to_string(proxy_host_port.second)};
142 auto resolver = std::make_shared<asio::ip::tcp::resolver>(*io_service);
143 connection->set_timeout(config.timeout_request);
144 async_resolve(*resolver, host_port, [
this, connection, resolver](
const error_code &ec, resolver_results results) {
145 connection->cancel_timeout();
146 auto lock = connection->handler_runner->continue_lock();
150 connection->set_timeout(this->config.timeout_request);
151 asio::async_connect(connection->socket->lowest_layer(), results, [
this, connection, resolver](
const error_code &ec, async_connect_endpoint ) {
152 connection->cancel_timeout();
153 auto lock = connection->handler_runner->continue_lock();
157 asio::ip::tcp::no_delay option(true);
159 connection->socket->lowest_layer().set_option(option, ec);
161 if(!this->config.proxy_server.empty()) {
162 auto streambuf = std::make_shared<asio::streambuf>();
163 std::ostream ostream(streambuf.get());
164 auto host_port = this->host +
':' + std::to_string(this->port);
165 ostream <<
"CONNECT " + host_port +
" HTTP/1.1\r\n"
166 <<
"Host: " << host_port <<
"\r\n";
167 if(!this->config.proxy_auth.empty())
168 ostream <<
"Proxy-Authorization: Basic " << Crypto::Base64::encode(this->config.proxy_auth) <<
"\r\n";
170 connection->set_timeout(this->config.timeout_request);
171 asio::async_write(connection->socket->next_layer(), *streambuf, [this, connection, streambuf](const error_code &ec, std::size_t ) {
172 connection->cancel_timeout();
173 auto lock = connection->handler_runner->continue_lock();
177 connection->set_timeout(this->config.timeout_request);
178 connection->in_message = std::shared_ptr<InMessage>(new InMessage());
179 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 ) {
180 connection->cancel_timeout();
181 auto lock = connection->handler_runner->continue_lock();
185 if(!ResponseMessage::parse(*connection->in_message, connection->http_version, connection->status_code, connection->header))
186 this->connection_error(connection, make_error_code::make_error_code(errc::protocol_error));
188 if(connection->status_code.compare(0, 3,
"200") != 0)
189 this->connection_error(connection, make_error_code::make_error_code(errc::permission_denied));
191 this->handshake(connection);
194 this->connection_error(connection, ec);
197 this->connection_error(connection, ec);
200 this->handshake(connection);
202 this->connection_error(connection, ec);
205 this->connection_error(connection, ec);