RTS API Documentation  1.10.11
xswitch.c
Go to the documentation of this file.
1 /*
2  * FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
3  * Copyright (C) 2020-2023, Seven Du <dujinfang@gmail.com>
4  *
5  * Version: MPL 1.1
6  *
7  * The contents of this file are subject to the Mozilla Public License Version
8  * 1.1 (the "License"); you may not use this file except in compliance with
9  * the License. You may obtain a copy of the License at
10  * http://www.mozilla.org/MPL/
11  *
12  * Software distributed under the License is distributed on an "AS IS" basis,
13  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
14  * for the specific language governing rights and limitations under the
15  * License.
16  *
17  * The Original Code is FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
18  *
19  * The Initial Developer of the Original Code is
20  * Seven Du <dujinfang@gmail.com>
21  * Portions created by the Initial Developer are Copyright (C)
22  * the Initial Developer. All Rights Reserved.
23  *
24  * Contributor(s):
25  *
26  * Seven Du <dujinfang@gmail.com>
27  *
28  * xswitch.c -- xswitch functions
29  *
30  */
31 
32 #include <xswitch.h>
33 // curl req
34 
35 #define BUFFER_LEN 1024
36 
37 static size_t file_callback(void *ptr, size_t size, size_t nmemb, void *data)
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 }
50 
51 static size_t header_callback(void *ptr, size_t size, size_t nmemb, void *data)
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 }
65 
66 SWITCH_DECLARE(http_data_t *) xswitch_http_post(const char *url, const char *data, switch_memory_pool_t *pool)
67 {
68  return xswitch_http_request(XSWITCH_CM_POST, url, (void *)data, strlen(data), NULL, pool, 0, 0);
69 }
70 
72 {
73  return xswitch_http_request(XSWITCH_CM_GET, url, NULL, 0, NULL, pool, 0, 0);
74 }
75 
77 xswitch_http_request(int method, const char *url, const void *data, size_t datalen, switch_curl_slist_t *headers,
78  switch_memory_pool_t *pool, int curl_connect_timeout, int curl_timeout)
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 }
switch_curl_slist_t * headers
Definition: xswitch.h:50
#define SWITCH_CHANNEL_LOG
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_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
http_data_t * xswitch_http_post(const char *url, const char *data, switch_memory_pool_t *pool)
Definition: xswitch.c:66
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
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
#define XSWITCH_CM_POST
Definition: xswitch.h:41
switch_curl_slist_t * switch_curl_slist_append(switch_curl_slist_t *list, const char *string)
Definition: switch_curl.c:35
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
http_data_t * xswitch_http_get(const char *url, switch_memory_pool_t *pool)
Definition: xswitch.c:71
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
struct curl_slist switch_curl_slist_t
Definition: switch_curl.h:38
char * switch_copy_string(_Out_z_cap_(dst_size) char *dst, _In_z_ const char *src, _In_ switch_size_t dst_size)
char * content_type
Definition: xswitch.h:46
switch_CURLcode switch_curl_easy_setopt(CURL *handle, switch_CURLoption option,...)
#define SWITCH_DECLARE(type)
#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.
struct fspr_pool_t switch_memory_pool_t
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.