/* * CallWeaver -- An open source telephony toolkit. * * Copyright (C) 2009, Eris Associates Limited, UK * * Mike Jagdis * * See http://www.callweaver.org for more information about * the CallWeaver project. Please do not directly contact * any of the maintainers of this project for assistance; * the project provides a web site, mailing lists and IRC * channels for your use. * * This program is free software, distributed under the terms of * the GNU General Public License Version 2. See the LICENSE file * at the top of the source tree. * * Based loosely on app_curl.c code from Asterisk which was: * * Copyright (C) 2004 - 2006, Tilghman Lesher * * Tilghman Lesher * and Brian Wilkins (Added POST option) * * app_curl.c is distributed with no restrictions on usage or * redistribution. * * See http://www.asterisk.org for more information about * the Asterisk project. Please do not directly contact * any of the maintainers of this project for assistance; * the project provides a web site, mailing lists and IRC * channels for your use. * */ /*! \file * * \brief CURL - Fetch a URL * * \author Mike Jagdis * * \extref Depends on the CURL library - http://curl.haxx.se/ * * \ingroup functions */ #include "callweaver.h" CALLWEAVER_FILE_VERSION("$HeadURL$", "$Revision$") #include #include "callweaver/module.h" #include "callweaver/channel.h" #include "callweaver/pbx.h" #include "callweaver/logger.h" #include "callweaver/utils.h" #include "callweaver/app.h" static void *curl_function; static const char curl_func_name[] = "CURL"; static const char curl_func_synopsis[] = "Retrieves the contents of a URL"; static const char curl_func_syntax[] = "CURL(url[, post-data])"; static const char curl_func_desc[] = "Fetches the given url, optionally passing post-data as part of the request.\n" "If there is post-data the request will be a POST, otherwise a GET\n"; struct curl_rw_args { char *buf; size_t len; }; static size_t write_data(void *ptr, size_t size, size_t nmemb, void *data) { struct curl_rw_args *write_args = data; int len; len = (size * nmemb < write_args->len ? size * nmemb : write_args->len); if (write_args->buf) { memcpy(write_args->buf, ptr, len); write_args->buf[len] = '\0'; write_args->buf += len; write_args->len -= len; } return len; } static char *curl_read(struct cw_channel *chan, int argc, char **argv, char *buf, size_t len) { CURL *curl; if (argc < 1 || argc > 2) { cw_log(LOG_ERROR, "Syntax: %s\n", curl_func_syntax); return NULL; } if (chan) cw_autoservice_start(chan); if ((curl = curl_easy_init())) { struct curl_rw_args write_args = { .buf = buf, .len = len, }; curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1); #ifdef CURLOPT_NOPROGRESS curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1); #endif curl_easy_setopt(curl, CURLOPT_TIMEOUT, 180); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data); curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&write_args); curl_easy_setopt(curl, CURLOPT_USERAGENT, "Callweaver-libcurl/1.0"); curl_easy_setopt(curl, CURLOPT_AUTOREFERER, 1); curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1); curl_easy_setopt(curl, CURLOPT_MAXREDIRS, 5); if (!strncasecmp(argv[0], "https", 5)) { curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0); curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0); } curl_easy_setopt(curl, CURLOPT_URL, argv[0]); if (argc == 2) { curl_easy_setopt(curl, CURLOPT_POST, 1); curl_easy_setopt(curl, CURLOPT_POSTFIELDS, argv[1]); } curl_easy_perform(curl); if (argc == 2) { curl_easy_setopt(curl, CURLOPT_POST, 0); curl_easy_setopt(curl, CURLOPT_HTTPGET, 1); } curl_easy_cleanup(curl); } else buf = NULL; if (chan) cw_autoservice_stop(chan); return buf; } static const char tdesc[] = "HTTP data retrieval function"; int unload_module(void) { return cw_unregister_function(curl_function); } int load_module(void) { curl_function = cw_register_function(curl_func_name, curl_read, NULL, curl_func_synopsis, curl_func_syntax, curl_func_desc); return 0; } char *description(void) { return tdesc; } int usecount(void) { return 0; }