Skip to content

Commit 57edf17

Browse files
authored
Merge pull request #6 from VerityIncorporated/main
Fix: Store and expose WebSocket headers for non-native clients
2 parents 5196fb0 + 6738815 commit 57edf17

2 files changed

Lines changed: 29 additions & 7 deletions

File tree

src/ws_server.cpp

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,14 @@ void WebSocketServer::OnMessage(const std::string& message, std::shared_ptr<ix::
5959
if (!pWebSocketClient->m_websocket_handle) return;
6060
pWebSocketClient->m_keepConnecting = true;
6161

62+
{
63+
std::lock_guard<std::mutex> lock(m_headersMutex);
64+
auto it = m_connectionHeaders.find(connectionState->getId());
65+
if (it != m_connectionHeaders.end()) {
66+
pWebSocketClient->m_headers = it->second;
67+
}
68+
}
69+
6270
std::string remoteAddress = connectionState->getRemoteIp() + ":" + std::to_string(connectionState->getRemotePort());
6371
pMessageForward->PushCell(m_webSocketServer_handle);
6472
pMessageForward->PushCell(pWebSocketClient->m_websocket_handle);
@@ -79,6 +87,11 @@ void WebSocketServer::OnOpen(ix::WebSocketOpenInfo openInfo, std::shared_ptr<ix:
7987
return;
8088
}
8189

90+
{
91+
std::lock_guard<std::mutex> lock(m_headersMutex);
92+
m_connectionHeaders[connectionState->getId()] = openInfo.headers;
93+
}
94+
8295
g_TaskQueue.Push([this, openInfo, connectionState]()
8396
{
8497
std::string remoteAddress = connectionState->getRemoteIp() + ":" + std::to_string(connectionState->getRemotePort());
@@ -95,6 +108,13 @@ void WebSocketServer::OnClose(ix::WebSocketCloseInfo closeInfo, std::shared_ptr<
95108
{
96109
return;
97110
}
111+
112+
std::string connectionId = connectionState->getId();
113+
114+
{
115+
std::lock_guard<std::mutex> lock(m_headersMutex);
116+
m_connectionHeaders.erase(connectionId);
117+
}
98118

99119
g_TaskQueue.Push([this, closeInfo, connectionState]()
100120
{
@@ -162,13 +182,13 @@ bool WebSocketServer::disconnectClient(const std::string& clientId) {
162182
}
163183

164184
std::vector<std::string> WebSocketServer::getClientIds() {
165-
std::vector<std::string> clientIds;
185+
std::vector<std::string> clientIds;
166186

167-
auto clients = m_webSocketServer.getClients();
187+
auto clients = m_webSocketServer.getClients();
168188

169-
for (const auto& client : clients) {
170-
clientIds.push_back(client.second);
171-
}
189+
for (const auto& client : clients) {
190+
clientIds.push_back(client.second);
191+
}
172192

173-
return clientIds;
193+
return clientIds;
174194
}

src/ws_server.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#include "extension.h"
22

3-
43
class WebSocketServer
54
{
65
public:
@@ -20,6 +19,9 @@ class WebSocketServer
2019
ix::WebSocketServer m_webSocketServer;
2120
Handle_t m_webSocketServer_handle = BAD_HANDLE;
2221

22+
std::mutex m_headersMutex;
23+
std::unordered_map<std::string, ix::WebSocketHttpHeaders> m_connectionHeaders;
24+
2325
IChangeableForward *pMessageForward = nullptr;
2426
IChangeableForward *pOpenForward = nullptr;
2527
IChangeableForward *pCloseForward = nullptr;

0 commit comments

Comments
 (0)