diff --git a/funcs/func_curl.c b/funcs/func_curl.c index 32dbf7686d798a55d787df6eca0c7c0437b50b60..6c0d9b7670768f84d77cea47d450400b4e7562e4 100644 --- a/funcs/func_curl.c +++ b/funcs/func_curl.c @@ -50,28 +50,14 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$") #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 @@ static void curl_instance_cleanup(void *data) 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 curl_internal(struct MemoryStruct *chunk, char *url, char *post) 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 @@ static int acf_curl_exec(struct ast_channel *chan, const char *cmd, char *info, if (ast_strlen_zero(info)) { ast_log(LOG_WARNING, "CURL requires an argument (URL)\n"); + ast_free(str); return -1; } @@ -146,18 +133,19 @@ static int acf_curl_exec(struct ast_channel *chan, const char *cmd, char *info, 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 @@ static int unload_module(void) res = ast_custom_function_unregister(&acf_curl); - curl_global_cleanup(); - return res; } @@ -190,10 +176,12 @@ static int load_module(void) { 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); diff --git a/res/res_config_curl.c b/res/res_config_curl.c index 722b08e9d62fd5ea9a0b527fa7441209b5b43bc0..37079adeaf989f3dc4a42354a3589b999ed92c52 100644 --- a/res/res_config_curl.c +++ b/res/res_config_curl.c @@ -538,15 +538,22 @@ static struct ast_config_engine curl_engine = { .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; diff --git a/res/res_curl.c b/res/res_curl.c new file mode 100644 index 0000000000000000000000000000000000000000..9e3284bd7c4b099f8b07123f8e66bfa981300cdd --- /dev/null +++ b/res/res_curl.c @@ -0,0 +1,75 @@ +/* + * Asterisk -- An open source telephony toolkit. + * + * Copyright (c) 2008, Digium, Inc. + * + * Tilghman Lesher <res_curl_v1@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@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"); + +