[Freeswitch-svn] [commit] r3570 - in freeswitch/trunk/src: . mod/dialplans/mod_dialplan_xml mod/xml_int/mod_xml_rpc

Freeswitch SVN anthm at freeswitch.org
Thu Dec 7 17:56:18 EST 2006


Author: anthm
Date: Thu Dec  7 17:56:17 2006
New Revision: 3570

Modified:
   freeswitch/trunk/src/mod/dialplans/mod_dialplan_xml/mod_dialplan_xml.c
   freeswitch/trunk/src/mod/xml_int/mod_xml_rpc/mod_xml_rpc.c
   freeswitch/trunk/src/switch_event.c

Log:
improve gateway stuff and minor tweak to event serialize

Modified: freeswitch/trunk/src/mod/dialplans/mod_dialplan_xml/mod_dialplan_xml.c
==============================================================================
--- freeswitch/trunk/src/mod/dialplans/mod_dialplan_xml/mod_dialplan_xml.c	(original)
+++ freeswitch/trunk/src/mod/dialplans/mod_dialplan_xml/mod_dialplan_xml.c	Thu Dec  7 17:56:17 2006
@@ -177,7 +177,12 @@
 	switch_channel_t *channel;
 	switch_xml_t cfg, xml, xcontext, xexten;
 	char *context = NULL;
-	char params[1024];
+    switch_stream_handle_t stream = {0};
+    switch_size_t encode_len = 1024, new_len = 0;
+    char *encode_buf = NULL;
+    char *prof[11] = {0}, *prof_names[11] = {0}, *e = NULL;
+    switch_hash_index_t *hi;
+    uint32_t x = 0;
 
 	channel = switch_core_session_get_channel(session);
 	if ((caller_profile = switch_channel_get_caller_profile(channel))) {
@@ -189,22 +194,95 @@
 
 	switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Processing %s->%s!\n", caller_profile->caller_id_name,
 					  caller_profile->destination_number);
-	
-	snprintf(params, sizeof(params), "context=%s&dest=%s&cid_name=%s&cid_num=%s&netaddr=%s&ani=%s&aniii=%s&rdnis=%s&source=%s&chan_name=%s&uuid=%s", 
-			caller_profile->context, caller_profile->destination_number,
-			caller_profile->caller_id_name, caller_profile->caller_id_number,
-			caller_profile->network_addr?caller_profile->network_addr:"", 
-			caller_profile->ani?caller_profile->ani:"", 
-			caller_profile->aniii?caller_profile->aniii:"",
-			caller_profile->rdnis?caller_profile->rdnis:"", 
-			caller_profile->source, caller_profile->chan_name, caller_profile->uuid);
 
-	if (switch_xml_locate("dialplan", NULL, NULL, NULL, &xml, &cfg, params) != SWITCH_STATUS_SUCCESS) {
+    SWITCH_STANDARD_STREAM(stream);
+    
+    if (!(encode_buf = malloc(encode_len))) {
+        return NULL;
+    }
+    
+    prof[0] = caller_profile->context;
+    prof[1] = caller_profile->destination_number;
+    prof[2] = caller_profile->caller_id_name;
+    prof[3] = caller_profile->caller_id_number;
+    prof[4] = caller_profile->network_addr;
+    prof[5] = caller_profile->ani;
+    prof[6] = caller_profile->aniii;
+    prof[7] = caller_profile->rdnis;
+    prof[8] = caller_profile->source;
+    prof[9] = caller_profile->chan_name;
+    prof[10] = caller_profile->uuid;
+
+    prof_names[0] = "context";
+    prof_names[1] = "destination_number";
+    prof_names[2] = "caller_id_name";
+    prof_names[3] = "caller_id_number";
+    prof_names[4] = "network_addr";
+    prof_names[5] = "ani";
+    prof_names[6] = "aniii";
+    prof_names[7] = "rdnis";
+    prof_names[8] = "source";
+    prof_names[9] = "chan_name";
+    prof_names[10] = "uuid";
+
+    for (x = 0; prof[x]; x++) {
+        new_len = (strlen(prof[x]) * 3) + 1;
+        if (encode_len < new_len) {
+            char *tmp;
+            
+            encode_len = new_len;
+
+            if (!(tmp = realloc(encode_buf, encode_len))) {
+                switch_safe_free(encode_buf);
+                return NULL;
+            }
+
+            encode_buf = tmp;
+        }
+        switch_url_encode(prof[x], encode_buf, encode_len - 1);
+        stream.write_function(&stream, "%s=%s&", prof_names[x], encode_buf);
+    }
+
+
+	for (hi = switch_channel_variable_first(channel, switch_core_session_get_pool(session)); hi; hi = switch_hash_next(hi)) {
+        void *val;
+        const void *var;
+		switch_hash_this(hi, &var, NULL, &val);
+        
+        new_len = (strlen((char *) var) * 3) + 1;
+        if (encode_len < new_len) {
+            char *tmp;
+            
+            encode_len = new_len;
+
+            if (!(tmp = realloc(encode_buf, encode_len))) {
+                switch_safe_free(encode_buf);
+                return NULL;
+            }
+
+            encode_buf = tmp;
+        }
+
+        switch_url_encode((char *) val, encode_buf, encode_len - 1);
+        stream.write_function(&stream, "%s=%s&", (char *) var, encode_buf);
+        
+	}
+    
+    e = (char *)stream.data + (strlen((char *)stream.data) - 1);
+
+    if (e && *e == '&') {
+        *e = '\0';
+    }
+        
+	if (switch_xml_locate("dialplan", NULL, NULL, NULL, &xml, &cfg, stream.data) != SWITCH_STATUS_SUCCESS) {
 		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "open of dialplan failed\n");
 		switch_channel_hangup(channel, SWITCH_CAUSE_DESTINATION_OUT_OF_ORDER);
 		return NULL;
 	}
 	
