From 65e917c4a01d8607819420e46f28492cba7a45ed Mon Sep 17 00:00:00 2001 From: Mathy Vanvoorden Date: Tue, 14 Nov 2017 23:15:33 +0100 Subject: [PATCH 1/3] Make it possible to set the user agent for websockets --- Release/include/cpprest/ws_client.h | 19 +++++++++++++++++++ .../src/websockets/client/ws_client_wspp.cpp | 7 +++++++ 2 files changed, 26 insertions(+) diff --git a/Release/include/cpprest/ws_client.h b/Release/include/cpprest/ws_client.h index 9c9e5d818d..c08d8d9265 100644 --- a/Release/include/cpprest/ws_client.h +++ b/Release/include/cpprest/ws_client.h @@ -150,6 +150,24 @@ class websocket_client_config return m_sni_hostname; } + /// + /// Sets the User Agent to be used for the connection + /// + /// The User Agent to use, as a string. + void set_user_agent(const utf8string &user_agent) + { + m_user_agent = user_agent; + } + + /// + /// Gets the User Agent to be used for the connection + /// + /// User Agent as a string. + const utf8string & user_agent() const + { + return m_user_agent; + } + /// /// Gets the headers of the HTTP request message used in the WebSocket protocol handshake. /// @@ -206,6 +224,7 @@ class websocket_client_config bool m_sni_enabled; utf8string m_sni_hostname; bool m_validate_certificates; + utf8string m_user_agent; }; /// diff --git a/Release/src/websockets/client/ws_client_wspp.cpp b/Release/src/websockets/client/ws_client_wspp.cpp index e9e7658b18..5a851b0675 100644 --- a/Release/src/websockets/client/ws_client_wspp.cpp +++ b/Release/src/websockets/client/ws_client_wspp.cpp @@ -312,6 +312,13 @@ class wspp_callback_client : public websocket_client_callback_impl, public std:: shutdown_wspp_impl(con_hdl, false); }); + // Set User Agent specified by the user. This needs to happen before any connection is created + const auto & user_agent = m_config.user_agent(); + if (!user_agent.empty()) + { + client.set_user_agent(user_agent); + } + // Get the connection handle to save for later, have to create temporary // because type erasure occurs with connection_hdl. websocketpp::lib::error_code ec; From 1621b36c328dc01677e16158512ea6382756ec88 Mon Sep 17 00:00:00 2001 From: Robert Schumacher Date: Wed, 24 Jan 2018 08:49:18 -0800 Subject: [PATCH 2/3] Use headers() to communicate the user agent for websockets. --- Release/include/cpprest/ws_client.h | 15 +-------------- Release/src/websockets/client/ws_client_wspp.cpp | 9 +++++---- Release/src/websockets/client/ws_msg.cpp | 6 ++++++ 3 files changed, 12 insertions(+), 18 deletions(-) diff --git a/Release/include/cpprest/ws_client.h b/Release/include/cpprest/ws_client.h index c08d8d9265..9a324cde51 100644 --- a/Release/include/cpprest/ws_client.h +++ b/Release/include/cpprest/ws_client.h @@ -154,19 +154,7 @@ class websocket_client_config /// Sets the User Agent to be used for the connection /// /// The User Agent to use, as a string. - void set_user_agent(const utf8string &user_agent) - { - m_user_agent = user_agent; - } - - /// - /// Gets the User Agent to be used for the connection - /// - /// User Agent as a string. - const utf8string & user_agent() const - { - return m_user_agent; - } + _ASYNCRTIMP void set_user_agent(const utf8string &user_agent); /// /// Gets the headers of the HTTP request message used in the WebSocket protocol handshake. @@ -224,7 +212,6 @@ class websocket_client_config bool m_sni_enabled; utf8string m_sni_hostname; bool m_validate_certificates; - utf8string m_user_agent; }; /// diff --git a/Release/src/websockets/client/ws_client_wspp.cpp b/Release/src/websockets/client/ws_client_wspp.cpp index 5a851b0675..7c3f2043bc 100644 --- a/Release/src/websockets/client/ws_client_wspp.cpp +++ b/Release/src/websockets/client/ws_client_wspp.cpp @@ -313,10 +313,12 @@ class wspp_callback_client : public websocket_client_callback_impl, public std:: }); // Set User Agent specified by the user. This needs to happen before any connection is created - const auto & user_agent = m_config.user_agent(); - if (!user_agent.empty()) + const auto& headers = m_config.headers(); + + auto user_agent_it = headers.find(web::http::header_names::user_agent); + if (user_agent_it != headers.end()) { - client.set_user_agent(user_agent); + client.set_user_agent(utility::conversions::to_utf8string(user_agent_it->second)); } // Get the connection handle to save for later, have to create temporary @@ -330,7 +332,6 @@ class wspp_callback_client : public websocket_client_callback_impl, public std:: } // Add any request headers specified by the user. - const auto & headers = m_config.headers(); for (const auto & header : headers) { if (!utility::details::str_icmp(header.first, g_subProtocolHeader)) diff --git a/Release/src/websockets/client/ws_msg.cpp b/Release/src/websockets/client/ws_msg.cpp index 10bce338e6..46bae706bf 100644 --- a/Release/src/websockets/client/ws_msg.cpp +++ b/Release/src/websockets/client/ws_msg.cpp @@ -28,6 +28,12 @@ namespace client { static ::utility::string_t g_subProtocolHeader = _XPLATSTR("Sec-WebSocket-Protocol"); + +void websocket_client_config::set_user_agent(const utf8string &user_agent) +{ + headers().add(web::http::header_names::user_agent, utility::conversions::to_utf16string(user_agent)); +} + void websocket_client_config::add_subprotocol(const ::utility::string_t &name) { m_headers.add(g_subProtocolHeader, name); From 17bd2d29bc798d45b8af95f325612690053f8239 Mon Sep 17 00:00:00 2001 From: Robert Schumacher Date: Wed, 24 Jan 2018 11:15:13 -0800 Subject: [PATCH 3/3] Fix set_user_agent on Linux; use to_string_t. --- Release/src/websockets/client/ws_msg.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Release/src/websockets/client/ws_msg.cpp b/Release/src/websockets/client/ws_msg.cpp index 46bae706bf..1d24409c67 100644 --- a/Release/src/websockets/client/ws_msg.cpp +++ b/Release/src/websockets/client/ws_msg.cpp @@ -31,7 +31,7 @@ static ::utility::string_t g_subProtocolHeader = _XPLATSTR("Sec-WebSocket-Protoc void websocket_client_config::set_user_agent(const utf8string &user_agent) { - headers().add(web::http::header_names::user_agent, utility::conversions::to_utf16string(user_agent)); + headers().add(web::http::header_names::user_agent, utility::conversions::to_string_t(user_agent)); } void websocket_client_config::add_subprotocol(const ::utility::string_t &name)