Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,8 @@ headers["foo"] = "bar";
webSocket.setExtraHeaders(headers);
```

You may also prevent header fields from being sent - like User-Agent or Origin - by setting the corresponding HTTP header fields to "".

### Subprotocols

You can specify subprotocols to be set during the WebSocket handshake. For more info you can refer to [this doc](https://hpbn.co/websocket/#subprotocol-negotiation).
Expand Down
1 change: 1 addition & 0 deletions ixwebsocket/IXWebSocketHandshake.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ namespace ix

for (auto& it : extraHeaders)
{
if (it.second.empty()) continue;
ss << it.first << ": " << it.second << "\r\n";
}

Expand Down
21 changes: 17 additions & 4 deletions test/IXWebSocketHostTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@

using namespace ix;

bool startServer(ix::WebSocketServer& server, std::string& subProtocols)
bool startServer(ix::WebSocketServer& server, std::string& subProtocols,
WebSocketHttpHeaders& receivedHeaders)
{
server.setOnClientMessageCallback(
[&server, &subProtocols](std::shared_ptr<ConnectionState> connectionState,
[&server, &subProtocols, &receivedHeaders](std::shared_ptr<ConnectionState> connectionState,
WebSocket& webSocket,
const ix::WebSocketMessagePtr& msg) {
auto remoteIp = connectionState->getRemoteIp();
Expand All @@ -34,6 +35,7 @@ bool startServer(ix::WebSocketServer& server, std::string& subProtocols)
}

subProtocols = msg->openInfo.headers["Sec-WebSocket-Protocol"];
receivedHeaders = msg->openInfo.headers;
}
else if (msg->type == ix::WebSocketMessageType::Close)
{
Expand Down Expand Up @@ -62,12 +64,14 @@ bool startServer(ix::WebSocketServer& server, std::string& subProtocols)
return true;
}

void runTest(int port, const ix::WebSocketHttpHeaders & headers)
WebSocketHttpHeaders runTest(int port, const ix::WebSocketHttpHeaders & headers)
{
ix::WebSocketServer server(port);

std::string subProtocols;
startServer(server, subProtocols);
WebSocketHttpHeaders receivedHeaders;

startServer(server, subProtocols, receivedHeaders);

std::atomic<bool> connected(false);
ix::WebSocket webSocket;
Expand Down Expand Up @@ -107,6 +111,7 @@ void runTest(int port, const ix::WebSocketHttpHeaders & headers)
server.stop();

REQUIRE(subProtocols == "json,msgpack");
return receivedHeaders;
}


Expand Down Expand Up @@ -141,4 +146,12 @@ TEST_CASE("host", "[websocket_host]")
int port = getFreePort();
runTest(port, {{"Host", "localhost"}});
}

SECTION("Connect with User-Agent and Origin header removed")
{
int port = getFreePort();
WebSocketHttpHeaders received = runTest(port, {{"User-Agent", ""}, {"Origin", ""}});
CHECK(received.count("User-Agent") == 0);
CHECK(received.count("Origin") == 0);
}
}