mirror of
https://github.com/weechat/weechat.git
synced 2026-06-29 22:36:38 +02:00
relay: move functions to get URL parameters from api to relay-http.c, add tests
This commit is contained in:
@@ -39,50 +39,6 @@
|
||||
#include "relay-api-protocol.h"
|
||||
|
||||
|
||||
/*
|
||||
* Returns value of an URL parameter as boolean (0 or 1), using a default value
|
||||
* if the parameter is not set.
|
||||
*/
|
||||
|
||||
int
|
||||
relay_api_protocol_get_param_boolean (struct t_relay_http_request *request,
|
||||
const char *name,
|
||||
int default_value)
|
||||
{
|
||||
const char *ptr_value;
|
||||
|
||||
ptr_value = weechat_hashtable_get (request->params, name);
|
||||
if (!ptr_value)
|
||||
return default_value;
|
||||
|
||||
return weechat_config_string_to_boolean (ptr_value);
|
||||
}
|
||||
|
||||
/*
|
||||
* Returns value of an URL parameter as long, using a default value if the
|
||||
* parameter is not set or if it's not a valid long integer.
|
||||
*/
|
||||
|
||||
long
|
||||
relay_api_protocol_get_param_long (struct t_relay_http_request *request,
|
||||
const char *name,
|
||||
long default_value)
|
||||
{
|
||||
const char *ptr_value;
|
||||
char *error;
|
||||
long number;
|
||||
|
||||
ptr_value = weechat_hashtable_get (request->params, name);
|
||||
if (!ptr_value)
|
||||
return default_value;
|
||||
|
||||
number = strtol (ptr_value, &error, 10);
|
||||
if (error && !error[0])
|
||||
return number;
|
||||
|
||||
return default_value;
|
||||
}
|
||||
|
||||
/*
|
||||
* Callback for signals "buffer_*".
|
||||
*/
|
||||
@@ -425,7 +381,7 @@ RELAY_API_PROTOCOL_CALLBACK(buffers)
|
||||
}
|
||||
}
|
||||
|
||||
nicks = relay_api_protocol_get_param_boolean (client->http_req, "nicks", 0);
|
||||
nicks = relay_http_get_param_boolean (client->http_req, "nicks", 0);
|
||||
colors = relay_api_search_colors (
|
||||
weechat_hashtable_get (client->http_req->params, "colors"));
|
||||
|
||||
@@ -434,7 +390,7 @@ RELAY_API_PROTOCOL_CALLBACK(buffers)
|
||||
/* sub-resource of buffers */
|
||||
if (strcmp (client->http_req->path_items[3], "lines") == 0)
|
||||
{
|
||||
lines = relay_api_protocol_get_param_long (client->http_req, "lines", -100L);
|
||||
lines = relay_http_get_param_long (client->http_req, "lines", -100L);
|
||||
json = relay_api_msg_lines_to_json (ptr_buffer, lines, colors);
|
||||
}
|
||||
else if (strcmp (client->http_req->path_items[3], "nicks") == 0)
|
||||
@@ -454,7 +410,7 @@ RELAY_API_PROTOCOL_CALLBACK(buffers)
|
||||
}
|
||||
else
|
||||
{
|
||||
lines = relay_api_protocol_get_param_long (client->http_req, "lines", 0L);
|
||||
lines = relay_http_get_param_long (client->http_req, "lines", 0L);
|
||||
if (ptr_buffer)
|
||||
{
|
||||
json = relay_api_msg_buffer_to_json (ptr_buffer, lines, nicks, colors);
|
||||
|
||||
@@ -182,6 +182,48 @@ relay_http_url_decode (const char *url)
|
||||
return weechat_string_dyn_free (out, 0);
|
||||
}
|
||||
|
||||
/*
|
||||
* Returns value of an URL parameter as boolean (0 or 1), using a default value
|
||||
* if the parameter is not set.
|
||||
*/
|
||||
|
||||
int
|
||||
relay_http_get_param_boolean (struct t_relay_http_request *request,
|
||||
const char *name, int default_value)
|
||||
{
|
||||
const char *ptr_value;
|
||||
|
||||
ptr_value = weechat_hashtable_get (request->params, name);
|
||||
if (!ptr_value)
|
||||
return default_value;
|
||||
|
||||
return weechat_config_string_to_boolean (ptr_value);
|
||||
}
|
||||
|
||||
/*
|
||||
* Returns value of an URL parameter as long, using a default value if the
|
||||
* parameter is not set or if it's not a valid long integer.
|
||||
*/
|
||||
|
||||
long
|
||||
relay_http_get_param_long (struct t_relay_http_request *request,
|
||||
const char *name, long default_value)
|
||||
{
|
||||
const char *ptr_value;
|
||||
char *error;
|
||||
long number;
|
||||
|
||||
ptr_value = weechat_hashtable_get (request->params, name);
|
||||
if (!ptr_value)
|
||||
return default_value;
|
||||
|
||||
number = strtol (ptr_value, &error, 10);
|
||||
if (error && !error[0])
|
||||
return number;
|
||||
|
||||
return default_value;
|
||||
}
|
||||
|
||||
/*
|
||||
* Get decoded path items from path.
|
||||
*/
|
||||
|
||||
@@ -73,6 +73,10 @@ struct t_relay_http_request
|
||||
|
||||
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_get_param_boolean (struct t_relay_http_request *request,
|
||||
const char *name, int default_value);
|
||||
extern long relay_http_get_param_long (struct t_relay_http_request *request,
|
||||
const char *name, long default_value);
|
||||
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);
|
||||
|
||||
@@ -178,6 +178,48 @@ TEST(RelayHttp, UrlDecode)
|
||||
WEE_TEST_STR("test*", relay_http_url_decode ("test%2a"));
|
||||
}
|
||||
|
||||
/*
|
||||
* Tests functions:
|
||||
* relay_http_get_param_boolean
|
||||
*/
|
||||
|
||||
TEST(RelayHttp, GetParamBoolean)
|
||||
{
|
||||
struct t_relay_http_request *request;
|
||||
|
||||
request = relay_http_request_alloc ();
|
||||
CHECK(request);
|
||||
relay_http_parse_method_path (request, "GET /api/test?key1=true&key2=1&key3=off");
|
||||
LONGS_EQUAL(1, relay_http_get_param_boolean (request, "key1", 0));
|
||||
LONGS_EQUAL(1, relay_http_get_param_boolean (request, "key1", 1));
|
||||
LONGS_EQUAL(1, relay_http_get_param_boolean (request, "key2", 0));
|
||||
LONGS_EQUAL(1, relay_http_get_param_boolean (request, "key2", 1));
|
||||
LONGS_EQUAL(0, relay_http_get_param_boolean (request, "key3", 0));
|
||||
LONGS_EQUAL(0, relay_http_get_param_boolean (request, "key3", 1));
|
||||
LONGS_EQUAL(0, relay_http_get_param_boolean (request, "xxx", 0));
|
||||
LONGS_EQUAL(1, relay_http_get_param_boolean (request, "xxx", 1));
|
||||
relay_http_request_free (request);
|
||||
}
|
||||
|
||||
/*
|
||||
* Tests functions:
|
||||
* relay_http_get_param_long
|
||||
*/
|
||||
|
||||
TEST(RelayHttp, GetParamLong)
|
||||
{
|
||||
struct t_relay_http_request *request;
|
||||
|
||||
request = relay_http_request_alloc ();
|
||||
CHECK(request);
|
||||
relay_http_parse_method_path (request, "GET /api/test?key1=123&key2=-4&key3=abc");
|
||||
LONGS_EQUAL(123, relay_http_get_param_long (request, "key1", 8));
|
||||
LONGS_EQUAL(-4, relay_http_get_param_long (request, "key2", 8));
|
||||
LONGS_EQUAL(8, relay_http_get_param_long (request, "key3", 8));
|
||||
LONGS_EQUAL(99, relay_http_get_param_long (request, "xxx", 99));
|
||||
relay_http_request_free (request);
|
||||
}
|
||||
|
||||
/*
|
||||
* Tests functions:
|
||||
* relay_http_parse_path
|
||||
|
||||
Reference in New Issue
Block a user