Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Fix for issue #444 "Unable to compile without OpenSSL"
- add an option CPP-NETLIB_ENABLE_HTTPS to enable/disable HTTPS in
  cmake config. By default it is turned ON (enable HTTPS) so as to
  preserve before-patch behavior. Turn it OFF to disable entirely
  SSL usage and linkage in libraries and header files.
- when SSL is disabled, the 'stream_handler' struct is replaced by
  a simple tcp_socket, and function calls in the files option.hpp,
  async_server.hpp and async_connection.hpp are adapted and compiled
  accordingly.

Note that sync_...hpp files don't have problems (yet) since SSL is
not integrated into these. Also note that a cleaner solution might
be to consider implementing two separate struct 'stream_handler' and
'stream_handler_ssl', or something similar.
  • Loading branch information
Alexis Moinet committed Jul 16, 2015
commit 716911adfc55a0f61feb434e3dc36adcf7185335
7 changes: 6 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ option( CPP-NETLIB_BUILD_SHARED_LIBS "Build cpp-netlib as shared libraries." OFF
option( CPP-NETLIB_BUILD_TESTS "Build the cpp-netlib project tests." ON)
option( CPP-NETLIB_BUILD_EXPERIMENTS "Build the cpp-netlib project experiments." ON)
option( CPP-NETLIB_BUILD_EXAMPLES "Build the cpp-netlib project examples." ON)
option( CPP-NETLIB_ENABLE_HTTPS "Build cpp-netlib with support for https if OpenSSL is found." ON)

include(GNUInstallDirs)

Expand Down Expand Up @@ -43,7 +44,11 @@ set(Boost_USE_MULTI_THREADED ON)
find_package( Boost 1.54.0
REQUIRED unit_test_framework system regex date_time thread filesystem
program_options chrono atomic )
find_package( OpenSSL )

if (CPP-NETLIB_ENABLE_HTTPS)
find_package( OpenSSL )
endif()

find_package( Threads )
set(CMAKE_VERBOSE_MAKEFILE true)

Expand Down
12 changes: 10 additions & 2 deletions boost/network/protocol/http/server/async_connection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,14 +174,18 @@ struct async_connection
public:
async_connection(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>())
boost::shared_ptr<ssl_context> ctx =
boost::shared_ptr<ssl_context>())
: strand(io_service),
handler(handler),
thread_pool_(thread_pool),
headers_buffer(
BOOST_NETWORK_HTTP_SERVER_CONNECTION_HEADER_BUFFER_MAX_SIZE),
#ifdef BOOST_NETWORK_ENABLE_HTTPS
socket_(io_service, ctx),
#else
socket_(io_service),
#endif
handshake_done(false),
headers_already_sent(false),
headers_in_progress(false) {
Expand Down Expand Up @@ -379,21 +383,25 @@ struct async_connection
}

void read_more(state_t state) {
#ifdef BOOST_NETWORK_ENABLE_HTTPS
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 {
#endif
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)));
#ifdef BOOST_NETWORK_ENABLE_HTTPS
}
#endif
}