+    switch_safe_free(stream.data);
+    switch_safe_free(encode_buf);
+    
 	if (!(xcontext = switch_xml_find_child(cfg, "context", "name", context))) {
 		if (!(xcontext = switch_xml_find_child(cfg, "context", "name", "global"))) {
 			switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "context %s not found\n", context);

Modified: freeswitch/trunk/src/mod/xml_int/mod_xml_rpc/mod_xml_rpc.c
==============================================================================
--- freeswitch/trunk/src/mod/xml_int/mod_xml_rpc/mod_xml_rpc.c	(original)
+++ freeswitch/trunk/src/mod/xml_int/mod_xml_rpc/mod_xml_rpc.c	Thu Dec  7 17:56:17 2006
@@ -82,30 +82,36 @@
 								  char *key_value,
 								  char *params)
 {
-	char url[1024] = "", filename[1024] = "";
+	char filename[1024] = "";
 	CURL *curl_handle = NULL;
 	struct config_data config_data;
 	switch_xml_t xml = NULL;
-	
-	snprintf(url, sizeof(url), "%s?section=%s&tag_name=%s&key_name=%s&&key_value=%s%s%s\n", 
-			 globals.url,
-			 section,
-			 tag_name ? tag_name : "",
-			 key_name ? key_name : "",
-			 key_value ? key_value : "",
-			 params ? "&" : "", params ? params : "");
+    char *data = NULL;
 
-	srand((unsigned int)(time(NULL) + strlen(url)));
+    if (!(data = switch_mprintf("section=%s&tag_name=%s&key_name=%s&key_value=%s%s%s\n", 
+                                section,
+                                tag_name ? tag_name : "",
+                                key_name ? key_name : "",
+                                key_value ? key_value : "",
+                                params ? "&" : "", params ? params : ""))) {
+
+        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Memory Error!\n");
+        return NULL;
+    }
+
+	srand((unsigned int)(time(NULL) + strlen(globals.url)));
 	snprintf(filename, sizeof(filename), "%s%04x.tmp", SWITCH_GLOBAL_dirs.temp_dir, (rand() & 0xffff));
 	curl_handle = curl_easy_init();
-	if (!strncasecmp(url, "https", 5)) {
+	if (!strncasecmp(globals.url, "https", 5)) {
 		curl_easy_setopt(curl_handle, CURLOPT_SSL_VERIFYPEER, 0);
 		curl_easy_setopt(curl_handle, CURLOPT_SSL_VERIFYHOST, 0);
 	}
 		
 	config_data.name = filename;
 	if ((config_data.fd = open(filename, O_CREAT | O_RDWR | O_TRUNC)) > -1) {
-		curl_easy_setopt(curl_handle, CURLOPT_URL, url);
+        curl_easy_setopt(curl_handle, CURLOPT_POST, 1);
+        curl_easy_setopt(curl_handle, CURLOPT_POSTFIELDS, data);
+		curl_easy_setopt(curl_handle, CURLOPT_URL, globals.url);
 		curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, file_callback);
 		curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, (void *)&config_data);
 		curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, "freeswitch-xml/1.0");
@@ -115,6 +121,8 @@
 	} else {
 		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error!\n");
 	}
+
+    switch_safe_free(data);
 
 	if (!(xml = switch_xml_parse_file(filename))) {
 		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error Parsing Result!\n");

Modified: freeswitch/trunk/src/switch_event.c
==============================================================================
--- freeswitch/trunk/src/switch_event.c	(original)
+++ freeswitch/trunk/src/switch_event.c	Thu Dec  7 17:56:17 2006
@@ -600,7 +600,7 @@
 {
 	switch_size_t len = 0;
 	switch_event_header_t *hp;
-	switch_size_t llen = 0, dlen = 0, blocksize = 512, encode_len = 1024;
+	switch_size_t llen = 0, dlen = 0, blocksize = 512, encode_len = 1024, new_len = 0;
 	char *buf;
     char *encode_buf = NULL; /* used for url encoding of variables to make sure unsafe things stay out of the serialzed copy */
 	
@@ -626,19 +626,24 @@
          * the memory, allocate and only reallocate if we need more.  This avoids an alloc, free CPU
          * destroying loop.
          */
-        if(encode_len < ((strlen(hp->value) * 3) + 1)) {
+
+        new_len = (strlen(hp->value) * 3) + 1;
+
+        if(encode_len < new_len) {
             char* tmp;
 	        //switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Allocing %d was %d.\n", ((strlen(hp->value) * 3) + 1), encode_len);
             /* we can use realloc for initial alloc as well, if encode_buf is zero it treats it as a malloc */
-            if(!(tmp = realloc(encode_buf, ((strlen(hp->value) * 3) + 1)))) {
+
+            /* keep track of the size of our allocation */
+            encode_len = new_len;
+            
+            if(!(tmp = realloc(encode_buf, encode_len))) {
                 /* oh boy, ram's gone, give back what little we grabbed and bail */
                 switch_safe_free(buf);
+                switch_safe_free(encode_buf);
                 return SWITCH_STATUS_MEMERR;
             }
-
-            /* keep track of the size of our allocation */
-            encode_len = (strlen(hp->value) * 3) + 1;
-
+            
             encode_buf = tmp;
         }
 



More information about the Freeswitch-svn mailing list