2008-01-14 21:31:17 +00:00
|
|
|
/*
|
|
|
|
* FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
|
2011-01-05 10:08:55 -06:00
|
|
|
* Copyright (C) 2005-2011, Anthony Minessale II <anthm@freeswitch.org>
|
2008-01-14 21:31:17 +00:00
|
|
|
*
|
|
|
|
* Version: MPL 1.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
|
|
|
*
|
|
|
|
* The Original Code is FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is
|
2009-02-04 21:20:54 +00:00
|
|
|
* Anthony Minessale II <anthm@freeswitch.org>
|
2008-01-14 21:31:17 +00:00
|
|
|
* Portions created by the Initial Developer are Copyright (C)
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
*
|
2009-02-04 21:20:54 +00:00
|
|
|
* Anthony Minessale II <anthm@freeswitch.org>
|
2008-01-14 21:31:17 +00:00
|
|
|
*
|
|
|
|
*
|
|
|
|
* mod_spidermonkey_teletone.c -- TeleTone Javascript Module
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
#include "mod_spidermonkey.h"
|
|
|
|
#include <curl/curl.h>
|
|
|
|
|
|
|
|
static const char modname[] = "CURL";
|
|
|
|
|
|
|
|
struct curl_obj {
|
|
|
|
CURL *curl_handle;
|
|
|
|
JSContext *cx;
|
|
|
|
JSObject *obj;
|
|
|
|
JSFunction *function;
|
2008-01-14 22:19:48 +00:00
|
|
|
JSObject *user_data;
|
2008-01-14 21:31:17 +00:00
|
|
|
jsrefcount saveDepth;
|
2008-05-27 04:54:52 +00:00
|
|
|
jsval ret;
|
2008-01-14 21:31:17 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
static size_t file_callback(void *ptr, size_t size, size_t nmemb, void *data)
|
|
|
|
{
|
|
|
|
register unsigned int realsize = (unsigned int) (size * nmemb);
|
|
|
|
struct curl_obj *co = data;
|
|
|
|
uintN argc = 0;
|
|
|
|
jsval argv[4];
|
2008-05-27 04:54:52 +00:00
|
|
|
|
2008-01-14 21:31:17 +00:00
|
|
|
|
|
|
|
if (!co) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (co->function) {
|
|
|
|
char *ret;
|
2008-05-27 04:54:52 +00:00
|
|
|
argv[argc++] = STRING_TO_JSVAL(JS_NewStringCopyZ(co->cx, (char *) ptr));
|
2008-01-14 22:19:48 +00:00
|
|
|
if (co->user_data) {
|
|
|
|
argv[argc++] = OBJECT_TO_JSVAL(co->user_data);
|
|
|
|
}
|
2008-01-14 21:31:17 +00:00
|
|
|
JS_ResumeRequest(co->cx, co->saveDepth);
|
|
|
|
JS_CallFunction(co->cx, co->obj, co->function, argc, argv, &co->ret);
|
|
|
|
co->saveDepth = JS_SuspendRequest(co->cx);
|
2008-05-27 04:54:52 +00:00
|
|
|
|
2008-01-14 21:31:17 +00:00
|
|
|
if ((ret = JS_GetStringBytes(JS_ValueToString(co->cx, co->ret)))) {
|
|
|
|
if (!strcmp(ret, "true") || !strcmp(ret, "undefined")) {
|
|
|
|
return realsize;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return realsize;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Curl Object */
|
|
|
|
/*********************************************************************************/
|
2008-05-27 04:54:52 +00:00
|
|
|
static JSBool curl_construct(JSContext * cx, JSObject * obj, uintN argc, jsval * argv, jsval * rval)
|
2008-01-14 21:31:17 +00:00
|
|
|
{
|
|
|
|
struct curl_obj *co = NULL;
|
|
|
|
|
|
|
|
co = malloc(sizeof(*co));
|
|
|
|
switch_assert(co);
|
|
|
|
|
|
|
|
memset(co, 0, sizeof(*co));
|
|
|
|
|
|
|
|
co->cx = cx;
|
|
|
|
co->obj = obj;
|
|
|
|
|
|
|
|
JS_SetPrivate(cx, obj, co);
|
|
|
|
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
|
2008-05-27 04:54:52 +00:00
|
|
|
static void curl_destroy(JSContext * cx, JSObject * obj)
|
2008-01-14 21:31:17 +00:00
|
|
|
{
|
|
|
|
struct curl_obj *co = JS_GetPrivate(cx, obj);
|
|
|
|
switch_safe_free(co);
|
|
|
|
JS_SetPrivate(cx, obj, NULL);
|
|
|
|
}
|
|
|
|
|
2008-05-27 04:54:52 +00:00
|
|
|
static JSBool curl_run(JSContext * cx, JSObject * obj, uintN argc, jsval * argv, jsval * rval)
|
2008-01-14 21:31:17 +00:00
|
|
|
{
|
|
|
|
struct curl_obj *co = JS_GetPrivate(cx, obj);
|
|
|
|
char *method = NULL, *url, *cred = NULL;
|
|
|
|
char *url_p = NULL, *data = NULL, *durl = NULL;
|
|
|
|
long httpRes = 0;
|
|
|
|
struct curl_slist *headers = NULL;
|
2008-07-17 16:27:25 +00:00
|
|
|
int32 timeout = 0;
|
2008-08-26 18:38:02 +00:00
|
|
|
char ct[80] = "Content-Type: application/x-www-form-urlencoded";
|
2008-01-14 21:31:17 +00:00
|
|
|
|
|
|
|
if (argc < 2 || !co) {
|
|
|
|
return JS_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
method = JS_GetStringBytes(JS_ValueToString(cx, argv[0]));
|
|
|
|
url = JS_GetStringBytes(JS_ValueToString(cx, argv[1]));
|
|
|
|
|
|
|
|
co->curl_handle = curl_easy_init();
|
|
|
|
if (!strncasecmp(url, "https", 5)) {
|
|
|
|
curl_easy_setopt(co->curl_handle, CURLOPT_SSL_VERIFYPEER, 0);
|
|
|
|
curl_easy_setopt(co->curl_handle, CURLOPT_SSL_VERIFYHOST, 0);
|
|
|
|
}
|
2010-02-06 03:38:24 +00:00
|
|
|
|
2008-05-27 04:54:52 +00:00
|
|
|
|
2008-01-14 21:31:17 +00:00
|
|
|
if (argc > 2) {
|
|
|
|
data = JS_GetStringBytes(JS_ValueToString(cx, argv[2]));
|
|
|
|
}
|
2008-05-27 04:54:52 +00:00
|
|
|
|
2008-01-14 21:31:17 +00:00
|
|
|
if (argc > 3) {
|
|
|
|
co->function = JS_ValueToFunction(cx, argv[3]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (argc > 4) {
|
2008-01-14 22:19:48 +00:00
|
|
|
JS_ValueToObject(cx, argv[4], &co->user_data);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (argc > 5) {
|
|
|
|
cred = JS_GetStringBytes(JS_ValueToString(cx, argv[5]));
|
2009-10-23 16:03:42 +00:00
|
|
|
if (!zstr(cred)) {
|
2008-01-14 21:31:17 +00:00
|
|
|
curl_easy_setopt(co->curl_handle, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
|
|
|
|
curl_easy_setopt(co->curl_handle, CURLOPT_USERPWD, cred);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-07-17 16:27:25 +00:00
|
|
|
if (argc > 6) {
|
|
|
|
JS_ValueToInt32(cx, argv[6], &timeout);
|
|
|
|
if (timeout > 0) {
|
|
|
|
curl_easy_setopt(co->curl_handle, CURLOPT_TIMEOUT, timeout);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-08-26 18:39:17 +00:00
|
|
|
if (argc > 7) {
|
|
|
|
char *content_type = JS_GetStringBytes(JS_ValueToString(cx, argv[7]));
|
2008-08-26 18:39:59 +00:00
|
|
|
switch_snprintf(ct, sizeof(ct), "Content-Type: %s", content_type);
|
2008-08-26 18:39:17 +00:00
|
|
|
}
|
2008-07-17 16:27:25 +00:00
|
|
|
|
2008-08-26 18:39:17 +00:00
|
|
|
headers = curl_slist_append(headers, ct);
|
2008-05-27 04:54:52 +00:00
|
|
|
|
2008-01-14 21:31:17 +00:00
|
|
|
curl_easy_setopt(co->curl_handle, CURLOPT_HTTPHEADER, headers);
|
|
|
|
|
|
|
|
url_p = url;
|
|
|
|
|
|
|
|
if (!strcasecmp(method, "post")) {
|
|
|
|
curl_easy_setopt(co->curl_handle, CURLOPT_POST, 1);
|
|
|
|
if (!data) {
|
|
|
|
data = "";
|
|
|
|
}
|
|
|
|
curl_easy_setopt(co->curl_handle, CURLOPT_POSTFIELDS, data);
|
2009-10-23 16:03:42 +00:00
|
|
|
} else if (!zstr(data)) {
|
2008-01-14 21:31:17 +00:00
|
|
|
durl = switch_mprintf("%s?%s", url, data);
|
|
|
|
url_p = durl;
|
|
|
|
}
|
|
|
|
|
2008-05-27 04:54:52 +00:00
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Running: method: [%s] url: [%s] data: [%s] cred=[%s] cb: [%s]\n",
|
2008-01-14 21:31:17 +00:00
|
|
|
method, url_p, data, switch_str_nil(cred), co->function ? "yes" : "no");
|
|
|
|
|
|
|
|
curl_easy_setopt(co->curl_handle, CURLOPT_URL, url_p);
|
|
|
|
curl_easy_setopt(co->curl_handle, CURLOPT_WRITEFUNCTION, file_callback);
|
|
|
|
curl_easy_setopt(co->curl_handle, CURLOPT_WRITEDATA, (void *) co);
|
|
|
|
|
|
|
|
curl_easy_setopt(co->curl_handle, CURLOPT_USERAGENT, "freeswitch-spidermonkey-curl/1.0");
|
2008-05-27 04:54:52 +00:00
|
|
|
|
2008-01-14 21:31:17 +00:00
|
|
|
co->saveDepth = JS_SuspendRequest(cx);
|
|
|
|
curl_easy_perform(co->curl_handle);
|
|
|
|
|
|
|
|
curl_easy_getinfo(co->curl_handle, CURLINFO_RESPONSE_CODE, &httpRes);
|
|
|
|
curl_easy_cleanup(co->curl_handle);
|
|
|
|
curl_slist_free_all(headers);
|
|
|
|
co->curl_handle = NULL;
|
|
|
|
co->function = NULL;
|
|
|
|
JS_ResumeRequest(cx, co->saveDepth);
|
|
|
|
switch_safe_free(durl);
|
2008-05-27 04:54:52 +00:00
|
|
|
|
2008-01-14 21:31:17 +00:00
|
|
|
|
2008-02-13 10:55:54 +00:00
|
|
|
return JS_TRUE;
|
2008-01-14 21:31:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static JSFunctionSpec curl_methods[] = {
|
|
|
|
{"run", curl_run, 2},
|
|
|
|
{0}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
static JSPropertySpec curl_props[] = {
|
|
|
|
{0}
|
|
|
|
};
|
|
|
|
|
2008-05-27 04:54:52 +00:00
|
|
|
static JSBool curl_getProperty(JSContext * cx, JSObject * obj, jsval id, jsval * vp)
|
2008-01-14 21:31:17 +00:00
|
|
|
{
|
|
|
|
JSBool res = JS_TRUE;
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSClass curl_class = {
|
|
|
|
modname, JSCLASS_HAS_PRIVATE,
|
|
|
|
JS_PropertyStub, JS_PropertyStub, curl_getProperty, DEFAULT_SET_PROPERTY,
|
|
|
|
JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, curl_destroy, NULL, NULL, NULL,
|
|
|
|
curl_construct
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2008-05-27 04:54:52 +00:00
|
|
|
switch_status_t curl_load(JSContext * cx, JSObject * obj)
|
2008-01-14 21:31:17 +00:00
|
|
|
{
|
|
|
|
JS_InitClass(cx, obj, NULL, &curl_class, curl_construct, 3, curl_props, curl_methods, curl_props, curl_methods);
|
|
|
|
return SWITCH_STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const sm_module_interface_t curl_module_interface = {
|
|
|
|
/*.name = */ modname,
|
|
|
|
/*.spidermonkey_load */ curl_load,
|
|
|
|
/*.next */ NULL
|
|
|
|
};
|
|
|
|
|
2010-02-06 03:38:24 +00:00
|
|
|
SWITCH_MOD_DECLARE_NONSTD(switch_status_t) spidermonkey_init(const sm_module_interface_t ** module_interface)
|
2008-01-14 21:31:17 +00:00
|
|
|
{
|
|
|
|
*module_interface = &curl_module_interface;
|
|
|
|
return SWITCH_STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* For Emacs:
|
|
|
|
* Local Variables:
|
|
|
|
* mode:c
|
2008-02-03 22:14:57 +00:00
|
|
|
* indent-tabs-mode:t
|
2008-01-14 21:31:17 +00:00
|
|
|
* tab-width:4
|
|
|
|
* c-basic-offset:4
|
|
|
|
* End:
|
|
|
|
* For VIM:
|
2008-07-03 19:12:26 +00:00
|
|
|
* vim:set softtabstop=4 shiftwidth=4 tabstop=4:
|
2008-01-14 21:31:17 +00:00
|
|
|
*/
|