Skip to content

Commit ea4eff7

Browse files
garethsbBillyONeal
authored andcommitted
Add the missing ssl::context callback in websocket_client_config (#1049)
* 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"
1 parent fac2ff7 commit ea4eff7

File tree

3 files changed

+41
-0
lines changed

3 files changed

+41
-0
lines changed

Release/include/cpprest/ws_client.h

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,17 @@
2828
#include <memory>
2929
#include <mutex>
3030

31+
#if !defined(_WIN32) || !defined(__cplusplus_winrt)
32+
#if defined(__clang__)
33+
#pragma clang diagnostic push
34+
#pragma clang diagnostic ignored "-Wconversion"
35+
#endif
36+
#include "boost/asio/ssl.hpp"
37+
#if defined(__clang__)
38+
#pragma clang diagnostic pop
39+
#endif
40+
#endif
41+
3142
namespace web
3243
{
3344
// For backwards compatibility for when in the experimental namespace.
@@ -168,13 +179,36 @@ class websocket_client_config
168179
/// caution.</remarks>
169180
void set_validate_certificates(bool validate_certs) { m_validate_certificates = validate_certs; }
170181

182+
#if !defined(_WIN32) || !defined(__cplusplus_winrt)
183+
/// <summary>
184+
/// Sets a callback to enable custom setting of the ssl context, at construction time.
185+
/// </summary>
186+
/// <param name="callback">A user callback allowing for customization of the ssl context at construction
187+
/// time.</param>
188+
void set_ssl_context_callback(const std::function<void(boost::asio::ssl::context&)>& callback)
189+
{
190+
m_ssl_context_callback = callback;
191+
}
192+
193+
/// <summary>
194+
/// Gets the user's callback to allow for customization of the ssl context.
195+
/// </summary>
196+
const std::function<void(boost::asio::ssl::context&)>& get_ssl_context_callback() const
197+
{
198+
return m_ssl_context_callback;
199+
}
200+
#endif
201+
171202
private:
172203
web::web_proxy m_proxy;
173204
web::credentials m_credentials;
174205
web::http::http_headers m_headers;
175206
bool m_sni_enabled;
176207
utf8string m_sni_hostname;
177208
bool m_validate_certificates;
209+
#if !defined(_WIN32) || !defined(__cplusplus_winrt)
210+
std::function<void(boost::asio::ssl::context&)> m_ssl_context_callback;
211+
#endif
178212
};
179213

180214
/// <summary>

Release/src/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ elseif(CPPREST_WEBSOCKETS_IMPL STREQUAL "wspp")
6767
)
6868
cpprest_find_websocketpp()
6969
target_link_libraries(cpprest PRIVATE cpprestsdk_websocketpp_internal)
70+
cpprest_find_boost()
71+
cpprest_find_openssl()
72+
target_link_libraries(cpprest PUBLIC cpprestsdk_boost_internal cpprestsdk_openssl_internal)
7073
else()
7174
message(FATAL_ERROR "Invalid implementation")
7275
endif()

Release/src/websockets/client/ws_client_wspp.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,10 @@ class wspp_callback_client : public websocket_client_callback_impl,
189189
new boost::asio::ssl::context(boost::asio::ssl::context::sslv23));
190190
sslContext->set_default_verify_paths();
191191
sslContext->set_options(boost::asio::ssl::context::default_workarounds);
192+
if (m_config.get_ssl_context_callback())
193+
{
194+
m_config.get_ssl_context_callback()(*sslContext);
195+
}
192196
if (m_config.validate_certificates())
193197
{
194198
sslContext->set_verify_mode(boost::asio::ssl::context::verify_peer);

0 commit comments

Comments
 (0)