uri: modify g_uri_parse_params() to take flags

This will allow to further enhance the parsing, without breaking API,
and also makes argument on call side a bit clearer than just TRUE/FALSE.

Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
This commit is contained in:
Marc-André Lureau 2020-06-30 17:12:10 +04:00
parent d022b7199b
commit 591d8676ee
5 changed files with 33 additions and 19 deletions

View File

@ -3371,6 +3371,7 @@ g_uri_get_query
g_uri_get_fragment
g_uri_get_flags
<SUBSECTION>
GUriParamsFlags
g_uri_parse_params
<SUBSECTION>
G_URI_RESERVED_CHARS_ALLOWED_IN_PATH

View File

@ -10,7 +10,7 @@ LLVMFuzzerTestOneInput (const unsigned char *data, size_t size)
if (size > G_MAXSSIZE)
return 0;
parsed_params = g_uri_parse_params ((const gchar *) data, (gssize) size, "&", FALSE);
parsed_params = g_uri_parse_params ((const gchar *) data, (gssize) size, "&", G_URI_PARAMS_NONE);
if (parsed_params == NULL)
return 0;

View File

@ -1755,7 +1755,7 @@ str_ascii_case_equal (gconstpointer v1,
* bytes not characters, so it can't be used to delimit UTF-8 strings for
* anything but ASCII characters. You may pass an empty set, in which case
* no splitting will occur.
* @case_insensitive: whether parameter names are case insensitive
* @flags: flags to modify the way the parameters are handled.
*
* Many URI schemes include one or more attribute/value pairs as part of the URI
* value. This method can be used to parse them into a hash table.
@ -1780,7 +1780,7 @@ GHashTable *
g_uri_parse_params (const gchar *params,
gssize length,
const gchar *separators,
gboolean case_insensitive)
GUriParamsFlags flags)
{
GHashTable *hash;
const gchar *end, *attr, *attr_end, *value, *value_end, *s;
@ -1791,7 +1791,7 @@ g_uri_parse_params (const gchar *params,
g_return_val_if_fail (length >= -1, NULL);
g_return_val_if_fail (separators != NULL, NULL);
if (case_insensitive)
if (flags & G_URI_PARAMS_CASE_INSENSITIVE)
{
hash = g_hash_table_new_full (str_ascii_case_hash,
str_ascii_case_equal,

View File

@ -222,11 +222,26 @@ const gchar *g_uri_get_fragment (GUri *uri);
GLIB_AVAILABLE_IN_2_66
GUriFlags g_uri_get_flags (GUri *uri);
/**
* GUriParamsFlags:
* @G_URI_PARAMS_NONE: No flags set.
* @G_URI_PARAMS_CASE_INSENSITIVE: whether parameter names are case insensitive.
*
* Flags modifying the way parameters are handled.
*
* Since: 2.66
*/
GLIB_AVAILABLE_TYPE_IN_2_66
typedef enum {
G_URI_PARAMS_NONE = 0,
G_URI_PARAMS_CASE_INSENSITIVE = 1 << 0,
} GUriParamsFlags;
GLIB_AVAILABLE_IN_2_66
GHashTable * g_uri_parse_params (const gchar *params,
gssize length,
const gchar *separators,
gboolean case_insensitive);
GHashTable *g_uri_parse_params (const gchar *params,
gssize length,
const gchar *separators,
GUriParamsFlags flags);
/**
* G_URI_ERROR:

View File

@ -1271,7 +1271,7 @@ test_uri_parse_params (gconstpointer test_data)
/* Inputs */
const gchar *uri;
gchar *separators;
gboolean case_insensitive;
GUriParamsFlags flags;
/* Outputs */
gssize expected_n_params; /* -1 => error expected */
/* key, value, key, value, …, limited to length 2*expected_n_params */
@ -1279,15 +1279,13 @@ test_uri_parse_params (gconstpointer test_data)
}
tests[] =
{
{ "", "&", FALSE, 0, { NULL, }},
{ "p1=foo&p2=bar", "&", FALSE, 2, { "p1", "foo", "p2", "bar" }},
{ "p1=foo&p2=bar;p3=baz", "&;", FALSE, 3, { "p1", "foo", "p2", "bar", "p3", "baz" }},
{ "p1=foo&p2=bar", "", FALSE, 1, { "p1", "foo&p2=bar" }},
{ "p1=foo&&P1=bar", "&", FALSE, -1, { NULL, }},
{ "%00=foo", "&", FALSE, -1, { NULL, }},
{ "p1=%00", "&", FALSE, -1, { NULL, }},
{ "p1=foo&P1=bar", "&", TRUE, 1, { "p1", "bar", NULL, }},
{ "=%", "&", FALSE, 1, { "", "%", NULL, }},
{ "p1=foo&p2=bar;p3=baz", "&;", G_URI_PARAMS_NONE, 3, { "p1", "foo", "p2", "bar", "p3", "baz" }},
{ "p1=foo&p2=bar", "", G_URI_PARAMS_NONE, 1, { "p1", "foo&p2=bar" }},
{ "p1=foo&&P1=bar", "&", G_URI_PARAMS_NONE, -1, { NULL, }},
{ "%00=foo", "&", G_URI_PARAMS_NONE, -1, { NULL, }},
{ "p1=%00", "&", G_URI_PARAMS_NONE, -1, { NULL, }},
{ "p1=foo&P1=bar", "&", G_URI_PARAMS_CASE_INSENSITIVE, 1, { "p1", "bar", NULL, }},
{ "=%", "&", G_URI_PARAMS_NONE, 1, { "", "%", NULL, }},
};
gsize i;
@ -1317,7 +1315,7 @@ test_uri_parse_params (gconstpointer test_data)
uri = g_memdup (tests[i].uri, uri_len);
}
params = g_uri_parse_params (uri, uri_len, tests[i].separators, tests[i].case_insensitive);
params = g_uri_parse_params (uri, uri_len, tests[i].separators, tests[i].flags);
if (tests[i].expected_n_params < 0)
{