From 943b1e45ab3134e9028f7eb3c89b13de84f7ebf4 Mon Sep 17 00:00:00 2001 From: Philip Withnall Date: Thu, 6 Aug 2020 15:03:18 +0100 Subject: [PATCH] =?UTF-8?q?guri:=20Don=E2=80=99t=20fail=20g=5Furi=5Fis=5Fv?= =?UTF-8?q?alid()=20if=20URI=20is=20missing=20a=20hostname?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit According to my reading of https://tools.ietf.org/html/rfc3986#section-4, the only requirement for a URI to be ‘absolute’ (actually, not a relative reference) is for the scheme to be specified. A hostname doesn’t have to be specified: see any of the options in the `hier-part` production in https://tools.ietf.org/html/rfc3986#appendix-A which don’t include `authority`. Signed-off-by: Philip Withnall --- glib/guri.c | 20 +++++++++++++++++++- glib/tests/uri.c | 2 ++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/glib/guri.c b/glib/guri.c index 508fd69e4..bcbce6021 100644 --- a/glib/guri.c +++ b/glib/guri.c @@ -1101,10 +1101,28 @@ g_uri_is_valid (const gchar *uri_string, GUriFlags flags, GError **error) { + gchar *my_scheme = NULL; + g_return_val_if_fail (uri_string != NULL, FALSE); g_return_val_if_fail (error == NULL || *error == NULL, FALSE); - return g_uri_split_network (uri_string, flags, NULL, NULL, NULL, error); + if (!g_uri_split_internal (uri_string, flags, + &my_scheme, NULL, NULL, NULL, NULL, + NULL, NULL, NULL, NULL, NULL, + error)) + return FALSE; + + if (!my_scheme) + { + g_set_error (error, G_URI_ERROR, G_URI_ERROR_BAD_SCHEME, + _("URI ‘%s’ is not an absolute URI"), + uri_string); + return FALSE; + } + + g_free (my_scheme); + + return TRUE; } diff --git a/glib/tests/uri.c b/glib/tests/uri.c index 85d2eb19d..cb765ea3c 100644 --- a/glib/tests/uri.c +++ b/glib/tests/uri.c @@ -1352,6 +1352,8 @@ test_uri_is_valid (void) g_assert_false (g_uri_is_valid ("http://host:6553l", G_URI_FLAGS_NONE, &error)); g_assert_error (error, G_URI_ERROR, G_URI_ERROR_BAD_PORT); g_clear_error (&error); + + g_assert_true (g_uri_is_valid ("data:,Hello", G_URI_FLAGS_NONE, &error)); } static const struct