LP#1703411 XMPP opensrf sub-element repairs
authorBill Erickson <berickxx@gmail.com>
Wed, 19 Sep 2018 19:34:49 +0000 (15:34 -0400)
committerMike Rylander <mrylander@gmail.com>
Fri, 21 Sep 2018 15:27:35 +0000 (11:27 -0400)
* Message template typo repair -- missing "'"
* XPath repair on path to opensrf sub-element for Perl
* Move 'type' attribute get/set back up to the <message> for Perl.
* Clean up code duplication in the C message building libs.
* Squash a centuries-old memory leak where xmlFree(sender) was only
  called if a router_from was not supplied.

Signed-off-by: Bill Erickson <berickxx@gmail.com>
Signed-off-by: Jason Stephenson <jason@sigio.com>
Signed-off-by: Mike Rylander <mrylander@gmail.com>
src/libopensrf/transport_message.c
src/perl/lib/OpenSRF/Transport/SlimJabber/XMPPMessage.pm
src/perl/lib/OpenSRF/Transport/SlimJabber/XMPPReader.pm

index f95debc..332b622 100644 (file)
@@ -120,25 +120,16 @@ transport_message* new_message_from_xml( const char* msg_xml ) {
        xmlChar* recipient      = xmlGetProp( root, BAD_CAST "to");
        xmlChar* subject        = xmlGetProp( root, BAD_CAST "subject");
        xmlChar* thread         = xmlGetProp( root, BAD_CAST "thread" );
        xmlChar* recipient      = xmlGetProp( root, BAD_CAST "to");
        xmlChar* subject        = xmlGetProp( root, BAD_CAST "subject");
        xmlChar* thread         = xmlGetProp( root, BAD_CAST "thread" );
-       xmlChar* router_from    = xmlGetProp( root, BAD_CAST "router_from" );
-       xmlChar* router_to      = xmlGetProp( root, BAD_CAST "router_to" );
-       xmlChar* router_class   = xmlGetProp( root, BAD_CAST "router_class" );
-       xmlChar* router_command = xmlGetProp( root, BAD_CAST "router_command" );
-       xmlChar* broadcast      = xmlGetProp( root, BAD_CAST "broadcast" );
-       xmlChar* osrf_xid       = xmlGetProp( root, BAD_CAST "osrf_xid" );
-
-       if( osrf_xid ) {
-               message_set_osrf_xid( new_msg, (char*) osrf_xid);
-               xmlFree(osrf_xid);
-       }
-
-       if( router_from ) {
-               new_msg->sender     = strdup((const char*)router_from);
-       } else {
-               if( sender ) {
-                       new_msg->sender = strdup((const char*)sender);
-                       xmlFree(sender);
-               }
+       xmlChar* router_from    = NULL;
+       xmlChar* router_to      = NULL;
+       xmlChar* router_class   = NULL;
+       xmlChar* router_command = NULL;
+       xmlChar* broadcast      = NULL;
+       xmlChar* osrf_xid       = NULL;
+
+       if( sender ) {
+               new_msg->sender = strdup((const char*)sender);
+               xmlFree(sender);
        }
 
        if( recipient ) {
        }
 
        if( recipient ) {
@@ -156,34 +147,8 @@ transport_message* new_message_from_xml( const char* msg_xml ) {
                xmlFree(thread);
        }
 
                xmlFree(thread);
        }
 
-       if(router_from) {
-               new_msg->router_from = strdup((const char*)router_from);
-               xmlFree(router_from);
-       }
-
-       if(router_to) {
-               new_msg->router_to  = strdup((const char*)router_to);
-               xmlFree(router_to);
-       }
-
-       if(router_class) {
-               new_msg->router_class = strdup((const char*)router_class);
-               xmlFree(router_class);
-       }
-
-       if(router_command) {
-               new_msg->router_command = strdup((const char*)router_command);
-               xmlFree(router_command);
-       }
-
-       if(broadcast) {
-               if(strcmp((const char*) broadcast,"0") )
-                       new_msg->broadcast = 1;
-               xmlFree(broadcast);
-       }
-
-       /* Within the message element, find the child nodes for "thread", "subject", and */
-       /* "body".  Extract their textual content into the corresponding members. */
+       /* Within the message element, find the child nodes for "thread", "subject" */
+       /* "body", and "opensrf".  Extract their textual content into the corresponding members. */
        xmlNodePtr search_node = root->children;
        while( search_node != NULL ) {
 
        xmlNodePtr search_node = root->children;
        while( search_node != NULL ) {
 
@@ -211,15 +176,13 @@ transport_message* new_message_from_xml( const char* msg_xml ) {
                        }
 
                        if( router_from ) {
                        }
 
                        if( router_from ) {
-                               new_msg->sender     = strdup((const char*)router_from);
-                       } else {
-                               if( sender ) {
-                                       new_msg->sender = strdup((const char*)sender);
-                                       xmlFree(sender);
+                               if (new_msg->sender) {
+                                       // Sender value applied above.  Clear it and
+                                       // use the router value instead.
+                                       free(new_msg->sender);
                                }
                                }
-                       }
 
 
-                       if(router_from) {
+                               new_msg->sender = strdup((const char*)router_from);
                                new_msg->router_from = strdup((const char*)router_from);
                                xmlFree(router_from);
                        }
                                new_msg->router_from = strdup((const char*)router_from);
                                xmlFree(router_from);
                        }
index 44e2040..0fc4124 100644 (file)
@@ -6,7 +6,7 @@ use strict; use warnings;
 use XML::LibXML;
 
 use constant JABBER_MESSAGE =>
 use XML::LibXML;
 
 use constant JABBER_MESSAGE =>
-    "<message to='%s' from='%s>".
+    "<message to='%s' from='%s'>".
     "<opensrf router_command='%s' router_class='%s' osrf_xid='%s'/>".
     "<thread>%s</thread><body>%s</body></message>";
 
     "<opensrf router_command='%s' router_class='%s' osrf_xid='%s'/>".
     "<thread>%s</thread><body>%s</body></message>";
 
@@ -121,7 +121,7 @@ sub parse_xml {
     throw $err if $err;
 
     my $root = $doc->documentElement;
     throw $err if $err;
 
     my $root = $doc->documentElement;
-    my $osrf_node = $root->findnodes('/opensrf')->shift;
+    my $osrf_node = $root->findnodes('/message/opensrf')->shift;
 
     $self->{body} = $root->findnodes('/message/body').'';
     $self->{thread} = $root->findnodes('/message/thread').'';
 
     $self->{body} = $root->findnodes('/message/body').'';
     $self->{thread} = $root->findnodes('/message/thread').'';
@@ -131,7 +131,7 @@ sub parse_xml {
 
     $self->{to} = $root->getAttribute('to');
 
 
     $self->{to} = $root->getAttribute('to');
 
-    $self->{type} = $osrf_node->getAttribute('type');
+    $self->{type} = $root->getAttribute('type');
     $self->{osrf_xid} = $osrf_node->getAttribute('osrf_xid');
 }
 
     $self->{osrf_xid} = $osrf_node->getAttribute('osrf_xid');
 }
 
index 737cf96..0a84ae1 100644 (file)
@@ -307,14 +307,14 @@ sub start_element {
         my $msg = $self->{message};
         $msg->{to} = $attrs{'to'};
         $msg->{from} = $attrs{from};
         my $msg = $self->{message};
         $msg->{to} = $attrs{'to'};
         $msg->{from} = $attrs{from};
+        $msg->{type} = $attrs{type};
 
     } elsif($name eq 'opensrf') {
 
         # These will be authoritative if they exist
         my $msg = $self->{message};
 
     } elsif($name eq 'opensrf') {
 
         # These will be authoritative if they exist
         my $msg = $self->{message};
-        $msg->{from} = $attrs{router_from};
+        $msg->{from} = $attrs{router_from} if $attrs{router_from};
         $msg->{osrf_xid} = $attrs{'osrf_xid'};
         $msg->{osrf_xid} = $attrs{'osrf_xid'};
-        $msg->{type} = $attrs{type};
 
     } elsif($name eq 'body') {
         $self->{xml_state} = IN_BODY;
 
     } elsif($name eq 'body') {
         $self->{xml_state} = IN_BODY;