mirror of
https://gitlab.gnome.org/GNOME/glib.git
synced 2025-04-01 05:13:06 +02:00
uri: teach g_uri_parse_params() to decode www-form query
This is a minor convenience, to avoid caller to do further '+' decoding. According to the W3C HTML specification, space characters are replaced by '+': https://url.spec.whatwg.org/#urlencoded-parsing Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
This commit is contained in:
parent
e2d3349c56
commit
30ad9c6711
19
glib/guri.c
19
glib/guri.c
@ -237,6 +237,7 @@ uri_decoder (gchar **out,
|
|||||||
const gchar *start,
|
const gchar *start,
|
||||||
gsize length,
|
gsize length,
|
||||||
gboolean just_normalize,
|
gboolean just_normalize,
|
||||||
|
gboolean www_form,
|
||||||
GUriFlags flags,
|
GUriFlags flags,
|
||||||
GUriError parse_error,
|
GUriError parse_error,
|
||||||
GError **error)
|
GError **error)
|
||||||
@ -287,6 +288,8 @@ uri_decoder (gchar **out,
|
|||||||
s += 2;
|
s += 2;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
else if (www_form && *s == '+')
|
||||||
|
*d++ = ' ';
|
||||||
else
|
else
|
||||||
*d++ = *s;
|
*d++ = *s;
|
||||||
}
|
}
|
||||||
@ -314,11 +317,12 @@ static gboolean
|
|||||||
uri_decode (gchar **out,
|
uri_decode (gchar **out,
|
||||||
const gchar *start,
|
const gchar *start,
|
||||||
gsize length,
|
gsize length,
|
||||||
|
gboolean www_form,
|
||||||
GUriFlags flags,
|
GUriFlags flags,
|
||||||
GUriError parse_error,
|
GUriError parse_error,
|
||||||
GError **error)
|
GError **error)
|
||||||
{
|
{
|
||||||
return uri_decoder (out, start, length, FALSE, flags,
|
return uri_decoder (out, start, length, FALSE, www_form, flags,
|
||||||
parse_error, error) != -1;
|
parse_error, error) != -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -330,7 +334,7 @@ uri_normalize (gchar **out,
|
|||||||
GUriError parse_error,
|
GUriError parse_error,
|
||||||
GError **error)
|
GError **error)
|
||||||
{
|
{
|
||||||
return uri_decoder (out, start, length, TRUE, flags,
|
return uri_decoder (out, start, length, TRUE, FALSE, flags,
|
||||||
parse_error, error) != -1;
|
parse_error, error) != -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -450,7 +454,7 @@ parse_host (const gchar *start,
|
|||||||
}
|
}
|
||||||
|
|
||||||
flags &= ~G_URI_FLAGS_ENCODED;
|
flags &= ~G_URI_FLAGS_ENCODED;
|
||||||
if (!uri_decode (&decoded, start, length, flags,
|
if (!uri_decode (&decoded, start, length, FALSE, flags,
|
||||||
G_URI_ERROR_BAD_HOST, error))
|
G_URI_ERROR_BAD_HOST, error))
|
||||||
return FALSE;
|
return FALSE;
|
||||||
|
|
||||||
@ -771,7 +775,7 @@ g_uri_split_internal (const gchar *uri_string,
|
|||||||
end = p + strcspn (p, "#");
|
end = p + strcspn (p, "#");
|
||||||
if (*end == '#')
|
if (*end == '#')
|
||||||
{
|
{
|
||||||
if (!uri_decode (fragment, end + 1, strlen (end + 1), flags,
|
if (!uri_decode (fragment, end + 1, strlen (end + 1), FALSE, flags,
|
||||||
G_URI_ERROR_BAD_FRAGMENT, error))
|
G_URI_ERROR_BAD_FRAGMENT, error))
|
||||||
goto fail;
|
goto fail;
|
||||||
}
|
}
|
||||||
@ -1786,6 +1790,7 @@ g_uri_parse_params (const gchar *params,
|
|||||||
const gchar *end, *attr, *attr_end, *value, *value_end, *s;
|
const gchar *end, *attr, *attr_end, *value, *value_end, *s;
|
||||||
gchar *decoded_attr, *decoded_value;
|
gchar *decoded_attr, *decoded_value;
|
||||||
guint8 sep_table[256]; /* 1 = index is a separator; 0 otherwise */
|
guint8 sep_table[256]; /* 1 = index is a separator; 0 otherwise */
|
||||||
|
gboolean www_form = flags & G_URI_PARAMS_WWW_FORM;
|
||||||
|
|
||||||
g_return_val_if_fail (length == 0 || params != NULL, NULL);
|
g_return_val_if_fail (length == 0 || params != NULL, NULL);
|
||||||
g_return_val_if_fail (length >= -1, NULL);
|
g_return_val_if_fail (length >= -1, NULL);
|
||||||
@ -1829,7 +1834,7 @@ g_uri_parse_params (const gchar *params,
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
if (!uri_decode (&decoded_attr, attr, attr_end - attr,
|
if (!uri_decode (&decoded_attr, attr, attr_end - attr,
|
||||||
0, G_URI_ERROR_MISC, NULL))
|
www_form, G_URI_FLAGS_NONE, G_URI_ERROR_MISC, NULL))
|
||||||
{
|
{
|
||||||
g_hash_table_destroy (hash);
|
g_hash_table_destroy (hash);
|
||||||
return NULL;
|
return NULL;
|
||||||
@ -1837,7 +1842,7 @@ g_uri_parse_params (const gchar *params,
|
|||||||
|
|
||||||
value = attr_end + 1;
|
value = attr_end + 1;
|
||||||
if (!uri_decode (&decoded_value, value, value_end - value,
|
if (!uri_decode (&decoded_value, value, value_end - value,
|
||||||
0, G_URI_ERROR_MISC, NULL))
|
www_form, G_URI_FLAGS_NONE, G_URI_ERROR_MISC, NULL))
|
||||||
{
|
{
|
||||||
g_free (decoded_attr);
|
g_free (decoded_attr);
|
||||||
g_hash_table_destroy (hash);
|
g_hash_table_destroy (hash);
|
||||||
@ -2118,6 +2123,7 @@ g_uri_unescape_segment (const gchar *escaped_string,
|
|||||||
|
|
||||||
if (!uri_decode (&unescaped,
|
if (!uri_decode (&unescaped,
|
||||||
escaped_string, length,
|
escaped_string, length,
|
||||||
|
FALSE,
|
||||||
G_URI_FLAGS_PARSE_STRICT,
|
G_URI_FLAGS_PARSE_STRICT,
|
||||||
0, NULL))
|
0, NULL))
|
||||||
return NULL;
|
return NULL;
|
||||||
@ -2232,6 +2238,7 @@ g_uri_unescape_bytes (const gchar *escaped_string,
|
|||||||
unescaped_length = uri_decoder (&buf,
|
unescaped_length = uri_decoder (&buf,
|
||||||
escaped_string, length,
|
escaped_string, length,
|
||||||
FALSE,
|
FALSE,
|
||||||
|
FALSE,
|
||||||
G_URI_FLAGS_PARSE_STRICT|G_URI_FLAGS_ENCODED,
|
G_URI_FLAGS_PARSE_STRICT|G_URI_FLAGS_ENCODED,
|
||||||
0, NULL);
|
0, NULL);
|
||||||
if (unescaped_length == -1)
|
if (unescaped_length == -1)
|
||||||
|
@ -226,6 +226,7 @@ GUriFlags g_uri_get_flags (GUri *uri);
|
|||||||
* GUriParamsFlags:
|
* GUriParamsFlags:
|
||||||
* @G_URI_PARAMS_NONE: No flags set.
|
* @G_URI_PARAMS_NONE: No flags set.
|
||||||
* @G_URI_PARAMS_CASE_INSENSITIVE: whether parameter names are case insensitive.
|
* @G_URI_PARAMS_CASE_INSENSITIVE: whether parameter names are case insensitive.
|
||||||
|
* @G_URI_PARAMS_WWW_FORM: replace `+` with space character.
|
||||||
*
|
*
|
||||||
* Flags modifying the way parameters are handled.
|
* Flags modifying the way parameters are handled.
|
||||||
*
|
*
|
||||||
@ -235,6 +236,7 @@ GLIB_AVAILABLE_TYPE_IN_2_66
|
|||||||
typedef enum {
|
typedef enum {
|
||||||
G_URI_PARAMS_NONE = 0,
|
G_URI_PARAMS_NONE = 0,
|
||||||
G_URI_PARAMS_CASE_INSENSITIVE = 1 << 0,
|
G_URI_PARAMS_CASE_INSENSITIVE = 1 << 0,
|
||||||
|
G_URI_PARAMS_WWW_FORM = 1 << 1,
|
||||||
} GUriParamsFlags;
|
} GUriParamsFlags;
|
||||||
|
|
||||||
GLIB_AVAILABLE_IN_2_66
|
GLIB_AVAILABLE_IN_2_66
|
||||||
|
@ -1288,6 +1288,10 @@ test_uri_parse_params (gconstpointer test_data)
|
|||||||
{ "=%", "&", G_URI_PARAMS_NONE, 1, { "", "%", NULL, }},
|
{ "=%", "&", G_URI_PARAMS_NONE, 1, { "", "%", NULL, }},
|
||||||
{ "=", "&", G_URI_PARAMS_NONE, 1, { "", "", NULL, }},
|
{ "=", "&", G_URI_PARAMS_NONE, 1, { "", "", NULL, }},
|
||||||
{ "foo", "&", G_URI_PARAMS_NONE, -1, { NULL, }},
|
{ "foo", "&", G_URI_PARAMS_NONE, -1, { NULL, }},
|
||||||
|
{ "foo=bar+%26+baz&saisons=%C3%89t%C3%A9%2Bhiver", "&", G_URI_PARAMS_WWW_FORM,
|
||||||
|
2, { "foo", "bar & baz", "saisons", "Été+hiver", NULL, }},
|
||||||
|
{ "foo=bar+%26+baz&saisons=%C3%89t%C3%A9%2Bhiver", "&", G_URI_PARAMS_NONE,
|
||||||
|
2, { "foo", "bar+&+baz", "saisons", "Été+hiver", NULL, }},
|
||||||
};
|
};
|
||||||
gsize i;
|
gsize i;
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user