RTS API Documentation  1.10.11
Macros | Functions
xswitch.c File Reference
#include <xswitch.h>
+ Include dependency graph for xswitch.c:

Go to the source code of this file.

Macros

#define BUFFER_LEN   1024
 

Functions

static size_t file_callback (void *ptr, size_t size, size_t nmemb, void *data)
 
static size_t header_callback (void *ptr, size_t size, size_t nmemb, void *data)
 
http_data_txswitch_http_post (const char *url, const char *data, switch_memory_pool_t *pool)
 
http_data_txswitch_http_get (const char *url, switch_memory_pool_t *pool)
 
http_data_txswitch_http_request (int method, const char *url, const void *data, size_t datalen, switch_curl_slist_t *headers, switch_memory_pool_t *pool, int curl_connect_timeout, int curl_timeout)
 

Macro Definition Documentation

◆ BUFFER_LEN

#define BUFFER_LEN   1024

Definition at line 35 of file xswitch.c.

Referenced by file_callback().

Function Documentation

◆ file_callback()

static size_t file_callback ( void *  ptr,
size_t  size,
size_t  nmemb,
void *  data 
)
static

Definition at line 37 of file xswitch.c.

References http_data_s::body_buffer, BUFFER_LEN, switch_buffer_create_dynamic(), and switch_buffer_write().

Referenced by xswitch_http_request().

38 {
39  register unsigned int realsize = (unsigned int)(size * nmemb);
40  http_data_t *http_data = data;
41 
42  if (http_data && !http_data->body_buffer) {
44  }
45 
46  if (http_data) switch_buffer_write(http_data->body_buffer, ptr, realsize);
47 
48  return realsize;
49 }
switch_status_t switch_buffer_create_dynamic(_Out_ switch_buffer_t **buffer, _In_ switch_size_t blocksize, _In_ switch_size_t start_len, _In_ switch_size_t max_len)
Allocate a new dynamic switch_buffer.
switch_buffer_t * body_buffer
Definition: xswitch.h:44
switch_size_t switch_buffer_write(_In_ switch_buffer_t *buffer, _In_bytecount_(datalen) const void *data, _In_ switch_size_t datalen)
Write data into a switch_buffer_t up to the length of datalen.
#define BUFFER_LEN
Definition: xswitch.c:35

◆ header_callback()

static size_t header_callback ( void *  ptr,
size_t  size,
size_t  nmemb,
void *  data 
)
static

Definition at line 51 of file xswitch.c.

References http_data_s::headers, http_data_s::pool, switch_copy_string(), switch_core_alloc, and switch_curl_slist_append().

Referenced by xswitch_http_request().

52 {
53  register unsigned int realsize = (unsigned int)(size * nmemb);
54  http_data_t *http_data = data;
55  char *header = NULL;
56 
57  header = switch_core_alloc(http_data->pool, realsize + 1);
58  switch_copy_string(header, ptr, realsize);
59  header[realsize] = '\0';
60  // printf("%s\n", header);
61  http_data->headers = switch_curl_slist_append(http_data->headers, header);
62 
63  return realsize;
64 }
switch_curl_slist_t * headers
Definition: xswitch.h:50
#define switch_core_alloc(_pool, _mem)
Allocate memory directly from a memory pool.
Definition: switch_core.h:684
switch_curl_slist_t * switch_curl_slist_append(switch_curl_slist_t *list, const char *string)
Definition: switch_curl.c:35
char * switch_copy_string(_Out_z_cap_(dst_size) char *dst, _In_z_ const char *src, _In_ switch_size_t dst_size)
switch_memory_pool_t * pool
Definition: xswitch.h:45

◆ xswitch_http_get()

http_data_t* xswitch_http_get ( const char *  url,
switch_memory_pool_t pool 
)

Definition at line 71 of file xswitch.c.

References SWITCH_DECLARE, XSWITCH_CM_GET, and xswitch_http_request().

