Issue 5: Added support for custom headers

This commit is contained in:
JCash
2020-10-17 12:26:20 +02:00
parent b54e3e07ad
commit 78d527d3a8
4 changed files with 35 additions and 8 deletions

View File

@@ -54,9 +54,17 @@ static Result SendClientHandshakeHeaders(WebsocketConnection* conn)
WS_SENDALL("\r\n");
WS_SENDALL("Sec-WebSocket-Version: 13\r\n");
// Add custom protocols
// Add custom headers
if (conn->m_CustomHeaders)
{
WS_SENDALL(conn->m_CustomHeaders);
// make sure we ended with '\r\n'
int len = strlen(conn->m_CustomHeaders);
bool ended_with_sentinel = len >= 2 && conn->m_CustomHeaders[len - 2] == '\r' && conn->m_CustomHeaders[len - 1] == '\n';
if (!ended_with_sentinel)
{
WS_SENDALL("\r\n");
}
}
WS_SENDALL("\r\n");

View File

@@ -211,6 +211,8 @@ static void DestroyConnection(WebsocketConnection* conn)
WSL_Exit(conn->m_Ctx);
#endif
free((void*)conn->m_CustomHeaders);
if (conn->m_Callback)
dmScript::DestroyCallback(conn->m_Callback);
@@ -228,6 +230,7 @@ static void DestroyConnection(WebsocketConnection* conn)
free((void*)conn->m_Buffer);
delete conn;
DebugLog(2, "DestroyConnection: %p", conn);
}
@@ -266,12 +269,20 @@ static int LuaConnect(lua_State* L)
return DM_LUA_ERROR("The web socket module isn't initialized");
const char* url = luaL_checkstring(L, 1);
lua_Number timeout = dmScript::CheckTableNumber(L, 2, "timeout", 3000);
const char* custom_headers = dmScript::CheckTableString(L, 2, "headers", 0);
if (custom_headers != 0)
{
if (strstr(custom_headers, "\r\n\r\n") != 0)
{
return DM_LUA_ERROR("The header field must not contain double '\\r\\n\\r\\n': '%s'", custom_headers);
}
}
WebsocketConnection* conn = CreateConnection(url);
// milliseconds
lua_Number timeout = dmScript::CheckTableNumber(L, 2, "timeout", 3000);
conn->m_ConnectTimeout = dmTime::GetTime() + timeout * 1000;
conn->m_CustomHeaders = custom_headers ? strdup(custom_headers) : 0;
conn->m_Callback = dmScript::CreateCallback(L, 3);

View File

@@ -91,6 +91,7 @@ namespace dmWebsocket
dmArray<Message> m_Messages; // lengths of the messages in the data buffer
uint64_t m_ConnectTimeout;
uint8_t m_Key[16];
const char* m_CustomHeaders;
State m_State;
char* m_Buffer;
int m_BufferSize;