RTS API Documentation  1.10.11
Data Structures | Macros | Typedefs | Functions
xswitch.h File Reference
#include <switch.h>
#include <switch_curl.h>
+ Include dependency graph for xswitch.h:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  http_data_s
 

Macros

#define XSWITCH_CM_GET   0
 
#define XSWITCH_CM_POST   1
 

Typedefs

typedef struct http_data_s http_data_t
 

Functions

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

◆ XSWITCH_CM_GET

#define XSWITCH_CM_GET   0

Definition at line 40 of file xswitch.h.

Referenced by xswitch_http_get(), and xswitch_http_request().

◆ XSWITCH_CM_POST

#define XSWITCH_CM_POST   1

Definition at line 41 of file xswitch.h.

Referenced by xswitch_http_post().

Typedef Documentation

◆ http_data_t

typedef struct http_data_s http_data_t

Function Documentation

◆ 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.