summaryrefslogtreecommitdiff
path: root/funcs/func_curl.c
diff options
context:
space:
mode:
authorTilghman Lesher <tilghman@meg.abyt.es>2009-04-29 18:53:01 +0000
committerTilghman Lesher <tilghman@meg.abyt.es>2009-04-29 18:53:01 +0000
commita866a7590085a1f635ca92459c3917e5cded257a (patch)
tree972bdf8f96c18f1b9667469307af69385f4a75f3 /funcs/func_curl.c
parent0ea83eab4811c8519e4e04bf4c59b1744cf1efc9 (diff)
Merge str_substitution branch.
This branch adds additional methods to dialplan functions, whereby the result buffers are now dynamic buffers, which can be expanded to the size of any result. No longer are variable substitutions limited to 4095 bytes of data. In addition, the common case of needing buffers much smaller than that will enable substitution to only take up the amount of memory actually needed. The existing variable substitution routines are still available, but users of those API calls should transition to using the dynamic-buffer APIs. Reviewboard: http://reviewboard.digium.com/r/174/ git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@191140 65c4cc65-6c06-0410-ace0-fbb531ad65f3
Diffstat (limited to 'funcs/func_curl.c')
-rw-r--r--funcs/func_curl.c110
1 files changed, 91 insertions, 19 deletions
diff --git a/funcs/func_curl.c b/funcs/func_curl.c
index d4f34b850..b7918836d 100644
--- a/funcs/func_curl.c
+++ b/funcs/func_curl.c
@@ -276,7 +276,7 @@ yuck:
return 0;
}
-static int acf_curlopt_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
+static int acf_curlopt_helper(struct ast_channel *chan, const char *cmd, char *data, char *buf, struct ast_str **bufstr, ssize_t len)
{
struct ast_datastore *store;
struct global_curl_info *list[2] = { &global_curl_info, NULL };
@@ -303,38 +303,78 @@ static int acf_curlopt_read(struct ast_channel *chan, const char *cmd, char *dat
AST_LIST_TRAVERSE(list[i], cur, list) {
if (cur->key == key) {
if (ot == OT_BOOLEAN || ot == OT_INTEGER) {
- snprintf(buf, len, "%ld", (long)cur->value);
+ if (buf) {
+ snprintf(buf, len, "%ld", (long) cur->value);
+ } else {
+ ast_str_set(bufstr, len, "%ld", (long) cur->value);
+ }
} else if (ot == OT_INTEGER_MS) {
- if ((long)cur->value % 1000 == 0) {
- snprintf(buf, len, "%ld", (long)cur->value / 1000);
+ if ((long) cur->value % 1000 == 0) {
+ if (buf) {
+ snprintf(buf, len, "%ld", (long)cur->value / 1000);
+ } else {
+ ast_str_set(bufstr, len, "%ld", (long) cur->value / 1000);
+ }
} else {
- snprintf(buf, len, "%.3f", (double)((long)cur->value) / 1000.0);
+ if (buf) {
+ snprintf(buf, len, "%.3f", (double) ((long) cur->value) / 1000.0);
+ } else {
+ ast_str_set(bufstr, len, "%.3f", (double) ((long) cur->value) / 1000.0);
+ }
}
} else if (ot == OT_STRING) {
ast_debug(1, "Found entry %p, with key %d and value %p\n", cur, cur->key, cur->value);
- ast_copy_string(buf, cur->value, len);
+ if (buf) {
+ ast_copy_string(buf, cur->value, len);
+ } else {
+ ast_str_set(bufstr, 0, "%s", (char *) cur->value);
+ }
} else if (key == CURLOPT_PROXYTYPE) {
if (0) {
#if CURLVERSION_ATLEAST(7,15,2)
} else if ((long)cur->value == CURLPROXY_SOCKS4) {
- ast_copy_string(buf, "socks4", len);
+ if (buf) {
+ ast_copy_string(buf, "socks4", len);
+ } else {
+ ast_str_set(bufstr, 0, "socks4");
+ }
#endif
#if CURLVERSION_ATLEAST(7,18,0)
} else if ((long)cur->value == CURLPROXY_SOCKS4A) {
- ast_copy_string(buf, "socks4a", len);
+ if (buf) {
+ ast_copy_string(buf, "socks4a", len);
+ } else {
+ ast_str_set(bufstr, 0, "socks4a");
+ }
#endif
} else if ((long)cur->value == CURLPROXY_SOCKS5) {
- ast_copy_string(buf, "socks5", len);
+ if (buf) {
+ ast_copy_string(buf, "socks5", len);
+ } else {
+ ast_str_set(bufstr, 0, "socks5");
+ }
#if CURLVERSION_ATLEAST(7,18,0)
} else if ((long)cur->value == CURLPROXY_SOCKS5_HOSTNAME) {
- ast_copy_string(buf, "socks5hostname", len);
+ if (buf) {
+ ast_copy_string(buf, "socks5hostname", len);
+ } else {
+ ast_str_set(bufstr, 0, "socks5hostname");
+ }
#endif
#if CURLVERSION_ATLEAST(7,10,0)
} else if ((long)cur->value == CURLPROXY_HTTP) {
- ast_copy_string(buf, "http", len);
+ if (buf) {
+ ast_copy_string(buf, "http", len);
+ } else {
+ ast_str_set(bufstr, 0, "http");
+ }
#endif
} else {
- ast_copy_string(buf, "unknown", len);
+ if (buf) {
+ ast_copy_string(buf, "unknown", len);
+ } else {
+ ast_str_set(bufstr, 0, "unknown");
+ }
}
}
break;
@@ -349,6 +389,16 @@ static int acf_curlopt_read(struct ast_channel *chan, const char *cmd, char *dat
return cur ? 0 : -1;
}
+static int acf_curlopt_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
+{
+ return acf_curlopt_helper(chan, cmd, data, buf, NULL, len);
+}
+
+static int acf_curlopt_read2(struct ast_channel *chan, const char *cmd, char *data, struct ast_str **buf, ssize_t len)
+{
+ return acf_curlopt_helper(chan, cmd, data, NULL, buf, len);
+}
+
static size_t WriteMemoryCallback(void *ptr, size_t size, size_t nmemb, void *data)
{
register int realsize = size * nmemb;
@@ -391,7 +441,7 @@ static void curl_instance_cleanup(void *data)
AST_THREADSTORAGE_CUSTOM(curl_instance, curl_instance_init, curl_instance_cleanup);
-static int acf_curl_exec(struct ast_channel *chan, const char *cmd, char *info, char *buf, size_t len)
+static int acf_curl_helper(struct ast_channel *chan, const char *cmd, char *info, char *buf, struct ast_str **input_str, ssize_t len)
{
struct ast_str *str = ast_str_create(16);
int ret = -1;
@@ -405,15 +455,17 @@ static int acf_curl_exec(struct ast_channel *chan, const char *cmd, char *info,
int hashcompat = 0;
AST_LIST_HEAD(global_curl_info, curl_settings) *list = NULL;
- *buf = '\0';
-
+ if (buf) {
+ *buf = '\0';
+ }
+
if (ast_strlen_zero(info)) {
ast_log(LOG_WARNING, "CURL requires an argument (URL)\n");
ast_free(str);
return -1;
}
- AST_STANDARD_APP_ARGS(args, info);
+ AST_STANDARD_APP_ARGS(args, info);
if (chan) {
ast_autoservice_start(chan);
@@ -483,11 +535,19 @@ static int acf_curl_exec(struct ast_channel *chan, const char *cmd, char *info,
rowcount++;
}
pbx_builtin_setvar_helper(chan, "~ODBCFIELDS~", ast_str_buffer(fields));
- ast_copy_string(buf, ast_str_buffer(values), len);
+ if (buf) {
+ ast_copy_string(buf, ast_str_buffer(values), len);
+ } else {
+ ast_str_set(input_str, len, "%s", ast_str_buffer(values));
+ }
ast_free(fields);
ast_free(values);
} else {
- ast_copy_string(buf, ast_str_buffer(str), len);
+ if (buf) {
+ ast_copy_string(buf, ast_str_buffer(str), len);
+ } else {
+ ast_str_set(input_str, len, "%s", ast_str_buffer(str));
+ }
}
ret = 0;
}
@@ -495,10 +555,20 @@ static int acf_curl_exec(struct ast_channel *chan, const char *cmd, char *info,
if (chan)
ast_autoservice_stop(chan);
-
+
return ret;
}
+static int acf_curl_exec(struct ast_channel *chan, const char *cmd, char *info, char *buf, size_t len)
+{
+ return acf_curl_helper(chan, cmd, info, buf, NULL, len);
+}
+
+static int acf_curl2_exec(struct ast_channel *chan, const char *cmd, char *info, struct ast_str **buf, ssize_t len)
+{
+ return acf_curl_helper(chan, cmd, info, NULL, buf, len);
+}
+
struct ast_custom_function acf_curl = {
.name = "CURL",
.synopsis = "Retrieves the contents of a URL",
@@ -507,6 +577,7 @@ struct ast_custom_function acf_curl = {
" url - URL to retrieve\n"
" post-data - Optional data to send as a POST (GET is default action)\n",
.read = acf_curl_exec,
+ .read2 = acf_curl2_exec,
};
struct ast_custom_function acf_curlopt = {
@@ -532,6 +603,7 @@ struct ast_custom_function acf_curlopt = {
" hashcompat - Result data will be compatible for use with HASH()\n"
"",
.read = acf_curlopt_read,
+ .read2 = acf_curlopt_read2,
.write = acf_curlopt_write,
};