From f8756bb3821cae3d60250fbc6e16a2d055b5f36d Mon Sep 17 00:00:00 2001 From: BrennanConroy Date: Mon, 8 Feb 2021 15:15:18 -0800 Subject: [PATCH] [This is not production ready code] Copy cookie from negotiate to websocket --- include/signalrclient/http_client.h | 35 +++++++++++++++++++++++ src/signalrclient/connection_impl.cpp | 2 ++ src/signalrclient/default_http_client.cpp | 6 ++-- 3 files changed, 41 insertions(+), 2 deletions(-) diff --git a/include/signalrclient/http_client.h b/include/signalrclient/http_client.h index ec01720a..04778dbe 100644 --- a/include/signalrclient/http_client.h +++ b/include/signalrclient/http_client.h @@ -64,5 +64,40 @@ namespace signalr virtual void send(const std::string& url, const http_request& request, std::function callback) = 0; virtual ~http_client() {} + + void set_cookies(std::string&& cookies) + { + // TODO: validate, append, parse? + auto pos = cookies.find(';'); + if (pos != -1) + { + m_cookies = cookies.substr(0, pos); + } + else + { + m_cookies = cookies; + } + } + + void set_cookies(const std::string& cookies) + { + // TODO: validate, append, parse? + auto pos = cookies.find(';'); + if (pos != -1) + { + m_cookies = cookies.substr(0, pos); + } + else + { + m_cookies = cookies; + } + } + + const std::string& get_cookies() const noexcept + { + return m_cookies; + } + private: + std::string m_cookies; }; } diff --git a/src/signalrclient/connection_impl.cpp b/src/signalrclient/connection_impl.cpp index ae9f9427..7abea3c6 100644 --- a/src/signalrclient/connection_impl.cpp +++ b/src/signalrclient/connection_impl.cpp @@ -236,6 +236,8 @@ namespace signalr return; } + connection->m_signalr_client_config.get_http_headers()["Cookie"] = connection->m_http_client->get_cookies(); + connection->start_transport(url, [weak_connection, callback, token](std::shared_ptr transport, std::exception_ptr exception) { auto connection = weak_connection.lock(); diff --git a/src/signalrclient/default_http_client.cpp b/src/signalrclient/default_http_client.cpp index dcb3a9f9..dd519226 100644 --- a/src/signalrclient/default_http_client.cpp +++ b/src/signalrclient/default_http_client.cpp @@ -78,17 +78,19 @@ namespace signalr web::http::client::http_client client(utility::conversions::to_string_t(url)); client.request(http_request, cts.get_token()) - .then([context, callback](pplx::task response_task) + .then([context, callback, this](pplx::task response_task) { try { auto http_response = response_task.get(); auto status_code = http_response.status_code(); + auto& cookies = http_response.headers()[U("set-cookie")]; + set_cookies(utility::conversions::to_utf8string(cookies)); http_response.extract_utf8string() .then([callback, status_code](std::string response_body) { signalr::http_response response; - response.content = response_body; + response.content = std::move(response_body); response.status_code = status_code; callback(std::move(response), nullptr); });