Add client handshake extra headers (#105)

Even though 6455 defines all the necessary headers needed for
client/server handshake, in practice most of the cases websocket servers
expect few more headers. Therefore adding this functionality.
This commit is contained in:
ozychhi 2019-08-26 11:37:40 -05:00 committed by Benjamin Sergeant
parent ff75846d2d
commit 93debc00dc
6 changed files with 34 additions and 12 deletions

View File

@ -70,6 +70,11 @@ namespace ix
std::lock_guard<std::mutex> lock(_configMutex);
_url = url;
}
void WebSocket::setExtraHeaders(const std::unordered_map<std::string, std::string>& headers)
{
std::lock_guard<std::mutex> lock(_configMutex);
_extraHeaders = headers;
}
const std::string& WebSocket::getUrl() const
{
@ -176,7 +181,7 @@ namespace ix
_pingTimeoutSecs);
}
WebSocketInitResult status = _ws.connectToUrl(_url, timeoutSecs);
WebSocketInitResult status = _ws.connectToUrl(_url, _extraHeaders, timeoutSecs);
if (!status.success)
{
return status;

View File

@ -21,6 +21,7 @@
#include <mutex>
#include <string>
#include <thread>
#include <unordered_map>
namespace ix
{
@ -44,6 +45,8 @@ namespace ix
~WebSocket();
void setUrl(const std::string& url);
void setExtraHeaders(const std::unordered_map<std::string, std::string>&
headers); // send extra headers in client handshake request
void setPerMessageDeflateOptions(
const WebSocketPerMessageDeflateOptions& perMessageDeflateOptions);
void setHeartBeatPeriod(int heartBeatPeriodSecs);
@ -111,6 +114,8 @@ namespace ix
WebSocketTransport _ws;
std::string _url;
std::unordered_map<std::string, std::string> _extraHeaders;
WebSocketPerMessageDeflateOptions _perMessageDeflateOptions;
mutable std::mutex _configMutex; // protect all config variables access

View File

@ -88,6 +88,7 @@ namespace ix
}
WebSocketInitResult WebSocketHandshake::clientHandshake(const std::string& url,
const std::unordered_map<std::string, std::string>& extraHeaders,
const std::string& host,
const std::string& path,
int port,
@ -127,6 +128,9 @@ namespace ix
ss << "Sec-WebSocket-Version: 13\r\n";
ss << "Sec-WebSocket-Key: " << secWebSocketKey << "\r\n";
for (auto& it : extraHeaders) {
ss << it.first << ":" << it.second << "\r\n";
}
if (_enablePerMessageDeflate)
{
ss << _perMessageDeflateOptions.generateHeader();

View File

@ -16,6 +16,7 @@
#include <memory>
#include <string>
#include <tuple>
#include <unordered_map>
namespace ix
{
@ -50,11 +51,13 @@ namespace ix
WebSocketPerMessageDeflateOptions& perMessageDeflateOptions,
std::atomic<bool>& enablePerMessageDeflate);
WebSocketInitResult clientHandshake(const std::string& url,
const std::string& host,
const std::string& path,
int port,
int timeoutSecs);
WebSocketInitResult clientHandshake(
const std::string& url,
const std::unordered_map<std::string, std::string>& extraHeaders,
const std::string& host,
const std::string& path,
int port,
int timeoutSecs);
WebSocketInitResult serverHandshake(int fd, int timeoutSecs);

View File

@ -127,8 +127,10 @@ namespace ix
}
// Client
WebSocketInitResult WebSocketTransport::connectToUrl(const std::string& url,
int timeoutSecs)
WebSocketInitResult WebSocketTransport::connectToUrl(
const std::string& url,
const std::unordered_map<std::string, std::string>& headers,
int timeoutSecs)
{
std::lock_guard<std::mutex> lock(_socketMutex);
@ -156,8 +158,8 @@ namespace ix
_perMessageDeflateOptions,
_enablePerMessageDeflate);
auto result = webSocketHandshake.clientHandshake(url, host, path, port,
timeoutSecs);
auto result = webSocketHandshake.clientHandshake(url, headers, host, path,
port, timeoutSecs);
if (result.success)
{
setReadyState(ReadyState::OPEN);

View File

@ -24,6 +24,7 @@
#include <memory>
#include <mutex>
#include <string>
#include <unordered_map>
#include <vector>
namespace ix
@ -75,8 +76,10 @@ namespace ix
int pingIntervalSecs,
int pingTimeoutSecs);
WebSocketInitResult connectToUrl(const std::string& url, // Client
int timeoutSecs);
WebSocketInitResult connectToUrl( // Client
const std::string& url,
const std::unordered_map<std::string, std::string>& headers,
int timeoutSecs);
WebSocketInitResult connectToSocket(int fd, // Server
int timeoutSecs);