RTS API Documentation  1.10.11
switch_curl.c
Go to the documentation of this file.
1 #include <switch.h>
2 #include "switch_curl.h"
3 #include <curl/curl.h>
4 
6 {
7  return curl_easy_init();
8 }
9 
11 {
12  return curl_easy_perform((CURL *)handle);
13 }
14 
15 
17 {
18  va_list ap;
19  switch_CURLcode code;
20 
21  va_start(ap, info);
22  code = curl_easy_getinfo(curl, info, va_arg(ap, void *));
23  va_end(ap);
24 
25  return code;
26 }
27 
29 {
30  curl_easy_cleanup((CURL *)handle);
31 }
32 
33 
34 
36 {
37  return (switch_curl_slist_t *) curl_slist_append((struct curl_slist *)list, string);
38 }
39 
40 
42 {
43  curl_slist_free_all((struct curl_slist *) list);
44 }
45 
47 {
48  return curl_easy_strerror(errornum);
49 }
50 
52 {
53  curl_global_init(CURL_GLOBAL_ALL);
54 }
55 
57 {
58  curl_global_cleanup();
59 }
60 
62 {
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;
67  switch_CURLcode curl_code = CURLE_OK;
68 #else
69  struct curl_httppost *formpost=NULL;
70  struct curl_httppost *lastptr=NULL;
71 #endif
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 
86 #if defined(LIBCURL_VERSION_NUM) && (LIBCURL_VERSION_NUM >= 0x073800)
87  mime = curl_mime_init(curl_handle);
88 #endif
89 
90  for (hp = event->headers; hp; hp = hp->next) {
91  if (!strncasecmp(hp->name, "attach_file:", 12)) {
92  char *pname = strdup(hp->name + 12);
93 
94  if (pname) {
95  char *fname = strchr(pname, ':');
96 
97  if (fname) {
98  *fname++ = '\0';
99 
100 #if defined(LIBCURL_VERSION_NUM) && (LIBCURL_VERSION_NUM >= 0x073800)
101  part = curl_mime_addpart(mime);
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 
117  added++;
118 #else
119  curl_formadd(&formpost,
120  &lastptr,
121  CURLFORM_COPYNAME, pname,
122  CURLFORM_FILENAME, fname,
123  CURLFORM_FILE, hp->value,
124  CURLFORM_END);
125 #endif
126  }
127 
128  free(pname);
129  }
130  } else {
131 #if defined(LIBCURL_VERSION_NUM) && (LIBCURL_VERSION_NUM >= 0x073800)
132  part = curl_mime_addpart(mime);
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 
141  added++;
142 #else
143  curl_formadd(&formpost,
144  &lastptr,
145  CURLFORM_COPYNAME, hp->name,
146  CURLFORM_COPYCONTENTS, hp->value,
147  CURLFORM_END);
148 #endif
149  }
150  }
151 
152 #if defined(LIBCURL_VERSION_NUM) && (LIBCURL_VERSION_NUM >= 0x073800)
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 
158  if (!added) {
159  curl_mime_free(mime);
160  mime = NULL;
161  }
162 
163  *mimep = mime;
164 #else
165  *mimep = formpost;
166 #endif
167 
168  return SWITCH_STATUS_SUCCESS;
169 }
170 
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 
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
190 }
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  */
void switch_curl_init(void)
Definition: switch_curl.c:51
void switch_curl_destroy(void)
Definition: switch_curl.c:56
#define SWITCH_CHANNEL_LOG
Representation of an event.
Definition: switch_event.h:80
switch_curl_slist_t * switch_curl_slist_append(switch_curl_slist_t *list, const char *string)
Definition: switch_curl.c:35
switch_CURLcode switch_curl_easy_getinfo(switch_CURL *curl, switch_CURLINFO info,...)
Definition: switch_curl.c:16
An event Header.
Definition: switch_event.h:65
switch_CURLcode switch_curl_easy_perform(switch_CURL *handle)
Definition: switch_curl.c:10
switch_CURL * switch_curl_easy_init(void)
Definition: switch_curl.c:5
void switch_curl_mime_free(switch_curl_mime **mimep)
Definition: switch_curl.c:171
Definition: cJSON.c:68
void switch_curl_mime
Definition: switch_curl.h:44
SWITCH_BEGIN_EXTERN_C typedef void switch_CURL
Definition: switch_curl.h:37
int switch_CURLcode
Definition: switch_curl.h:40
struct curl_slist switch_curl_slist_t
Definition: switch_curl.h:38
switch_CURLcode switch_curl_easy_setopt_mime(switch_CURL *curl_handle, switch_curl_mime *mime)
Definition: switch_curl.c:183
switch_status_t
Common return values.
struct switch_event_header * next
Definition: switch_event.h:76
Main Library Header.
#define SWITCH_DECLARE(type)
void switch_curl_easy_cleanup(switch_CURL *handle)
Definition: switch_curl.c:28
void switch_log_printf(_In_ switch_text_channel_t channel, _In_z_ const char *file, _In_z_ const char *func, _In_ int line, _In_opt_z_ const char *userdata, _In_ switch_log_level_t level, _In_z_ _Printf_format_string_ const char *fmt,...) PRINTF_FUNCTION(7
Write log data to the logging engine.
switch_status_t switch_curl_process_mime(switch_event_t *event, switch_CURL *curl_handle, switch_curl_mime **mimep)
Definition: switch_curl.c:61
const char * switch_curl_easy_strerror(switch_CURLcode errornum)
Definition: switch_curl.c:46
void switch_curl_slist_free_all(switch_curl_slist_t *list)
Definition: switch_curl.c:41
switch_event_header_t * headers
Definition: switch_event.h:90
int switch_CURLINFO
Definition: switch_curl.h:39