Merge pull request #15 from defold/issue-5-custom-headers

Issue 5: Added support for custom headers
This commit is contained in:
Mathias Westerdahl 2020-10-17 14:02:21 +02:00 committed by GitHub
commit ea2fe97943
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 35 additions and 8 deletions

View File

@ -19,7 +19,11 @@
members: members:
- name: timeout - name: timeout
type: number type: number
desc: Timeout for the connection sequence (milliseconds). Default: 3000 desc: Timeout for the connection sequence (milliseconds). Not used on HTML5. Default: 3000
- name: headers
type: string
desc: list of http headers. Each pair is separated with "\r\n". Not used on HTML5.
- name: callback - name: callback
type: function type: function
@ -83,7 +87,10 @@
function init(self) function init(self)
self.url = "ws://echo.websocket.org" self.url = "ws://echo.websocket.org"
local params = {} local params = {
timeout = 3000,
headers = "Sec-WebSocket-Protocol: chat\r\nOrigin: mydomain.com\r\n"
}
self.connection = websocket.connect(self.url, params, websocket_callback) self.connection = websocket.connect(self.url, params, websocket_callback)
end end

View File

@ -54,9 +54,17 @@ static Result SendClientHandshakeHeaders(WebsocketConnection* conn)
WS_SENDALL("\r\n"); WS_SENDALL("\r\n");
WS_SENDALL("Sec-WebSocket-Version: 13\r\n"); WS_SENDALL("Sec-WebSocket-Version: 13\r\n");
// Add custom protocols if (conn->m_CustomHeaders)
{
// Add custom headers 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"); WS_SENDALL("\r\n");

View File

@ -211,6 +211,8 @@ static void DestroyConnection(WebsocketConnection* conn)
WSL_Exit(conn->m_Ctx); WSL_Exit(conn->m_Ctx);
#endif #endif
free((void*)conn->m_CustomHeaders);
if (conn->m_Callback) if (conn->m_Callback)
dmScript::DestroyCallback(conn->m_Callback); dmScript::DestroyCallback(conn->m_Callback);
@ -228,6 +230,7 @@ static void DestroyConnection(WebsocketConnection* conn)
free((void*)conn->m_Buffer); free((void*)conn->m_Buffer);
delete conn; 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"); return DM_LUA_ERROR("The web socket module isn't initialized");
const char* url = luaL_checkstring(L, 1); 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); WebsocketConnection* conn = CreateConnection(url);
// milliseconds
lua_Number timeout = dmScript::CheckTableNumber(L, 2, "timeout", 3000);
conn->m_ConnectTimeout = dmTime::GetTime() + timeout * 1000; conn->m_ConnectTimeout = dmTime::GetTime() + timeout * 1000;
conn->m_CustomHeaders = custom_headers ? strdup(custom_headers) : 0;
conn->m_Callback = dmScript::CreateCallback(L, 3); 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 dmArray<Message> m_Messages; // lengths of the messages in the data buffer
uint64_t m_ConnectTimeout; uint64_t m_ConnectTimeout;
uint8_t m_Key[16]; uint8_t m_Key[16];
const char* m_CustomHeaders;
State m_State; State m_State;
char* m_Buffer; char* m_Buffer;
int m_BufferSize; int m_BufferSize;