Skip to content
Open
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
1 change: 1 addition & 0 deletions CONTRIBUTORS.txt
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,4 @@ René Meusel (reneme)

Sony Corporation
Gareth Sylvester-Bradley (garethsb-sony)
Simon Lo (lo-simon)
25 changes: 25 additions & 0 deletions Release/include/cpprest/http_listener.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <limits>
#if !defined(_WIN32) && !defined(__cplusplus_winrt) || defined(CPPREST_FORCE_HTTP_LISTENER_ASIO)
#include <boost/asio/ssl.hpp>
#include <boost/asio.hpp>
#endif

#if !defined(_WIN32) || (_WIN32_WINNT >= _WIN32_WINNT_VISTA && !defined(__cplusplus_winrt)) || \
Expand Down Expand Up @@ -53,6 +54,7 @@ class http_listener_config
, m_backlog(other.m_backlog)
#if !defined(_WIN32) || defined(CPPREST_FORCE_HTTP_LISTENER_ASIO)
, m_ssl_context_callback(other.m_ssl_context_callback)
, m_tcp_socket_callback(other.m_tcp_socket_callback)
#endif
{
}
Expand All @@ -66,6 +68,7 @@ class http_listener_config
, m_backlog(std::move(other.m_backlog))
#if !defined(_WIN32) || defined(CPPREST_FORCE_HTTP_LISTENER_ASIO)
, m_ssl_context_callback(std::move(other.m_ssl_context_callback))
, m_tcp_socket_callback(std::move(other.m_tcp_socket_callback))
#endif
{
}
Expand All @@ -82,6 +85,7 @@ class http_listener_config
m_backlog = rhs.m_backlog;
#if !defined(_WIN32) || defined(CPPREST_FORCE_HTTP_LISTENER_ASIO)
m_ssl_context_callback = rhs.m_ssl_context_callback;
m_tcp_socket_callback = rhs.m_tcp_socket_callback;
#endif
}
return *this;
Expand All @@ -99,6 +103,7 @@ class http_listener_config
m_backlog = std::move(rhs.m_backlog);
#if !defined(_WIN32) || defined(CPPREST_FORCE_HTTP_LISTENER_ASIO)
m_ssl_context_callback = std::move(rhs.m_ssl_context_callback);
m_tcp_socket_callback = std::move(rhs.m_tcp_socket_callback);
#endif
}
return *this;
Expand Down Expand Up @@ -149,13 +154,33 @@ class http_listener_config
{
m_ssl_context_callback = ssl_context_callback;
}

/// <summary>
/// Get the callback of tcp socket
/// </summary>
/// <returns>The function defined by the user of http_listener_config to configure a tcp socket.</returns>
const std::function<void(boost::asio::ip::tcp::socket&)>& get_tcp_socket_callback() const
{
return m_tcp_socket_callback;
}

/// <summary>
/// Set the callback of tcp socket
/// </summary>
/// <param name="socket_callback">The function to configure a tcp socket which will setup https
/// connections.</param>
void set_tcp_socket_callback(const std::function<void(boost::asio::ip::tcp::socket&)>& tcp_socket_callback)
{
m_tcp_socket_callback = tcp_socket_callback;
}
#endif

private:
utility::seconds m_timeout;
int m_backlog;
#if !defined(_WIN32) || defined(CPPREST_FORCE_HTTP_LISTENER_ASIO)
std::function<void(boost::asio::ssl::context&)> m_ssl_context_callback;
std::function<void(boost::asio::ip::tcp::socket&)> m_tcp_socket_callback;
#endif
};

Expand Down
7 changes: 7 additions & 0 deletions Release/src/http/listener/http_server_asio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ class hostport_listener

bool m_is_https;
const std::function<void(boost::asio::ssl::context&)>& m_ssl_context_callback;
const std::function<void(boost::asio::ip::tcp::socket&)>& m_tcp_socket_callback;

public:
hostport_listener(http_linux_server* server,
Expand All @@ -150,6 +151,7 @@ class hostport_listener
, m_p_server(server)
, m_is_https(is_https)
, m_ssl_context_callback(config.get_ssl_context_callback())
, m_tcp_socket_callback(config.get_tcp_socket_callback())
{
m_all_connections_complete.set();

Expand Down Expand Up @@ -597,6 +599,11 @@ void hostport_listener::on_accept(std::unique_ptr<ip::tcp::socket> socket, const
boost::system::error_code error_ignored;
socket->set_option(option, error_ignored);

if (m_tcp_socket_callback)
{
m_tcp_socket_callback(*socket);
}

auto conn = asio_server_connection::create(std::move(socket), m_p_server, this);

m_connections.insert(conn.get());
Expand Down