22#define BOOST_NETWORK_PROTOCOL_HTTP_SERVER_CONNECTION_HPP_20101027
33
44// Copyright 2010 Dean Michael Berris.
5+ // Copyright 2014 Jelle Van den Driessche.
56// Distributed under the Boost Software License, Version 1.0.
67// (See accompanying file LICENSE_1_0.txt or copy at
78// http://www.boost.org/LICENSE_1_0.txt)
1819#include < boost/asio/strand.hpp>
1920#include < boost/asio/buffer.hpp>
2021#include < boost/make_shared.hpp>
22+ #include < boost/network/protocol/stream_handler.hpp>
2123#include < boost/network/protocol/http/server/request_parser.hpp>
2224#include < boost/range/iterator_range.hpp>
2325#include < boost/optional.hpp>
@@ -149,14 +151,16 @@ namespace boost { namespace network { namespace http {
149151 asio::io_service & io_service
150152 , Handler & handler
151153 , utils::thread_pool & thread_pool
154+ , boost::shared_ptr<boost::asio::ssl::context> ctx = boost::shared_ptr<boost::asio::ssl::context>( )
152155 )
153- : socket_(io_service)
154- , strand(io_service)
156+ : strand(io_service)
155157 , handler(handler)
156158 , thread_pool_(thread_pool)
157159 , headers_already_sent(false )
158160 , headers_in_progress(false )
161+ , handshake_done(false )
159162 , headers_buffer(BOOST_NETWORK_HTTP_SERVER_CONNECTION_HEADER_BUFFER_MAX_SIZE)
163+ , socket_(io_service, ctx)
160164 {
161165 new_start = read_buffer_.begin ();
162166 }
@@ -285,7 +289,7 @@ namespace boost { namespace network { namespace http {
285289 , asio::placeholders::error, asio::placeholders::bytes_transferred)));
286290 }
287291
288- asio::ip::tcp::socket &socket () {return socket_; }
292+ boost::network::stream_handler &socket () {return socket_; }
289293 utils::thread_pool &thread_pool () {return thread_pool_; }
290294bool has_error () {return (!!error_encountered); }
291295 optional<boost::system::system_error>error ()
@@ -319,12 +323,13 @@ namespace boost { namespace network { namespace http {
319323typedef boost::lock_guard<boost::recursive_mutex> lock_guard;
320324typedef std::list<boost::function<void ()> > pending_actions_list;
321325
322- asio::ip::tcp::socket socket_;
326+ boost::network::stream_handler socket_;
323327 asio::io_service::strand strand;
324328 Handler & handler;
325329 utils::thread_pool & thread_pool_;
326330volatile bool headers_already_sent, headers_in_progress;
327331 asio::streambuf headers_buffer;
332+ bool handshake_done;
328333
329334 boost::recursive_mutex headers_mutex;
330335 buffer_type read_buffer_;
@@ -350,21 +355,29 @@ namespace boost { namespace network { namespace http {
350355read_more (method);
351356 }
352357
358+
353359void read_more (state_t state) {
354- socket_.async_read_some (
355- asio::buffer (read_buffer_)
356- , strand.wrap (
357- boost::bind (
358- &async_connection<Tag,Handler>::handle_read_data,
359- async_connection<Tag,Handler>::shared_from_this (),
360- state,
361- boost::asio::placeholders::error,
362- boost::asio::placeholders::bytes_transferred
360+ if (socket_.is_ssl_enabled () && !handshake_done) {
361+ socket_.async_handshake (boost::asio::ssl::stream_base::server,
362+ boost::bind (&async_connection::handle_handshake, async_connection<Tag,Handler>::shared_from_this (),
363+ boost::asio::placeholders::error, state));
364+ }else {
365+ socket_.async_read_some (
366+ asio::buffer (read_buffer_)
367+ , strand.wrap (
368+ boost::bind (
369+ &async_connection<Tag,Handler>::handle_read_data,
370+ async_connection<Tag,Handler>::shared_from_this (),
371+ state,
372+ boost::asio::placeholders::error,
373+ boost::asio::placeholders::bytes_transferred
363374 )
364375 )
365376 );
377+ }
366378 }
367379
380+
368381void handle_read_data (state_t state, boost::system::error_codeconst & ec, std::size_t bytes_transferred) {
369382if (!ec) {
370383 logic::tribool parsed_ok;
@@ -645,6 +658,14 @@ namespace boost { namespace network { namespace http {
645658 ,asio::placeholders::bytes_transferred)
646659 );
647660 }
661+ void handle_handshake (const boost::system::error_code& ec,state_t state) {
662+ if (!ec) {
663+ handshake_done =true ;
664+ read_more (state);
665+ }else {
666+ error_encountered = in_place<boost::system::system_error>(ec);
667+ }
668+ }
648669 };
649670
650671}/* http*/