Skip to content
Snippets Groups Projects
func_curl.c 4.55 KiB
Newer Older
  • Learn to ignore specific revisions
  •  * Asterisk -- An open source telephony toolkit.
    
     * Copyright (C)  2004 - 2006, Tilghman Lesher
    
    Mark Spencer's avatar
    Mark Spencer committed
     * Tilghman Lesher <curl-20050919@the-tilghman.com>
    
     * and Brian Wilkins <bwilkins@cfl.rr.com> (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.
     *
     */
    
    
     * \brief Curl - Load a URL
    
     *
     * \author Tilghman Lesher <curl-20050919@the-tilghman.com>
     *
     * \note Brian Wilkins <bwilkins@cfl.rr.com> (Added POST option) 
    
     *
     * \extref Depends on the CURL library  - http://curl.haxx.se/
    
     * \ingroup functions
    
    #include "asterisk.h"
    
    ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
    
    
    Kevin P. Fleming's avatar
    Kevin P. Fleming committed
    #include <stdio.h>
    
    #include <stdlib.h>
    #include <string.h>
    #include <curl/curl.h>
    
    
    #include "asterisk/lock.h"
    #include "asterisk/file.h"
    #include "asterisk/logger.h"
    #include "asterisk/channel.h"
    #include "asterisk/pbx.h"
    #include "asterisk/cli.h"
    #include "asterisk/options.h"
    #include "asterisk/module.h"
    
    #include "asterisk/app.h"
    
    #include "asterisk/utils.h"
    
    #include "asterisk/threadstorage.h"
    
    
    struct MemoryStruct {
    	char *memory;
    	size_t size;
    };
    
    
    Joshua Colp's avatar
    Joshua Colp committed
    /* There might be a realloc() out there that doesn't like reallocing
     * NULL pointers, so we take care of it here
     */
    
    static void *myrealloc(void *ptr, size_t size)
    {
    
    Joshua Colp's avatar
    Joshua Colp committed
    	return (ptr ? ast_realloc(ptr, size) : ast_malloc(size));
    
    }
    
    static size_t WriteMemoryCallback(void *ptr, size_t size, size_t nmemb, void *data)
    {
    	register int realsize = size * nmemb;
    	struct MemoryStruct *mem = (struct MemoryStruct *)data;
    
    
    Joshua Colp's avatar
    Joshua Colp committed
    	if ((mem->memory = (char *)myrealloc(mem->memory, mem->size + realsize + 1))) {
    
    		memcpy(&(mem->memory[mem->size]), ptr, realsize);
    		mem->size += realsize;
    		mem->memory[mem->size] = 0;
    	}
    
    	return realsize;
    }
    
    
    static const char *global_useragent = "asterisk-libcurl-agent/1.0";
    
    
    static int curl_instance_init(void *data)
    {
    	CURL **curl = data;
    
    	if (!(*curl = curl_easy_init()))
    		return -1;
    
    	curl_easy_setopt(*curl, CURLOPT_NOSIGNAL, 1);
    	curl_easy_setopt(*curl, CURLOPT_TIMEOUT, 180);
    	curl_easy_setopt(*curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
    	curl_easy_setopt(*curl, CURLOPT_USERAGENT, global_useragent);
    
    	return 0;
    }
    
    
    static void curl_instance_cleanup(void *data)
    
    Mark Spencer's avatar
    Mark Spencer committed
    {
    
    	CURL **curl = data;
    
    Mark Spencer's avatar
    Mark Spencer committed
    
    
    	curl_easy_cleanup(*curl);
    }
    
    AST_THREADSTORAGE_CUSTOM(curl_instance, curl_instance_init, curl_instance_cleanup);
    
    Mark Spencer's avatar
    Mark Spencer committed
    
    
    static int curl_internal(struct MemoryStruct *chunk, char *url, char *post)
    {
    	CURL **curl;
    
    	if (!(curl = ast_threadstorage_get(&curl_instance, sizeof(*curl))))
    
    Mark Spencer's avatar
    Mark Spencer committed
    		return -1;
    
    
    	curl_easy_setopt(*curl, CURLOPT_URL, url);
    	curl_easy_setopt(*curl, CURLOPT_WRITEDATA, (void *) chunk);
    
    Mark Spencer's avatar
    Mark Spencer committed
    
    	if (post) {
    
    		curl_easy_setopt(*curl, CURLOPT_POST, 1);
    		curl_easy_setopt(*curl, CURLOPT_POSTFIELDS, post);
    
    	curl_easy_perform(*curl);
    
    	if (post)
    		curl_easy_setopt(*curl, CURLOPT_POST, 0);
    
    
    Mark Spencer's avatar
    Mark Spencer committed
    	return 0;
    }
    
    
    static int acf_curl_exec(struct ast_channel *chan, const char *cmd, char *info, char *buf, size_t len)
    
    Mark Spencer's avatar
    Mark Spencer committed
    {
    	struct MemoryStruct chunk = { NULL, 0 };
    
    	AST_DECLARE_APP_ARGS(args,
    		AST_APP_ARG(url);
    		AST_APP_ARG(postdata);
    	);
    
    Mark Spencer's avatar
    Mark Spencer committed
    
    
    	if (ast_strlen_zero(info)) {
    
    Mark Spencer's avatar
    Mark Spencer committed
    		ast_log(LOG_WARNING, "CURL requires an argument (URL)\n");
    
    		return -1;
    
    Mark Spencer's avatar
    Mark Spencer committed
    	}
    
    	AST_STANDARD_APP_ARGS(args, info);	
    
    	if (!curl_internal(&chunk, args.url, args.postdata)) {
    
    		if (chunk.memory) {
    			chunk.memory[chunk.size] = '\0';
    			if (chunk.memory[chunk.size - 1] == 10)
    				chunk.memory[chunk.size - 1] = '\0';
    
    Mark Spencer's avatar
    Mark Spencer committed
    			ast_copy_string(buf, chunk.memory, len);
    
    	} else {
    		ast_log(LOG_ERROR, "Cannot allocate curl structure\n");
    	}
    
    
    	return 0;
    
    Mark Spencer's avatar
    Mark Spencer committed
    struct ast_custom_function acf_curl = {
    	.name = "CURL",
    	.synopsis = "Retrieves the contents of a URL",
    
    Mark Spencer's avatar
    Mark Spencer committed
    	.desc =
    
    	"  url       - URL to retrieve\n"
    	"  post-data - Optional data to send as a POST (GET is default action)\n",
    
    Mark Spencer's avatar
    Mark Spencer committed
    	.read = acf_curl_exec,
    };
    
    
    static int unload_module(void)
    
    	int res;
    
    	res = ast_custom_function_unregister(&acf_curl);
    
    
    static int load_module(void)
    
    	if (curl_global_init(CURL_GLOBAL_ALL)) {
    		ast_log(LOG_ERROR, "Unable to initialize the CURL library. Cannot load func_curl\n");
    		return AST_MODULE_LOAD_DECLINE;
    	}	
    
    
    	res = ast_custom_function_register(&acf_curl);
    
    	return res;
    
    AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Load external URL");