1
0
mirror of https://github.com/weechat/weechat.git synced 2026-06-26 12:56:37 +02:00

relay: remove request from some function arguments (issue #2066)

This commit is contained in:
Sébastien Helleu
2024-01-31 08:57:20 +01:00
parent 83567fd871
commit 89aeb03a13
7 changed files with 91 additions and 122 deletions
+39 -51
View File
@@ -276,9 +276,6 @@ RELAY_API_PROTOCOL_CALLBACK(version)
char *version, *error;
long number;
/* make C compiler happy */
(void) request;
json = cJSON_CreateObject ();
if (!json)
return WEECHAT_RC_ERROR;
@@ -337,31 +334,31 @@ RELAY_API_PROTOCOL_CALLBACK(buffers)
enum t_relay_api_colors colors;
ptr_buffer = NULL;
if (request->num_path_items > 2)
if (client->http_req->num_path_items > 2)
{
ptr_buffer = weechat_buffer_search ("==", request->path_items[2]);
ptr_buffer = weechat_buffer_search ("==", client->http_req->path_items[2]);
if (!ptr_buffer)
{
relay_api_msg_send_error_json (client, RELAY_HTTP_404_NOT_FOUND, NULL,
"Buffer \"%s\" not found",
request->path_items[2]);
client->http_req->path_items[2]);
return WEECHAT_RC_OK;
}
}
nicks = relay_api_protocol_get_param_boolean (request, "nicks", 0);
nicks = relay_api_protocol_get_param_boolean (client->http_req, "nicks", 0);
colors = relay_api_search_colors (
weechat_hashtable_get (request->params, "colors"));
weechat_hashtable_get (client->http_req->params, "colors"));
if (request->num_path_items > 3)
if (client->http_req->num_path_items > 3)
{
/* sub-resource of buffers */
if (strcmp (request->path_items[3], "lines") == 0)
if (strcmp (client->http_req->path_items[3], "lines") == 0)
{
lines = relay_api_protocol_get_param_long (request, "lines", -100L);
lines = relay_api_protocol_get_param_long (client->http_req, "lines", -100L);
json = relay_api_msg_lines_to_json (ptr_buffer, lines, colors);
}
else if (strcmp (request->path_items[3], "nicks") == 0)
else if (strcmp (client->http_req->path_items[3], "nicks") == 0)
{
json = relay_api_msg_nick_group_to_json (
weechat_hdata_pointer (relay_hdata_buffer,
@@ -372,13 +369,13 @@ RELAY_API_PROTOCOL_CALLBACK(buffers)
relay_api_msg_send_error_json (
client, RELAY_HTTP_404_NOT_FOUND, NULL,
"Sub-resource of buffers not found: \"%s\"",
request->path_items[3]);
client->http_req->path_items[3]);
return WEECHAT_RC_OK;
}
}
else
{
lines = relay_api_protocol_get_param_long (request, "lines", 0L);
lines = relay_api_protocol_get_param_long (client->http_req, "lines", 0L);
if (ptr_buffer)
{
json = relay_api_msg_buffer_to_json (ptr_buffer, lines, nicks, colors);
@@ -430,7 +427,7 @@ RELAY_API_PROTOCOL_CALLBACK(input)
struct t_gui_buffer *ptr_buffer;
struct t_hashtable *options;
json_body = cJSON_Parse(request->body);
json_body = cJSON_Parse(client->http_req->body);
if (!json_body)
return WEECHAT_RC_ERROR;
@@ -522,7 +519,7 @@ RELAY_API_PROTOCOL_CALLBACK(ping)
const char *ptr_data;
ptr_data = NULL;
json_body = cJSON_Parse(request->body);
json_body = cJSON_Parse(client->http_req->body);
if (json_body)
{
json_data = cJSON_GetObjectItem (json_body, "data");
@@ -577,7 +574,7 @@ RELAY_API_PROTOCOL_CALLBACK(sync)
RELAY_API_DATA(client, sync_nicks) = 1;
RELAY_API_DATA(client, sync_colors) = RELAY_API_COLORS_ANSI;
json_body = cJSON_Parse(request->body);
json_body = cJSON_Parse(client->http_req->body);
if (json_body)
{
json_sync = cJSON_GetObjectItem (json_body, "sync");
@@ -607,8 +604,7 @@ RELAY_API_PROTOCOL_CALLBACK(sync)
*/
void
relay_api_protocol_recv_http (struct t_relay_client *client,
struct t_relay_http_request *request)
relay_api_protocol_recv_http (struct t_relay_client *client)
{
int i, return_code, num_args;
struct t_relay_api_protocol_cb protocol_cb[] =
@@ -620,11 +616,11 @@ relay_api_protocol_recv_http (struct t_relay_client *client,
{ NULL, NULL, 0, 0, NULL },
};
if (!request || RELAY_CLIENT_HAS_ENDED(client))
if (!client->http_req || RELAY_CLIENT_HAS_ENDED(client))
return;
if ((client->status != RELAY_STATUS_CONNECTED)
&& !relay_http_check_auth (client, request))
&& !relay_http_check_auth (client))
{
relay_client_set_status (client, RELAY_STATUS_AUTH_FAILED);
return;
@@ -639,26 +635,26 @@ relay_api_protocol_recv_http (struct t_relay_client *client,
RELAY_COLOR_CHAT_CLIENT,
client->desc,
RELAY_COLOR_CHAT,
request->method,
request->path,
request->body);
client->http_req->method,
client->http_req->path,
client->http_req->body);
}
if ((request->num_path_items < 2) || !request->path_items
|| !request->path_items[0] || !request->path_items[1])
if ((client->http_req->num_path_items < 2) || !client->http_req->path_items
|| !client->http_req->path_items[0] || !client->http_req->path_items[1])
{
goto error404;
}
if (strcmp (request->path_items[0], "api") != 0)
if (strcmp (client->http_req->path_items[0], "api") != 0)
goto error404;
num_args = request->num_path_items - 2;
num_args = client->http_req->num_path_items - 2;
for (i = 0; protocol_cb[i].resource; i++)
{
if ((strcmp (protocol_cb[i].method, request->method) == 0)
&& (strcmp (protocol_cb[i].resource, request->path_items[1]) == 0))
if ((strcmp (protocol_cb[i].method, client->http_req->method) == 0)
&& (strcmp (protocol_cb[i].resource, client->http_req->path_items[1]) == 0))
{
if (num_args < protocol_cb[i].min_args)
{
@@ -674,7 +670,7 @@ relay_api_protocol_recv_http (struct t_relay_client *client,
RELAY_COLOR_CHAT_CLIENT,
client->desc,
RELAY_COLOR_CHAT,
request->path_items[1],
client->http_req->path_items[1],
num_args,
protocol_cb[i].min_args);
}
@@ -694,14 +690,13 @@ relay_api_protocol_recv_http (struct t_relay_client *client,
RELAY_COLOR_CHAT_CLIENT,
client->desc,
RELAY_COLOR_CHAT,
request->path_items[1],
client->http_req->path_items[1],
num_args,
protocol_cb[i].max_args);
}
goto error404;
}
return_code = (int) (protocol_cb[i].cmd_function) (
client, request);
return_code = (int) (protocol_cb[i].cmd_function) (client);
if (return_code == WEECHAT_RC_OK)
return;
else
@@ -727,8 +722,8 @@ error:
"for client %s%s%s"),
weechat_prefix ("error"),
RELAY_PLUGIN_NAME,
request->method,
request->path,
client->http_req->method,
client->http_req->path,
RELAY_COLOR_CHAT_CLIENT,
client->desc,
RELAY_COLOR_CHAT);
@@ -766,9 +761,8 @@ relay_api_protocol_recv_json (struct t_relay_client *client, const char *json)
cJSON *json_obj, *json_request, *json_body;
char *string_body;
int length;
struct t_relay_http_request *request;
request = NULL;
relay_http_request_reinit (client->http_req);
json_obj = cJSON_Parse(json);
if (!json_obj)
@@ -778,11 +772,7 @@ relay_api_protocol_recv_json (struct t_relay_client *client, const char *json)
if (!json_request)
goto error;
request = relay_http_request_alloc ();
if (!request)
goto error;
if (!relay_http_parse_method_path (request,
if (!relay_http_parse_method_path (client->http_req,
cJSON_GetStringValue (json_request)))
{
goto error;
@@ -795,18 +785,18 @@ relay_api_protocol_recv_json (struct t_relay_client *client, const char *json)
if (string_body)
{
length = strlen (string_body);
request->body = malloc (length + 1);
if (request->body)
client->http_req->body = malloc (length + 1);
if (client->http_req->body)
{
memcpy (request->body, string_body, length + 1);
request->content_length = length;
request->body_size = length;
memcpy (client->http_req->body, string_body, length + 1);
client->http_req->content_length = length;
client->http_req->body_size = length;
}
free (string_body);
}
}
relay_api_protocol_recv_http (client, request);
relay_api_protocol_recv_http (client);
goto end;
error:
@@ -815,6 +805,4 @@ error:
end:
if (json_obj)
cJSON_Delete (json_obj);
if (request)
relay_http_request_free (request);
}
+3 -14
View File
@@ -22,19 +22,9 @@
#define RELAY_API_PROTOCOL_CALLBACK(__command) \
int \
relay_api_protocol_cb_##__command ( \
struct t_relay_client *client, \
struct t_relay_http_request *request)
relay_api_protocol_cb_##__command (struct t_relay_client *client)
struct t_relay_api_protocol_ctxt
{
struct t_relay_client *client;
struct t_relay_http_request *request;
char *resource;
};
typedef int (t_relay_api_cmd_func)(struct t_relay_client *client,
struct t_relay_http_request *request);
typedef int (t_relay_api_cmd_func)(struct t_relay_client *client);
struct t_relay_api_protocol_cb
{
@@ -59,8 +49,7 @@ extern int relay_api_protocol_signal_upgrade_cb (const void *pointer,
const char *signal,
const char *type_data,
void *signal_data);
extern void relay_api_protocol_recv_http (struct t_relay_client *client,
struct t_relay_http_request *request);
extern void relay_api_protocol_recv_http (struct t_relay_client *client);
extern void relay_api_protocol_recv_json (struct t_relay_client *client,
const char *json);
+2 -3
View File
@@ -133,10 +133,9 @@ relay_api_unhook_signals (struct t_relay_client *client)
*/
void
relay_api_recv_http (struct t_relay_client *client,
struct t_relay_http_request *request)
relay_api_recv_http (struct t_relay_client *client)
{
relay_api_protocol_recv_http (client, request);
relay_api_protocol_recv_http (client);
}
/*
+1 -3
View File
@@ -21,7 +21,6 @@
#define WEECHAT_PLUGIN_RELAY_API_H
struct t_relay_client;
struct t_relay_http_request;
enum t_relay_status;
#define RELAY_API_VERSION_MAJOR 0
@@ -65,8 +64,7 @@ struct t_relay_api_data
extern enum t_relay_api_colors relay_api_search_colors (const char *colors);
extern void relay_api_hook_signals (struct t_relay_client *client);
extern void relay_api_unhook_signals (struct t_relay_client *client);
extern void relay_api_recv_http (struct t_relay_client *client,
struct t_relay_http_request *request);
extern void relay_api_recv_http (struct t_relay_client *client);
extern void relay_api_recv_json (struct t_relay_client *client,
const char *json);
extern void relay_api_close_connection (struct t_relay_client *client);
+7 -9
View File
@@ -530,8 +530,7 @@ relay_http_add_to_body (struct t_relay_http_request *request,
*/
int
relay_http_get_auth_status (struct t_relay_client *client,
struct t_relay_http_request *request)
relay_http_get_auth_status (struct t_relay_client *client)
{
const char *auth, *client_totp, *pos;
char *relay_password, *totp_secret, *info_totp_args, *info_totp;
@@ -552,7 +551,7 @@ relay_http_get_auth_status (struct t_relay_client *client,
goto end;
}
auth = weechat_hashtable_get (request->headers, "authorization");
auth = weechat_hashtable_get (client->http_req->headers, "authorization");
if (!auth || (weechat_strncasecmp (auth, "basic ", 6) != 0))
{
rc = -1;
@@ -625,7 +624,7 @@ relay_http_get_auth_status (struct t_relay_client *client,
NULL, NULL, NULL);
if (totp_secret && totp_secret[0])
{
client_totp = weechat_hashtable_get (request->headers, "x-weechat-totp");
client_totp = weechat_hashtable_get (client->http_req->headers, "x-weechat-totp");
if (!client_totp || !client_totp[0])
{
rc = -3;
@@ -674,12 +673,11 @@ end:
*/
int
relay_http_check_auth (struct t_relay_client *client,
struct t_relay_http_request *request)
relay_http_check_auth (struct t_relay_client *client)
{
int rc;
rc = relay_http_get_auth_status (client, request);
rc = relay_http_get_auth_status (client);
switch (rc)
{
case 0: /* authentication OK */
@@ -778,7 +776,7 @@ relay_http_process_websocket (struct t_relay_client *client)
/* handshake from client is valid, auth is mandatory for "api" protocol */
if (client->protocol == RELAY_PROTOCOL_API)
{
if (relay_http_check_auth (client, client->http_req))
if (relay_http_check_auth (client))
{
relay_client_set_status (client, RELAY_STATUS_CONNECTED);
}
@@ -852,7 +850,7 @@ relay_http_process_request (struct t_relay_client *client)
{
#ifdef HAVE_CJSON
if (client->protocol == RELAY_PROTOCOL_API)
relay_api_recv_http (client, client->http_req);
relay_api_recv_http (client);
#endif /* HAVE_CJSON */
}
}
+1 -2
View File
@@ -75,8 +75,7 @@ extern void relay_http_request_reinit (struct t_relay_http_request *request);
extern struct t_relay_http_request *relay_http_request_alloc ();
extern int relay_http_parse_method_path (struct t_relay_http_request *request,
const char *method_path);
extern int relay_http_check_auth (struct t_relay_client *client,
struct t_relay_http_request *request);
extern int relay_http_check_auth (struct t_relay_client *client);
extern void relay_http_recv (struct t_relay_client *client, const char *data);
extern int relay_http_send (struct t_relay_client *client,
int return_code, const char *message,
+38 -40
View File
@@ -50,8 +50,7 @@ extern int relay_http_parse_header (struct t_relay_http_request *request,
const char *header);
extern void relay_http_add_to_body (struct t_relay_http_request *request,
char **partial_message);
extern int relay_http_get_auth_status (struct t_relay_client *client,
struct t_relay_http_request *request);
extern int relay_http_get_auth_status (struct t_relay_client *client);
extern char *relay_http_compress (struct t_relay_http_request *request,
const char *data, int data_size,
int *compressed_size,
@@ -602,7 +601,6 @@ TEST(RelayHttp, AddToBody)
TEST(RelayHttp, GetAuthStatus)
{
struct t_relay_client *client;
struct t_relay_http_request *request;
const char *good_pwd = "secret_password";
const char *bad_pwd = "test";
char *totp, *totp2, salt[1024], salt_pass[1024], hash[1024], hash_hexa[2048];
@@ -614,26 +612,26 @@ TEST(RelayHttp, GetAuthStatus)
client = (struct t_relay_client *)calloc (1, sizeof (*client));
CHECK(client);
client->protocol = RELAY_PROTOCOL_API;
request = relay_http_request_alloc ();
CHECK(request);
client->http_req = relay_http_request_alloc ();
LONGS_EQUAL(-1, relay_http_get_auth_status (client, request));
hashtable_set (request->headers, "authorization", "Basic ");
LONGS_EQUAL(-2, relay_http_get_auth_status (client, request));
hashtable_set (request->headers, "authorization", "Basic \u26c4");
LONGS_EQUAL(-2, relay_http_get_auth_status (client, request));
LONGS_EQUAL(-1, relay_http_get_auth_status (client));
hashtable_set (client->http_req->headers, "authorization", "Basic ");
LONGS_EQUAL(-2, relay_http_get_auth_status (client));
hashtable_set (client->http_req->headers, "authorization", "Basic \u26c4");
LONGS_EQUAL(-2, relay_http_get_auth_status (client));
/* test invalid plain-text password ("test") */
hashtable_set (request->headers, "authorization", "Basic cGxhaW46dGVzdA==");
LONGS_EQUAL(-2, relay_http_get_auth_status (client, request));
hashtable_set (client->http_req->headers, "authorization", "Basic cGxhaW46dGVzdA==");
LONGS_EQUAL(-2, relay_http_get_auth_status (client));
/* test valid plain-text password ("secret_password") */
hashtable_set (request->headers,
hashtable_set (client->http_req->headers,
"authorization",
"Basic cGxhaW46c2VjcmV0X3Bhc3N3b3Jk");
LONGS_EQUAL(0, relay_http_get_auth_status (client, request));
LONGS_EQUAL(0, relay_http_get_auth_status (client));
/* test invalid hash: "SHA128" */
time_now = time (NULL);
@@ -648,8 +646,8 @@ TEST(RelayHttp, GetAuthStatus)
hash_hexa);
string_base_encode ("64", auth, strlen (auth), auth_base64);
snprintf (auth_header, sizeof (auth_header), "Basic %s", auth_base64);
hashtable_set (request->headers, "authorization", auth_header);
LONGS_EQUAL(-5, relay_http_get_auth_status (client, request));
hashtable_set (client->http_req->headers, "authorization", auth_header);
LONGS_EQUAL(-5, relay_http_get_auth_status (client));
/* test invalid password hashed with SHA256: "test" */
time_now = time (NULL);
@@ -664,8 +662,8 @@ TEST(RelayHttp, GetAuthStatus)
hash_hexa);
string_base_encode ("64", auth, strlen (auth), auth_base64);
snprintf (auth_header, sizeof (auth_header), "Basic %s", auth_base64);
hashtable_set (request->headers, "authorization", auth_header);
LONGS_EQUAL(-2, relay_http_get_auth_status (client, request));
hashtable_set (client->http_req->headers, "authorization", auth_header);
LONGS_EQUAL(-2, relay_http_get_auth_status (client));
/* test invalid password hashed with SHA512: "test" */
time_now = time (NULL);
@@ -680,8 +678,8 @@ TEST(RelayHttp, GetAuthStatus)
hash_hexa);
string_base_encode ("64", auth, strlen (auth), auth_base64);
snprintf (auth_header, sizeof (auth_header), "Basic %s", auth_base64);
hashtable_set (request->headers, "authorization", auth_header);
LONGS_EQUAL(-2, relay_http_get_auth_status (client, request));
hashtable_set (client->http_req->headers, "authorization", auth_header);
LONGS_EQUAL(-2, relay_http_get_auth_status (client));
/* test valid password hashed with SHA256: "secret_password" but too old time (salt) */
time_now = time (NULL) - 10;
@@ -696,8 +694,8 @@ TEST(RelayHttp, GetAuthStatus)
hash_hexa);
string_base_encode ("64", auth, strlen (auth), auth_base64);
snprintf (auth_header, sizeof (auth_header), "Basic %s", auth_base64);
hashtable_set (request->headers, "authorization", auth_header);
LONGS_EQUAL(-6, relay_http_get_auth_status (client, request));
hashtable_set (client->http_req->headers, "authorization", auth_header);
LONGS_EQUAL(-6, relay_http_get_auth_status (client));
/* test valid password hashed with SHA256: "secret_password" */
time_now = time (NULL);
@@ -712,8 +710,8 @@ TEST(RelayHttp, GetAuthStatus)
hash_hexa);
string_base_encode ("64", auth, strlen (auth), auth_base64);
snprintf (auth_header, sizeof (auth_header), "Basic %s", auth_base64);
hashtable_set (request->headers, "authorization", auth_header);
LONGS_EQUAL(0, relay_http_get_auth_status (client, request));
hashtable_set (client->http_req->headers, "authorization", auth_header);
LONGS_EQUAL(0, relay_http_get_auth_status (client));
/* test valid password hashed with SHA512: "secret_password" */
time_now = time (NULL);
@@ -728,8 +726,8 @@ TEST(RelayHttp, GetAuthStatus)
hash_hexa);
string_base_encode ("64", auth, strlen (auth), auth_base64);
snprintf (auth_header, sizeof (auth_header), "Basic %s", auth_base64);
hashtable_set (request->headers, "authorization", auth_header);
LONGS_EQUAL(0, relay_http_get_auth_status (client, request));
hashtable_set (client->http_req->headers, "authorization", auth_header);
LONGS_EQUAL(0, relay_http_get_auth_status (client));
/* test invalid number of iterations */
time_now = time (NULL);
@@ -746,8 +744,8 @@ TEST(RelayHttp, GetAuthStatus)
hash_hexa);
string_base_encode ("64", auth, strlen (auth), auth_base64);
snprintf (auth_header, sizeof (auth_header), "Basic %s", auth_base64);
hashtable_set (request->headers, "authorization", auth_header);
LONGS_EQUAL(-7, relay_http_get_auth_status (client, request));
hashtable_set (client->http_req->headers, "authorization", auth_header);
LONGS_EQUAL(-7, relay_http_get_auth_status (client));
/* test invalid password hashed with PBKDF2+SHA256: "test" */
time_now = time (NULL);
@@ -764,8 +762,8 @@ TEST(RelayHttp, GetAuthStatus)
hash_hexa);
string_base_encode ("64", auth, strlen (auth), auth_base64);
snprintf (auth_header, sizeof (auth_header), "Basic %s", auth_base64);
hashtable_set (request->headers, "authorization", auth_header);
LONGS_EQUAL(-2, relay_http_get_auth_status (client, request));
hashtable_set (client->http_req->headers, "authorization", auth_header);
LONGS_EQUAL(-2, relay_http_get_auth_status (client));
/* test valid password hashed with PBKDF2+SHA256: "secret_password" */
time_now = time (NULL);
@@ -782,8 +780,8 @@ TEST(RelayHttp, GetAuthStatus)
hash_hexa);
string_base_encode ("64", auth, strlen (auth), auth_base64);
snprintf (auth_header, sizeof (auth_header), "Basic %s", auth_base64);
hashtable_set (request->headers, "authorization", auth_header);
LONGS_EQUAL(0, relay_http_get_auth_status (client, request));
hashtable_set (client->http_req->headers, "authorization", auth_header);
LONGS_EQUAL(0, relay_http_get_auth_status (client));
/* test valid password hashed with PBKDF2+SHA512: "secret_password" */
time_now = time (NULL);
@@ -800,21 +798,21 @@ TEST(RelayHttp, GetAuthStatus)
hash_hexa);
string_base_encode ("64", auth, strlen (auth), auth_base64);
snprintf (auth_header, sizeof (auth_header), "Basic %s", auth_base64);
hashtable_set (request->headers, "authorization", auth_header);
LONGS_EQUAL(0, relay_http_get_auth_status (client, request));
hashtable_set (client->http_req->headers, "authorization", auth_header);
LONGS_EQUAL(0, relay_http_get_auth_status (client));
/* test missing/invalid TOTP */
config_file_option_set (relay_config_network_totp_secret, "secretbase32", 1);
config_file_option_set (relay_config_network_totp_window, "1", 1);
LONGS_EQUAL(-3, relay_http_get_auth_status (client, request));
LONGS_EQUAL(-3, relay_http_get_auth_status (client));
totp = hook_info_get (NULL, "totp_generate", "secretbase32");
CHECK(totp);
totp2 = strdup (totp);
totp2[0] = (totp2[0] == '1') ? '2' : '1';
hashtable_set (request->headers, "x-weechat-totp", totp2);
LONGS_EQUAL(-4, relay_http_get_auth_status (client, request));
hashtable_set (request->headers, "x-weechat-totp", totp);
LONGS_EQUAL(0, relay_http_get_auth_status (client, request));
hashtable_set (client->http_req->headers, "x-weechat-totp", totp2);
LONGS_EQUAL(-4, relay_http_get_auth_status (client));
hashtable_set (client->http_req->headers, "x-weechat-totp", totp);
LONGS_EQUAL(0, relay_http_get_auth_status (client));
free (totp);
free (totp2);
config_file_option_reset (relay_config_network_totp_secret, 1);
@@ -822,7 +820,7 @@ TEST(RelayHttp, GetAuthStatus)
config_file_option_reset (relay_config_network_password, 1);
relay_http_request_free (request);
relay_http_request_free (client->http_req);
free (client);
}