[libsoup] soup_uri_normalize: more fixes



commit 3c3b2fe1e3fb103d9c37702971dad39a768142e2
Author: Dan Winship <danw gnome org>
Date:   Tue Jul 17 10:11:07 2012 -0400

    soup_uri_normalize: more fixes
    
    uri_normalized_copy() should not go into fixup mode when it sees a
    non-printable character if that character is in @unescape_extra.
    Likewise, if it does go into fixup mode, it shouldn't fix up the
    @unescape_extra characters.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=680018

 libsoup/soup-uri.c  |   11 ++++++++---
 tests/uri-parsing.c |   48 ++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 56 insertions(+), 3 deletions(-)
---
diff --git a/libsoup/soup-uri.c b/libsoup/soup-uri.c
index 382a2a9..4be679d 100644
--- a/libsoup/soup-uri.c
+++ b/libsoup/soup-uri.c
@@ -693,6 +693,9 @@ uri_normalized_copy (const char *part, int length,
 	char *normalized = g_strndup (part, length);
 	gboolean need_fixup = FALSE;
 
+	if (!unescape_extra)
+		unescape_extra = "";
+
 	s = d = (unsigned char *)normalized;
 	while (*s) {
 		if (*s == '%') {
@@ -704,7 +707,7 @@ uri_normalized_copy (const char *part, int length,
 
 			c = HEXCHAR (s);
 			if (soup_char_is_uri_unreserved (c) ||
-			    (unescape_extra && strchr (unescape_extra, c))) {
+			    strchr (unescape_extra, c)) {
 				*d++ = c;
 				s += 3;
 			} else {
@@ -717,7 +720,8 @@ uri_normalized_copy (const char *part, int length,
 				*d++ = *s++;
 			}
 		} else {
-			if (!g_ascii_isgraph (*s))
+			if (!g_ascii_isgraph (*s) &&
+			    !strchr (unescape_extra, *s))
 				need_fixup = TRUE;
 			*d++ = *s++;
 		}
@@ -730,7 +734,8 @@ uri_normalized_copy (const char *part, int length,
 		fixed = g_string_new (NULL);
 		s = (guchar *)normalized;
 		while (*s) {
-			if (g_ascii_isgraph (*s))
+			if (g_ascii_isgraph (*s) ||
+			    strchr (unescape_extra, *s))
 				g_string_append_c (fixed, *s);
 			else
 				g_string_append_printf (fixed, "%%%02X", (int)*s);
diff --git a/tests/uri-parsing.c b/tests/uri-parsing.c
index 124dfe9..08274a2 100644
--- a/tests/uri-parsing.c
+++ b/tests/uri-parsing.c
@@ -497,6 +497,53 @@ do_soup_uri_null_tests (void)
 	soup_uri_free (uri);
 }
 
+static struct {
+	const char *uri_string, *unescape_extra, *result;
+} normalization_tests[] = {
+	{ "fo%6fbar",         NULL, "foobar" },
+	{ "foo%2fbar",        NULL, "foo%2fbar" },
+	{ "foo%2Fbar",        NULL, "foo%2Fbar" },
+	{ "foo%2fbar",        "/",  "foo/bar" },
+	{ "foo bar",          NULL, "foo%20bar" },
+	{ "foo bar",          " ",  "foo bar" },
+	{ "fo\xc3\xb6" "bar", NULL, "fo%C3%B6bar" },
+	{ "fo\xc3\xb6 bar",   " ",  "fo%C3%B6 bar" }
+};
+static int num_normalization_tests = G_N_ELEMENTS (normalization_tests);
+
+static void
+do_normalization_tests (void)
+{
+	char *normalized;
+	int i;
+
+	debug_printf (1, "\nsoup_uri_normalize\n");
+
+	for (i = 0; i < num_normalization_tests; i++) {
+		if (normalization_tests[i].unescape_extra) {
+			debug_printf (1, "<%s> unescaping <%s> => <%s>: ",
+				      normalization_tests[i].uri_string,
+				      normalization_tests[i].unescape_extra,
+				      normalization_tests[i].result);
+		} else {
+			debug_printf (1, "<%s> => <%s>: ",
+				      normalization_tests[i].uri_string,
+				      normalization_tests[i].result);
+		}
+
+		normalized = soup_uri_normalize (normalization_tests[i].uri_string,
+						 normalization_tests[i].unescape_extra);
+
+		if (!strcmp (normalized, normalization_tests[i].result))
+			debug_printf (1, "OK\n");
+		else {
+			debug_printf (1, "NO, got <%s>\n", normalized);
+			errors++;
+		}
+		g_free (normalized);
+	}
+}
+
 int
 main (int argc, char **argv)
 {
@@ -554,6 +601,7 @@ main (int argc, char **argv)
 	}
 
 	do_soup_uri_null_tests ();
+	do_normalization_tests ();
 
 	test_cleanup ();
 	return errors != 0;



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