Skip to content

Commit

Permalink
Add the missing ssl::context callback in websocket_client_config (#1049)
Browse files Browse the repository at this point in the history
* Add the missing ssl::context callback in websocket_client_config, borrowing heavily from http_client

* Add dependency on Boost and OpenSSL when websocketpp is used for Secure WebSocket, just like for http_client when CPPREST_HTTP_CLIENT_IMPL STREQUAL "asio"
  • Loading branch information
garethsb authored and BillyONeal committed Mar 20, 2019
1 parent fac2ff7 commit ea4eff7
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
34 changes: 34 additions & 0 deletions Release/include/cpprest/ws_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,17 @@
#include <memory>
#include <mutex>

#if !defined(_WIN32) || !defined(__cplusplus_winrt)
#if defined(__clang__)
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wconversion"
#endif
#include "boost/asio/ssl.hpp"
#if defined(__clang__)
#pragma clang diagnostic pop
#endif
#endif

namespace web
{
// For backwards compatibility for when in the experimental namespace.
Expand Down Expand Up @@ -168,13 +179,36 @@ class websocket_client_config
/// caution.</remarks>
void set_validate_certificates(bool validate_certs) { m_validate_certificates = validate_certs; }

#if !defined(_WIN32) || !defined(__cplusplus_winrt)
/// <summary>
/// Sets a callback to enable custom setting of the ssl context, at construction time.
/// </summary>
/// <param name="callback">A user callback allowing for customization of the ssl context at construction
/// time.</param>
void set_ssl_context_callback(const std::function<void(boost::asio::ssl::context&)>& callback)
{
m_ssl_context_callback = callback;
}

/// <summary>
/// Gets the user's callback to allow for customization of the ssl context.
/// </summary>
const std::function<void(boost::asio::ssl::context&)>& get_ssl_context_callback() const
{
return m_ssl_context_callback;
}
#endif

private:
web::web_proxy m_proxy;
web::credentials m_credentials;
web::http::http_headers m_headers;
bool m_sni_enabled;
utf8string m_sni_hostname;
bool m_validate_certificates;
#if !defined(_WIN32) || !defined(__cplusplus_winrt)
std::function<void(boost::asio::ssl::context&)> m_ssl_context_callback;
#endif
};

/// <summary>
Expand Down
3 changes: 3 additions & 0 deletions Release/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ elseif(CPPREST_WEBSOCKETS_IMPL STREQUAL "wspp")
)
cpprest_find_websocketpp()
target_link_libraries(cpprest PRIVATE cpprestsdk_websocketpp_internal)
cpprest_find_boost()
cpprest_find_openssl()
target_link_libraries(cpprest PUBLIC cpprestsdk_boost_internal cpprestsdk_openssl_internal)
else()
message(FATAL_ERROR "Invalid implementation")
endif()
Expand Down
4 changes: 4 additions & 0 deletions Release/src/websockets/client/ws_client_wspp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,10 @@ class wspp_callback_client : public websocket_client_callback_impl,
new boost::asio::ssl::context(boost::asio::ssl::context::sslv23));
sslContext->set_default_verify_paths();
sslContext->set_options(boost::asio::ssl::context::default_workarounds);
if (m_config.get_ssl_context_callback())
{
m_config.get_ssl_context_callback()(*sslContext);
}
if (m_config.validate_certificates())
{
sslContext->set_verify_mode(boost::asio::ssl::context::verify_peer);
Expand Down

0 comments on commit ea4eff7

Please sign in to comment.