[glib/wip/danw/guri: 2/2] gio: port to use guri functions



commit 6ac2781bf997aefdc1c0782c508d79a74e6ec200
Author: Dan Winship <danw gnome org>
Date:   Sun Dec 1 15:17:33 2013 -0500

    gio: port to use guri functions

 gio/gicon.c                   |    6 +-
 gio/gnetworkaddress.c         |  297 ++---------------------------------------
 gio/gnetworkingprivate.h      |    9 --
 gio/gnetworkservice.c         |   11 +-
 gio/gproxyaddressenumerator.c |   45 ++----
 gio/gsimpleproxyresolver.c    |   10 +-
 gio/gsocketaddress.c          |    4 +-
 7 files changed, 44 insertions(+), 338 deletions(-)
---
diff --git a/gio/gicon.c b/gio/gicon.c
index b5080da..cf4dbaf 100644
--- a/gio/gicon.c
+++ b/gio/gicon.c
@@ -391,14 +391,14 @@ ensure_builtin_icon_types (void)
 static GIcon *
 g_icon_new_for_string_simple (const gchar *str)
 {
-  gchar *scheme;
+  const gchar *scheme;
   GIcon *icon;
 
   if (str[0] == '.')
     return NULL;
 
   /* handle special GFileIcon and GThemedIcon cases */
-  scheme = g_uri_parse_scheme (str);
+  scheme = g_uri_peek_scheme (str);
   if (scheme != NULL || str[0] == '/' || str[0] == G_DIR_SEPARATOR)
     {
       GFile *location;
@@ -409,8 +409,6 @@ g_icon_new_for_string_simple (const gchar *str)
   else
     icon = g_themed_icon_new (str);
 
-  g_free (scheme);
-
   return icon;
 }
 
diff --git a/gio/gnetworkaddress.c b/gio/gnetworkaddress.c
index 5cb4c74..fc5898d 100644
--- a/gio/gnetworkaddress.c
+++ b/gio/gnetworkaddress.c
@@ -441,279 +441,6 @@ g_network_address_parse (const gchar  *host_and_port,
   return connectable;
 }
 
-/* Allowed characters outside alphanumeric for unreserved. */
-#define G_URI_OTHER_UNRESERVED "-._~"
-
-/* This or something equivalent will eventually go into glib/guri.h */
-gboolean
-_g_uri_parse_authority (const char  *uri,
-                       char       **host,
-                       guint16     *port,
-                       char       **userinfo)
-{
-  char *tmp_str;
-  const char *start, *p, *at, *delim;
-  char c;
-
-  g_return_val_if_fail (uri != NULL, FALSE);
-
-  if (host)
-    *host = NULL;
-
-  if (port)
-    *port = 0;
-
-  if (userinfo)
-    *userinfo = NULL;
-
-  /* From RFC 3986 Decodes:
-   * URI          = scheme ":" hier-part [ "?" query ] [ "#" fragment ]
-   * hier-part    = "//" authority path-abempty
-   * path-abempty = *( "/" segment )
-   * authority    = [ userinfo "@" ] host [ ":" port ]
-   */
-
-  /* Check we have a valid scheme */
-  tmp_str = g_uri_parse_scheme (uri);
-
-  if (tmp_str == NULL)
-    return FALSE;
-
-  g_free (tmp_str);
-
-  /* Decode hier-part:
-   *  hier-part   = "//" authority path-abempty
-   */
-  p = uri;
-  start = strstr (p, "//");
-
-  if (start == NULL)
-    return FALSE;
-
-  start += 2;
-
-  /* check if the @ sign is part of the authority before attempting to
-   * decode the userinfo */
-  delim = strpbrk (start, "/?#[]");
-  at = strchr (start, '@');
-  if (at && delim && at > delim)
-    at = NULL;
-
-  if (at != NULL)
-    {
-      /* Decode userinfo:
-       * userinfo      = *( unreserved / pct-encoded / sub-delims / ":" )
-       * unreserved    = ALPHA / DIGIT / "-" / "." / "_" / "~"
-       * pct-encoded   = "%" HEXDIG HEXDIG
-       */
-      p = start;
-      while (1)
-       {
-         c = *p++;
-
-         if (c == '@')
-           break;
-
-         /* pct-encoded */
-         if (c == '%')
-           {
-             if (!(g_ascii_isxdigit (p[0]) ||
-                   g_ascii_isxdigit (p[1])))
-               return FALSE;
-
-             p++;
-
-             continue;
-           }
-
-         /* unreserved /  sub-delims / : */
-         if (!(g_ascii_isalnum (c) ||
-               strchr (G_URI_OTHER_UNRESERVED, c) ||
-               strchr (G_URI_RESERVED_CHARS_SUBCOMPONENT_DELIMITERS, c) ||
-               c == ':'))
-           return FALSE;
-       }
-
-      if (userinfo)
-       *userinfo = g_strndup (start, p - start - 1);
-
-      start = p;
-    }
-  else
-    {
-      p = start;
-    }
-
-
-  /* decode host:
-   * host          = IP-literal / IPv4address / reg-name
-   * reg-name      = *( unreserved / pct-encoded / sub-delims )
-   */
-
-  /* If IPv6 or IPvFuture */
-  if (*p == '[')
-    {
-      gboolean has_scope_id = FALSE, has_bad_scope_id = FALSE;
-
-      start++;
-      p++;
-      while (1)
-       {
-         c = *p++;
-
-         if (c == ']')
-           break;
-
-          if (c == '%' && !has_scope_id)
-            {
-              has_scope_id = TRUE;
-              if (p[0] != '2' || p[1] != '5')
-                has_bad_scope_id = TRUE;
-              continue;
-            }
-
-         /* unreserved /  sub-delims */
-         if (!(g_ascii_isalnum (c) ||
-               strchr (G_URI_OTHER_UNRESERVED, c) ||
-               strchr (G_URI_RESERVED_CHARS_SUBCOMPONENT_DELIMITERS, c) ||
-               c == ':' ||
-               c == '.'))
-           goto error;
-       }
-
-      if (host)
-        {
-          if (has_bad_scope_id)
-            *host = g_strndup (start, p - start - 1);
-          else
-            *host = g_uri_unescape_segment (start, p - 1, NULL);
-        }
-
-      c = *p++;
-    }
-  else
-    {
-      while (1)
-       {
-         c = *p++;
-
-         if (c == ':' ||
-             c == '/' ||
-             c == '?' ||
-             c == '#' ||
-             c == '\0')
-           break;
-
-         /* pct-encoded */
-         if (c == '%')
-           {
-             if (!(g_ascii_isxdigit (p[0]) ||
-                   g_ascii_isxdigit (p[1])))
-               goto error;
-
-             p++;
-
-             continue;
-           }
-
-         /* unreserved /  sub-delims */
-         if (!(g_ascii_isalnum (c) ||
-               strchr (G_URI_OTHER_UNRESERVED, c) ||
-               strchr (G_URI_RESERVED_CHARS_SUBCOMPONENT_DELIMITERS, c)))
-           goto error;
-       }
-
-      if (host)
-        *host = g_uri_unescape_segment (start, p - 1, NULL);
-    }
-
-  if (c == ':')
-    {
-      /* Decode port:
-       *  port          = *DIGIT
-       */
-      guint tmp = 0;
-
-      while (1)
-       {
-         c = *p++;
-
-         if (c == '/' ||
-             c == '?' ||
-             c == '#' ||
-             c == '\0')
-           break;
-
-         if (!g_ascii_isdigit (c))
-           goto error;
-
-         tmp = (tmp * 10) + (c - '0');
-
-         if (tmp > 65535)
-           goto error;
-       }
-      if (port)
-       *port = (guint16) tmp;
-    }
-
-  return TRUE;
-
-error:
-  if (host && *host)
-    {
-      g_free (*host);
-      *host = NULL;
-    }
-
-  if (userinfo && *userinfo)
-    {
-      g_free (*userinfo);
-      *userinfo = NULL;
-    }
-
-  return FALSE;
-}
-
-gchar *
-_g_uri_from_authority (const gchar *protocol,
-                       const gchar *host,
-                       guint        port,
-                       const gchar *userinfo)
-{
-  GString *uri;
-
-  uri = g_string_new (protocol);
-  g_string_append (uri, "://");
-
-  if (userinfo)
-    {
-      g_string_append_uri_escaped (uri, userinfo, G_URI_RESERVED_CHARS_ALLOWED_IN_USERINFO, FALSE);
-      g_string_append_c (uri, '@');
-    }
-
-  if (g_hostname_is_non_ascii (host))
-    {
-      gchar *ace_encoded = g_hostname_to_ascii (host);
-
-      if (!ace_encoded)
-        {
-          g_string_free (uri, TRUE);
-          return NULL;
-        }
-      g_string_append (uri, ace_encoded);
-      g_free (ace_encoded);
-    }
-  else if (strchr (host, ':'))
-    g_string_append_printf (uri, "[%s]", host);
-  else
-    g_string_append (uri, host);
-
-  if (port != 0)
-    g_string_append_printf (uri, ":%u", port);
-
-  return g_string_free (uri, FALSE);
-}
-
 /**
  * g_network_address_parse_uri:
  * @uri: the hostname and optionally a port
@@ -739,21 +466,21 @@ g_network_address_parse_uri (const gchar  *uri,
   GSocketConnectable *conn;
   gchar *scheme;
   gchar *hostname;
-  guint16 port;
+  gint port;
 
-  if (!_g_uri_parse_authority (uri, &hostname, &port, NULL))
+  if (!g_uri_split_network (uri, G_URI_PARSE_STRICT,
+                            &scheme, &hostname, &port,
+                            NULL))
     {
       g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
-                  "Invalid URI '%s'",
-                  uri);
+                   "Invalid URI '%s'",
+                   uri);
       return NULL;
     }
 
-  if (port == 0)
+  if (port == -1)
     port = default_port;
 
-  scheme = g_uri_parse_scheme (uri);
-
   conn = g_object_new (G_TYPE_NETWORK_ADDRESS,
                        "hostname", hostname,
                        "port", port,
@@ -1058,10 +785,12 @@ g_network_address_connectable_proxy_enumerate (GSocketConnectable *connectable)
   GSocketAddressEnumerator *proxy_enum;
   gchar *uri;
 
-  uri = _g_uri_from_authority (self->priv->scheme ? self->priv->scheme : "none",
-                               self->priv->hostname,
-                               self->priv->port,
-                               NULL);
+  uri = g_uri_join (0,
+                    self->priv->scheme ? self->priv->scheme : "none",
+                    NULL,
+                    self->priv->hostname,
+                    self->priv->port,
+                    "", NULL, NULL);
 
   proxy_enum = g_object_new (G_TYPE_PROXY_ADDRESS_ENUMERATOR,
                              "connectable", connectable,
diff --git a/gio/gnetworkingprivate.h b/gio/gnetworkingprivate.h
index b28ff44..335dd6c 100644
--- a/gio/gnetworkingprivate.h
+++ b/gio/gnetworkingprivate.h
@@ -23,15 +23,6 @@
 
 G_BEGIN_DECLS
 
-gboolean _g_uri_parse_authority            (const char       *uri,
-                                           char            **host,
-                                           guint16          *port,
-                                           char            **userinfo);
-gchar *  _g_uri_from_authority             (const gchar      *protocol,
-                                           const gchar      *host,
-                                           guint             port,
-                                           const gchar      *userinfo);
-
 guint64  g_resolver_get_serial             (GResolver        *resolver);
 
 gint g_socket (gint     domain,
diff --git a/gio/gnetworkservice.c b/gio/gnetworkservice.c
index 497d6fd..30484f2 100644
--- a/gio/gnetworkservice.c
+++ b/gio/gnetworkservice.c
@@ -29,7 +29,7 @@
 #include "ginetsocketaddress.h"
 #include "gioerror.h"
 #include "gnetworkaddress.h"
-#include "gnetworkingprivate.h"
+#include "gnetworking.h"
 #include "gresolver.h"
 #include "gtask.h"
 #include "gsocketaddressenumerator.h"
@@ -463,10 +463,11 @@ g_network_service_address_enumerator_next (GSocketAddressEnumerator  *enumerator
               continue;
             }
 
-          uri = _g_uri_from_authority (g_network_service_get_scheme (srv_enum->srv),
-                                       hostname,
-                                       g_srv_target_get_port (target),
-                                       NULL);
+          uri = g_uri_join (0, g_network_service_get_scheme (srv_enum->srv),
+                            NULL,
+                            hostname,
+                            g_srv_target_get_port (target),
+                            "", NULL, NULL);
           g_free (hostname);
 
           addr = g_network_address_parse_uri (uri,
diff --git a/gio/gproxyaddressenumerator.c b/gio/gproxyaddressenumerator.c
index c34fca9..02e8c30 100644
--- a/gio/gproxyaddressenumerator.c
+++ b/gio/gproxyaddressenumerator.c
@@ -27,7 +27,6 @@
 #include "ginetaddress.h"
 #include "glibintl.h"
 #include "gnetworkaddress.h"
-#include "gnetworkingprivate.h"
 #include "gproxy.h"
 #include "gproxyaddress.h"
 #include "gproxyresolver.h"
@@ -65,7 +64,7 @@ struct _GProxyAddressEnumeratorPrivate
   GSocketAddressEnumerator *addr_enum;
   GSocketAddress           *proxy_address;
   const gchar              *proxy_uri;
-  gchar                    *proxy_type;
+  const gchar              *proxy_type;
   gchar                    *proxy_username;
   gchar                    *proxy_password;
   gboolean                  supports_hostname;
@@ -79,8 +78,6 @@ static void
 save_userinfo (GProxyAddressEnumeratorPrivate *priv,
               const gchar *proxy)
 {
-  gchar *userinfo;
-
   if (priv->proxy_username)
     {
       g_free (priv->proxy_username);
@@ -93,23 +90,14 @@ save_userinfo (GProxyAddressEnumeratorPrivate *priv,
       priv->proxy_password = NULL;
     }
   
-  if (_g_uri_parse_authority (proxy, NULL, NULL, &userinfo))
-    {
-      if (userinfo)
-       {
-         gchar **split = g_strsplit (userinfo, ":", 2);
-
-         if (split[0] != NULL)
-           {
-             priv->proxy_username = g_uri_unescape_string (split[0], NULL);
-             if (split[1] != NULL)
-               priv->proxy_password = g_uri_unescape_string (split[1], NULL);
-           }
-
-         g_strfreev (split);
-         g_free (userinfo);
-       }
-    }
+  g_uri_split_with_user (proxy, 0,
+                         NULL, /* scheme */
+                         &priv->proxy_username,
+                         &priv->proxy_password,
+                         NULL, /* auth params */
+                         NULL, NULL, /* host, port */
+                         NULL, NULL, NULL, /* path, query, fragment */
+                         NULL);
 }
 
 static void
