[sr-dev] git:master: modules/utils: http_query now stores result also in case of 4xx replies

Juha Heinanen jh at tutpro.com
Thu May 8 14:26:19 CEST 2014


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

Author: Juha Heinanen <jh at tutpro.com>
Committer: Juha Heinanen <jh at tutpro.com>
Date:   Thu May  8 15:24:51 2014 +0300

modules/utils: http_query now stores result also in case of 4xx replies

- Applied patch by Mikko Lehto.

---

 modules/utils/README              |   41 +++++++++++++++++++++++++++----------
 modules/utils/doc/utils_admin.xml |    2 +-
 modules/utils/functions.c         |   22 +++++++++++++------
 3 files changed, 46 insertions(+), 19 deletions(-)

diff --git a/modules/utils/README b/modules/utils/README
index 61d4276..43e3f21 100644
--- a/modules/utils/README
+++ b/modules/utils/README
@@ -4,7 +4,13 @@ Juha Heinanen
 
    TutPro Inc.
 
-   Copyright © 2008-2009 Juha Heinanen
+Carsten Bock
+
+   ng-voice GmbH
+
+   Copyright (c) 2008-2009 Juha Heinanen
+
+   Copyright (c) 2013 Carsten Bock, ng-voice GmbH
      __________________________________________________________________
 
    Table of Contents
@@ -26,7 +32,7 @@ Juha Heinanen
 
         4. Functions
 
-              4.1. http_query(url, result)
+              4.1. http_query(url, [post-data], result)
               4.2. xcap_auth_status(watcher_uri, presentity_uri)
 
         5. MI Commands
@@ -70,7 +76,7 @@ Chapter 1. Admin Guide
 
    4. Functions
 
-        4.1. http_query(url, result)
+        4.1. http_query(url, [post-data], result)
         4.2. xcap_auth_status(watcher_uri, presentity_uri)
 
    5. MI Commands
@@ -174,17 +180,20 @@ modparam("utils", "xcap_table", "pres_xcap")
 
 4. Functions
 
-   4.1. http_query(url, result)
+   4.1. http_query(url, [post-data], result)
    4.2. xcap_auth_status(watcher_uri, presentity_uri)
 
-4.1. http_query(url, result)
+4.1.  http_query(url, [post-data], result)
 
-   Sends HTTP GET request according to URL given in "url" parameter, which
-   is a string that may contain pseudo variables.
+   Sends HTTP GET or POST request according to URL given in "url"
+   parameter, which is a string that may contain pseudo variables.
 
-   If HTTP server returns a class 2xx or 3xx reply, the first line of the
-   reply's body (if any) is stored in "result" parameter, which must be a
-   writable pseudo variable.
+   If you want to make a POST-Request, you have to define the "post"-data,
+   that should be submitted in that request as the second parameter.
+
+   If HTTP server returns a class 2xx, 3xx or 4xx reply, the first line of
+   the reply's body (if any) is stored in "result" parameter, which must
+   be a writable pseudo variable.
 
    Function returns reply code of HTTP reply or -1 if something went
    wrong.
@@ -194,6 +203,7 @@ modparam("utils", "xcap_table", "pres_xcap")
 
    Example 1.5. http_query() usage
 ...
+# GET-Request
 http_query("http://tutpro.com/index.php?r_uri=$(ru{s.escape.param})&f_uri=$(fu{s
 .escape.param})",
            "$var(result)")
@@ -201,8 +211,17 @@ switch ($retcode) {
        ...
 }
 ...
+...
+# POST-Request
+http_query("http://tutpro.com/index.php", "r_uri=$(ru{s.escape.param})&f_uri=$(f
+u{s.escape.param})",
+           "$var(result)")
+switch ($retcode) {
+       ...
+}
+...
 
-4.2. xcap_auth_status(watcher_uri, presentity_uri)
+4.2.  xcap_auth_status(watcher_uri, presentity_uri)
 
    Function checks in the presence server database if a watcher is
    authorized to subscribe to event "presence" of presentity. Sphere
diff --git a/modules/utils/doc/utils_admin.xml b/modules/utils/doc/utils_admin.xml
index 3600f7d..7c4f788 100644
--- a/modules/utils/doc/utils_admin.xml
+++ b/modules/utils/doc/utils_admin.xml
@@ -171,7 +171,7 @@ modparam("utils", "xcap_table", "pres_xcap")
 			in that request as the second parameter.
 	    	        </para>
 		        <para>
-			If HTTP server returns a class 2xx or 3xx reply,
+			If HTTP server returns a class 2xx, 3xx or 4xx reply,
 			the first line of the reply's body (if any) is
 			stored in <quote>result</quote> parameter,
 			which must be a	writable pseudo	variable.
diff --git a/modules/utils/functions.c b/modules/utils/functions.c
index ed1a591..dc4e529 100644
--- a/modules/utils/functions.c
+++ b/modules/utils/functions.c
@@ -139,16 +139,24 @@ int http_query(struct sip_msg* _m, char* _url, char* _dst, char* _post)
 	pkg_free(post);
     }
 
-    if (res != CURLE_OK) {
-	LM_ERR("failed to perform curl\n");
-	curl_easy_cleanup(curl);
-	if(stream)
-	    pkg_free(stream);
-	return -1;
+	if (res != CURLE_OK) {
+		/* http://curl.haxx.se/libcurl/c/libcurl-errors.html */
+		if (res == CURLE_COULDNT_CONNECT) {
+			LM_WARN("failed to connect() to host\n");
+		} else if ( res == CURLE_COULDNT_RESOLVE_HOST ) {
+			LM_WARN("couldn't resolve host\n");
+		} else {
+			LM_ERR("failed to perform curl (%d)\n", res);
+		}
+	
+		curl_easy_cleanup(curl);
+		if(stream)
+			pkg_free(stream);
+		return -1;
     }
 
     curl_easy_getinfo(curl, CURLINFO_HTTP_CODE, &stat);
-    if ((stat >= 200) && (stat < 400)) {
+    if ((stat >= 200) && (stat < 500)) {
 	curl_easy_getinfo(curl, CURLINFO_SIZE_DOWNLOAD, &download_size);
 	LM_DBG("http_query download size: %u\n", (unsigned int)download_size);
 	/* search for line feed */




More information about the sr-dev mailing list