--- app_curl.c 2004-12-24 14:20:32.000000000 +0000 +++ app_curl.c 2004-12-27 11:23:09.000000000 +0000 @@ -6,6 +6,7 @@ * Copyright (C) 2004, Tilghman Lesher * * Tilghman Lesher + * and Brian Wilkins (Added POST option) * * This program is distributed with no restrictions on usage or * redistribution. @@ -30,9 +31,9 @@ static char *synopsis = "Load an external URL"; static char *descrip = -" Curl(URL): Requests the URL. Mainly used for signalling external\n" -"applications of an event. Returns 0 or -1 on fatal error. Also sets\n" -"CURL variable with the resulting page.\n"; +" Curl(URL|[postdata]): Requests the URL. Mainly used for signalling external\n" +"applications of an event. Returns 0 or -1 on fatal error. Argument\n" +"specified treated as POST data. Also sets CURL variable with the resulting page.\n"; STANDARD_LOCAL_USER; @@ -74,24 +75,54 @@ int res = 0; struct localuser *u; CURL *curl; + char *info, *options; + char *post_data = ""; + char *url; + char *var; if (!data || !strlen((char *)data)) { ast_log(LOG_WARNING, "Curl requires an argument (URL)\n"); return -1; } + if (!(info = ast_strdupa((char *)data))) { + ast_log(LOG_WARNING, "Unable to dupe data :(\n"); + return -1; + } + + /* app_curl takes in arguments in this fashion Curl(url|postdata) + * if postdata is left out, then it will do a GET instead */ + LOCAL_USER_ADD(u); + options = info; + if(options) { + var = strsep(&options, "|"); + if(var) { + url = var; + var = strsep(&options, "|"); + if(var) { + post_data = var; + } + } + } + else { + ast_log(LOG_ERROR, "Cannot use options!\n"); + return -1; + } curl_global_init(CURL_GLOBAL_ALL); curl = curl_easy_init(); if (curl) { struct MemoryStruct chunk; - chunk.memory=NULL; /* we expect realloc(NULL, size) to work */ chunk.size = 0; /* no data at this point */ - curl_easy_setopt(curl, CURLOPT_URL, (char *)data); + curl_easy_setopt(curl, CURLOPT_URL, url); + if(post_data) { + curl_easy_setopt(curl, CURLOPT_POST, 1); + curl_easy_setopt(curl, CURLOPT_POSTFIELDS, post_data); + } 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"); @@ -103,16 +134,16 @@ if (chunk.memory[chunk.size - 1] == 10) chunk.memory[chunk.size - 1] = '\0'; - pbx_builtin_setvar_helper(chan, "CURL", chunk.memory); + pbx_builtin_setvar_helper(chan, "CURL", chunk.memory); - free(chunk.memory); - } else { - ast_log(LOG_ERROR, "Cannot allocate curl structure\n"); - res = -1; - } + free(chunk.memory); + } else { + ast_log(LOG_ERROR, "Cannot allocate curl structure\n"); + res = -1; + } - LOCAL_USER_REMOVE(u); - return res; + LOCAL_USER_REMOVE(u); + return res; } int unload_module(void)