@@ -124,8 +112,7 @@ next_enumerator (GProxyAddressEnumeratorPrivate *priv)
       GProxy *proxy;
 
       priv->proxy_uri = *priv->next_proxy++;
-      g_free (priv->proxy_type);
-      priv->proxy_type = g_uri_parse_scheme (priv->proxy_uri);
+      priv->proxy_type = g_uri_peek_scheme (priv->proxy_uri);
 
       if (priv->proxy_type == NULL)
        continue;
@@ -195,7 +182,7 @@ g_proxy_address_enumerator_next (GSocketAddressEnumerator  *enumerator,
   while (result == NULL && (*priv->next_proxy || priv->addr_enum))
     {
       gchar *dest_hostname;
-      gchar *dest_protocol;
+      const gchar *dest_protocol;
       GInetSocketAddress *inetsaddr;
       GInetAddress *inetaddr;
       guint16 port;
@@ -269,7 +256,7 @@ g_proxy_address_enumerator_next (GSocketAddressEnumerator  *enumerator,
        {
          dest_hostname = g_strdup (priv->dest_hostname);
        }
-      dest_protocol = g_uri_parse_scheme (priv->dest_uri);
+      dest_protocol = g_uri_peek_scheme (priv->dest_uri);
                                  
       g_return_val_if_fail (G_IS_INET_SOCKET_ADDRESS (priv->proxy_address),
                            NULL);
@@ -290,7 +277,6 @@ g_proxy_address_enumerator_next (GSocketAddressEnumerator  *enumerator,
                             "uri", priv->proxy_uri,
                             NULL);
       g_free (dest_hostname);
-      g_free (dest_protocol);
 
       if (priv->supports_hostname || priv->next_dest_ip == NULL)
        {
@@ -338,7 +324,8 @@ return_result (GTask *task)
     }
   else
     {
-      gchar *dest_hostname, *dest_protocol;
+      gchar *dest_hostname;
+      const gchar *dest_protocol;
       GInetSocketAddress *inetsaddr;
       GInetAddress *inetaddr;
       guint16 port;
@@ -359,7 +346,7 @@ return_result (GTask *task)
        {
          dest_hostname = g_strdup (priv->dest_hostname);
        }
-      dest_protocol = g_uri_parse_scheme (priv->dest_uri);
+      dest_protocol = g_uri_peek_scheme (priv->dest_uri);
 
       g_return_if_fail (G_IS_INET_SOCKET_ADDRESS (priv->proxy_address));
 
@@ -379,7 +366,6 @@ return_result (GTask *task)
                             "uri", priv->proxy_uri,
                             NULL);
       g_free (dest_hostname);
-      g_free (dest_protocol);
 
       if (priv->supports_hostname || priv->next_dest_ip == NULL)
        {
@@ -683,7 +669,6 @@ g_proxy_address_enumerator_finalize (GObject *object)
   if (priv->addr_enum)
     g_object_unref (priv->addr_enum);
 
-  g_free (priv->proxy_type);
   g_free (priv->proxy_username);
   g_free (priv->proxy_password);
 
diff --git a/gio/gsimpleproxyresolver.c b/gio/gsimpleproxyresolver.c
index 8610e2b..3453142 100644
--- a/gio/gsimpleproxyresolver.c
+++ b/gio/gsimpleproxyresolver.c
@@ -25,7 +25,6 @@
 #include "gsimpleproxyresolver.h"
 #include "ginetaddress.h"
 #include "ginetaddressmask.h"
-#include "gnetworkingprivate.h"
 #include "gtask.h"
 
 #include "glibintl.h"
@@ -325,10 +324,13 @@ g_simple_proxy_resolver_lookup (GProxyResolver  *proxy_resolver,
   if (priv->ignore_ips || priv->ignore_domains)
     {
       gchar *host = NULL;
-      gushort port;
+      gint port;
 
-      if (_g_uri_parse_authority (uri, &host, &port, NULL) &&
-          ignore_host (resolver, host, port))
+      if (g_uri_split (uri, 0, NULL, NULL,
+                       &host, &port,
+                       NULL, NULL, NULL,
+                       NULL) &&
+          ignore_host (resolver, host, CLAMP (port, 0, 65535)))
         proxy = "direct://";
 
       g_free (host);
diff --git a/gio/gsocketaddress.c b/gio/gsocketaddress.c
index 676d94a..740fac6 100644
--- a/gio/gsocketaddress.c
+++ b/gio/gsocketaddress.c
@@ -26,7 +26,7 @@
 #include "gsocketaddress.h"
 #include "ginetaddress.h"
 #include "ginetsocketaddress.h"
-#include "gnetworkingprivate.h"
+#include "gnetworking.h"
 #include "gproxyaddress.h"
 #include "gproxyaddressenumerator.h"
 #include "gsocketaddressenumerator.h"
@@ -396,7 +396,7 @@ g_socket_address_connectable_proxy_enumerate (GSocketConnectable *connectable)
       g_object_get (connectable, "address", &addr, "port", &port, NULL);
 
       ip = g_inet_address_to_string (addr);
-      uri = _g_uri_from_authority ("none", ip, port, NULL);
+      uri = g_uri_join (0, "none", NULL, ip, port, "", NULL, NULL);
 
       addr_enum = g_object_new (G_TYPE_PROXY_ADDRESS_ENUMERATOR,
                                "connectable", connectable,


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