[glib: 1/8] guri: Move IP-literal parsing out into a separate function




commit 58fce4b92b6fbe7c60f3800e9a19d3b7f594f1ed
Author: Philip Withnall <pwithnall endlessos org>
Date:   Wed Sep 30 17:45:19 2020 +0100

    guri: Move IP-literal parsing out into a separate function
    
    This introduces no functional changes, but will make future changes to
    the code a little cleaner.
    
    Signed-off-by: Philip Withnall <pwithnall endlessos org>

 glib/guri.c | 98 ++++++++++++++++++++++++++++++++++++++++---------------------
 1 file changed, 65 insertions(+), 33 deletions(-)
---
diff --git a/glib/guri.c b/glib/guri.c
index dc885a5d6..73258fdaf 100644
--- a/glib/guri.c
+++ b/glib/guri.c
@@ -442,50 +442,82 @@ _uri_encoder (GString      *out,
     }
 }
 
+/* Parse the IP-literal construction from RFC 6874 (which extends RFC 3986 to
+ * support IPv6 zone identifiers.
+ *
+ * Rules:
+ *
+ * IP-literal = "[" ( IPv6address / IPvFuture  ) "]"
+ *
+ * IP-literal = "[" ( IPv6address / IPv6addrz / IPvFuture  ) "]"
+ *
+ * ZoneID = 1*( unreserved / pct-encoded )
+ *
+ * IPv6addrz = IPv6address "%25" ZoneID
+ */
 static gboolean
-parse_host (const gchar  *start,
-            gsize         length,
-            GUriFlags     flags,
-            gchar       **out,
-            GError      **error)
+parse_ip_literal (const gchar  *start,
+                  gsize         length,
+                  GUriFlags     flags,
+                  gchar       **out,
+                  GError      **error)
 {
-  gchar *decoded, *host, *pct;
+  gchar *pct;
   gchar *addr = NULL;
 
-  if (*start == '[')
+  if (start[length - 1] != ']')
     {
-      if (start[length - 1] != ']')
-        {
-        bad_ipv6_literal:
-          g_free (addr);
-          g_set_error (error, G_URI_ERROR, G_URI_ERROR_BAD_HOST,
-                       _("Invalid IPv6 address ā€˜%.*sā€™ in URI"),
-                       (gint)length, start);
-          return FALSE;
-        }
+    bad_ipv6_literal:
+      g_free (addr);
+      g_set_error (error, G_URI_ERROR, G_URI_ERROR_BAD_HOST,
+                   _("Invalid IPv6 address ā€˜%.*sā€™ in URI"),
+                   (gint)length, start);
+      return FALSE;
+    }
 
-      addr = g_strndup (start + 1, length - 2);
+  addr = g_strndup (start + 1, length - 2);
 
-      /* If there's an IPv6 scope id, ignore it for the moment. */
-      pct = strchr (addr, '%');
-      if (pct)
-        *pct = '\0';
+  /* If there's an IPv6 scope id, ignore it for the moment. */
+  pct = strchr (addr, '%');
+  if (pct)
+    *pct = '\0';
 
-      /* addr must be an IPv6 address */
-      if (!g_hostname_is_ip_address (addr) || !strchr (addr, ':'))
+  /* addr must be an IPv6 address */
+  if (!g_hostname_is_ip_address (addr) || !strchr (addr, ':'))
+    goto bad_ipv6_literal;
+
+  if (pct)
+    {
+      *pct = '%';
+      if (strchr (pct + 1, '%'))
         goto bad_ipv6_literal;
+      /* If the '%' is encoded as '%25' (which it should be), decode it */
+      if (pct[1] == '2' && pct[2] == '5' && pct[3])
+        memmove (pct + 1, pct + 3, strlen (pct + 3) + 1);
+    }
 
-      if (pct)
-        {
-          *pct = '%';
-          if (strchr (pct + 1, '%'))
-            goto bad_ipv6_literal;
-          /* If the '%' is encoded as '%25' (which it should be), decode it */
-          if (pct[1] == '2' && pct[2] == '5' && pct[3])
-            memmove (pct + 1, pct + 3, strlen (pct + 3) + 1);
-        }
+  /* Success */
+  if (out != NULL)
+    *out = g_steal_pointer (&addr);
+  g_free (addr);
+
+  return TRUE;
+}
+
+static gboolean
+parse_host (const gchar  *start,
+            gsize         length,
+            GUriFlags     flags,
+            gchar       **out,
+            GError      **error)
+{
+  gchar *decoded, *host;
+  gchar *addr = NULL;
 
-      host = addr;
+  if (*start == '[')
+    {
+      if (!parse_ip_literal (start, length, flags, &host, error))
+        return FALSE;
       goto ok;
     }
 


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