diff options
author | Sébastien Helleu <flashcode@flashtux.org> | 2024-02-18 23:13:20 +0100 |
---|---|---|
committer | Sébastien Helleu <flashcode@flashtux.org> | 2024-02-18 23:13:20 +0100 |
commit | 0f30a4e0204cbcd084fafab4aeb28f6211240b9f (patch) | |
tree | f88d16b879584ec735836f755fa54a08dcbdffa0 | |
parent | 9b9b36bb02c3609a44ef91cc70f3bca20b71ec56 (diff) | |
download | weechat-0f30a4e0204cbcd084fafab4aeb28f6211240b9f.zip |
relay: move functions to get URL parameters from api to relay-http.c, add tests
-rw-r--r-- | src/plugins/relay/api/relay-api-protocol.c | 50 | ||||
-rw-r--r-- | src/plugins/relay/relay-http.c | 42 | ||||
-rw-r--r-- | src/plugins/relay/relay-http.h | 4 | ||||
-rw-r--r-- | tests/unit/plugins/relay/test-relay-http.cpp | 42 |
4 files changed, 91 insertions, 47 deletions
diff --git a/src/plugins/relay/api/relay-api-protocol.c b/src/plugins/relay/api/relay-api-protocol.c index b4763cb53..bddf9c833 100644 --- a/src/plugins/relay/api/relay-api-protocol.c +++ b/src/plugins/relay/api/relay-api-protocol.c @@ -40,50 +40,6 @@ /* - * 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); diff --git a/src/plugins/relay/relay-http.c b/src/plugins/relay/relay-http.c index 479f99af2..97a70b395 100644 --- a/src/plugins/relay/relay-http.c +++ b/src/plugins/relay/relay-http.c @@ -183,6 +183,48 @@ relay_http_url_decode (const char *url) } /* + * 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. */ diff --git a/src/plugins/relay/relay-http.h b/src/plugins/relay/relay-http.h index dbe34f0ee..c53c74bdf 100644 --- a/src/plugins/relay/relay-http.h +++ b/src/plugins/relay/relay-http.h @@ -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); diff --git a/tests/unit/plugins/relay/test-relay-http.cpp b/tests/unit/plugins/relay/test-relay-http.cpp index 243173442..4e735179d 100644 --- a/tests/unit/plugins/relay/test-relay-http.cpp +++ b/tests/unit/plugins/relay/test-relay-http.cpp @@ -180,6 +180,48 @@ TEST(RelayHttp, UrlDecode) /* * 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 */ |