[glib: 12/15] guri: Don’t fail g_uri_is_valid() if URI is missing a hostname




commit 943b1e45ab3134e9028f7eb3c89b13de84f7ebf4
Author: Philip Withnall <withnall endlessm com>
Date:   Thu Aug 6 15:03:18 2020 +0100

    guri: Don’t fail g_uri_is_valid() if URI is missing a hostname
    
    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 <withnall endlessm com>

 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


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]