[asterisk-commits] mmichelson: branch mmichelson/pubsub_bodies r406131 - in /team/mmichelson/pub...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Tue Jan 21 15:30:48 CST 2014


Author: mmichelson
Date: Tue Jan 21 15:30:45 2014
New Revision: 406131

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=406131
Log:
Fix XML errors for XPDIF bodies.

At this point, I am satisfied that the feature works, and so
I will be moving on to writing automated tests.


Modified:
    team/mmichelson/pubsub_bodies/include/asterisk/res_pjsip_presence_xml.h
    team/mmichelson/pubsub_bodies/res/res_pjsip/presence_xml.c
    team/mmichelson/pubsub_bodies/res/res_pjsip_pidf_body_generator.c
    team/mmichelson/pubsub_bodies/res/res_pjsip_xpidf_body_generator.c

Modified: team/mmichelson/pubsub_bodies/include/asterisk/res_pjsip_presence_xml.h
URL: http://svnview.digium.com/svn/asterisk/team/mmichelson/pubsub_bodies/include/asterisk/res_pjsip_presence_xml.h?view=diff&rev=406131&r1=406130&r2=406131
==============================================================================
--- team/mmichelson/pubsub_bodies/include/asterisk/res_pjsip_presence_xml.h (original)
+++ team/mmichelson/pubsub_bodies/include/asterisk/res_pjsip_presence_xml.h Tue Jan 21 15:30:45 2014
@@ -28,7 +28,22 @@
 	NOTIFY_CLOSED
 };
 
-/*
+/*!
+ * \brief Replace offensive XML characters with XML entities
+ *
+ * " = "
+ * < = <
+ * > = >
+ * ' = '
+ * & = &
+ *
+ * \param input String to sanitize
+ * \param[out] output Sanitized string
+ * \param len Size of output buffer
+ */
+void ast_sip_sanitize_xml(const char *input, char *output, size_t len);
+
+/*!
  * \brief Convert extension state to relevant PIDF strings
  *
  * \param state The extension state
@@ -76,7 +91,10 @@
  * \param parent Starting node for search
  * \param node_name Name of node where to find attribute
  * \param attr_name Name of attribute to find
+ * \param[out] node Node that was found or created
+ * \param[out] attr Attribute that was found or created
  * \return The found attribute
  */
-pj_xml_attr *ast_sip_presence_xml_find_node_attr(pj_pool_t* pool, 
-		pj_xml_node *parent, const char *node_name, const char *attr_name);
+void ast_sip_presence_xml_find_node_attr(pj_pool_t* pool, 
+		pj_xml_node *parent, const char *node_name, const char *attr_name,
+		pj_xml_node **node, pj_xml_attr **attr);

Modified: team/mmichelson/pubsub_bodies/res/res_pjsip/presence_xml.c
URL: http://svnview.digium.com/svn/asterisk/team/mmichelson/pubsub_bodies/res/res_pjsip/presence_xml.c?view=diff&rev=406131&r1=406130&r2=406131
==============================================================================
--- team/mmichelson/pubsub_bodies/res/res_pjsip/presence_xml.c (original)
+++ team/mmichelson/pubsub_bodies/res/res_pjsip/presence_xml.c Tue Jan 21 15:30:45 2014
@@ -35,6 +35,46 @@
 #include "asterisk/res_pjsip_pubsub.h"
 #include "asterisk/res_pjsip_presence_xml.h"
 #include "asterisk/res_pjsip_body_generator_types.h"