72 {
73  return xswitch_http_request(XSWITCH_CM_GET, url, NULL, 0, NULL, pool, 0, 0);
74 }
switch_memory_pool_t * pool
http_data_t * xswitch_http_request(int method, const char *url, const void *data, size_t datalen, switch_curl_slist_t *headers, switch_memory_pool_t *pool, int curl_connect_timeout, int curl_timeout)
Definition: xswitch.c:77
#define XSWITCH_CM_GET
Definition: xswitch.h:40

◆ xswitch_http_post()

http_data_t* xswitch_http_post ( const char *  url,
const char *  data,
switch_memory_pool_t pool 
)

Definition at line 66 of file xswitch.c.

References XSWITCH_CM_POST, and xswitch_http_request().

67 {
68  return xswitch_http_request(XSWITCH_CM_POST, url, (void *)data, strlen(data), NULL, pool, 0, 0);
69 }
switch_memory_pool_t * pool
#define XSWITCH_CM_POST
Definition: xswitch.h:41
http_data_t * xswitch_http_request(int method, const char *url, const void *data, size_t datalen, switch_curl_slist_t *headers, switch_memory_pool_t *pool, int curl_connect_timeout, int curl_timeout)
Definition: xswitch.c:77

◆ xswitch_http_request()

http_data_t* xswitch_http_request ( int  method,
const char *  url,
const void *  data,
size_t  datalen,
switch_curl_slist_t headers,
switch_memory_pool_t pool,
int  curl_connect_timeout,
int  curl_timeout 
)

Definition at line 77 of file xswitch.c.

References http_data_s::body_buffer, http_data_s::body_size, http_data_s::code, http_data_s::content_type, file_callback(), header_callback(), http_data_s::perform_code, http_data_s::pool, pool, switch_buffer_inuse(), SWITCH_CHANNEL_LOG, switch_core_alloc, switch_core_strdup, switch_CURL, switch_curl_easy_cleanup(), switch_curl_easy_getinfo(), switch_curl_easy_init(), switch_curl_easy_perform(), switch_curl_easy_setopt(), switch_curl_slist_free_all(), SWITCH_LOG_DEBUG, switch_log_printf(), and XSWITCH_CM_GET.

Referenced by xswitch_http_get(), and xswitch_http_post().

