572622bae1bf4fd04fb807f5c6d943c5f0d3af4c
[asterisk/asterisk.git] / apps / app_curl.c
1 /*
2  * Asterisk -- A telephony toolkit for Linux.
3  *
4  * Curl - App to load a URL
5  * 
6  * Copyright (C)  2004 - 2005, Tilghman Lesher
7  *
8  * Tilghman Lesher <curl-20041222@the-tilghman.com>
9  * and Brian Wilkins <bwilkins@cfl.rr.com> (Added POST option)
10  *
11  * app_curl.c is distributed with no restrictions on usage or
12  * redistribution.
13  */
14  
15 #include <stdlib.h>
16 #include <string.h>
17 #include <curl/curl.h>
18
19 #include "asterisk.h"
20
21 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
22
23 #include "asterisk/lock.h"
24 #include "asterisk/file.h"
25 #include "asterisk/logger.h"
26 #include "asterisk/channel.h"
27 #include "asterisk/pbx.h"
28 #include "asterisk/cli.h"
29 #include "asterisk/options.h"
30 #include "asterisk/module.h"
31
32 static char *tdesc = "Load external URL";
33
34 static char *app = "Curl";
35
36 static char *synopsis = "Load an external URL";
37
38 static char *descrip = 
39 "  Curl(URL[|postdata]): Requests the URL.  Mainly used for signalling\n"
40 "external applications of an event.  Returns 0 or -1 on fatal error.\n"
41 "Argument specified treated as POST data.  Also sets CURL variable with the\n"
42 "resulting page.\n";
43
44 STANDARD_LOCAL_USER;
45
46 LOCAL_USER_DECL;
47
48 extern int errno;
49
50 struct MemoryStruct {
51         char *memory;
52         size_t size;
53 };
54
55 static void *myrealloc(void *ptr, size_t size)
56 {
57         /* There might be a realloc() out there that doesn't like reallocing
58            NULL pointers, so we take care of it here */
59         if (ptr)
60                 return realloc(ptr, size);
61         else
62                 return malloc(size);
63 }
64
65 static size_t WriteMemoryCallback(void *ptr, size_t size, size_t nmemb, void *data)
66 {
67         register int realsize = size * nmemb;
68         struct MemoryStruct *mem = (struct MemoryStruct *)data;
69
70         mem->memory = (char *)myrealloc(mem->memory, mem->size + realsize + 1);
71         if (mem->memory) {
72                 memcpy(&(mem->memory[mem->size]), ptr, realsize);
73                 mem->size += realsize;
74                 mem->memory[mem->size] = 0;
75         }
76         return realsize;
77 }
78
79 static int curl_exec(struct ast_channel *chan, void *data)
80 {
81         int res = 0;
82         struct localuser *u;
83         CURL *curl;
84         char *info, *post_data=NULL, *url;
85
86         if (!data || !strlen((char *)data)) {
87                 ast_log(LOG_WARNING, "Curl requires an argument (URL)\n");
88                 return -1;
89         }
90
91         if ((info = ast_strdupa((char *)data))) {
92                 url = strsep(&info, "|");
93                 post_data = info;
94         } else {
95                 ast_log(LOG_ERROR, "Out of memory\n");
96                 return -1;
97         }
98
99         LOCAL_USER_ADD(u);
100
101         curl_global_init(CURL_GLOBAL_ALL);
102         curl = curl_easy_init();
103
104         if (curl) {
105                 struct MemoryStruct chunk;
106
107                 chunk.memory=NULL; /* we expect realloc(NULL, size) to work */
108                 chunk.size = 0;    /* no data at this point */
109
110                 curl_easy_setopt(curl, CURLOPT_URL, url);
111                 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
112                 curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&chunk);
113                 curl_easy_setopt(curl, CURLOPT_USERAGENT, "asterisk-libcurl-agent/1.0");
114
115                 if (post_data) {
116                         curl_easy_setopt(curl, CURLOPT_POST, 1);
117                         curl_easy_setopt(curl, CURLOPT_POSTFIELDS, post_data);
118                 }
119
120                 curl_easy_perform(curl);
121                 curl_easy_cleanup(curl);
122
123                 if (chunk.memory) {
124                         chunk.memory[chunk.size] = '\0';
125                         if (chunk.memory[chunk.size - 1] == 10)
126                                 chunk.memory[chunk.size - 1] = '\0';
127
128                         pbx_builtin_setvar_helper(chan, "CURL", chunk.memory);
129
130                         free(chunk.memory);
131                 }
132         } else {
133                 ast_log(LOG_ERROR, "Cannot allocate curl structure\n");
134                 res = -1;
135         }
136
137         LOCAL_USER_REMOVE(u);
138         return res;
139 }
140
141 int unload_module(void)
142 {
143         STANDARD_HANGUP_LOCALUSERS;
144         return ast_unregister_application(app);
145 }
146
147 int load_module(void)
148 {
149         return ast_register_application(app, curl_exec, synopsis, descrip);
150 }
151
152 char *description(void)
153 {
154         return tdesc;
155 }
156
157 int usecount(void)
158 {
159         int res;
160         STANDARD_USECOUNT(res);
161         return res;
162 }
163
164 char *key()
165 {
166         return ASTERISK_GPL_KEY;
167 }