uri: change g_uri_is_valid() to check absolute URI

g_uri_is_valid() should check the given URI is valid following RFC-3986,
and reject relative references.

Fixes: https://gitlab.gnome.org/GNOME/glib/-/issues/2169

Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
This commit is contained in:
Marc-André Lureau 2020-07-28 15:42:33 +04:00
parent 44d4640c47
commit 82ad7853ba
3 changed files with 12 additions and 13 deletions

View File

@ -1039,32 +1039,29 @@ g_uri_split_network (const gchar *uri_string,
/**
* g_uri_is_valid:
* @uri_ref: a string containing a relative or absolute URI
* @flags: flags for parsing @uri_ref
* @uri_string: a string containing an absolute URI
* @flags: flags for parsing @uri_string
* @error: #GError for error reporting, or %NULL to ignore.
*
* Parses @uri_ref (which can be an absolute or relative URI)
* according to @flags, to determine whether it is valid.
* Parses @uri_string according to @flags, to determine whether it is valid
* absolute URI.
*
* See g_uri_split(), and the definition of #GUriFlags, for more
* information on the effect of @flags.
*
* Returns: %TRUE if @uri_ref parsed successfully, %FALSE on error.
* Returns: %TRUE if @uri_string parsed successfully, %FALSE on error.
*
* Since: 2.66
*/
gboolean
g_uri_is_valid (const gchar *uri_ref,
g_uri_is_valid (const gchar *uri_string,
GUriFlags flags,
GError **error)
{
g_return_val_if_fail (uri_ref != NULL, FALSE);
g_return_val_if_fail (uri_string != NULL, FALSE);
g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
return g_uri_split_internal (uri_ref, flags,
NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL,
error);
return g_uri_split_network (uri_string, flags, NULL, NULL, NULL, error);
}

View File

@ -110,7 +110,7 @@ gboolean g_uri_split_network (const gchar *uri_string,
GError **error);
GLIB_AVAILABLE_IN_2_66
gboolean g_uri_is_valid (const gchar *uri_ref,
gboolean g_uri_is_valid (const gchar *uri_string,
GUriFlags flags,
GError **error);

View File

@ -1267,7 +1267,9 @@ test_uri_is_valid (void)
g_assert_true (g_uri_is_valid ("http://\xc3\x89XAMPLE.COM/", G_URI_FLAGS_NONE, NULL));
g_assert_true (g_uri_is_valid (" \r http\t://f oo \t\n ", G_URI_FLAGS_NONE, NULL));
g_assert_true (g_uri_is_valid (" \r http\t://f oo \t\n ", G_URI_FLAGS_PARSE_STRICT, NULL));
g_assert_false (g_uri_is_valid (" \r http\t://f oo \t\n ", G_URI_FLAGS_PARSE_STRICT, &error));
g_assert_error (error, G_URI_ERROR, G_URI_ERROR_BAD_SCHEME);
g_clear_error (&error);
g_assert_false (g_uri_is_valid ("http://[::192.9.5.5/ipng", G_URI_FLAGS_NONE, &error));
g_assert_error (error, G_URI_ERROR, G_URI_ERROR_BAD_HOST);