]> git.ipfire.org Git - thirdparty/freeswitch.git/blame - src/switch_curl.c
[core] Coverity fixes
[thirdparty/freeswitch.git] / src / switch_curl.c
CommitLineData
17e9c50e 1#include <switch.h>
e767b17c 2#include "switch_curl.h"
17e9c50e
AM
3#include <curl/curl.h>
4
df1ab07c 5SWITCH_DECLARE(switch_CURL *) switch_curl_easy_init(void)
17e9c50e
AM
6{
7 return curl_easy_init();
8}
9
10SWITCH_DECLARE(switch_CURLcode) switch_curl_easy_perform(switch_CURL *handle)
11{
12 return curl_easy_perform((CURL *)handle);
13}
14
15
16SWITCH_DECLARE(switch_CURLcode) switch_curl_easy_getinfo(switch_CURL *curl, switch_CURLINFO info, ... )
17{
18 va_list ap;
19 switch_CURLcode code;
20
21 va_start(ap, info);
13dc2f9e 22 code = curl_easy_getinfo(curl, info, va_arg(ap, void *));
17e9c50e
AM
23 va_end(ap);
24
25 return code;
26}
27
17e9c50e
AM
28SWITCH_DECLARE(void) switch_curl_easy_cleanup(switch_CURL *handle)
29{
30 curl_easy_cleanup((CURL *)handle);
31}
32
33
34
35SWITCH_DECLARE(switch_curl_slist_t *) switch_curl_slist_append(switch_curl_slist_t * list, const char * string )
36{
37 return (switch_curl_slist_t *) curl_slist_append((struct curl_slist *)list, string);
38}
39
40
41SWITCH_DECLARE(void) switch_curl_slist_free_all(switch_curl_slist_t * list)
42{
43 curl_slist_free_all((struct curl_slist *) list);
44}
45
17e9c50e
AM
46SWITCH_DECLARE(const char *) switch_curl_easy_strerror(switch_CURLcode errornum )
47{
48 return curl_easy_strerror(errornum);
49}
50
51SWITCH_DECLARE(void) switch_curl_init(void)
52{
53 curl_global_init(CURL_GLOBAL_ALL);
54}
55
56SWITCH_DECLARE(void) switch_curl_destroy(void)
57{
58 curl_global_cleanup();
59}
60
59660770 61SWITCH_DECLARE(switch_status_t) switch_curl_process_mime(switch_event_t *event, switch_CURL *curl_handle, switch_curl_mime **mimep)
7fab8f15 62{
59660770
AV
63#if defined(LIBCURL_VERSION_NUM) && (LIBCURL_VERSION_NUM >= 0x073800)
64 curl_mime *mime = NULL;
65 curl_mimepart *part = NULL;
66 uint8_t added = 0;
bb9afcb3 67 switch_CURLcode curl_code = CURLE_OK;
59660770 68#else
7fab8f15
AM
69 struct curl_httppost *formpost=NULL;
70 struct curl_httppost *lastptr=NULL;
59660770 71#endif
7fab8f15
AM
72 switch_event_header_t *hp;
73 int go = 0;
74
75 for (hp = event->headers; hp; hp = hp->next) {
76 if (!strncasecmp(hp->name, "attach_file:", 12)) {
77 go = 1;
78 break;
79 }
80 }
81
82 if (!go) {
83 return SWITCH_STATUS_FALSE;
84 }
85
59660770
AV
86#if defined(LIBCURL_VERSION_NUM) && (LIBCURL_VERSION_NUM >= 0x073800)
87 mime = curl_mime_init(curl_handle);
88#endif
7fab8f15 89
59660770 90 for (hp = event->headers; hp; hp = hp->next) {
7fab8f15
AM
91 if (!strncasecmp(hp->name, "attach_file:", 12)) {
92 char *pname = strdup(hp->name + 12);
df1ab07c 93
e3b4e6b2
MJ
94 if (pname) {
95 char *fname = strchr(pname, ':');
59660770 96
e3b4e6b2
MJ
97 if (fname) {
98 *fname++ = '\0';
99
59660770
AV
100#if defined(LIBCURL_VERSION_NUM) && (LIBCURL_VERSION_NUM >= 0x073800)
101 part = curl_mime_addpart(mime);
bb9afcb3
JK
102 if ((curl_code = curl_mime_name(part, pname))) {
103 free(pname);
104 goto error;
105 }
106
107 if ((curl_code = curl_mime_filename(part, fname))) {
108 free(pname);
109 goto error;
110 }
111
112 if ((curl_code = curl_mime_filedata(part, hp->value))) {
113 free(pname);
114 goto error;
115 }
116
59660770
AV
117 added++;
118#else
e3b4e6b2
MJ
119 curl_formadd(&formpost,
120 &lastptr,
121 CURLFORM_COPYNAME, pname,
122 CURLFORM_FILENAME, fname,
123 CURLFORM_FILE, hp->value,
124 CURLFORM_END);
59660770 125#endif
e3b4e6b2 126 }
59660770 127
e3b4e6b2 128 free(pname);
7fab8f15 129 }
7fab8f15 130 } else {
59660770
AV
131#if defined(LIBCURL_VERSION_NUM) && (LIBCURL_VERSION_NUM >= 0x073800)
132 part = curl_mime_addpart(mime);
bb9afcb3
JK
133 if ((curl_code = curl_mime_name(part, hp->name))) {
134 goto error;
135 }
136
137 if ((curl_code = curl_mime_data(part, hp->value, CURL_ZERO_TERMINATED))) {
138 goto error;
139 }
140
59660770
AV
141 added++;
142#else
7fab8f15
AM
143 curl_formadd(&formpost,
144 &lastptr,
145 CURLFORM_COPYNAME, hp->name,
146 CURLFORM_COPYCONTENTS, hp->value,
147 CURLFORM_END);
59660770 148#endif
7fab8f15
AM
149 }
150 }
151
59660770 152#if defined(LIBCURL_VERSION_NUM) && (LIBCURL_VERSION_NUM >= 0x073800)
bb9afcb3
JK
153 error:
154 if (curl_code) {
155 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "CURL error occured. Error code: %d Error msg: [%s]\n", curl_code, switch_curl_easy_strerror(curl_code));
156 }
157
59660770
AV
158 if (!added) {
159 curl_mime_free(mime);
160 mime = NULL;
161 }
59660770
AV
162
163 *mimep = mime;
c37ed7c8
AV
164#else
165 *mimep = formpost;
166#endif
7fab8f15
AM
167
168 return SWITCH_STATUS_SUCCESS;
59660770 169}
7fab8f15 170
59660770
AV
171SWITCH_DECLARE(void) switch_curl_mime_free(switch_curl_mime **mimep)
172{
173 if (mimep && *mimep) {
174#if defined(LIBCURL_VERSION_NUM) && (LIBCURL_VERSION_NUM >= 0x073800)
175 curl_mime_free(*mimep);
176#else
177 curl_formfree(*mimep);
178#endif
179 mimep = NULL;
180 }
181}
182
183SWITCH_DECLARE(switch_CURLcode) switch_curl_easy_setopt_mime(switch_CURL *curl_handle, switch_curl_mime *mime)
184{
185#if defined(LIBCURL_VERSION_NUM) && (LIBCURL_VERSION_NUM >= 0x073800)
186 return curl_easy_setopt(curl_handle, CURLOPT_MIMEPOST, mime);
187#else
188 return curl_easy_setopt(curl_handle, CURLOPT_HTTPPOST, mime);
189#endif
7fab8f15 190}
fc247a22
TC
191
192/* For Emacs:
193 * Local Variables:
194 * mode:c
195 * indent-tabs-mode:t
196 * tab-width:4
197 * c-basic-offset:4
198 * End:
199 * For VIM:
200 * vim:set softtabstop=4 shiftwidth=4 tabstop=4 noet:
201 */