Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 34 additions & 13 deletions boost/network/protocol/http/server/async_connection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define BOOST_NETWORK_PROTOCOL_HTTP_SERVER_CONNECTION_HPP_20101027

// Copyright 2010 Dean Michael Berris.
// Copyright 2014 Jelle Van den Driessche.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
Expand All @@ -18,6 +19,7 @@
#include <boost/asio/strand.hpp>
#include <boost/asio/buffer.hpp>
#include <boost/make_shared.hpp>
#include <boost/network/protocol/stream_handler.hpp>
#include <boost/network/protocol/http/server/request_parser.hpp>
#include <boost/range/iterator_range.hpp>
#include <boost/optional.hpp>
Expand Down Expand Up @@ -149,14 +151,16 @@ namespace boost { namespace network { namespace http {
asio::io_service & io_service
, Handler & handler
, utils::thread_pool & thread_pool
, boost::shared_ptr<boost::asio::ssl::context> ctx = boost::shared_ptr<boost::asio::ssl::context>( )
)
: socket_(io_service)
, strand(io_service)
: strand(io_service)
, handler(handler)
, thread_pool_(thread_pool)
, headers_already_sent(false)
, headers_in_progress(false)
, handshake_done(false)
, headers_buffer(BOOST_NETWORK_HTTP_SERVER_CONNECTION_HEADER_BUFFER_MAX_SIZE)
, socket_(io_service, ctx)
{
new_start = read_buffer_.begin();
}
Expand Down Expand Up @@ -285,7 +289,7 @@ namespace boost { namespace network { namespace http {
, asio::placeholders::error, asio::placeholders::bytes_transferred)));
}

asio::ip::tcp::socket & socket() { return socket_; }
boost::network::stream_handler & socket() { return socket_; }
utils::thread_pool & thread_pool() { return thread_pool_; }
bool has_error() { return (!!error_encountered); }
optional<boost::system::system_error> error()
Expand Down Expand Up @@ -319,12 +323,13 @@ namespace boost { namespace network { namespace http {
typedef boost::lock_guard<boost::recursive_mutex> lock_guard;
typedef std::list<boost::function<void()> > pending_actions_list;

asio::ip::tcp::socket socket_;
boost::network::stream_handler socket_;
asio::io_service::strand strand;
Handler & handler;
utils::thread_pool & thread_pool_;
volatile bool headers_already_sent, headers_in_progress;
asio::streambuf headers_buffer;
bool handshake_done;

boost::recursive_mutex headers_mutex;
buffer_type read_buffer_;
Expand All @@ -350,21 +355,29 @@ namespace boost { namespace network { namespace http {
read_more(method);
}


void read_more(state_t state) {
socket_.async_read_some(
asio::buffer(read_buffer_)
, strand.wrap(
boost::bind(
&async_connection<Tag,Handler>::handle_read_data,
async_connection<Tag,Handler>::shared_from_this(),
state,
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred
if(socket_.is_ssl_enabled() && !handshake_done) {
socket_.async_handshake(boost::asio::ssl::stream_base::server,
boost::bind(&async_connection::handle_handshake, async_connection<Tag,Handler>::shared_from_this(),
boost::asio::placeholders::error, state));
} else {
socket_.async_read_some(
asio::buffer(read_buffer_)
, strand.wrap(
boost::bind(
&async_connection<Tag,Handler>::handle_read_data,
async_connection<Tag,Handler>::shared_from_this(),
state,
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred
)
)
);
}
}


void handle_read_data(state_t state, boost::system::error_code const & ec, std::size_t bytes_transferred) {
if (!ec) {
logic::tribool parsed_ok;
Expand Down Expand Up @@ -645,6 +658,14 @@ namespace boost { namespace network { namespace http {
,asio::placeholders::bytes_transferred)
);
}
void handle_handshake(const boost::system::error_code& ec, state_t state) {
if (!ec) {
handshake_done = true;
read_more(state);
} else {
error_encountered = in_place<boost::system::system_error>(ec);
}
}
};

} /* http */
Expand Down
16 changes: 10 additions & 6 deletions boost/network/protocol/http/server/async_server.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define BOOST_NETWORK_PROTOCOL_HTTP_SERVER_ASYNC_SERVER_HPP_20101025

// Copyright 2010 Dean Michael Berris.
// Copyright 2014 Jelle Van den Driessche.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
Expand Down Expand Up @@ -38,6 +39,7 @@ namespace boost { namespace network { namespace http {
, listening_mutex_()
, stopping_mutex_()
, listening(false)
, ctx_(options.context())
{}

void run() {
Expand Down Expand Up @@ -79,6 +81,7 @@ namespace boost { namespace network { namespace http {
boost::mutex listening_mutex_;
boost::mutex stopping_mutex_;
bool listening;
boost::shared_ptr<boost::asio::ssl::context> ctx_;

void handle_stop() {
scoped_mutex_lock stopping_lock(stopping_mutex_);
Expand All @@ -96,13 +99,14 @@ namespace boost { namespace network { namespace http {
<< ec);
}

socket_options_base::socket_options(new_connection->socket());
socket_options_base::socket_options(new_connection->socket().next_layer());

new_connection->start();
new_connection.reset(
new connection(service_, handler, *thread_pool));
new connection(service_, handler, *thread_pool, ctx_));
acceptor.async_accept(
new_connection->socket(),
boost::bind(&async_server_base<Tag, Handler>::handle_accept,
new_connection->socket().next_layer(),
boost::bind(&async_server_base<Tag, Handler>::handle_accept,
this,
boost::asio::placeholders::error));
}
Expand Down Expand Up @@ -135,8 +139,8 @@ namespace boost { namespace network { namespace http {
BOOST_NETWORK_MESSAGE("Error listening on socket: '" << error << "' on " << address_ << ":" << port_);
return;
}
new_connection.reset(new connection(service_, handler, *thread_pool));
acceptor.async_accept(new_connection->socket(),
new_connection.reset(new connection(service_, handler, *thread_pool, ctx_));
acceptor.async_accept(new_connection->socket().next_layer(),
boost::bind(
&async_server_base<Tag,Handler>::handle_accept
, this
Expand Down
9 changes: 8 additions & 1 deletion boost/network/protocol/http/server/options.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@

// Copyright 2013 Google, Inc.
// Copyright 2013 Dean Michael Berris <dberris@google.com>
// Copyright 2014 Jelle Van den Driessche
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)

#include <boost/asio/io_service.hpp>
#include <boost/asio/socket_base.hpp>
#include <boost/asio/ssl.hpp>
#include <boost/network/traits/string.hpp>
#include <boost/network/utils/thread_pool.hpp>
#include <boost/optional.hpp>
Expand All @@ -35,6 +37,7 @@ struct server_options {
, receive_low_watermark_()
, send_low_watermark_()
, thread_pool_()
, context_()
{}

server_options(const server_options &other)
Expand All @@ -52,6 +55,7 @@ struct server_options {
, receive_low_watermark_(other.receive_low_watermark_)
, send_low_watermark_(other.send_low_watermark_)
, thread_pool_(other.thread_pool_)
, context_(other.context_)
{}

server_options &operator= (server_options other) {
Expand All @@ -74,8 +78,10 @@ struct server_options {
swap(receive_low_watermark_, other.receive_low_watermark_);
swap(send_low_watermark_, other.send_low_watermark_);
swap(thread_pool_, other.thread_pool_);
swap(context_, other.context_);
}

server_options &context(boost::shared_ptr<boost::asio::ssl::context> v) { context_ = v; return *this; }
server_options &io_service(boost::shared_ptr<boost::asio::io_service> v) { io_service_ = v; return *this; }
server_options &address(string_type const &v) { address_ = v; return *this; }
server_options &port(string_type const &v) { port_ = v; return *this; }
Expand Down Expand Up @@ -104,8 +110,9 @@ struct server_options {
boost::optional<boost::asio::socket_base::receive_low_watermark> receive_low_watermark() const { return receive_low_watermark_; }
boost::optional<boost::asio::socket_base::send_low_watermark> send_low_watermark() const { return send_low_watermark_; }
boost::shared_ptr<utils::thread_pool> thread_pool() const { return thread_pool_; }

boost::shared_ptr<boost::asio::ssl::context> context() const { return context_; }
private:
boost::shared_ptr<boost::asio::ssl::context> context_;
boost::shared_ptr<boost::asio::io_service> io_service_;
Handler &handler_;
string_type address_;
Expand Down
Loading