Skip to content

Commit

Permalink
fix(ssl): specify SSL paths for both requests/websockets for unix sys…
Browse files Browse the repository at this point in the history
…tems
  • Loading branch information
ADRFranklin committed Nov 14, 2022
1 parent 39bf0c8 commit 32f2829
Show file tree
Hide file tree
Showing 2 changed files with 187 additions and 122 deletions.
146 changes: 101 additions & 45 deletions src/impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,20 @@ int Impl::websocketClientsTableCounter = 0;
int Impl::RequestsClient(std::string endpoint, int headers)
{
int id = clientsTableCounter++;
try {
http_client* client = new http_client(utility::conversions::to_string_t(endpoint));
clientsTable[id] = { client, headersTable[headers] };
} catch (std::exception& e) {
try
{
http_client_config config;
#ifndef _WIN32
// TODO: in the future allow the user to specify options to configure
// TODO: the client with
config.set_ssl_context_callback([](boost::asio::ssl::context &ctx)
{ ctx.load_verify_file("/etc/ssl/certs/ca-certificates.crt"); });
#endif
http_client *client = new http_client(utility::conversions::to_string_t(endpoint), config);
clientsTable[id] = {client, headersTable[headers]};
}
catch (std::exception &e)
{
logprintf("ERROR: Failed to create new HTTP client: %s", e.what());
id = -1;
}
Expand All @@ -34,7 +44,7 @@ int Impl::RequestHeaders(std::vector<std::pair<std::string, std::string>> header
return id;
}

int Impl::Request(AMX* amx, int id, std::string path, E_HTTP_METHOD method, std::string callback, char* data, int headers)
int Impl::Request(AMX *amx, int id, std::string path, E_HTTP_METHOD method, std::string callback, char *data, int headers)
{
RequestData requestData;
requestData.amx = amx;
Expand All @@ -47,13 +57,14 @@ int Impl::Request(AMX* amx, int id, std::string path, E_HTTP_METHOD method, std:
requestData.bodyString = data;

int ret = doRequest(id, requestData);
if (ret < 0) {
if (ret < 0)
{
return ret;
}
return requestCounter++;
}

int Impl::RequestJSON(AMX* amx, int id, std::string path, E_HTTP_METHOD method, std::string callback, web::json::value json, int headers)
int Impl::RequestJSON(AMX *amx, int id, std::string path, E_HTTP_METHOD method, std::string callback, web::json::value json, int headers)
{
RequestData requestData;
requestData.amx = amx;
Expand All @@ -66,7 +77,8 @@ int Impl::RequestJSON(AMX* amx, int id, std::string path, E_HTTP_METHOD method,
requestData.bodyJson = json;

int ret = doRequest(id, requestData);
if (ret < 0) {
if (ret < 0)
{
return ret;
}
return requestCounter++;
Expand All @@ -80,16 +92,20 @@ int Impl::headersCleanup(int id)

int Impl::doRequest(int id, RequestData requestData)
{
if (clientsTable.find(id) == clientsTable.end()) {
if (clientsTable.find(id) == clientsTable.end())
{
logprintf("ERROR: invalid client ID %d used", id);
return -1;
}
ClientData cd = clientsTable[id];

try {
try
{
std::thread t(doRequestWithClient, cd, requestData);
t.detach();
} catch (std::exception e) {
}
catch (std::exception e)
{
logprintf("ERROR: failed to dispatch request thread: '%s'", e.what());
return -2;
}
Expand All @@ -105,25 +121,36 @@ void Impl::doRequestWithClient(ClientData cd, RequestData requestData)
responseData.callback = requestData.callback;
responseData.responseType = E_CONTENT_TYPE::empty;

try {
try
{
doRequestSync(cd, requestData, responseData);
} catch (http::http_exception e) {
}
catch (http::http_exception e)
{
logprintf("ERROR: HTTP error %s", e.what());
responseData.callback = "OnRequestFailure";
responseData.rawBody = e.what();
responseData.status = 1;
} catch (std::exception e) {
}
catch (std::exception e)
{
logprintf("ERROR: General error %s", e.what());
responseData.callback = "OnRequestFailure";
responseData.rawBody = e.what();
responseData.status = 2;
} catch (...) {
try {
}
catch (...)
{
try
{
auto eptr = std::current_exception();
if (eptr) {
if (eptr)
{
std::rethrow_exception(eptr);
}
} catch (const std::exception& e) {
}
catch (const std::exception &e)
{
logprintf("ERROR: Unknown error %s", e.what());
responseData.callback = "OnRequestFailure";
responseData.rawBody = e.what();
Expand All @@ -136,30 +163,36 @@ void Impl::doRequestWithClient(ClientData cd, RequestData requestData)
responseQueueLock.unlock();
}

void Impl::doRequestSync(ClientData cd, RequestData requestData, ResponseData& responseData)
void Impl::doRequestSync(ClientData cd, RequestData requestData, ResponseData &responseData)
{
http_request request(methodName(requestData.method));
for (auto h : cd.headers) {
for (auto h : cd.headers)
{
request.headers().add(
utility::conversions::to_string_t(h.first),
utility::conversions::to_string_t(h.second));
}
for (auto h : headersTable[requestData.headers]) {
for (auto h : headersTable[requestData.headers])
{
request.headers().add(
utility::conversions::to_string_t(h.first),
utility::conversions::to_string_t(h.second));
}
request.set_request_uri(utility::conversions::to_string_t(requestData.path));

switch (requestData.requestType) {
case E_CONTENT_TYPE::json: {
if (!requestData.bodyJson.is_null()) {
switch (requestData.requestType)
{
case E_CONTENT_TYPE::json:
{
if (!requestData.bodyJson.is_null())
{
request.set_body(requestData.bodyJson);
}
request.headers().set_content_type(U("application/json"));
break;
}
case E_CONTENT_TYPE::string: {
case E_CONTENT_TYPE::string:
{
request.set_body(requestData.bodyString);
break;
}
Expand All @@ -175,7 +208,8 @@ void Impl::doRequestSync(ClientData cd, RequestData requestData, ResponseData& r

web::http::method Impl::methodName(E_HTTP_METHOD id)
{
switch (id) {
switch (id)
{
case E_HTTP_METHOD::HTTP_METHOD_GET:
return utility::conversions::to_string_t("GET");
case E_HTTP_METHOD::HTTP_METHOD_HEAD:
Expand Down Expand Up @@ -203,9 +237,11 @@ std::vector<Impl::ResponseData> Impl::gatherResponses()
std::vector<ResponseData> tasks;

// if we can't lock the mutex, don't block, just return and try next tick
if (responseQueueLock.try_lock()) {
if (responseQueueLock.try_lock())
{
ResponseData response;
while (!responseQueue.empty()) {
while (!responseQueue.empty())
{
response = responseQueue.top();
tasks.push_back(response);
responseQueue.pop();
Expand All @@ -220,16 +256,25 @@ int Impl::WebSocketClient(std::string address, std::string callback)
{
int id = websocketClientsTableCounter++;

websocket_callback_client* client = new websocket_callback_client();
if (client == nullptr) {
websocket_client_config wcc;
#ifndef _WIN32
wcc.set_ssl_context_callback([](boost::asio::ssl::context &ctx)
{ ctx.load_verify_file("/etc/ssl/certs/ca-certificates.crt"); });
#endif
websocket_callback_client *client = new websocket_callback_client(wcc);
if (client == nullptr)
{
return -1;
}

WebSocketClientData wsc = { id, client, address, callback, false };
WebSocketClientData wsc = {id, client, address, callback, false};
websocketClientsTable[id] = wsc;
try {
try
{
startWebSocketListener(wsc);
} catch (std::exception& e) {
}
catch (std::exception &e)
{
logprintf("ERROR: WebSocketClient failed: %s", e.what());
return -1;
}
Expand All @@ -240,9 +285,12 @@ int Impl::WebSocketClient(std::string address, std::string callback)
int Impl::WebSocketSend(int id, std::string data)
{
WebSocketClientData wsc;
try {
try
{
wsc = websocketClientsTable[id];
} catch (std::exception e) {
}
catch (std::exception e)
{
return -1;
}

Expand All @@ -257,16 +305,20 @@ int Impl::JsonWebSocketClient(std::string address, std::string callback)
{
int id = websocketClientsTableCounter++;

websocket_callback_client* client = new websocket_callback_client();
if (client == nullptr) {
websocket_callback_client *client = new websocket_callback_client();
if (client == nullptr)
{
return -1;
}

WebSocketClientData wsc = { id, client, address, callback, true };
WebSocketClientData wsc = {id, client, address, callback, true};
websocketClientsTable[id] = wsc;
try {
try
{
startWebSocketListener(wsc);
} catch (std::exception& e) {
}
catch (std::exception &e)
{
logprintf("ERROR: JsonWebSocketClient failed: %s", e.what());
return -1;
}
Expand All @@ -277,9 +329,12 @@ int Impl::JsonWebSocketClient(std::string address, std::string callback)
int Impl::JsonWebSocketSend(int id, web::json::value json)
{
WebSocketClientData wsc;
try {
try
{
wsc = websocketClientsTable[id];
} catch (std::exception e) {
}
catch (std::exception e)
{
return -1;
}

Expand All @@ -292,7 +347,8 @@ int Impl::JsonWebSocketSend(int id, web::json::value json)

void Impl::startWebSocketListener(WebSocketClientData wsc)
{
wsc.client->set_message_handler([wsc](const websocket_incoming_message& msg) -> void {
wsc.client->set_message_handler([wsc](const websocket_incoming_message &msg) -> void
{
std::string raw = msg.extract_string().get();

ResponseData responseData;
Expand All @@ -304,7 +360,7 @@ void Impl::startWebSocketListener(WebSocketClientData wsc)

responseQueueLock.lock();
responseQueue.push(responseData);
responseQueueLock.unlock();
});
responseQueueLock.unlock(); });

wsc.client->connect(utility::conversions::to_string_t(wsc.address)).wait();
}
Loading

0 comments on commit 32f2829

Please sign in to comment.