void handle_read_data(state_t state, boost::system::error_code const& ec,
Expand Down
14 changes: 13 additions & 1 deletion boost/network/protocol/http/server/async_server.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ struct async_server_base : server_storage_base, socket_options_base {
boost::mutex listening_mutex_;
boost::mutex stopping_mutex_;
bool listening;
boost::shared_ptr<boost::asio::ssl::context> ctx_;
boost::shared_ptr<ssl_context> ctx_;

void handle_stop() {
scoped_mutex_lock stopping_lock(stopping_mutex_);
Expand All @@ -111,12 +111,20 @@ struct async_server_base : server_storage_base, socket_options_base {
BOOST_NETWORK_MESSAGE("Error accepting connection, reason: " << ec);
}

#ifdef BOOST_NETWORK_ENABLE_HTTPS
socket_options_base::socket_options(new_connection->socket().next_layer());
#else
socket_options_base::socket_options(new_connection->socket());
#endif

new_connection->start();
new_connection.reset(new connection(service_, handler, *thread_pool, ctx_));
acceptor.async_accept(
#ifdef BOOST_NETWORK_ENABLE_HTTPS
new_connection->socket().next_layer(),
#else
new_connection->socket(),
#endif
boost::bind(&async_server_base<Tag, Handler>::handle_accept, this,
boost::asio::placeholders::error));
}
Expand Down Expand Up @@ -155,7 +163,11 @@ struct async_server_base : server_storage_base, socket_options_base {
}
new_connection.reset(new connection(service_, handler, *thread_pool, ctx_));
acceptor.async_accept(
#ifdef BOOST_NETWORK_ENABLE_HTTPS
new_connection->socket().next_layer(),
#else
new_connection->socket(),
#endif
boost::bind(&async_server_base<Tag, Handler>::handle_accept, this,
boost::asio::placeholders::error));
listening = true;
Expand Down
8 changes: 4 additions & 4 deletions boost/network/protocol/http/server/options.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

#include <boost/asio/io_service.hpp>
#include <boost/asio/socket_base.hpp>
#include <boost/asio/ssl.hpp>
#include <boost/network/protocol/stream_handler.hpp>
#include <boost/network/traits/string.hpp>
#include <boost/network/utils/thread_pool.hpp>
#include <boost/optional.hpp>
Expand Down Expand Up @@ -81,7 +81,7 @@ struct server_options {
swap(context_, other.context_);
}

server_options &context(boost::shared_ptr<boost::asio::ssl::context> v) {
server_options &context(boost::shared_ptr<ssl_context> v) {
context_ = v;
return *this;
}
Expand Down Expand Up @@ -172,7 +172,7 @@ struct server_options {
boost::shared_ptr<utils::thread_pool> thread_pool() const {
return thread_pool_;
}
boost::shared_ptr<boost::asio::ssl::context> context() const {
boost::shared_ptr<ssl_context> context() const {
return context_;
}

Expand All @@ -194,7 +194,7 @@ struct server_options {
boost::optional<boost::asio::socket_base::send_low_watermark>
send_low_watermark_;
boost::shared_ptr<utils::thread_pool> thread_pool_;
boost::shared_ptr<boost::asio::ssl::context> context_;
boost::shared_ptr<ssl_context> context_;
};

template <class Tag, class Handler>
Expand Down
16 changes: 13 additions & 3 deletions boost/network/protocol/stream_handler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
#include <boost/asio/detail/throw_error.hpp>
#include <boost/asio/error.hpp>
#include <boost/asio.hpp>
#ifdef BOOST_NETWORK_ENABLE_HTTPS
#include <boost/asio/ssl.hpp>
#endif
#include <boost/asio/detail/push_options.hpp>
#include <boost/asio/detail/config.hpp>
#include <boost/asio/detail/handler_type_requirements.hpp>
Expand All @@ -28,9 +30,16 @@
namespace boost {
namespace network {

typedef boost::asio::ssl::stream<boost::asio::ip::tcp::socket> ssl_socket;
typedef boost::asio::ip::tcp::socket tcp_socket;

#ifndef BOOST_NETWORK_ENABLE_HTTPS
typedef tcp_socket stream_handler;
typedef void ssl_context;
#else

typedef boost::asio::ssl::stream<boost::asio::ip::tcp::socket> ssl_socket;
typedef boost::asio::ssl::context ssl_context;

struct stream_handler {
public:
stream_handler(boost::shared_ptr<tcp_socket> socket)
Expand All @@ -42,8 +51,8 @@ struct stream_handler {
: ssl_sock_(socket), ssl_enabled(true) {}

stream_handler(boost::asio::io_service& io,
boost::shared_ptr<boost::asio::ssl::context> ctx =
boost::shared_ptr<boost::asio::ssl::context>()) {
boost::shared_ptr<ssl_context> ctx =
boost::shared_ptr<ssl_context>()) {
tcp_sock_ = boost::make_shared<tcp_socket>(boost::ref(io));
ssl_enabled = false;
if (ctx) {
Expand Down Expand Up @@ -172,6 +181,7 @@ struct stream_handler {
boost::shared_ptr<ssl_socket> ssl_sock_;
bool ssl_enabled;
};
#endif
}
}

Expand Down