switch_curl.c
2.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include <switch.h>
#include "switch_curl.h"
#include <curl/curl.h>
CURLcode Curl_setopt(switch_CURL *curl, CURLoption option, va_list arg);
SWITCH_DECLARE(switch_CURL *) switch_curl_easy_init(void)
{
return curl_easy_init();
}
SWITCH_DECLARE(switch_CURLcode) switch_curl_easy_perform(switch_CURL *handle)
{
return curl_easy_perform((CURL *)handle);
}
SWITCH_DECLARE(switch_CURLcode) switch_curl_easy_getinfo(switch_CURL *curl, switch_CURLINFO info, ... )
{
va_list ap;
switch_CURLcode code;
va_start(ap, info);
code = curl_easy_getinfo(curl, info, va_arg(ap, void *));
va_end(ap);
return code;
}
SWITCH_DECLARE(void) switch_curl_easy_cleanup(switch_CURL *handle)
{
curl_easy_cleanup((CURL *)handle);
}
SWITCH_DECLARE(switch_curl_slist_t *) switch_curl_slist_append(switch_curl_slist_t * list, const char * string )
{
return (switch_curl_slist_t *) curl_slist_append((struct curl_slist *)list, string);
}
SWITCH_DECLARE(void) switch_curl_slist_free_all(switch_curl_slist_t * list)
{
curl_slist_free_all((struct curl_slist *) list);
}
SWITCH_DECLARE(switch_CURLcode) switch_curl_easy_setopt(CURL *handle, switch_CURLoption option, ...)
{
va_list ap;
switch_CURLcode code;
va_start(ap, option);
code = Curl_setopt(handle, option, ap);
va_end(ap);
return code;
}
SWITCH_DECLARE(const char *) switch_curl_easy_strerror(switch_CURLcode errornum )
{
return curl_easy_strerror(errornum);
}
SWITCH_DECLARE(void) switch_curl_init(void)
{
curl_global_init(CURL_GLOBAL_ALL);
}
SWITCH_DECLARE(void) switch_curl_destroy(void)
{
curl_global_cleanup();
}
SWITCH_DECLARE(switch_status_t) switch_curl_process_form_post_params(switch_event_t *event, switch_CURL *curl_handle, struct curl_httppost **formpostp)
{
struct curl_httppost *formpost=NULL;
struct curl_httppost *lastptr=NULL;
switch_event_header_t *hp;
int go = 0;
for (hp = event->headers; hp; hp = hp->next) {
if (!strncasecmp(hp->name, "attach_file:", 12)) {
go = 1;
break;
}
}
if (!go) {
return SWITCH_STATUS_FALSE;
}
for (hp = event->headers; hp; hp = hp->next) {
if (!strncasecmp(hp->name, "attach_file:", 12)) {
char *pname = strdup(hp->name + 12);
char *fname = strchr(pname, ':');
if (fname && pname) {
*fname++ = '\0';
curl_formadd(&formpost,
&lastptr,
CURLFORM_COPYNAME, pname,
CURLFORM_FILENAME, fname,
CURLFORM_FILE, hp->value,
CURLFORM_END);
}
free(pname);
} else {
curl_formadd(&formpost,
&lastptr,
CURLFORM_COPYNAME, hp->name,
CURLFORM_COPYCONTENTS, hp->value,
CURLFORM_END);
}
}
*formpostp = formpost;
return SWITCH_STATUS_SUCCESS;
}