79 {
80  switch_CURL *curl_handle = NULL;
81  long http_res = 0;
82  http_data_t *http_data = NULL;
83  char *content_type_res = NULL;
84 
85  http_data = switch_core_alloc(pool, sizeof(http_data_t));
86  http_data->pool = pool;
87 
88  // switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "url: %s, data: %s\n", url, data);
89  curl_handle = switch_curl_easy_init();
90 
91  if (curl_connect_timeout == 0) curl_connect_timeout = 3;
92  if (curl_timeout == 0) curl_timeout = 30;
93 
94  switch_curl_easy_setopt(curl_handle, CURLOPT_CONNECTTIMEOUT, curl_connect_timeout);
95  switch_curl_easy_setopt(curl_handle, CURLOPT_TIMEOUT, curl_timeout);
96 
97  if (!strncasecmp(url, "https", 5)) {
98  // switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "Not verifying TLS cert for %s; connection is not
99  // secure\n", url);
100  switch_curl_easy_setopt(curl_handle, CURLOPT_SSL_VERIFYPEER, 0);
101  switch_curl_easy_setopt(curl_handle, CURLOPT_SSL_VERIFYHOST, 0);
102  }
103 
104  if (method == XSWITCH_CM_GET) {
105  switch_curl_easy_setopt(curl_handle, CURLOPT_HTTPGET, 1);
106  } else {
107  switch_curl_easy_setopt(curl_handle, CURLOPT_POSTFIELDSIZE, datalen);
108  switch_curl_easy_setopt(curl_handle, CURLOPT_POSTFIELDS, data);
109  }
110 
111  if (headers) {
112  switch_curl_easy_setopt(curl_handle, CURLOPT_HTTPHEADER, headers);
113  }
114 
115  // switch_curl_easy_setopt(curl_handle, CURLOPT_VERBOSE, 1);
116  switch_curl_easy_setopt(curl_handle, CURLOPT_FOLLOWLOCATION, 1);
117  switch_curl_easy_setopt(curl_handle, CURLOPT_MAXREDIRS, 15);
118  switch_curl_easy_setopt(curl_handle, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
119  switch_curl_easy_setopt(curl_handle, CURLOPT_URL, url);
120  switch_curl_easy_setopt(curl_handle, CURLOPT_NOSIGNAL, 1);
121  switch_curl_easy_setopt(curl_handle, CURLOPT_HEADERFUNCTION, header_callback);
122  switch_curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, file_callback);
123  switch_curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, (void *)http_data);
124  switch_curl_easy_setopt(curl_handle, CURLOPT_HEADERDATA, (void *)http_data);
125  switch_curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, "freeswitch-curl/1.0");
126 
127  http_data->perform_code = switch_curl_easy_perform(curl_handle);
128 
129  switch_curl_easy_getinfo(curl_handle, CURLINFO_RESPONSE_CODE, &http_res);
130  switch_curl_easy_getinfo(curl_handle, CURLINFO_CONTENT_TYPE, &content_type_res);
131  http_data->code = http_res;
132  http_data->content_type = switch_core_strdup(pool, content_type_res);
133  switch_curl_easy_cleanup(curl_handle);
134  if (headers) switch_curl_slist_free_all(headers);
135 
136  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "code: %ld, content_type_res: %s\n", http_res,
137  http_data->content_type);
138 
139  if (http_data->body_buffer) {
140  switch_size_t body_len = 0;
141  body_len = switch_buffer_inuse(http_data->body_buffer);
142  http_data->body_size = body_len;
143  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "body_len: %zd\n", body_len);
144  }
145 
146  return http_data;
147 }
#define SWITCH_CHANNEL_LOG
switch_CURLcode perform_code
Definition: xswitch.h:51
switch_CURLcode switch_curl_easy_getinfo(switch_CURL *curl, switch_CURLINFO info,...)
Definition: switch_curl.c:16
#define switch_core_strdup(_pool, _todup)
Copy a string using memory allocation from a given pool.
Definition: switch_core.h:733
switch_memory_pool_t * pool
switch_size_t body_size
Definition: xswitch.h:49
switch_CURL * switch_curl_easy_init(void)
Definition: switch_curl.c:5
switch_CURLcode switch_curl_easy_perform(switch_CURL *handle)
Definition: switch_curl.c:10
long code
Definition: xswitch.h:48
switch_buffer_t * body_buffer
Definition: xswitch.h:44
void switch_curl_easy_cleanup(switch_CURL *handle)
Definition: switch_curl.c:28
#define switch_core_alloc(_pool, _mem)
Allocate memory directly from a memory pool.
Definition: switch_core.h:684
static size_t header_callback(void *ptr, size_t size, size_t nmemb, void *data)
Definition: xswitch.c:51
SWITCH_BEGIN_EXTERN_C typedef void switch_CURL
Definition: switch_curl.h:37
uintptr_t switch_size_t
char * content_type
Definition: xswitch.h:46
switch_CURLcode switch_curl_easy_setopt(CURL *handle, switch_CURLoption option,...)
#define XSWITCH_CM_GET
Definition: xswitch.h:40
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_memory_pool_t * pool
Definition: xswitch.h:45
void switch_curl_slist_free_all(switch_curl_slist_t *list)
Definition: switch_curl.c:41
static size_t file_callback(void *ptr, size_t size, size_t nmemb, void *data)
Definition: xswitch.c:37
switch_size_t switch_buffer_inuse(_In_ switch_buffer_t *buffer)
Get the in use amount of a switch_buffer_t.