Browse Source

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

- Applied patch by Mikko Lehto.
Juha Heinanen 11 years ago
parent
commit
feba7dd8e0
3 changed files with 46 additions and 19 deletions
  1. 30 11
      modules/utils/README
  2. 1 1
      modules/utils/doc/utils_admin.xml
  3. 15 7
      modules/utils/functions.c

+ 30 - 11
modules/utils/README

@@ -4,7 +4,13 @@ Juha Heinanen
 
 
    TutPro Inc.
    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
    Table of Contents
@@ -26,7 +32,7 @@ Juha Heinanen
 
 
         4. Functions
         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.2. xcap_auth_status(watcher_uri, presentity_uri)
 
 
         5. MI Commands
         5. MI Commands
@@ -70,7 +76,7 @@ Chapter 1. Admin Guide
 
 
    4. Functions
    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.2. xcap_auth_status(watcher_uri, presentity_uri)
 
 
    5. MI Commands
    5. MI Commands
@@ -174,17 +180,20 @@ modparam("utils", "xcap_table", "pres_xcap")
 
 
 4. Functions
 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.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
    Function returns reply code of HTTP reply or -1 if something went
    wrong.
    wrong.
@@ -194,6 +203,7 @@ modparam("utils", "xcap_table", "pres_xcap")
 
 
    Example 1.5. http_query() usage
    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
 http_query("http://tutpro.com/index.php?r_uri=$(ru{s.escape.param})&f_uri=$(fu{s
 .escape.param})",
 .escape.param})",
            "$var(result)")
            "$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
    Function checks in the presence server database if a watcher is
    authorized to subscribe to event "presence" of presentity. Sphere
    authorized to subscribe to event "presence" of presentity. Sphere

+ 1 - 1
modules/utils/doc/utils_admin.xml

@@ -171,7 +171,7 @@ modparam("utils", "xcap_table", "pres_xcap")
 			in that request as the second parameter.
 			in that request as the second parameter.
 	    	        </para>
 	    	        </para>
 		        <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
 			the first line of the reply's body (if any) is
 			stored in <quote>result</quote> parameter,
 			stored in <quote>result</quote> parameter,
 			which must be a	writable pseudo	variable.
 			which must be a	writable pseudo	variable.

+ 15 - 7
modules/utils/functions.c

@@ -139,16 +139,24 @@ int http_query(struct sip_msg* _m, char* _url, char* _dst, char* _post)
 	pkg_free(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);
     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);
 	curl_easy_getinfo(curl, CURLINFO_SIZE_DOWNLOAD, &download_size);
 	LM_DBG("http_query download size: %u\n", (unsigned int)download_size);
 	LM_DBG("http_query download size: %u\n", (unsigned int)download_size);
 	/* search for line feed */
 	/* search for line feed */