Merge changes from team/russell/iax2_find_callno and iax2_find_callno_1.4
[asterisk-bristuff.git] / funcs / func_curl.c
blob63d6a05970dd055df01f379e4a8481f43812ff76
1 /*
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
10 * redistribution.
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.
20 /*! \file
22 * \brief Curl - 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 functions
31 /*** MODULEINFO
32 <depend>curl</depend>
33 ***/
35 #include "asterisk.h"
37 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <curl/curl.h>
44 #include "asterisk/lock.h"
45 #include "asterisk/file.h"
46 #include "asterisk/logger.h"
47 #include "asterisk/channel.h"
48 #include "asterisk/pbx.h"
49 #include "asterisk/cli.h"
50 #include "asterisk/options.h"
51 #include "asterisk/module.h"
52 #include "asterisk/app.h"
53 #include "asterisk/utils.h"
54 #include "asterisk/threadstorage.h"
56 struct MemoryStruct {
57 char *memory;
58 size_t size;
61 static void *myrealloc(void *ptr, size_t size)
63 /* There might be a realloc() out there that doesn't like reallocing
64 NULL pointers, so we take care of it here */
65 if (ptr)
66 return ast_realloc(ptr, size);
67 else
68 return ast_malloc(size);
71 static size_t WriteMemoryCallback(void *ptr, size_t size, size_t nmemb, void *data)
73 register int realsize = size * nmemb;
74 struct MemoryStruct *mem = (struct MemoryStruct *)data;
76 mem->memory = (char *)myrealloc(mem->memory, mem->size + realsize + 1);
77 if (mem->memory) {
78 memcpy(&(mem->memory[mem->size]), ptr, realsize);
79 mem->size += realsize;
80 mem->memory[mem->size] = 0;
82 return realsize;
85 static const char *global_useragent = "asterisk-libcurl-agent/1.0";
87 static void curl_instance_cleanup(void *data)
89 CURL **curl = data;
91 curl_easy_cleanup(*curl);
94 AST_THREADSTORAGE_CUSTOM(curl_instance, curl_instance_init, curl_instance_cleanup);
96 static int curl_internal(struct MemoryStruct *chunk, char *url, char *post)
98 CURL **curl;
100 if (!(curl = ast_threadstorage_get(&curl_instance, sizeof(*curl))))
101 return -1;
103 if (!*curl) {
104 if (!(*curl = curl_easy_init()))
105 return -1;
106 curl_easy_setopt(*curl, CURLOPT_NOSIGNAL, 1);
107 curl_easy_setopt(*curl, CURLOPT_TIMEOUT, 180);
108 curl_easy_setopt(*curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
109 curl_easy_setopt(*curl, CURLOPT_USERAGENT, global_useragent);
112 curl_easy_setopt(*curl, CURLOPT_URL, url);
113 curl_easy_setopt(*curl, CURLOPT_WRITEDATA, (void *) chunk);
115 if (post) {
116 curl_easy_setopt(*curl, CURLOPT_POST, 1);
117 curl_easy_setopt(*curl, CURLOPT_POSTFIELDS, post);
120 curl_easy_perform(*curl);
122 if (post)
123 curl_easy_setopt(*curl, CURLOPT_POST, 0);
125 return 0;
128 static int acf_curl_exec(struct ast_channel *chan, char *cmd, char *info, char *buf, size_t len)
130 struct ast_module_user *u;
131 struct MemoryStruct chunk = { NULL, 0 };
132 AST_DECLARE_APP_ARGS(args,
133 AST_APP_ARG(url);
134 AST_APP_ARG(postdata);
137 *buf = '\0';
139 if (ast_strlen_zero(info)) {
140 ast_log(LOG_WARNING, "CURL requires an argument (URL)\n");
141 return -1;
144 u = ast_module_user_add(chan);
146 AST_STANDARD_APP_ARGS(args, info);
148 if (chan)
149 ast_autoservice_start(chan);
151 if (!curl_internal(&chunk, args.url, args.postdata)) {
152 if (chunk.memory) {
153 chunk.memory[chunk.size] = '\0';
154 if (chunk.memory[chunk.size - 1] == 10)
155 chunk.memory[chunk.size - 1] = '\0';
157 ast_copy_string(buf, chunk.memory, len);
158 free(chunk.memory);
160 } else {
161 ast_log(LOG_ERROR, "Cannot allocate curl structure\n");
164 if (chan)
165 ast_autoservice_stop(chan);
167 ast_module_user_remove(u);
169 return 0;
172 struct ast_custom_function acf_curl = {
173 .name = "CURL",
174 .synopsis = "Retrieves the contents of a URL",
175 .syntax = "CURL(url[|post-data])",
176 .desc =
177 " url - URL to retrieve\n"
178 " post-data - Optional data to send as a POST (GET is default action)\n",
179 .read = acf_curl_exec,
182 static int unload_module(void)
184 int res;
186 res = ast_custom_function_unregister(&acf_curl);
188 ast_module_user_hangup_all();
190 curl_global_cleanup();
192 return res;
195 static int load_module(void)
197 int res;
199 if (curl_global_init(CURL_GLOBAL_ALL)) {
200 ast_log(LOG_ERROR, "Unable to initialize the CURL library. Cannot load func_curl\n");
201 return AST_MODULE_LOAD_DECLINE;
204 res = ast_custom_function_register(&acf_curl);
206 return res;
209 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Load external URL");