pidgin: 35146492: jabber: Use a better method for dealing ...

darkrain42 at pidgin.im darkrain42 at pidgin.im
Fri Aug 28 23:00:48 EDT 2009


-----------------------------------------------------------------
Revision: 35146492fae55977da91e4562590c279918913fc
Ancestor: 92715526a86bcd996399908add5b5ab17faee600
Author: darkrain42 at pidgin.im
Date: 2009-08-29T02:38:28
Branch: im.pidgin.pidgin
URL: http://d.pidgin.im/viewmtn/revision/info/35146492fae55977da91e4562590c279918913fc

Modified files:
        libpurple/protocols/jabber/jutil.c
        libpurple/tests/test_jabber_jutil.c

ChangeLog: 

jabber: Use a better method for dealing with terminating slashes in JIDs.

This saves an allocation/free in jabber_normalize

-------------- next part --------------
============================================================
--- libpurple/protocols/jabber/jutil.c	ac608eab4a23bf8aac38a987c7e1c36cc654938f
+++ libpurple/protocols/jabber/jutil.c	694a03e07125889652733d996810266608851f69
@@ -277,8 +277,8 @@ gboolean jabber_resourceprep_validate(co
 #endif /* USE_IDN */
 }
 
-JabberID*
-jabber_id_new(const char *str)
+static JabberID*
+jabber_id_new_internal(const char *str, gboolean allow_terminating_slash)
 {
 	const char *at = NULL;
 	const char *slash = NULL;
@@ -323,7 +323,7 @@ jabber_id_new(const char *str)
 						/* JIDs cannot start with / */
 						return NULL;
 					}
-					if (c[1] == '\0') {
+					if (c[1] == '\0' && !allow_terminating_slash) {
 						/* JIDs cannot end with / */
 						return NULL;
 					}
@@ -386,14 +386,16 @@ jabber_id_new(const char *str)
 			jid->node = g_ascii_strdown(str, at - str);
 			if (slash) {
 				jid->domain = g_ascii_strdown(at + 1, slash - (at + 1));
-				jid->resource = g_strdup(slash + 1);
+				if (*(slash + 1))
+					jid->resource = g_strdup(slash + 1);
 			} else {
 				jid->domain = g_ascii_strdown(at + 1, -1);
 			}
 		} else {
 			if (slash) {
 				jid->domain = g_ascii_strdown(str, slash - str);
-				jid->resource = g_strdup(slash + 1);
+				if (*(slash + 1))
+					jid->resource = g_strdup(slash + 1);
 			} else {
 				jid->domain = g_ascii_strdown(str, -1);
 			}
@@ -421,14 +423,16 @@ jabber_id_new(const char *str)
 		node = g_utf8_casefold(str, at-str);
 		if(slash) {
 			domain = g_utf8_casefold(at+1, slash-(at+1));
-			jid->resource = g_utf8_normalize(slash+1, -1, G_NORMALIZE_NFKC);
+			if (*(slash + 1))
+				jid->resource = g_utf8_normalize(slash+1, -1, G_NORMALIZE_NFKC);
 		} else {
 			domain = g_utf8_casefold(at+1, -1);
 		}
 	} else {
 		if(slash) {
 			domain = g_utf8_casefold(str, slash-str);
-			jid->resource = g_utf8_normalize(slash+1, -1, G_NORMALIZE_NFKC);
+			if (*(slash + 1))
+				jid->resource = g_utf8_normalize(slash+1, -1, G_NORMALIZE_NFKC);
 		} else {
 			domain = g_utf8_casefold(str, -1);
 		}
@@ -500,30 +504,20 @@ char *jabber_get_bare_jid(const char *in
 	return out;
 }
 
+JabberID *
+jabber_id_new(const char *str)
+{
+	return jabber_id_new_internal(str, FALSE);
+}
+
 const char *jabber_normalize(const PurpleAccount *account, const char *in)
 {
 	PurpleConnection *gc = account ? account->gc : NULL;
 	JabberStream *js = gc ? gc->proto_data : NULL;
 	static char buf[3072]; /* maximum legal length of a jabber jid */
 	JabberID *jid;
-	char *tmp;
-	size_t len = strlen(in);
 
-	/*
-	 * If the JID ends with a '/', jabber_id_new is going to throw it away as
-	 * invalid.  However, this is what the UI generates for a JID with no
-	 * resource. Deal with that by dropping away the '/'...
-	 */
-	if (in[len - 1] == '/')
-		tmp = g_strndup(in, len - 1);
-	else
-		tmp = (gchar *)in;
-
-	jid = jabber_id_new(tmp);
-
-	if (tmp != in)
-		g_free(tmp);
-
+	jid = jabber_id_new_internal(in, TRUE);
 	if(!jid)
 		return NULL;
 
============================================================
--- libpurple/tests/test_jabber_jutil.c	71f9a6dcb303b95056187127e7d92d1f42fa68e1
+++ libpurple/tests/test_jabber_jutil.c	38e36ccd4fbf2682828c201fe178cd637217fc25
@@ -153,6 +153,14 @@ END_TEST
 }
 END_TEST
 
+START_TEST(test_jabber_normalize)
+{
+	assert_string_equal("paul at darkrain42.org", jabber_normalize(NULL, "PaUL at DaRkRain42.org"));
+	assert_string_equal("paul at darkrain42.org", jabber_normalize(NULL, "PaUL at DaRkRain42.org/"));
+	assert_string_equal("paul at darkrain42.org", jabber_normalize(NULL, "PaUL at DaRkRain42.org/resource"));
+}
+END_TEST
+
 Suite *
 jabber_jutil_suite(void)
 {
@@ -172,6 +180,7 @@ jabber_jutil_suite(void)
 	tcase_add_test(tc, test_nodeprep_validate_illegal_chars);
 	tcase_add_test(tc, test_nodeprep_validate_too_long);
 	tcase_add_test(tc, test_jabber_id_new);
+	tcase_add_test(tc, test_jabber_normalize);
 	suite_add_tcase(s, tc);
 
 	return s;


More information about the Commits mailing list