2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 2004 - 2006, Tilghman Lesher
6 * Tilghman Lesher <curl-20050919@the-tilghman.com>
7 * and Brian Wilkins <bwilkins@cfl.rr.com> (Added POST option)
9 * app_curl.c is distributed with no restrictions on usage or
12 * See http://www.asterisk.org for more information about
13 * the Asterisk project. Please do not directly contact
14 * any of the maintainers of this project for assistance;
15 * the project provides a web site, mailing lists and IRC
16 * channels for your use.
22 * \brief Curl - App to load a URL
24 * \author Tilghman Lesher <curl-20050919@the-tilghman.com>
26 * \note Brian Wilkins <bwilkins@cfl.rr.com> (Added POST option)
28 * \ingroup applications
34 #include <curl/curl.h>
38 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
40 #include "asterisk/lock.h"
41 #include "asterisk/file.h"
42 #include "asterisk/logger.h"
43 #include "asterisk/channel.h"
44 #include "asterisk/pbx.h"
45 #include "asterisk/cli.h"
46 #include "asterisk/options.h"
47 #include "asterisk/module.h"
48 #include "asterisk/app.h"
49 #include "asterisk/utils.h"
51 static char *tdesc = "Load external URL";
60 static void *myrealloc(void *ptr, size_t size)
62 /* There might be a realloc() out there that doesn't like reallocing
63 NULL pointers, so we take care of it here */
65 return ast_realloc(ptr, size);
67 return ast_malloc(size);
70 static size_t WriteMemoryCallback(void *ptr, size_t size, size_t nmemb, void *data)
72 register int realsize = size * nmemb;
73 struct MemoryStruct *mem = (struct MemoryStruct *)data;
75 mem->memory = (char *)myrealloc(mem->memory, mem->size + realsize + 1);
77 memcpy(&(mem->memory[mem->size]), ptr, realsize);
78 mem->size += realsize;
79 mem->memory[mem->size] = 0;
84 static int curl_internal(struct MemoryStruct *chunk, char *url, char *post)
88 curl_global_init(CURL_GLOBAL_ALL);
89 curl = curl_easy_init();
95 curl_easy_setopt(curl, CURLOPT_URL, url);
96 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
97 curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)chunk);
98 curl_easy_setopt(curl, CURLOPT_USERAGENT, "asterisk-libcurl-agent/1.0");
101 curl_easy_setopt(curl, CURLOPT_POST, 1);
102 curl_easy_setopt(curl, CURLOPT_POSTFIELDS, post);
105 curl_easy_perform(curl);
106 curl_easy_cleanup(curl);
110 static int acf_curl_exec(struct ast_channel *chan, char *cmd, char *info, char *buf, size_t len)
113 struct MemoryStruct chunk = { NULL, 0 };
114 AST_DECLARE_APP_ARGS(args,
116 AST_APP_ARG(postdata);
121 if (ast_strlen_zero(info)) {
122 ast_log(LOG_WARNING, "CURL requires an argument (URL)\n");
128 AST_STANDARD_APP_ARGS(args, info);
130 if (!curl_internal(&chunk, args.url, args.postdata)) {
132 chunk.memory[chunk.size] = '\0';
133 if (chunk.memory[chunk.size - 1] == 10)
134 chunk.memory[chunk.size - 1] = '\0';
136 ast_copy_string(buf, chunk.memory, len);
140 ast_log(LOG_ERROR, "Cannot allocate curl structure\n");
143 LOCAL_USER_REMOVE(u);
148 struct ast_custom_function acf_curl = {
150 .synopsis = "Retrieves the contents of a URL",
151 .syntax = "CURL(url[|post-data])",
153 " url - URL to retrieve\n"
154 " post-data - Optional data to send as a POST (GET is default action)\n",
155 .read = acf_curl_exec,
158 int unload_module(void)
162 res = ast_custom_function_unregister(&acf_curl);
164 STANDARD_HANGUP_LOCALUSERS;
169 int load_module(void)
173 res = ast_custom_function_register(&acf_curl);
178 char *description(void)
186 STANDARD_USECOUNT(res);
192 return ASTERISK_GPL_KEY;