[sr-dev] git:master: Add support for sending POST-Requests to http_query() method.

Carsten Bock carsten at ng-voice.com
Thu Sep 12 17:17:09 CEST 2013


Module: sip-router
Branch: master
Commit: 8017fa7c67dbb3dd4bdd937be9b841097a44674e
URL:    http://git.sip-router.org/cgi-bin/gitweb.cgi/sip-router/?a=commit;h=8017fa7c67dbb3dd4bdd937be9b841097a44674e

Author: Carsten Bock <carsten at ng-voice.com>
Committer: Carsten Bock <carsten at ng-voice.com>
Date:   Wed Aug 21 20:02:23 2013 +0200

Add support for sending POST-Requests to http_query() method.

---

 modules/utils/doc/utils.xml       |   12 ++++++++++++
 modules/utils/doc/utils_admin.xml |   20 ++++++++++++++++++--
 modules/utils/functions.c         |   32 +++++++++++++++++++++++++++++---
 modules/utils/functions.h         |    3 ++-
 modules/utils/utils.c             |   28 +++++++++++++++++++++++++---
 5 files changed, 86 insertions(+), 9 deletions(-)

diff --git a/modules/utils/doc/utils.xml b/modules/utils/doc/utils.xml
index dfdb290..49b2cbb 100644
--- a/modules/utils/doc/utils.xml
+++ b/modules/utils/doc/utils.xml
@@ -21,11 +21,23 @@
 			<email>jh at tutpro.com</email>
 		</address>
 		</author>
+		<author>
+		<firstname>Carsten</firstname>
+		<surname>Bock</surname>
+		<affiliation><orgname>ng-voice GmbH</orgname></affiliation>
+		<address>
+			<email>carsten at ng-voice.com</email>
+		</address>
+		</author>
 	</authorgroup>
 	<copyright>
 		<year>2008-2009</year>
 		<holder>Juha Heinanen</holder>
 	</copyright>
+	<copyright>
+		<year>2013</year>
+		<holder>Carsten Bock, ng-voice GmbH</holder>
+	</copyright>
 	</bookinfo>
 	<toc></toc>
 	
diff --git a/modules/utils/doc/utils_admin.xml b/modules/utils/doc/utils_admin.xml
index ab59350..3600f7d 100644
--- a/modules/utils/doc/utils_admin.xml
+++ b/modules/utils/doc/utils_admin.xml
@@ -158,13 +158,18 @@ modparam("utils", "xcap_table", "pres_xcap")
 	<title>Functions</title>
 		<section id="utils.f.http_query">
 			<title>
-				<function moreinfo="none">http_query(url, result)</function>
+				<function moreinfo="none">http_query(url, [post-data], result)</function>
 			</title>
 			<para>
-			Sends HTTP GET request according to URL given in
+			Sends HTTP GET or POST request according to URL given in
 			<quote>url</quote> parameter, which is a string that may
 			contain pseudo variables.
 	    	        </para>
+			<para>
+			If you want to make a POST-Request, you have to define
+			the <quote>post</quote>-data, that should be submitted
+			in that request as the second parameter.
+	    	        </para>
 		        <para>
 			If HTTP server returns a class 2xx or 3xx reply,
 			the first line of the reply's body (if any) is
@@ -183,6 +188,7 @@ modparam("utils", "xcap_table", "pres_xcap")
 				<title><function>http_query()</function> usage</title>
 				<programlisting format="linespecific">
 ...