+
+void ast_sip_sanitize_xml(const char *input, char *output, size_t len)
+{
+	char *copy = ast_strdupa(input);
+	char *break_point;
+
+	output[0] = '\0';
+
+	while ((break_point = strpbrk(copy, "<>\"&'"))) {
+		char to_escape = *break_point;
+
+		*break_point = '\0';
+		strncat(output, copy, len);
+
+		switch (to_escape) {
+		case '<':
+			strncat(output, "<", len);
+			break;
+		case '>':
+			strncat(output, ">", len);
+			break;
+		case '"':
+			strncat(output, """, len);
+			break;
+		case '&':
+			strncat(output, "&", len);
+			break;
+		case '\'':
+			strncat(output, "'", len);
+			break;
+		};
+
+		copy = break_point + 1;
+	}
+
+	/* Be sure to copy everything after the final bracket */
+	if (*copy) {
+		strncat(output, copy, len);
+	}
+}
 
 void ast_sip_presence_exten_state_to_str(int state, char **statestring, char **pidfstate,
 			       char **pidfnote, enum ast_sip_pidf_state *local_state)
@@ -110,20 +150,17 @@
 	return node;
 }
 
-pj_xml_attr *ast_sip_presence_xml_find_node_attr(pj_pool_t* pool,
-		pj_xml_node *parent, const char *node_name, const char *attr_name)
+void ast_sip_presence_xml_find_node_attr(pj_pool_t* pool,
+		pj_xml_node *parent, const char *node_name, const char *attr_name,
+		pj_xml_node **node, pj_xml_attr **attr)
 {
 	pj_str_t name;
-	pj_xml_node *node;
-	pj_xml_attr *attr;
 
-	if (!(node = pj_xml_find_node(parent, pj_cstr(&name, node_name)))) {
-		node = ast_sip_presence_xml_create_node(pool, parent, node_name);
+	if (!(*node = pj_xml_find_node(parent, pj_cstr(&name, node_name)))) {
+		*node = ast_sip_presence_xml_create_node(pool, parent, node_name);
 	}
 
-	if (!(attr = pj_xml_find_attr(node, pj_cstr(&name, attr_name), NULL))) {
-		attr = ast_sip_presence_xml_create_attr(pool, node, attr_name, "");
+	if (!(*attr = pj_xml_find_attr(*node, pj_cstr(&name, attr_name), NULL))) {
+		*attr = ast_sip_presence_xml_create_attr(pool, *node, attr_name, "");
 	}
-
-	return attr;
 }

Modified: team/mmichelson/pubsub_bodies/res/res_pjsip_pidf_body_generator.c
URL: http://svnview.digium.com/svn/asterisk/team/mmichelson/pubsub_bodies/res/res_pjsip_pidf_body_generator.c?view=diff&rev=406131&r1=406130&r2=406131
==============================================================================
--- team/mmichelson/pubsub_bodies/res/res_pjsip_pidf_body_generator.c (original)
+++ team/mmichelson/pubsub_bodies/res/res_pjsip_pidf_body_generator.c Tue Jan 21 15:30:45 2014
@@ -72,42 +72,6 @@
 	ast_sip_presence_xml_create_attr(pool, node, XMLNS_EP, XMLNS_RPID_PERSON);
 }
 
-/*!
- * \internal
- * \brief Convert angle brackets in input into escaped forms suitable for XML
- *
- * \param input Raw input string
- * \param output Sanitized string
- * \param len Size of output buffer
- */
-static void sanitize_xml(const char *input, char *output, size_t len)
-{
-	char *copy = ast_strdupa(input);
-	char *break_point;
-
-	output[0] = '\0';
-
-	while ((break_point = strpbrk(copy, "<>"))) {
-		char bracket = *break_point;
-
-		*break_point = '\0';
-		strncat(output, copy, len);
-
-		if (bracket == '<') {
-			strncat(output, "<", len);
-		} else {
-			strncat(output, "&rt;", len);
-		}
-
-		copy = break_point + 1;
-	}
-
-	/* Be sure to copy everything after the final bracket */
-	if (*copy) {
-		strncat(output, copy, len);
-	}
-}
-
 static void *pidf_allocate_body(void *data)
 {
 	struct ast_sip_exten_state_data *state_data = data;
@@ -145,7 +109,7 @@
 		return -1;
 	}
 
-	sanitize_xml(state_data->remote, sanitized, sizeof(sanitized));
+	ast_sip_sanitize_xml(state_data->remote, sanitized, sizeof(sanitized));
 	pjpidf_tuple_set_contact(state_data->pool, tuple, pj_cstr(&contact, sanitized));
 	pjpidf_tuple_set_contact_prio(state_data->pool, tuple, pj_cstr(&priority, "1"));
 	pjpidf_status_set_basic_open(pjpidf_tuple_get_status(tuple),

Modified: team/mmichelson/pubsub_bodies/res/res_pjsip_xpidf_body_generator.c
URL: http://svnview.digium.com/svn/asterisk/team/mmichelson/pubsub_bodies/res/res_pjsip_xpidf_body_generator.c?view=diff&rev=406131&r1=406130&r2=406131
==============================================================================
--- team/mmichelson/pubsub_bodies/res/res_pjsip_xpidf_body_generator.c (original)
+++ team/mmichelson/pubsub_bodies/res/res_pjsip_xpidf_body_generator.c Tue Jan 21 15:30:45 2014
@@ -54,34 +54,41 @@
 	char *statestring = NULL, *pidfstate = NULL, *pidfnote = NULL;
 	pj_xml_attr *attr;
 	enum ast_sip_pidf_state local_state;
-	pj_str_t name, uri;
+	pj_str_t uri;
+	char sanitized[PJSIP_MAX_URL_SIZE];
+	pj_xml_node *atom;
+	pj_xml_node *address;
+	pj_xml_node *status;
+	pj_xml_node *msnsubstatus;
 
 	ast_sip_presence_exten_state_to_str(state_data->exten_state, &statestring,
 			&pidfstate, &pidfnote, &local_state);
 
-	attr = ast_sip_presence_xml_find_node_attr(state_data->pool, pres, "atom", "id");
+	ast_sip_presence_xml_find_node_attr(state_data->pool, pres, "atom", "id", 
+			&atom, &attr);
 	pj_strdup2(state_data->pool, &attr->value, state_data->exten);
 
-	attr = ast_sip_presence_xml_find_node_attr(state_data->pool, pres, "address", "uri");
+	ast_sip_presence_xml_find_node_attr(state_data->pool, atom, "address",
+			"uri", &address, &attr);
+
+	ast_sip_sanitize_xml(state_data->remote, sanitized, sizeof(sanitized));
 
 	uri.ptr = (char*) pj_pool_alloc(state_data->pool,
-			strlen(state_data->remote) + STR_ADDR_PARAM.slen);
-	pj_strcpy2( &uri, state_data->remote);
+			strlen(sanitized) + STR_ADDR_PARAM.slen);
+	pj_strcpy2( &uri, sanitized);
 	pj_strcat( &uri, &STR_ADDR_PARAM);
 	pj_strdup(state_data->pool, &attr->value, &uri);
 
-	ast_sip_presence_xml_create_attr(state_data->pool,
-			pj_xml_find_node(pres, pj_cstr(&name, "address")),
-		    "priority", "0.80000");
+	ast_sip_presence_xml_create_attr(state_data->pool, address, "priority", "0.80000");
 
-	attr = ast_sip_presence_xml_find_node_attr(state_data->pool, pres,
-			"status", "status");
+	ast_sip_presence_xml_find_node_attr(state_data->pool, address,
+			"status", "status", &status, &attr);
 	pj_strdup2(state_data->pool, &attr->value,
 		   (local_state ==  NOTIFY_OPEN) ? "open" :
 		   (local_state == NOTIFY_INUSE) ? "inuse" : "closed");
 
-	attr = ast_sip_presence_xml_find_node_attr(state_data->pool, pres,
-			"msnsubstatus", "substatus");
+	ast_sip_presence_xml_find_node_attr(state_data->pool, address,
+			"msnsubstatus", "substatus", &msnsubstatus, &attr);
 	pj_strdup2(state_data->pool, &attr->value,
 		   (local_state == NOTIFY_OPEN) ? "online" :
 		   (local_state == NOTIFY_INUSE) ? "onthephone" : "offline");




More information about the asterisk-commits mailing list