soup-uri: %-encode non-ASCII characters when parsing URIs
authorDan Winship <danw@gnome.org>
Sat, 12 Nov 2011 14:27:33 +0000 (09:27 -0500)
committerDan Winship <danw@gnome.org>
Sat, 12 Nov 2011 14:27:33 +0000 (09:27 -0500)
https://bugzilla.gnome.org/show_bug.cgi?id=662806

libsoup/soup-uri.c
tests/uri-parsing.c

index 1aa801b..9170feb 100644 (file)
@@ -673,7 +673,7 @@ uri_normalized_copy (const char *part, int length,
                                *d++ = *s;
                        }
                } else {
-                       if (*s == ' ')
+                       if (!g_ascii_isgraph (*s))
                                need_fixup = TRUE;
                        *d++ = *s;
                }
@@ -681,16 +681,16 @@ uri_normalized_copy (const char *part, int length,
 
        if (need_fixup) {
                GString *fixed;
-               char *sp, *p;
 
                fixed = g_string_new (NULL);
-               p = normalized;
-               while ((sp = strchr (p, ' '))) {
-                       g_string_append_len (fixed, p, sp - p);
-                       g_string_append (fixed, "%20");
-                       p = sp + 1;
+               s = (guchar *)normalized;
+               while (*s) {
+                       if (g_ascii_isgraph (*s))
+                               g_string_append_c (fixed, *s);
+                       else
+                               g_string_append_printf (fixed, "%%%02X", (int)*s);
+                       s++;
                }
-               g_string_append (fixed, p);
                g_free (normalized);
                normalized = g_string_free (fixed, FALSE);
        }
index e8568a9..285b41e 100644 (file)
@@ -76,7 +76,10 @@ static struct {
        { "http://host/path%%%", "http://host/path%%%" },
        { "http://host/path%/x/", "http://host/path%/x/" },
        { "http://host/path%0x/", "http://host/path%0x/" },
-       { "http://host/path%ax", "http://host/path%ax" }
+       { "http://host/path%ax", "http://host/path%ax" },
+
+       /* Bug 662806; %-encode non-ASCII characters */
+       { "http://host/p\xc3\xa4th/", "http://host/p%C3%A4th/" }
 };
 static int num_abs_tests = G_N_ELEMENTS(abs_tests);