+# GET-Request
 http_query("http://tutpro.com/index.php?r_uri=$(ru{s.escape.param})&f_uri=$(fu{s.escape.param})",
            "$var(result)")
 switch ($retcode) {
@@ -190,6 +196,16 @@ switch ($retcode) {
 }
 ...
 				</programlisting>
+				<programlisting format="linespecific">
+...
+# POST-Request
+http_query("http://tutpro.com/index.php", "r_uri=$(ru{s.escape.param})&f_uri=$(fu{s.escape.param})",
+           "$var(result)")
+switch ($retcode) {
+       ...
+}
+...
+				</programlisting>
 			</example>
 		</section>
 		<section id="utils.f.xcap_auth_status">
diff --git a/modules/utils/functions.c b/modules/utils/functions.c
index aadfdd5..1225ba8 100644
--- a/modules/utils/functions.c
+++ b/modules/utils/functions.c
@@ -2,6 +2,7 @@
  * script functions of utils module
  *
  * Copyright (C) 2008 Juha Heinanen
+ * Copyright (C) 2013 Carsten Bock, ng-voice GmbH
  *
  * This file is part of Kamailio, a free SIP server.
  *
@@ -70,12 +71,12 @@ size_t write_function( void *ptr, size_t size, size_t nmemb, void *stream)
  * Performs http_query and saves possible result (first body line of reply)
  * to pvar.
  */
-int http_query(struct sip_msg* _m, char* _url, char* _dst)
+int http_query(struct sip_msg* _m, char* _url, char* _dst, char* _post)
 {
     CURL *curl;
     CURLcode res;  
-    str value;
-    char *url, *at;
+    str value, post_value;
+    char *url, *at, *post;
     char* stream;
     long stat;
     pv_spec_t *dst;
@@ -103,6 +104,28 @@ int http_query(struct sip_msg* _m, char* _url, char* _dst)
     *(url + value.len) = (char)0;
     curl_easy_setopt(curl, CURLOPT_URL, url);
 
+    if (_post) {
+        /* Now specify we want to POST data */ 
+	curl_easy_setopt(curl, CURLOPT_POST, 1L);
+
+    	if (fixup_get_svalue(_m, (gparam_p)_post, &post_value) != 0) {
+		LM_ERR("cannot get post value\n");
+		pkg_free(url);
+		return -1;
+    	}
+        post = pkg_malloc(post_value.len + 1);
+        if (post == NULL) {
+		curl_easy_cleanup(curl);
+		pkg_free(url);
+        	LM_ERR("cannot allocate pkg memory for post\n");
+        	return -1;
+	}
+	memcpy(post, post_value.s, post_value.len);
+	*(post + post_value.len) = (char)0;
+ 	curl_easy_setopt(curl, CURLOPT_POSTFIELDS, post);
+    }
+       
+
     curl_easy_setopt(curl, CURLOPT_NOSIGNAL, (long)1);
     curl_easy_setopt(curl, CURLOPT_TIMEOUT, (long)http_query_timeout);
 
@@ -112,6 +135,9 @@ int http_query(struct sip_msg* _m, char* _url, char* _dst)
 
     res = curl_easy_perform(curl);  
     pkg_free(url);
+    if (_post) {
+	pkg_free(post);
+    }
     curl_easy_cleanup(curl);
 
     if (res != CURLE_OK) {
diff --git a/modules/utils/functions.h b/modules/utils/functions.h
index 5e666f9..910a4d5 100644
--- a/modules/utils/functions.h
+++ b/modules/utils/functions.h
@@ -2,6 +2,7 @@
  * headers of script functions of utils module
  *
  * Copyright (C) 2008 Juha Heinanen
+ * Copyright (C) 2013 Carsten Bock, ng-voice GmbH
  *
  * This file is part of Kamailio, a free SIP server.
  *
@@ -39,7 +40,7 @@
  * Performs http_query and saves possible result (first body line of reply)
  * to pvar.
  */
-int http_query(struct sip_msg* _m, char* _page, char* _params, char* _dst);
+int http_query(struct sip_msg* _m, char* _url, char* _dst, char* _post);
 
 
 #endif /* UTILS_FUNCTIONS_H */
diff --git a/modules/utils/utils.c b/modules/utils/utils.c
index b734a69..8109a11 100644
--- a/modules/utils/utils.c
+++ b/modules/utils/utils.c
@@ -3,6 +3,7 @@
  *
  * Copyright (C) 2008 Juha Heinanen
  * Copyright (C) 2009 1&1 Internet AG
+ * Copyright (C) 2013 Carsten Bock, ng-voice GmbH
  *
  * This file is part of Kamailio, a free SIP server.
  *
@@ -91,12 +92,19 @@ static void destroy(void);
 static int fixup_http_query(void** param, int param_no);
 static int fixup_free_http_query(void** param, int param_no);
 
+/* Wrappers for http_query to be defined later */
+static int w_http_query(struct sip_msg* _m, char* _url, char* _result);
+static int w_http_query_post(struct sip_msg* _m, char* _url, char* _post, char* _result);
+
 /* forward function */
 int utils_forward(struct sip_msg *msg, int id, int proto);
 
 /* Exported functions */
 static cmd_export_t cmds[] = {
-    {"http_query", (cmd_function)http_query, 2, fixup_http_query,
+    {"http_query", (cmd_function)w_http_query, 2, fixup_http_query,
+     fixup_free_http_query,
+     REQUEST_ROUTE|ONREPLY_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE},
+    {"http_query", (cmd_function)w_http_query_post, 3, fixup_http_query,
      fixup_free_http_query,
      REQUEST_ROUTE|ONREPLY_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE},
     {"xcap_auth_status", (cmd_function)xcap_auth_status, 2, fixup_pvar_pvar,
@@ -325,7 +333,7 @@ static int fixup_http_query(void** param, int param_no)
 	return fixup_spve_null(param, 1);
     }
 
-    if (param_no == 2) {
+    if ((param_no == 2) || (param_no == 3)) {
 	if (fixup_pvar_null(param, 1) != 0) {
 	    LM_ERR("failed to fixup result pvar\n");
 	    return -1;
@@ -351,7 +359,7 @@ static int fixup_free_http_query(void** param, int param_no)
 	return 0;
     }
 
-    if (param_no == 2) {
+    if ((param_no == 2) || (param_no == 3)) {
 	return fixup_free_pvar_null(param, 1);
     }
     
@@ -359,6 +367,20 @@ static int fixup_free_http_query(void** param, int param_no)
     return -1;
 }
 
+/*
+ * Wrapper for HTTP-Query (GET)
+ */
+static int w_http_query(struct sip_msg* _m, char* _url, char* _result) {
+	return http_query(_m, _url, _result, NULL);
+}
+
+
+/*
+ * Wrapper for HTTP-Query (POST-Variant)
+ */
+static int w_http_query_post(struct sip_msg* _m, char* _url, char* _post, char* _result) {
+	return http_query(_m, _url, _result, _post);
+}
 
 /*!
  * \brief checks precondition, switch, filter and forwards msg if necessary




More information about the sr-dev mailing list