1818#include < boost/asio/ssl.hpp>
1919#include < boost/asio/placeholders.hpp>
2020#include < network/config.hpp>
21- #include < network/http/v2/client/connection/connection .hpp>
21+ #include < network/http/v2/client/connection/async_connection .hpp>
2222#include < network/http/v2/client/client.hpp>
2323
2424namespace network {
2525namespace http {
2626namespace v2 {
2727namespace client_connection {
28- class ssl_connection
29- : public connection, std::enable_shared_from_this<ssl_connection> {
28+ class ssl_connection :public async_connection {
3029
3130ssl_connection (const ssl_connection &) =delete ;
3231 ssl_connection &operator = (const ssl_connection &) =delete ;
@@ -44,12 +43,11 @@ namespace network {
4443 }
4544
4645virtual void async_connect (const boost::asio::ip::tcp::endpoint &endpoint,
46+ const std::string &host,
4747 connect_callback callback) {
4848 context_.reset (new boost::asio::ssl::context (boost::asio::ssl::context::sslv23));
49- std::vector<std::string>const & certificate_paths =
50- options_.openssl_certificate_paths ();
51- std::vector<std::string>const & verifier_paths =
52- options_.openssl_verify_paths ();
49+ auto certificate_paths = options_.openssl_certificate_paths ();
50+ auto verifier_paths = options_.openssl_verify_paths ();
5351bool use_default_verification = certificate_paths.empty () && verifier_paths.empty ();
5452if (!use_default_verification) {
5553for (auto path : certificate_paths) {
@@ -59,7 +57,7 @@ namespace network {
5957 context_->add_verify_path (path);
6058 }
6159 context_->set_verify_mode (boost::asio::ssl::context::verify_peer);
62- // context_->set_verify_callback(boost::asio::ssl::rfc2818_verification(host));
60+ context_->set_verify_callback (boost::asio::ssl::rfc2818_verification (host));
6361 }
6462else {
6563 context_->set_default_verify_paths ();
@@ -69,18 +67,27 @@ namespace network {
6967 boost::asio::ip::tcp::socket>(io_service_, *context_));
7068
7169using namespace std ::placeholders;
72- socket_->lowest_layer ()
73- .async_connect (endpoint, callback);
70+ socket_->lowest_layer ().async_connect (endpoint,
71+ [=] (const boost::system::error_code &ec) {
72+ handle_connected (ec, callback);
73+ });
7474 }
7575
7676virtual void async_write (boost::asio::streambuf &command_streambuf,
7777 write_callback callback) {
7878boost::asio::async_write (*socket_, command_streambuf, callback);
7979 }
8080
81- virtual void async_read_some (const boost::asio::mutable_buffers_1 &read_buffer,
82- read_callback callback) {
83- socket_->async_read_some (read_buffer, callback);
81+ virtual void async_read_until (boost::asio::streambuf &command_streambuf,
82+ const std::string &delim,
83+ read_callback callback) {
84+ boost::asio::async_read_until (*socket_, command_streambuf, delim, callback);
85+ }
86+
87+ virtual void async_read (boost::asio::streambuf &command_streambuf,
88+ read_callback callback) {
89+ boost::asio::async_read (*socket_, command_streambuf,
90+ boost::asio::transfer_at_least (1 ), callback);
8491 }
8592
8693virtual void cancel () {
@@ -89,6 +96,23 @@ namespace network {
8996
9097private:
9198
99+ void handle_connected (const boost::system::error_code &ec, connect_callback callback) {
100+ if (!ec) {
101+ auto existing_session =SSL_get1_session (socket_->native_handle ());
102+ if (existing_session) {
103+ socket_->async_handshake (boost::asio::ssl::stream_base::client, callback);
104+ }
105+ else {
106+ SSL_set_session (socket_->native_handle (), existing_session);
107+ SSL_connect (socket_->native_handle ());
108+ callback (ec);
109+ }
110+ }
111+ else {
112+ callback (ec);
113+ }
114+ }
115+
92116 boost::asio::io_service &io_service_;
93117 client_options options_;
94118 std::unique_ptr<boost::asio::ssl::context> context_;