Merged revisions 336316 via svnmerge from
[asterisk/asterisk.git] / funcs / func_curl.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C)  2004 - 2006, Tilghman Lesher
5  *
6  * Tilghman Lesher <curl-20050919@the-tilghman.com>
7  * and Brian Wilkins <bwilkins@cfl.rr.com> (Added POST option)
8  *
9  * app_curl.c is distributed with no restrictions on usage or
10  * redistribution.
11  *
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.
17  *
18  */
19
20 /*! \file
21  * 
22  * \brief Curl - Load a URL
23  *
24  * \author Tilghman Lesher <curl-20050919@the-tilghman.com>
25  *
26  * \note Brian Wilkins <bwilkins@cfl.rr.com> (Added POST option) 
27  *
28  * \extref Depends on the CURL library  - http://curl.haxx.se/
29  * 
30  * \ingroup functions
31  */
32  
33 /*** MODULEINFO
34         <depend>curl</depend>
35         <support_level>core</support_level>
36  ***/
37
38 #include "asterisk.h"
39
40 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
41
42 #include <curl/curl.h>
43
44 #include "asterisk/lock.h"
45 #include "asterisk/file.h"
46 #include "asterisk/channel.h"
47 #include "asterisk/pbx.h"
48 #include "asterisk/cli.h"
49 #include "asterisk/module.h"
50 #include "asterisk/app.h"
51 #include "asterisk/utils.h"
52 #include "asterisk/threadstorage.h"
53
54 /*** DOCUMENTATION
55         <function name="CURL" language="en_US">
56                 <synopsis>
57                         Retrieve content from a remote web or ftp server
58                 </synopsis>
59                 <syntax>
60                         <parameter name="url" required="true" />
61                         <parameter name="post-data">
62                                 <para>If specified, an <literal>HTTP POST</literal> will be
63                                 performed with the content of
64                                 <replaceable>post-data</replaceable>, instead of an
65                                 <literal>HTTP GET</literal> (default).</para>
66                         </parameter>
67                 </syntax>
68                 <description />
69                 <see-also>
70                         <ref type="function">CURLOPT</ref>
71                 </see-also>
72         </function>
73         <function name="CURLOPT" language="en_US">
74                 <synopsis>
75                         Sets various options for future invocations of CURL.
76                 </synopsis>
77                 <syntax>
78                         <parameter name="key" required="yes">
79                                 <enumlist>
80                                         <enum name="cookie">
81                                                 <para>A cookie to send with the request.  Multiple
82                                                 cookies are supported.</para>
83                                         </enum>
84                                         <enum name="conntimeout">
85                                                 <para>Number of seconds to wait for a connection to succeed</para>
86                                         </enum>
87                                         <enum name="dnstimeout">
88                                                 <para>Number of seconds to wait for DNS to be resolved</para>
89                                         </enum>
90                                         <enum name="ftptext">
91                                                 <para>For FTP URIs, force a text transfer (boolean)</para>
92                                         </enum>
93                                         <enum name="ftptimeout">
94                                                 <para>For FTP URIs, number of seconds to wait for a
95                                                 server response</para>
96                                         </enum>
97                                         <enum name="header">
98                                                 <para>Include header information in the result
99                                                 (boolean)</para>
100                                         </enum>
101                                         <enum name="httptimeout">
102                                                 <para>For HTTP(S) URIs, number of seconds to wait for a
103                                                 server response</para>
104                                         </enum>
105                                         <enum name="maxredirs">
106                                                 <para>Maximum number of redirects to follow</para>
107                                         </enum>
108                                         <enum name="proxy">
109                                                 <para>Hostname or IP address to use as a proxy server</para>
110                                         </enum>
111                                         <enum name="proxytype">
112                                                 <para>Type of <literal>proxy</literal></para>
113                                                 <enumlist>
114                                                         <enum name="http" />
115                                                         <enum name="socks4" />
116                                                         <enum name="socks5" />
117                                                 </enumlist>
118                                         </enum>
119                                         <enum name="proxyport">
120                                                 <para>Port number of the <literal>proxy</literal></para>
121                                         </enum>
122                                         <enum name="proxyuserpwd">
123                                                 <para>A <replaceable>username</replaceable><literal>:</literal><replaceable>password</replaceable>
124                                                 combination to use for authenticating requests through a
125                                                 <literal>proxy</literal></para>
126                                         </enum>
127                                         <enum name="referer">
128                                                 <para>Referer URL to use for the request</para>
129                                         </enum>
130                                         <enum name="useragent">
131                                                 <para>UserAgent string to use for the request</para>
132                                         </enum>
133                                         <enum name="userpwd">
134                                                 <para>A <replaceable>username</replaceable><literal>:</literal><replaceable>password</replaceable>
135                                                 to use for authentication when the server response to
136                                                 an initial request indicates a 401 status code.</para>
137                                         </enum>
138                                         <enum name="ssl_verifypeer">
139                                                 <para>Whether to verify the server certificate against
140                                                 a list of known root certificate authorities (boolean).</para>
141                                         </enum>
142                                         <enum name="hashcompat">
143                                                 <para>Assuming the responses will be in <literal>key1=value1&amp;key2=value2</literal>
144                                                 format, reformat the response such that it can be used
145                                                 by the <literal>HASH</literal> function.</para>
146                                                 <enumlist>
147                                                         <enum name="yes" />
148                                                         <enum name="no" />
149                                                         <enum name="legacy">
150                                                                 <para>Also translate <literal>+</literal> to the
151                                                                 space character, in violation of current RFC
152                                                                 standards.</para>
153                                                         </enum>
154                                                 </enumlist>
155                                         </enum>
156                                 </enumlist>
157                         </parameter>
158                 </syntax>
159                 <description>
160                         <para>Options may be set globally or per channel.  Per-channel
161                         settings will override global settings.</para>
162                 </description>
163                 <see-also>
164                         <ref type="function">CURL</ref>
165                         <ref type="function">HASH</ref>
166                 </see-also>
167         </function>
168  ***/
169
170 #define CURLVERSION_ATLEAST(a,b,c) \
171         ((LIBCURL_VERSION_MAJOR > (a)) || ((LIBCURL_VERSION_MAJOR == (a)) && (LIBCURL_VERSION_MINOR > (b))) || ((LIBCURL_VERSION_MAJOR == (a)) && (LIBCURL_VERSION_MINOR == (b)) && (LIBCURL_VERSION_PATCH >= (c))))
172
173 #define CURLOPT_SPECIAL_HASHCOMPAT -500
174
175 static void curlds_free(void *data);
176
177 static struct ast_datastore_info curl_info = {
178         .type = "CURL",
179         .destroy = curlds_free,
180 };
181
182 struct curl_settings {
183         AST_LIST_ENTRY(curl_settings) list;
184         CURLoption key;
185         void *value;
186 };
187
188 AST_LIST_HEAD_STATIC(global_curl_info, curl_settings);
189
190 static void curlds_free(void *data)
191 {
192         AST_LIST_HEAD(global_curl_info, curl_settings) *list = data;
193         struct curl_settings *setting;
194         if (!list) {
195                 return;
196         }
197         while ((setting = AST_LIST_REMOVE_HEAD(list, list))) {
198                 free(setting);
199         }
200         AST_LIST_HEAD_DESTROY(list);
201 }
202
203 enum optiontype {
204         OT_BOOLEAN,
205         OT_INTEGER,
206         OT_INTEGER_MS,
207         OT_STRING,
208         OT_ENUM,
209 };
210
211 enum hashcompat {
212         HASHCOMPAT_NO = 0,
213         HASHCOMPAT_YES,
214         HASHCOMPAT_LEGACY,
215 };
216
217 static int parse_curlopt_key(const char *name, CURLoption *key, enum optiontype *ot)
218 {
219         if (!strcasecmp(name, "header")) {
220                 *key = CURLOPT_HEADER;
221                 *ot = OT_BOOLEAN;
222         } else if (!strcasecmp(name, "proxy")) {
223                 *key = CURLOPT_PROXY;
224                 *ot = OT_STRING;
225         } else if (!strcasecmp(name, "proxyport")) {
226                 *key = CURLOPT_PROXYPORT;
227                 *ot = OT_INTEGER;
228         } else if (!strcasecmp(name, "proxytype")) {
229                 *key = CURLOPT_PROXYTYPE;
230                 *ot = OT_ENUM;
231         } else if (!strcasecmp(name, "dnstimeout")) {
232                 *key = CURLOPT_DNS_CACHE_TIMEOUT;
233                 *ot = OT_INTEGER;
234         } else if (!strcasecmp(name, "userpwd")) {
235                 *key = CURLOPT_USERPWD;
236                 *ot = OT_STRING;
237         } else if (!strcasecmp(name, "proxyuserpwd")) {
238                 *key = CURLOPT_PROXYUSERPWD;
239                 *ot = OT_STRING;
240         } else if (!strcasecmp(name, "maxredirs")) {
241                 *key = CURLOPT_MAXREDIRS;
242                 *ot = OT_INTEGER;
243         } else if (!strcasecmp(name, "referer")) {
244                 *key = CURLOPT_REFERER;
245                 *ot = OT_STRING;
246         } else if (!strcasecmp(name, "useragent")) {
247                 *key = CURLOPT_USERAGENT;
248                 *ot = OT_STRING;
249         } else if (!strcasecmp(name, "cookie")) {
250                 *key = CURLOPT_COOKIE;
251                 *ot = OT_STRING;
252         } else if (!strcasecmp(name, "ftptimeout")) {
253                 *key = CURLOPT_FTP_RESPONSE_TIMEOUT;
254                 *ot = OT_INTEGER;
255         } else if (!strcasecmp(name, "httptimeout")) {
256 #if CURLVERSION_ATLEAST(7,16,2)
257                 *key = CURLOPT_TIMEOUT_MS;
258                 *ot = OT_INTEGER_MS;
259 #else
260                 *key = CURLOPT_TIMEOUT;
261                 *ot = OT_INTEGER;
262 #endif
263         } else if (!strcasecmp(name, "conntimeout")) {
264 #if CURLVERSION_ATLEAST(7,16,2)
265                 *key = CURLOPT_CONNECTTIMEOUT_MS;
266                 *ot = OT_INTEGER_MS;
267 #else
268                 *key = CURLOPT_CONNECTTIMEOUT;
269                 *ot = OT_INTEGER;
270 #endif
271         } else if (!strcasecmp(name, "ftptext")) {
272                 *key = CURLOPT_TRANSFERTEXT;
273                 *ot = OT_BOOLEAN;
274         } else if (!strcasecmp(name, "ssl_verifypeer")) {
275                 *key = CURLOPT_SSL_VERIFYPEER;
276                 *ot = OT_BOOLEAN;
277         } else if (!strcasecmp(name, "hashcompat")) {
278                 *key = CURLOPT_SPECIAL_HASHCOMPAT;
279                 *ot = OT_ENUM;
280         } else {
281                 return -1;
282         }
283         return 0;
284 }
285
286 static int acf_curlopt_write(struct ast_channel *chan, const char *cmd, char *name, const char *value)
287 {
288         struct ast_datastore *store;
289         struct global_curl_info *list;
290         struct curl_settings *cur, *new = NULL;
291         CURLoption key;
292         enum optiontype ot;
293
294         if (chan) {
295                 if (!(store = ast_channel_datastore_find(chan, &curl_info, NULL))) {
296                         /* Create a new datastore */
297                         if (!(store = ast_datastore_alloc(&curl_info, NULL))) {
298                                 ast_log(LOG_ERROR, "Unable to allocate new datastore.  Cannot set any CURL options\n");
299                                 return -1;
300                         }
301
302                         if (!(list = ast_calloc(1, sizeof(*list)))) {
303                                 ast_log(LOG_ERROR, "Unable to allocate list head.  Cannot set any CURL options\n");
304                                 ast_datastore_free(store);
305                         }
306
307                         store->data = list;
308                         AST_LIST_HEAD_INIT(list);
309                         ast_channel_datastore_add(chan, store);
310                 } else {
311                         list = store->data;
312                 }
313         } else {
314                 /* Populate the global structure */
315                 list = &global_curl_info;
316         }
317
318         if (!parse_curlopt_key(name, &key, &ot)) {
319                 if (ot == OT_BOOLEAN) {
320                         if ((new = ast_calloc(1, sizeof(*new)))) {
321                                 new->value = (void *)((long) ast_true(value));
322                         }
323                 } else if (ot == OT_INTEGER) {
324                         long tmp = atol(value);
325                         if ((new = ast_calloc(1, sizeof(*new)))) {
326                                 new->value = (void *)tmp;
327                         }
328                 } else if (ot == OT_INTEGER_MS) {
329                         long tmp = atof(value) * 1000.0;
330                         if ((new = ast_calloc(1, sizeof(*new)))) {
331                                 new->value = (void *)tmp;
332                         }
333                 } else if (ot == OT_STRING) {
334                         if ((new = ast_calloc(1, sizeof(*new) + strlen(value) + 1))) {
335                                 new->value = (char *)new + sizeof(*new);
336                                 strcpy(new->value, value);
337                         }
338                 } else if (ot == OT_ENUM) {
339                         if (key == CURLOPT_PROXYTYPE) {
340                                 long ptype =
341 #if CURLVERSION_ATLEAST(7,10,0)
342                                         CURLPROXY_HTTP;
343 #else
344                                         CURLPROXY_SOCKS5;
345 #endif
346                                 if (0) {
347 #if CURLVERSION_ATLEAST(7,15,2)
348                                 } else if (!strcasecmp(value, "socks4")) {
349                                         ptype = CURLPROXY_SOCKS4;
350 #endif
351 #if CURLVERSION_ATLEAST(7,18,0)
352                                 } else if (!strcasecmp(value, "socks4a")) {
353                                         ptype = CURLPROXY_SOCKS4A;
354 #endif
355 #if CURLVERSION_ATLEAST(7,18,0)
356                                 } else if (!strcasecmp(value, "socks5")) {
357                                         ptype = CURLPROXY_SOCKS5;
358 #endif
359 #if CURLVERSION_ATLEAST(7,18,0)
360                                 } else if (!strncasecmp(value, "socks5", 6)) {
361                                         ptype = CURLPROXY_SOCKS5_HOSTNAME;
362 #endif
363                                 }
364
365                                 if ((new = ast_calloc(1, sizeof(*new)))) {
366                                         new->value = (void *)ptype;
367                                 }
368                         } else if (key == CURLOPT_SPECIAL_HASHCOMPAT) {
369                                 if ((new = ast_calloc(1, sizeof(*new)))) {
370                                         new->value = (void *) (long) (!strcasecmp(value, "legacy") ? HASHCOMPAT_LEGACY : ast_true(value) ? HASHCOMPAT_YES : HASHCOMPAT_NO);
371                                 }
372                         } else {
373                                 /* Highly unlikely */
374                                 goto yuck;
375                         }
376                 }
377
378                 /* Memory allocation error */
379                 if (!new) {
380                         return -1;
381                 }
382
383                 new->key = key;
384         } else {
385 yuck:
386                 ast_log(LOG_ERROR, "Unrecognized option: %s\n", name);
387                 return -1;
388         }
389
390         /* Remove any existing entry */
391         AST_LIST_LOCK(list);
392         AST_LIST_TRAVERSE_SAFE_BEGIN(list, cur, list) {
393                 if (cur->key == new->key) {
394                         AST_LIST_REMOVE_CURRENT(list);
395                         free(cur);
396                         break;
397                 }
398         }
399         AST_LIST_TRAVERSE_SAFE_END
400
401         /* Insert new entry */
402         ast_debug(1, "Inserting entry %p with key %d and value %p\n", new, new->key, new->value);
403         AST_LIST_INSERT_TAIL(list, new, list);
404         AST_LIST_UNLOCK(list);
405
406         return 0;
407 }
408
409 static int acf_curlopt_helper(struct ast_channel *chan, const char *cmd, char *data, char *buf, struct ast_str **bufstr, ssize_t len)
410 {
411         struct ast_datastore *store;
412         struct global_curl_info *list[2] = { &global_curl_info, NULL };
413         struct curl_settings *cur = NULL;
414         CURLoption key;
415         enum optiontype ot;
416         int i;
417
418         if (parse_curlopt_key(data, &key, &ot)) {
419                 ast_log(LOG_ERROR, "Unrecognized option: '%s'\n", data);
420                 return -1;
421         }
422
423         if (chan && (store = ast_channel_datastore_find(chan, &curl_info, NULL))) {
424                 list[0] = store->data;
425                 list[1] = &global_curl_info;
426         }
427
428         for (i = 0; i < 2; i++) {
429                 if (!list[i]) {
430                         break;
431                 }
432                 AST_LIST_LOCK(list[i]);
433                 AST_LIST_TRAVERSE(list[i], cur, list) {
434                         if (cur->key == key) {
435                                 if (ot == OT_BOOLEAN || ot == OT_INTEGER) {
436                                         if (buf) {
437                                                 snprintf(buf, len, "%ld", (long) cur->value);
438                                         } else {
439                                                 ast_str_set(bufstr, len, "%ld", (long) cur->value);
440                                         }
441                                 } else if (ot == OT_INTEGER_MS) {
442                                         if ((long) cur->value % 1000 == 0) {
443                                                 if (buf) {
444                                                         snprintf(buf, len, "%ld", (long)cur->value / 1000);
445                                                 } else {
446                                                         ast_str_set(bufstr, len, "%ld", (long) cur->value / 1000);
447                                                 }
448                                         } else {
449                                                 if (buf) {
450                                                         snprintf(buf, len, "%.3f", (double) ((long) cur->value) / 1000.0);
451                                                 } else {
452                                                         ast_str_set(bufstr, len, "%.3f", (double) ((long) cur->value) / 1000.0);
453                                                 }
454                                         }
455                                 } else if (ot == OT_STRING) {
456                                         ast_debug(1, "Found entry %p, with key %d and value %p\n", cur, cur->key, cur->value);
457                                         if (buf) {
458                                                 ast_copy_string(buf, cur->value, len);
459                                         } else {
460                                                 ast_str_set(bufstr, 0, "%s", (char *) cur->value);
461                                         }
462                                 } else if (key == CURLOPT_PROXYTYPE) {
463                                         const char *strval = "unknown";
464                                         if (0) {
465 #if CURLVERSION_ATLEAST(7,15,2)
466                                         } else if ((long)cur->value == CURLPROXY_SOCKS4) {
467                                                 strval = "socks4";
468 #endif
469 #if CURLVERSION_ATLEAST(7,18,0)
470                                         } else if ((long)cur->value == CURLPROXY_SOCKS4A) {
471                                                 strval = "socks4a";
472 #endif
473                                         } else if ((long)cur->value == CURLPROXY_SOCKS5) {
474                                                 strval = "socks5";
475 #if CURLVERSION_ATLEAST(7,18,0)
476                                         } else if ((long)cur->value == CURLPROXY_SOCKS5_HOSTNAME) {
477                                                 strval = "socks5hostname";
478 #endif
479 #if CURLVERSION_ATLEAST(7,10,0)
480                                         } else if ((long)cur->value == CURLPROXY_HTTP) {
481                                                 strval = "http";
482 #endif
483                                         }
484                                         if (buf) {
485                                                 ast_copy_string(buf, strval, len);
486                                         } else {
487                                                 ast_str_set(bufstr, 0, "%s", strval);
488                                         }
489                                 } else if (key == CURLOPT_SPECIAL_HASHCOMPAT) {
490                                         const char *strval = "unknown";
491                                         if ((long) cur->value == HASHCOMPAT_LEGACY) {
492                                                 strval = "legacy";
493                                         } else if ((long) cur->value == HASHCOMPAT_YES) {
494                                                 strval = "yes";
495                                         } else if ((long) cur->value == HASHCOMPAT_NO) {
496                                                 strval = "no";
497                                         }
498                                         if (buf) {
499                                                 ast_copy_string(buf, strval, len);
500                                         } else {
501                                                 ast_str_set(bufstr, 0, "%s", strval);
502                                         }
503                                 }
504                                 break;
505                         }
506                 }
507                 AST_LIST_UNLOCK(list[i]);
508                 if (cur) {
509                         break;
510                 }
511         }
512
513         return cur ? 0 : -1;
514 }
515
516 static int acf_curlopt_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
517 {
518         return acf_curlopt_helper(chan, cmd, data, buf, NULL, len);
519 }
520
521 static int acf_curlopt_read2(struct ast_channel *chan, const char *cmd, char *data, struct ast_str **buf, ssize_t len)
522 {
523         return acf_curlopt_helper(chan, cmd, data, NULL, buf, len);
524 }
525
526 static size_t WriteMemoryCallback(void *ptr, size_t size, size_t nmemb, void *data)
527 {
528         register int realsize = size * nmemb;
529         struct ast_str **pstr = (struct ast_str **)data;
530
531         ast_debug(3, "Called with data=%p, str=%p, realsize=%d, len=%zu, used=%zu\n", data, *pstr, realsize, ast_str_size(*pstr), ast_str_strlen(*pstr));
532
533         ast_str_append_substr(pstr, 0, ptr, realsize);
534
535         ast_debug(3, "Now, len=%zu, used=%zu\n", ast_str_size(*pstr), ast_str_strlen(*pstr));
536
537         return realsize;
538 }
539
540 static const char * const global_useragent = "asterisk-libcurl-agent/1.0";
541
542 static int curl_instance_init(void *data)
543 {
544         CURL **curl = data;
545
546         if (!(*curl = curl_easy_init()))
547                 return -1;
548
549         curl_easy_setopt(*curl, CURLOPT_NOSIGNAL, 1);
550         curl_easy_setopt(*curl, CURLOPT_TIMEOUT, 180);
551         curl_easy_setopt(*curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
552         curl_easy_setopt(*curl, CURLOPT_USERAGENT, global_useragent);
553
554         return 0;
555 }
556
557 static void curl_instance_cleanup(void *data)
558 {
559         CURL **curl = data;
560
561         curl_easy_cleanup(*curl);
562
563         ast_free(data);
564 }
565
566 AST_THREADSTORAGE_CUSTOM(curl_instance, curl_instance_init, curl_instance_cleanup);
567
568 static int acf_curl_helper(struct ast_channel *chan, const char *cmd, char *info, char *buf, struct ast_str **input_str, ssize_t len)
569 {
570         struct ast_str *str = ast_str_create(16);
571         int ret = -1;
572         AST_DECLARE_APP_ARGS(args,
573                 AST_APP_ARG(url);
574                 AST_APP_ARG(postdata);
575         );
576         CURL **curl;
577         struct curl_settings *cur;
578         struct ast_datastore *store = NULL;
579         int hashcompat = 0;
580         AST_LIST_HEAD(global_curl_info, curl_settings) *list = NULL;
581
582         if (buf) {
583                 *buf = '\0';
584         }
585
586         if (!str) {
587                 return -1;
588         }
589
590         if (ast_strlen_zero(info)) {
591                 ast_log(LOG_WARNING, "CURL requires an argument (URL)\n");
592                 ast_free(str);
593                 return -1;
594         }
595
596         AST_STANDARD_APP_ARGS(args, info);
597
598         if (chan) {
599                 ast_autoservice_start(chan);
600         }
601
602         if (!(curl = ast_threadstorage_get(&curl_instance, sizeof(*curl)))) {
603                 ast_log(LOG_ERROR, "Cannot allocate curl structure\n");
604                 return -1;
605         }
606
607         AST_LIST_LOCK(&global_curl_info);
608         AST_LIST_TRAVERSE(&global_curl_info, cur, list) {
609                 if (cur->key == CURLOPT_SPECIAL_HASHCOMPAT) {
610                         hashcompat = (long) cur->value;
611                 } else {
612                         curl_easy_setopt(*curl, cur->key, cur->value);
613                 }
614         }
615
616         if (chan && (store = ast_channel_datastore_find(chan, &curl_info, NULL))) {
617                 list = store->data;
618                 AST_LIST_LOCK(list);
619                 AST_LIST_TRAVERSE(list, cur, list) {
620                         if (cur->key == CURLOPT_SPECIAL_HASHCOMPAT) {
621                                 hashcompat = (long) cur->value;
622                         } else {
623                                 curl_easy_setopt(*curl, cur->key, cur->value);
624                         }
625                 }
626         }
627
628         curl_easy_setopt(*curl, CURLOPT_URL, args.url);
629         curl_easy_setopt(*curl, CURLOPT_FILE, (void *) &str);
630
631         if (args.postdata) {
632                 curl_easy_setopt(*curl, CURLOPT_POST, 1);
633                 curl_easy_setopt(*curl, CURLOPT_POSTFIELDS, args.postdata);
634         }
635
636         curl_easy_perform(*curl);
637
638         if (store) {
639                 AST_LIST_UNLOCK(list);
640         }
641         AST_LIST_UNLOCK(&global_curl_info);
642
643         if (args.postdata) {
644                 curl_easy_setopt(*curl, CURLOPT_POST, 0);
645         }
646
647         if (ast_str_strlen(str)) {
648                 ast_str_trim_blanks(str);
649
650                 ast_debug(3, "str='%s'\n", ast_str_buffer(str));
651                 if (hashcompat) {
652                         char *remainder = ast_str_buffer(str);
653                         char *piece;
654                         struct ast_str *fields = ast_str_create(ast_str_strlen(str) / 2);
655                         struct ast_str *values = ast_str_create(ast_str_strlen(str) / 2);
656                         int rowcount = 0;
657                         while (fields && values && (piece = strsep(&remainder, "&"))) {
658                                 char *name = strsep(&piece, "=");
659                                 /* Do this before the decode, because if something has encoded
660                                  * a literal plus-sign, we don't want to translate that to a
661                                  * space. */
662                                 if (hashcompat == HASHCOMPAT_LEGACY) {
663                                         if (piece) {
664                                                 ast_uri_decode(piece, ast_uri_http_legacy);
665                                         }
666                                         ast_uri_decode(name, ast_uri_http_legacy);
667                                 } else {
668                                         if (piece) {
669                                                 ast_uri_decode(piece, ast_uri_http);
670                                         }
671                                         ast_uri_decode(name, ast_uri_http);
672                                 }
673                                 ast_str_append(&fields, 0, "%s%s", rowcount ? "," : "", name);
674                                 ast_str_append(&values, 0, "%s%s", rowcount ? "," : "", S_OR(piece, ""));
675                                 rowcount++;
676                         }
677                         pbx_builtin_setvar_helper(chan, "~ODBCFIELDS~", ast_str_buffer(fields));
678                         if (buf) {
679                                 ast_copy_string(buf, ast_str_buffer(values), len);
680                         } else {
681                                 ast_str_set(input_str, len, "%s", ast_str_buffer(values));
682                         }
683                         ast_free(fields);
684                         ast_free(values);
685                 } else {
686                         if (buf) {
687                                 ast_copy_string(buf, ast_str_buffer(str), len);
688                         } else {
689                                 ast_str_set(input_str, len, "%s", ast_str_buffer(str));
690                         }
691                 }
692                 ret = 0;
693         }
694         ast_free(str);
695
696         if (chan)
697                 ast_autoservice_stop(chan);
698
699         return ret;
700 }
701
702 static int acf_curl_exec(struct ast_channel *chan, const char *cmd, char *info, char *buf, size_t len)
703 {
704         return acf_curl_helper(chan, cmd, info, buf, NULL, len);
705 }
706
707 static int acf_curl2_exec(struct ast_channel *chan, const char *cmd, char *info, struct ast_str **buf, ssize_t len)
708 {
709         return acf_curl_helper(chan, cmd, info, NULL, buf, len);
710 }
711
712 static struct ast_custom_function acf_curl = {
713         .name = "CURL",
714         .synopsis = "Retrieves the contents of a URL",
715         .syntax = "CURL(url[,post-data])",
716         .desc =
717         "  url       - URL to retrieve\n"
718         "  post-data - Optional data to send as a POST (GET is default action)\n",
719         .read = acf_curl_exec,
720         .read2 = acf_curl2_exec,
721 };
722
723 static struct ast_custom_function acf_curlopt = {
724         .name = "CURLOPT",
725         .synopsis = "Set options for use with the CURL() function",
726         .syntax = "CURLOPT(<option>)",
727         .desc =
728 "  cookie         - Send cookie with request [none]\n"
729 "  conntimeout    - Number of seconds to wait for connection\n"
730 "  dnstimeout     - Number of seconds to wait for DNS response\n"
731 "  ftptext        - For FTP, force a text transfer (boolean)\n"
732 "  ftptimeout     - For FTP, the server response timeout\n"
733 "  header         - Retrieve header information (boolean)\n"
734 "  httptimeout    - Number of seconds to wait for HTTP response\n"
735 "  maxredirs      - Maximum number of redirects to follow\n"
736 "  proxy          - Hostname or IP to use as a proxy\n"
737 "  proxytype      - http, socks4, or socks5\n"
738 "  proxyport      - port number of the proxy\n"
739 "  proxyuserpwd   - A <user>:<pass> to use for authentication\n"
740 "  referer        - Referer URL to use for the request\n"
741 "  useragent      - UserAgent string to use\n"
742 "  userpwd        - A <user>:<pass> to use for authentication\n"
743 "  ssl_verifypeer - Whether to verify the peer certificate (boolean)\n"
744 "  hashcompat     - Result data will be compatible for use with HASH()\n"
745 "                 - if value is \"legacy\", will translate '+' to ' '\n"
746 "",
747         .read = acf_curlopt_read,
748         .read2 = acf_curlopt_read2,
749         .write = acf_curlopt_write,
750 };
751
752 static int unload_module(void)
753 {
754         int res;
755
756         res = ast_custom_function_unregister(&acf_curl);
757         res |= ast_custom_function_unregister(&acf_curlopt);
758
759         return res;
760 }
761
762 static int load_module(void)
763 {
764         int res;
765
766         if (!ast_module_check("res_curl.so")) {
767                 if (ast_load_resource("res_curl.so") != AST_MODULE_LOAD_SUCCESS) {
768                         ast_log(LOG_ERROR, "Cannot load res_curl, so func_curl cannot be loaded\n");
769                         return AST_MODULE_LOAD_DECLINE;
770                 }
771         }
772
773         res = ast_custom_function_register(&acf_curl);
774         res |= ast_custom_function_register(&acf_curlopt);
775
776         return res;
777 }
778
779 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Load external URL",
780                 .load = load_module,
781                 .unload = unload_module,
782                 .load_pri = AST_MODPRI_REALTIME_DEPEND2,
783         );
784