Преглед изворни кода

utils: option to tream leading and trailing whitespaces

- new parameter http_response_trim to control it; default is 0 - don't
  trimi (backward compatible); set to 1 for enabling trimming
- based on the request of PR GH#321
Daniel-Constantin Mierla пре 10 година
родитељ
комит
de925d64cb
2 измењених фајлова са 16 додато и 4 уклоњено
  1. 14 4
      modules/utils/functions.c
  2. 2 0
      modules/utils/utils.c

+ 14 - 4
modules/utils/functions.c

@@ -36,6 +36,7 @@
 #include "../../pvar.h"
 #include "../../route_struct.h"
 #include "../../ut.h"
+#include "../../trim.h"
 #include "../../mem/mem.h"
 #include "../../parser/msg_parser.h"
 #include "../../lvalue.h"
@@ -43,6 +44,8 @@
 #include "utils.h"
 
 
+extern int http_response_trim;
+
 /* 
  * curl write function that saves received data as zero terminated
  * to stream. Returns the amount of data taken care of.
@@ -86,6 +89,7 @@ int http_query(struct sip_msg* _m, char* _url, char* _dst, char* _post, char* _h
 	pv_value_t val;
 	double download_size;
 	struct curl_slist *chunk = NULL;
+	str hres;
 
 	memset(&stream, 0, sizeof(http_res_stream_t));
 
@@ -199,14 +203,20 @@ int http_query(struct sip_msg* _m, char* _url, char* _dst, char* _post, char* _h
 		curl_easy_getinfo(curl, CURLINFO_SIZE_DOWNLOAD, &download_size);
 		LM_DBG("http_query download size: %u\n", (unsigned int)download_size);
 
+		hres.s = stream.buf;
+		hres.len = download_size;
+		if(http_response_trim) {
+			trim(&hres);
+		}
 		/* search for line feed */
-		at = memchr(stream.buf, (char)10, download_size);
+		at = memchr(hres.s, (char)10, hres.len);
 		if (at == NULL) {
 			/* not found: use whole stream */
-			at = stream.buf + (unsigned int)download_size;
+			val.rs = hres;
+		} else {
+			val.rs.s = hres.s;
+			val.rs.len = at - hres.s;
 		}
-		val.rs.s = stream.buf;
-		val.rs.len = at - stream.buf;
 		LM_DBG("http_query result: %.*s\n", val.rs.len, val.rs.s);
 		val.flags = PV_VAL_STR;
 		dst = (pv_spec_t *)_dst;

+ 2 - 0
modules/utils/utils.c

@@ -58,6 +58,7 @@ MODULE_VERSION
 
 /* Module parameter variables */
 int http_query_timeout = 4;
+int http_response_trim = 0;
 static int forward_active = 0;
 static int   mp_max_id = 0;
 static char* mp_switch = "";
@@ -122,6 +123,7 @@ static param_export_t params[] = {
     {"pres_db_url", PARAM_STR, &pres_db_url},
     {"xcap_table", PARAM_STR, &xcap_table},
     {"http_query_timeout", INT_PARAM, &http_query_timeout},
+    {"http_response_trim", INT_PARAM, &http_response_trim},
     {"forward_active", INT_PARAM, &forward_active},
     {0, 0, 0}
 };