1
0
mirror of https://github.com/weechat/weechat.git synced 2026-07-01 07:16:37 +02:00

relay/api: use cjson lib to build JSON body of handshake request

This commit is contained in:
Sébastien Helleu
2025-01-07 07:18:01 +01:00
parent 10b4fffaca
commit d10af1037b
2 changed files with 29 additions and 21 deletions
@@ -1385,26 +1385,35 @@ relay_remote_network_url_handshake_cb (const void *pointer,
char *
relay_remote_network_get_handshake_request ()
{
char **body;
cJSON *json, *json_algos;
char *result;
int i;
body = weechat_string_dyn_alloc (256);
if (!body)
json = cJSON_CreateObject ();
if (!json)
return NULL;
weechat_string_dyn_concat (body, "{\"password_hash_algo\": [", -1);
json_algos = cJSON_CreateArray ();
if (!json_algos)
{
cJSON_Delete (json);
return NULL;
}
/* all password hash algorithms are supported */
for (i = 0; i < RELAY_NUM_PASSWORD_HASH_ALGOS; i++)
{
if (i > 0)
weechat_string_dyn_concat (body, ", ", -1);
weechat_string_dyn_concat (body, "\"", -1);
weechat_string_dyn_concat (body,
relay_auth_password_hash_algo_name[i], -1);
weechat_string_dyn_concat (body, "\"", -1);
cJSON_AddItemToArray (
json_algos,
cJSON_CreateString (relay_auth_password_hash_algo_name[i]));
}
weechat_string_dyn_concat (body, "]}", -1);
return weechat_string_dyn_free (body, 0);
cJSON_AddItemToObject (json, "password_hash_algo", json_algos);
result = cJSON_PrintUnformatted (json);
cJSON_Delete (json);
return result;
}
/*
@@ -232,17 +232,16 @@ TEST(RelayRemoteNetwork, UrlHandshakeCb)
TEST(RelayRemoteNetwork, GetHandshakeRequest)
{
const char *str_start = "{\"password_hash_algo\": [\"";
char *str;
int i;
str = relay_remote_network_get_handshake_request ();
CHECK(str);
STRNCMP_EQUAL(str_start, str, strlen (str_start));
for (i = 0; i < RELAY_NUM_PASSWORD_HASH_ALGOS; i++)
{
CHECK(strstr (str, relay_auth_password_hash_algo_name[i]));
}
WEE_TEST_STR("{\"password_hash_algo\":["
"\"plain\","
"\"sha256\","
"\"sha512\","
"\"pbkdf2+sha256\","
"\"pbkdf2+sha512\""
"]}",
relay_remote_network_get_handshake_request ());
}
/*