From 49778c34e5aeb2506ad83286aa21ba353727c751 Mon Sep 17 00:00:00 2001
From: Michael Jerris <mike@jerris.com>
Date: Tue, 2 Jun 2015 19:26:28 -0400
Subject: [PATCH] FS-7258, FS-7571: [mod_xml_cdr] properly encode xml cdr for
 post to web server

---
 src/include/switch_utils.h                | 1 +
 src/mod/xml_int/mod_xml_cdr/mod_xml_cdr.c | 2 +-
 src/switch_utils.c                        | 9 +++++++--
 3 files changed, 9 insertions(+), 3 deletions(-)

diff --git a/src/include/switch_utils.h b/src/include/switch_utils.h
index 5d85353669..c6768d5860 100644
--- a/src/include/switch_utils.h
+++ b/src/include/switch_utils.h
@@ -1052,6 +1052,7 @@ static inline int switch_needs_url_encode(const char *s)
 	return 0;
 }
 
+SWITCH_DECLARE(char *) switch_url_encode_opt(const char *url, char *buf, size_t len, switch_bool_t double_encode);
 SWITCH_DECLARE(char *) switch_url_encode(const char *url, char *buf, size_t len);
 SWITCH_DECLARE(char *) switch_url_decode(char *s);
 SWITCH_DECLARE(switch_bool_t) switch_simple_email(const char *to,
diff --git a/src/mod/xml_int/mod_xml_cdr/mod_xml_cdr.c b/src/mod/xml_int/mod_xml_cdr/mod_xml_cdr.c
index 71f51d30ce..968e16e4ad 100644
--- a/src/mod/xml_int/mod_xml_cdr/mod_xml_cdr.c
+++ b/src/mod/xml_int/mod_xml_cdr/mod_xml_cdr.c
@@ -272,7 +272,7 @@ static switch_status_t my_on_reporting(switch_core_session_t *session)
 			memset(xml_text_escaped, 0, need_bytes);
 			if (globals.encode == ENCODING_DEFAULT) {
 				headers = switch_curl_slist_append(headers, "Content-Type: application/x-www-form-urlencoded");
-				switch_url_encode(xml_text, xml_text_escaped, need_bytes);
+				switch_url_encode_opt(xml_text, xml_text_escaped, need_bytes, SWITCH_TRUE);
 			} else {
 				headers = switch_curl_slist_append(headers, "Content-Type: application/x-www-form-base64-encoded");
 				switch_b64_encode((unsigned char *) xml_text, need_bytes / 3, (unsigned char *) xml_text_escaped, need_bytes);
diff --git a/src/switch_utils.c b/src/switch_utils.c
index 59b474045c..0b415492dc 100644
--- a/src/switch_utils.c
+++ b/src/switch_utils.c
@@ -3162,7 +3162,7 @@ SWITCH_DECLARE(int) switch_socket_waitfor(switch_pollfd_t *poll, int ms)
 	return nsds;
 }
 
-SWITCH_DECLARE(char *) switch_url_encode(const char *url, char *buf, size_t len)
+SWITCH_DECLARE(char *) switch_url_encode_opt(const char *url, char *buf, size_t len, switch_bool_t double_encode)
 {
 	const char *p, *e = end_of_p(url);
 	size_t x = 0;
@@ -3185,7 +3185,7 @@ SWITCH_DECLARE(char *) switch_url_encode(const char *url, char *buf, size_t len)
 			break;
 		}
 
-		if (*p == '%' && e-p > 1) {
+		if (!double_encode && *p == '%' && e-p > 1) {
 			if (strchr(hex, *(p+1)) && strchr(hex, *(p+2))) {
 				ok = 1;
 			}
@@ -3207,6 +3207,11 @@ SWITCH_DECLARE(char *) switch_url_encode(const char *url, char *buf, size_t len)
 	return buf;
 }
 
+SWITCH_DECLARE(char *) switch_url_encode(const char *url, char *buf, size_t len)
+{
+	return switch_url_encode_opt(url, buf, len, SWITCH_FALSE);
+}
+
 SWITCH_DECLARE(char *) switch_url_decode(char *s)
 {
 	char *o;