[asterisk-commits] tilghman: trunk r125055 - in /trunk: funcs/ res/

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Wed Jun 25 11:00:55 CDT 2008


Author: tilghman
Date: Wed Jun 25 11:00:54 2008
New Revision: 125055

URL: http://svn.digium.com/view/asterisk?view=rev&rev=125055
Log:
Separate the global initialization routines for cURL into its own separate
module.

Added:
    trunk/res/res_curl.c   (with props)
Modified:
    trunk/funcs/func_curl.c
    trunk/res/res_config_curl.c

Modified: trunk/funcs/func_curl.c
URL: http://svn.digium.com/view/asterisk/trunk/funcs/func_curl.c?view=diff&rev=125055&r1=125054&r2=125055
==============================================================================
--- trunk/funcs/func_curl.c (original)
+++ trunk/funcs/func_curl.c Wed Jun 25 11:00:54 2008
@@ -50,28 +50,14 @@
 #include "asterisk/utils.h"
 #include "asterisk/threadstorage.h"
 
-struct MemoryStruct {
-	char *memory;
-	size_t size;
-};
-
-/* There might be a realloc() out there that doesn't like reallocing
- * NULL pointers, so we take care of it here
- */
-static void *myrealloc(void *ptr, size_t size)
-{
-	return (ptr ? ast_realloc(ptr, size) : ast_malloc(size));
-}
-
 static size_t WriteMemoryCallback(void *ptr, size_t size, size_t nmemb, void *data)
 {
 	register int realsize = size * nmemb;
-	struct MemoryStruct *mem = (struct MemoryStruct *)data;
+	struct ast_str **str = (struct ast_str **)data;
 
-	if ((mem->memory = (char *)myrealloc(mem->memory, mem->size + realsize + 1))) {
-		memcpy(&(mem->memory[mem->size]), ptr, realsize);
-		mem->size += realsize;
-		mem->memory[mem->size] = 0;
+	if (ast_str_make_space(str, (*str)->used + realsize + 1) == 0) {
+		memcpy(&(*str)->str[(*str)->used], ptr, realsize);
+		(*str)->used += realsize;
 	}
 
 	return realsize;
@@ -103,7 +89,7 @@
 
 AST_THREADSTORAGE_CUSTOM(curl_instance, curl_instance_init, curl_instance_cleanup);
 
-static int curl_internal(struct MemoryStruct *chunk, char *url, char *post)
+static int curl_internal(struct ast_str **chunk, char *url, char *post)
 {
 	CURL **curl;
 
@@ -128,7 +114,7 @@
 
 static int acf_curl_exec(struct ast_channel *chan, const char *cmd, char *info, char *buf, size_t len)
 {
-	struct MemoryStruct chunk = { NULL, 0 };
+	struct ast_str *str = ast_str_create(16);
 	AST_DECLARE_APP_ARGS(args,
 		AST_APP_ARG(url);
 		AST_APP_ARG(postdata);
@@ -138,6 +124,7 @@
 	
 	if (ast_strlen_zero(info)) {
 		ast_log(LOG_WARNING, "CURL requires an argument (URL)\n");
+		ast_free(str);
 		return -1;
 	}
 
@@ -146,18 +133,19 @@
 	if (chan)
 		ast_autoservice_start(chan);
 
-	if (!curl_internal(&chunk, args.url, args.postdata)) {
-		if (chunk.memory) {
-			chunk.memory[chunk.size] = '\0';
-			if (chunk.memory[chunk.size - 1] == 10)
-				chunk.memory[chunk.size - 1] = '\0';
+	if (!curl_internal(&str, args.url, args.postdata)) {
+		if (str->used) {
+			str->str[str->used] = '\0';
+			if (str->str[str->used - 1] == '\n') {
+				str->str[str->used - 1] = '\0';
+			}
 
-			ast_copy_string(buf, chunk.memory, len);
-			ast_free(chunk.memory);
+			ast_copy_string(buf, str->str, len);
 		}
 	} else {
 		ast_log(LOG_ERROR, "Cannot allocate curl structure\n");
 	}
+	ast_free(str);
 
 	if (chan)
 		ast_autoservice_stop(chan);
@@ -181,8 +169,6 @@
 
 	res = ast_custom_function_unregister(&acf_curl);
 
-	curl_global_cleanup();
-	
 	return res;
 }
 
@@ -190,10 +176,12 @@
 {
 	int res;
 
-	if (curl_global_init(CURL_GLOBAL_ALL)) {
-		ast_log(LOG_ERROR, "Unable to initialize the CURL library. Cannot load func_curl\n");
-		return AST_MODULE_LOAD_DECLINE;
-	}	
+	if (!ast_module_check("res_curl.so")) {
+		if (ast_load_resource("res_curl.so") != AST_MODULE_LOAD_SUCCESS) {
+			ast_log(LOG_ERROR, "Cannot load res_curl, so func_curl cannot be loaded\n");
+			return AST_MODULE_LOAD_DECLINE;
+		}
+	}
 
 	res = ast_custom_function_register(&acf_curl);
 

Modified: trunk/res/res_config_curl.c
URL: http://svn.digium.com/view/asterisk/trunk/res/res_config_curl.c?view=diff&rev=125055&r1=125054&r2=125055
==============================================================================
--- trunk/res/res_config_curl.c (original)
+++ trunk/res/res_config_curl.c Wed Jun 25 11:00:54 2008
@@ -538,15 +538,22 @@
 	.require_func = require_curl,
 };
 
-static int unload_module (void)
+static int unload_module(void)
 {
 	ast_config_engine_deregister(&curl_engine);
 	ast_verb(1, "res_config_curl unloaded.\n");
 	return 0;
 }
 
-static int load_module (void)
-{
+static int load_module(void)
+{
+	if (!ast_module_check("res_curl.so")) {
+		if (ast_load_resource("res_curl.so") != AST_MODULE_LOAD_SUCCESS) {
+			ast_log(LOG_ERROR, "Cannot load res_curl, so res_config_curl cannot be loaded\n");
+			return AST_MODULE_LOAD_DECLINE;
+		}
+	}
+
 	ast_config_engine_register(&curl_engine);
 	ast_verb(1, "res_config_curl loaded.\n");
 	return 0;

Added: trunk/res/res_curl.c
URL: http://svn.digium.com/view/asterisk/trunk/res/res_curl.c?view=auto&rev=125055
==============================================================================
--- trunk/res/res_curl.c (added)
+++ trunk/res/res_curl.c Wed Jun 25 11:00:54 2008
@@ -1,0 +1,75 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (c) 2008, Digium, Inc.
+ *
+ * Tilghman Lesher <res_curl_v1 at the-tilghman.com>
+ *
+ * See http://www.asterisk.org for more information about
+ * the Asterisk project. Please do not directly contact
+ * any of the maintainers of this project for assistance;
+ * the project provides a web site, mailing lists and IRC
+ * channels for your use.
+ *
+ * This program is free software, distributed under the terms of
+ * the GNU General Public License Version 2. See the LICENSE file
+ * at the top of the source tree.
+ */
+
+/*! \file
+ *
+ * \brief curl resource engine
+ *
+ * \author Tilghman Lesher <res_curl_v1 at the-tilghman.com>
+ *
+ * \extref Depends on the CURL library  - http://curl.haxx.se/
+ * 
+ */
+
+/*** MODULEINFO
+	<depend>curl</depend>
+ ***/
+
+#include "asterisk.h"
+
+ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
+
+#include <curl/curl.h>
+
+#include "asterisk/module.h"
+
+static int unload_module(void)
+{
+	int res = 0;
+
+	/* If the dependent modules are still in memory, forbid unload */
+	if (ast_module_check("func_curl.so")) {
+		ast_log(LOG_ERROR, "func_curl.so (dependent module) is still loaded.  Cannot unload res_curl.so\n");
+		return -1;
+	}
+
+	if (ast_module_check("res_config_curl.so")) {
+		ast_log(LOG_ERROR, "res_config_curl.so (dependent module) is still loaded.  Cannot unload res_curl.so\n");
+		return -1;
+	}
+
+	curl_global_cleanup();
+
+	return res;
+}
+
+static int load_module(void)
+{
+	int res = 0;
+
+	if (curl_global_init(CURL_GLOBAL_ALL)) {
+		ast_log(LOG_ERROR, "Unable to initialize the CURL library. Cannot load res_curl\n");
+		return AST_MODULE_LOAD_DECLINE;
+	}	
+
+	return res;
+}
+
+AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "cURL Resource Module");
+
+

Propchange: trunk/res/res_curl.c
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: trunk/res/res_curl.c
------------------------------------------------------------------------------
    svn:keywords = Author Id Revision Date

Propchange: trunk/res/res_curl.c
------------------------------------------------------------------------------
    svn:mime-type = text/plain




More information about the asterisk-commits mailing list