--- ../asterisk-1.2.4/apps/app_curl.c 2005-11-29 20:24:39.000000000 +0200 +++ apps/app_curl.c 2006-02-23 13:26:01.000000000 +0200 @@ -48,13 +48,14 @@ static char *synopsis = "Load an external URL"; static char *descrip = -" Curl(URL[|postdata]): This application will request the specified URL.\n" +" Curl(URL[|postdata[|timeout]]): This application will request the specified URL.\n" "It is mainly used for signalling external applications of an event.\n" "Parameters:\n" " URL - This is the external URL to request.\n" " postdata - This information will be treated as POST data.\n" "This application will set the following variable:\n" -" CURL - This variable will contain the resulting page.\n" +" CURLRES - This variable will contain the result code of the URL hit.\n" +" CURL - This variable will contain the resulting page.\n" "This application has been deprecated in favor of the CURL function.\n"; STANDARD_LOCAL_USER; @@ -90,9 +91,11 @@ return realsize; } -static int curl_internal(struct MemoryStruct *chunk, char *url, char *post) +static int curl_internal(struct MemoryStruct *chunk, char *url, char *post, int tm) { CURL *curl; + CURLcode res; + int result; curl_global_init(CURL_GLOBAL_ALL); curl = curl_easy_init(); @@ -105,24 +108,35 @@ curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback); curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)chunk); curl_easy_setopt(curl, CURLOPT_USERAGENT, "asterisk-libcurl-agent/1.0"); + if (tm > -1) + curl_easy_setopt(curl, CURLOPT_TIMEOUT, tm); if (post) { curl_easy_setopt(curl, CURLOPT_POST, 1); curl_easy_setopt(curl, CURLOPT_POSTFIELDS, post); } - curl_easy_perform(curl); + res = curl_easy_perform(curl); + if (res != 0) { + ast_log(LOG_WARNING, "CURL error: %s\n", curl_easy_strerror(res)); + result = res; + } else { + curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &result); + } curl_easy_cleanup(curl); - return 0; + return result; } static int curl_exec(struct ast_channel *chan, void *data) { - int res = 0; + int result = 0; struct localuser *u; - char *info, *post_data=NULL, *url; + char *info, *post_data=NULL, *timeout=NULL, *url; struct MemoryStruct chunk = { NULL, 0 }; + int tm = -1; static int dep_warning = 0; + int res; + char tmp[64]; if (!dep_warning) { ast_log(LOG_WARNING, "The application Curl is deprecated. Please use the CURL() function instead.\n"); @@ -139,13 +153,26 @@ if ((info = ast_strdupa(data))) { url = strsep(&info, "|"); post_data = info; + if (post_data != NULL) { + post_data = strsep(&info, "|"); + timeout = info; + if (timeout != NULL) { + res = sscanf(timeout, "%d", &tm); + if (res != 1) { + ast_log(LOG_WARNING, "Not a valid timeout (%s)\n", timeout); + LOCAL_USER_REMOVE(u); + return -1; + } + } + } } else { ast_log(LOG_ERROR, "Out of memory\n"); LOCAL_USER_REMOVE(u); return -1; } - if (! curl_internal(&chunk, url, post_data)) { + res = curl_internal(&chunk, url, post_data, tm); + if (res != 0) { if (chunk.memory) { chunk.memory[chunk.size] = '\0'; if (chunk.memory[chunk.size - 1] == 10) @@ -157,21 +184,27 @@ } } else { ast_log(LOG_ERROR, "Cannot allocate curl structure\n"); - res = -1; + result = -1; } + memset(tmp, 0, sizeof(tmp)); + snprintf(tmp, sizeof(tmp), "%d", res); + pbx_builtin_setvar_helper(chan, "CURLRES", tmp); LOCAL_USER_REMOVE(u); - return res; + return result; } static char *acf_curl_exec(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len) { struct localuser *u; - char *info, *post_data=NULL, *url; + char *info, *post_data=NULL, *timeout=NULL, *url; struct MemoryStruct chunk = { NULL, 0 }; + int tm = -1; + int res; + char tmp[64]; *buf = '\0'; - + if (ast_strlen_zero(data)) { ast_log(LOG_WARNING, "CURL requires an argument (URL)\n"); return buf; @@ -185,11 +218,24 @@ LOCAL_USER_REMOVE(u); return buf; } - + url = strsep(&info, "|"); post_data = info; - - if (! curl_internal(&chunk, url, post_data)) { + if (post_data != NULL) { + post_data = strsep(&info, "|"); + timeout = info; + if (timeout != NULL) { + res = sscanf(timeout, "%d", &tm); + if (res != 1) { + ast_log(LOG_WARNING, "Not a valid timeout (%s)\n", timeout); + LOCAL_USER_REMOVE(u); + return buf; + } + } + } + + res = curl_internal(&chunk, url, post_data, tm); + if (res != 0) { if (chunk.memory) { chunk.memory[chunk.size] = '\0'; if (chunk.memory[chunk.size - 1] == 10) @@ -199,8 +245,11 @@ free(chunk.memory); } } else { - ast_log(LOG_ERROR, "Cannot allocate curl structure\n"); + ast_log(LOG_ERROR, "Failed to perform URL hit.\n"); } + memset(tmp, 0, sizeof(tmp)); + snprintf(tmp, sizeof(tmp), "%d", res); + pbx_builtin_setvar_helper(chan, "CURLRES", tmp); LOCAL_USER_REMOVE(u); return buf; @@ -209,10 +258,11 @@ struct ast_custom_function acf_curl = { .name = "CURL", .synopsis = "Retrieves the contents of a URL", - .syntax = "CURL(url[|post-data])", + .syntax = "CURL(url[|post-data[|timeout]])", .desc = " url - URL to retrieve\n" - " post-data - Optional data to send as a POST (GET is default action)\n", + " post-data - Optional data to send as a POST (GET is default action)\n" + " timeout - Optional timeout in seconds of teh curl call\n", .read = acf_curl_exec, };