eliminate compiler warning
[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 struct MemoryStruct {
49         char *memory;
50         size_t size;
51 };
52
53 static void *myrealloc(void *ptr, size_t size)
54 {
55         /* There might be a realloc() out there that doesn't like reallocing
56            NULL pointers, so we take care of it here */
57         if (ptr)
58                 return realloc(ptr, size);
59         else
60                 return malloc(size);
61 }
62
63 static size_t WriteMemoryCallback(void *ptr, size_t size, size_t nmemb, void *data)
64 {
65         register int realsize = size * nmemb;
66         struct MemoryStruct *mem = (struct MemoryStruct *)data;
67
68         mem->memory = (char *)myrealloc(mem->memory, mem->size + realsize + 1);
69         if (mem->memory) {
70                 memcpy(&(mem->memory[mem->size]), ptr, realsize);
71                 mem->size += realsize;
72                 mem->memory[mem->size] = 0;
73         }
74         return realsize;
75 }
76
77 static int curl_exec(struct ast_channel *chan, void *data)
78 {
79         int res = 0;
80         struct localuser *u;
81         CURL *curl;
82         char *info, *post_data=NULL, *url;
83
84         if (!data || !strlen((char *)data)) {
85                 ast_log(LOG_WARNING, "Curl requires an argument (URL)\n");
86                 return -1;
87         }
88
89         if ((info = ast_strdupa((char *)data))) {
90                 url = strsep(&info, "|");
91                 post_data = info;
92         } else {
93                 ast_log(LOG_ERROR, "Out of memory\n");
94                 return -1;
95         }
96
97         LOCAL_USER_ADD(u);
98
99         curl_global_init(CURL_GLOBAL_ALL);
100         curl = curl_easy_init();
101
102         if (curl) {
103                 struct MemoryStruct chunk;
104
105                 chunk.memory=NULL; /* we expect realloc(NULL, size) to work */
106                 chunk.size = 0;    /* no data at this point */
107
108                 curl_easy_setopt(curl, CURLOPT_URL, url);
109                 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
110                 curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&chunk);
111                 curl_easy_setopt(curl, CURLOPT_USERAGENT, "asterisk-libcurl-agent/1.0");
112
113                 if (post_data) {
114                         curl_easy_setopt(curl, CURLOPT_POST, 1);
115                         curl_easy_setopt(curl, CURLOPT_POSTFIELDS, post_data);
116                 }
117
118                 curl_easy_perform(curl);
119                 curl_easy_cleanup(curl);
120
121                 if (chunk.memory) {
122                         chunk.memory[chunk.size] = '\0';
123                         if (chunk.memory[chunk.size - 1] == 10)
124                                 chunk.memory[chunk.size - 1] = '\0';
125
126                         pbx_builtin_setvar_helper(chan, "CURL", chunk.memory);
127
128                         free(chunk.memory);
129                 }
130         } else {
131                 ast_log(LOG_ERROR, "Cannot allocate curl structure\n");
132                 res = -1;
133         }
134
135         LOCAL_USER_REMOVE(u);
136         return res;
137 }
138
139 int unload_module(void)
140 {
141         STANDARD_HANGUP_LOCALUSERS;
142         return ast_unregister_application(app);
143 }
144
145 int load_module(void)
146 {
147         return ast_register_application(app, curl_exec, synopsis, descrip);
148 }
149
150 char *description(void)
151 {
152         return tdesc;
153 }
154
155 int usecount(void)
156 {
157         int res;
158         STANDARD_USECOUNT(res);
159         return res;
160 }
161
162 char *key()
163 {
164         return ASTERISK_GPL_KEY;
165 }