2
2
#define BOOST_NETWORK_PROTOCOL_HTTP_SERVER_CONNECTION_HPP_20101027
3
3
4
4
// Copyright 2010 Dean Michael Berris.
5
+ // Copyright 2014 Jelle Van den Driessche.
5
6
// Distributed under the Boost Software License, Version 1.0.
6
7
// (See accompanying file LICENSE_1_0.txt or copy at
7
8
// http://www.boost.org/LICENSE_1_0.txt)
18
19
#include < boost/asio/strand.hpp>
19
20
#include < boost/asio/buffer.hpp>
20
21
#include < boost/make_shared.hpp>
22
+ #include < boost/network/protocol/stream_handler.hpp>
21
23
#include < boost/network/protocol/http/server/request_parser.hpp>
22
24
#include < boost/range/iterator_range.hpp>
23
25
#include < boost/optional.hpp>
@@ -149,14 +151,16 @@ namespace boost { namespace network { namespace http {
149
151
asio::io_service & io_service
150
152
, Handler & handler
151
153
, utils::thread_pool & thread_pool
154
+ , boost::shared_ptr<boost::asio::ssl::context> ctx = boost::shared_ptr<boost::asio::ssl::context>( )
152
155
)
153
- : socket_(io_service)
154
- , strand(io_service)
156
+ : strand(io_service)
155
157
, handler(handler)
156
158
, thread_pool_(thread_pool)
157
159
, headers_already_sent(false )
158
160
, headers_in_progress(false )
161
+ , handshake_done(false )
159
162
, headers_buffer(BOOST_NETWORK_HTTP_SERVER_CONNECTION_HEADER_BUFFER_MAX_SIZE)
163
+ , socket_(io_service, ctx)
160
164
{
161
165
new_start = read_buffer_.begin ();
162
166
}
@@ -285,7 +289,7 @@ namespace boost { namespace network { namespace http {
285
289
, asio::placeholders::error, asio::placeholders::bytes_transferred)));
286
290
}
287
291
288
- asio::ip::tcp::socket & socket () { return socket_; }
292
+ boost::network::stream_handler & socket () { return socket_; }
289
293
utils::thread_pool & thread_pool () { return thread_pool_; }
290
294
bool has_error () { return (!!error_encountered); }
291
295
optional<boost::system::system_error> error ()
@@ -319,12 +323,13 @@ namespace boost { namespace network { namespace http {
319
323
typedef boost::lock_guard<boost::recursive_mutex> lock_guard;
320
324
typedef std::list<boost::function<void ()> > pending_actions_list;
321
325
322
- asio::ip::tcp::socket socket_;
326
+ boost::network::stream_handler socket_;
323
327
asio::io_service::strand strand;
324
328
Handler & handler;
325
329
utils::thread_pool & thread_pool_;
326
330
volatile bool headers_already_sent, headers_in_progress;
327
331
asio::streambuf headers_buffer;
332
+ bool handshake_done;
328
333
329
334
boost::recursive_mutex headers_mutex;
330
335
buffer_type read_buffer_;
@@ -350,21 +355,29 @@ namespace boost { namespace network { namespace http {
350
355
read_more (method);
351
356
}
352
357
358
+
353
359
void 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
363
374
)
364
375
)
365
376
);
377
+ }
366
378
}
367
379
380
+
368
381
void handle_read_data (state_t state, boost::system::error_code const & ec, std::size_t bytes_transferred) {
369
382
if (!ec) {
370
383
logic::tribool parsed_ok;
@@ -645,6 +658,14 @@ namespace boost { namespace network { namespace http {
645
658
,asio::placeholders::bytes_transferred)
646
659
);
647
660
}
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
+ }
648
669
};
649
670
650
671
} /* http */
0 commit comments