[glib: 1/2] uri: add ENCODED_PATH & ENCODED_FRAGMENT flags




commit c9c349aeaa31d2d3784271fc1b33d45d9c5ec2d1
Author: Marc-André Lureau <marcandre lureau redhat com>
Date:   Mon Jul 27 19:56:12 2020 +0400

    uri: add ENCODED_PATH & ENCODED_FRAGMENT flags
    
    Add encoded flags, similar to what was done in commit 7bee36b4 ("uri:
    add G_FLAGS_ENCODED_QUERY").
    
    SoupURI has manual handling of encoded path & fragment, but it can rely
    on GUri decoding for the rest.
    
    Signed-off-by: Marc-André Lureau <marcandre lureau redhat com>

 glib/guri.c      | 12 +++++++-----
 glib/guri.h      |  5 +++++
 glib/tests/uri.c | 28 ++++++++++++++++++++++++++++
 3 files changed, 40 insertions(+), 5 deletions(-)
---
diff --git a/glib/guri.c b/glib/guri.c
index cffed414c..528fc14bf 100644
--- a/glib/guri.c
+++ b/glib/guri.c
@@ -787,8 +787,9 @@ g_uri_split_internal (const gchar  *uri_string,
   end = p + strcspn (p, "#");
   if (*end == '#')
     {
-      if (!uri_decode (fragment, NULL, end + 1, strlen (end + 1), FALSE, flags,
-                       G_URI_ERROR_BAD_FRAGMENT, error))
+      if (!uri_normalize (fragment, end + 1, strlen (end + 1),
+                          flags | (flags & G_URI_FLAGS_ENCODED_FRAGMENT ? G_URI_FLAGS_ENCODED : 0),
+                          G_URI_ERROR_BAD_FRAGMENT, error))
         goto fail;
     }
 
@@ -803,7 +804,8 @@ g_uri_split_internal (const gchar  *uri_string,
       end = question;
     }
 
-  if (!uri_normalize (path, p, end - p, flags,
+  if (!uri_normalize (path, p, end - p,
+                      flags | (flags & G_URI_FLAGS_ENCODED_PATH ? G_URI_FLAGS_ENCODED : 0),
                       G_URI_ERROR_BAD_PATH, error))
     goto fail;
 
@@ -1404,7 +1406,7 @@ g_uri_join_internal (GUriFlags    flags,
         g_string_append_printf (str, ":%d", port);
     }
 
-  if (encoded)
+  if (encoded || flags & G_URI_FLAGS_ENCODED_PATH)
     g_string_append (str, path);
   else
     g_string_append_uri_escaped (str, path, PATH_ALLOWED_CHARS, TRUE);
@@ -1420,7 +1422,7 @@ g_uri_join_internal (GUriFlags    flags,
   if (fragment)
     {
       g_string_append_c (str, '#');
-      if (encoded)
+      if (encoded || flags & G_URI_FLAGS_ENCODED_FRAGMENT)
         g_string_append (str, fragment);
       else
         g_string_append_uri_escaped (str, fragment, FRAGMENT_ALLOWED_CHARS, TRUE);
diff --git a/glib/guri.h b/glib/guri.h
index a9a4cd57e..da0bc9bc4 100644
--- a/glib/guri.h
+++ b/glib/guri.h
@@ -55,6 +55,9 @@ void         g_uri_unref            (GUri *uri);
  *     should not do any encoding itself.
  * @G_URI_FLAGS_ENCODED_QUERY: Same as %G_URI_FLAGS_ENCODED, for the query
  *     field only.
+ * @G_URI_FLAGS_ENCODED_PATH: Same as %G_URI_FLAGS_ENCODED, for the path only.
+ * @G_URI_FLAGS_ENCODED_FRAGMENT: Same as %G_URI_FLAGS_ENCODED, for the
+ *     fragment only.
  * @G_URI_FLAGS_NONE: No flags set.
  *
  * Flags that describe a URI.
@@ -75,6 +78,8 @@ typedef enum {
   G_URI_FLAGS_ENCODED         = 1 << 3,
   G_URI_FLAGS_NON_DNS         = 1 << 4,
   G_URI_FLAGS_ENCODED_QUERY   = 1 << 5,
+  G_URI_FLAGS_ENCODED_PATH    = 1 << 6,
+  G_URI_FLAGS_ENCODED_FRAGMENT = 1 << 7,
 } GUriFlags;
 
 GLIB_AVAILABLE_IN_2_66
diff --git a/glib/tests/uri.c b/glib/tests/uri.c
index 83279747a..4c5120424 100644
--- a/glib/tests/uri.c
+++ b/glib/tests/uri.c
@@ -1115,6 +1115,34 @@ test_uri_split (void)
   g_free (host);
   g_free (query);
 
+  g_uri_split ("http://h%01st/%C3%89t%C3%A9%2Bhiver";,
+               G_URI_FLAGS_ENCODED_PATH,
+               NULL,
+               NULL,
+               NULL,
+               NULL,
+               &path,
+               NULL,
+               NULL,
+               &error);
+  g_assert_no_error (error);
+  g_assert_cmpstr (path, ==, "/%C3%89t%C3%A9%2Bhiver");
+  g_free (path);
+
+  g_uri_split ("http://h%01st/path#%C3%89t%C3%A9%2Bhiver";,
+               G_URI_FLAGS_ENCODED_FRAGMENT,
+               NULL,
+               NULL,
+               NULL,
+               NULL,
+               NULL,
+               NULL,
+               &fragment,
+               &error);
+  g_assert_no_error (error);
+  g_assert_cmpstr (fragment, ==, "%C3%89t%C3%A9%2Bhiver");
+  g_free (fragment);
+
   g_uri_split_with_user ("scheme://user:pass;auth@host:1234/path?query#fragment",
                          G_URI_FLAGS_HAS_AUTH_PARAMS|G_URI_FLAGS_HAS_PASSWORD,
                          NULL,


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