mirror of
https://github.com/signalwire/freeswitch.git
synced 2025-06-04 04:12:03 +00:00
Merge pull request #213 in FS/freeswitch from ~MISHEHU/freeswitch:bugfix/FS-7340-mod_curl-json-api-change to master
* commit '5303101df05a01e82060df253d68d14a44f4260b': FS-7340: Converting the json handling to use the types and functions in switch_json.h .
This commit is contained in:
commit
3c9711f160
@ -33,7 +33,6 @@
|
|||||||
|
|
||||||
#include <switch.h>
|
#include <switch.h>
|
||||||
#include <switch_curl.h>
|
#include <switch_curl.h>
|
||||||
#include <json.h>
|
|
||||||
#ifdef _MSC_VER
|
#ifdef _MSC_VER
|
||||||
#include <WinSock2.h>
|
#include <WinSock2.h>
|
||||||
#else
|
#else
|
||||||
@ -269,21 +268,28 @@ static http_data_t *do_lookup_url(switch_memory_pool_t *pool, const char *url, c
|
|||||||
|
|
||||||
static char *print_json(switch_memory_pool_t *pool, http_data_t *http_data)
|
static char *print_json(switch_memory_pool_t *pool, http_data_t *http_data)
|
||||||
{
|
{
|
||||||
struct json_object *top = NULL;
|
cJSON *top = cJSON_CreateObject(),
|
||||||
struct json_object *headers = NULL;
|
*headers = cJSON_CreateObject();
|
||||||
char *data = NULL;
|
char *data = NULL;
|
||||||
|
char tmp[32], *f = NULL;
|
||||||
switch_curl_slist_t *header = http_data->headers;
|
switch_curl_slist_t *header = http_data->headers;
|
||||||
|
|
||||||
top = json_object_new_object();
|
if(!top || !headers) {
|
||||||
headers = json_object_new_array();
|
cJSON_Delete(headers);
|
||||||
json_object_object_add(top, "status_code", json_object_new_int(http_data->http_response_code));
|
|
||||||
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Unable to alloc memory for cJSON structures.\n");
|
||||||
|
goto curl_json_output_end;
|
||||||
|
}
|
||||||
|
|
||||||
|
switch_snprintf(tmp, sizeof(tmp), "%ld", http_data->http_response_code);
|
||||||
|
cJSON_AddItemToObject(top, "status_code", cJSON_CreateString(tmp));
|
||||||
if (http_data->http_response) {
|
if (http_data->http_response) {
|
||||||
json_object_object_add(top, "body", json_object_new_string(http_data->http_response));
|
cJSON_AddItemToObject(top, "body", cJSON_CreateString(http_data->http_response));
|
||||||
}
|
}
|
||||||
|
|
||||||
/* parse header data */
|
/* parse header data */
|
||||||
while (header) {
|
while (header) {
|
||||||
struct json_object *obj = NULL;
|
cJSON *obj = NULL;
|
||||||
/* remove trailing \r */
|
/* remove trailing \r */
|
||||||
if ((data = strrchr(header->data, '\r'))) {
|
if ((data = strrchr(header->data, '\r'))) {
|
||||||
*data = '\0';
|
*data = '\0';
|
||||||
@ -300,18 +306,18 @@ static char *print_json(switch_memory_pool_t *pool, http_data_t *http_data)
|
|||||||
while (*data == ' ' && *data != '\0') {
|
while (*data == ' ' && *data != '\0') {
|
||||||
data++;
|
data++;
|
||||||
}
|
}
|
||||||
obj = json_object_new_object();
|
obj = cJSON_CreateObject();
|
||||||
json_object_object_add(obj, "key", json_object_new_string(header->data));
|
cJSON_AddItemToObject(obj, "key", cJSON_CreateString(header->data));
|
||||||
json_object_object_add(obj, "value", json_object_new_string(data));
|
cJSON_AddItemToObject(obj, "value", cJSON_CreateString(data));
|
||||||
json_object_array_add(headers, obj);
|
cJSON_AddItemToArray(headers, obj);
|
||||||
} else {
|
} else {
|
||||||
if (!strncmp("HTTP", header->data, 4)) {
|
if (!strncmp("HTTP", header->data, 4)) {
|
||||||
char *argv[3] = { 0 };
|
char *argv[3] = { 0 };
|
||||||
int argc;
|
int argc;
|
||||||
if ((argc = switch_separate_string(header->data, ' ', argv, (sizeof(argv) / sizeof(argv[0]))))) {
|
if ((argc = switch_separate_string(header->data, ' ', argv, (sizeof(argv) / sizeof(argv[0]))))) {
|
||||||
if (argc > 2) {
|
if (argc > 2) {
|
||||||
json_object_object_add(top, "version", json_object_new_string(argv[0]));
|
cJSON_AddItemToObject(top, "version", cJSON_CreateString(argv[0]));
|
||||||
json_object_object_add(top, "phrase", json_object_new_string(argv[2]));
|
cJSON_AddItemToObject(top, "phrase", cJSON_CreateString(argv[2]));
|
||||||
} else {
|
} else {
|
||||||
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Unparsable header: argc: %d\n", argc);
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Unparsable header: argc: %d\n", argc);
|
||||||
}
|
}
|
||||||
@ -324,10 +330,13 @@ static char *print_json(switch_memory_pool_t *pool, http_data_t *http_data)
|
|||||||
}
|
}
|
||||||
header = header->next;
|
header = header->next;
|
||||||
}
|
}
|
||||||
json_object_object_add(top, "headers", headers);
|
cJSON_AddItemToObject(top, "headers", headers);
|
||||||
data = switch_core_strdup(pool, json_object_to_json_string(top));
|
f = cJSON_PrintUnformatted(top);
|
||||||
json_object_put(top); /* should free up all children */
|
data = switch_core_strdup(pool, f);
|
||||||
|
switch_safe_free(f);
|
||||||
|
|
||||||
|
curl_json_output_end:
|
||||||
|
cJSON_Delete(top); /* should free up all children */
|
||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user