mirror of
https://github.com/signalwire/freeswitch.git
synced 2025-03-14 04:54:49 +00:00
Major update. Many bug fixes.
git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@10865 d0543943-73ff-0310-b7d9-9358b9ac24b2
This commit is contained in:
parent
91cb518004
commit
9c0f8adad6
@ -308,6 +308,29 @@ int http_parse_response(char *buf, ssize_t buf_len, http_response_t *response)
|
|||||||
sm.buf = buf;
|
sm.buf = buf;
|
||||||
sm.buf_len = buf_len;
|
sm.buf_len = buf_len;
|
||||||
|
|
||||||
|
fprintf(
|
||||||
|
stderr,
|
||||||
|
"ERRORS\n"
|
||||||
|
"VERSION = %d\n"
|
||||||
|
"STATUS_CODE = %d\n"
|
||||||
|
"PHRASE = %d\n"
|
||||||
|
"HEADER = %d\n"
|
||||||
|
"NEWLINE = %d\n"
|
||||||
|
"SYNTAX_ERROR = %d\n",
|
||||||
|
VERSION,
|
||||||
|
STATUS_CODE,
|
||||||
|
PHRASE,
|
||||||
|
HEADER,
|
||||||
|
NEWLINE,
|
||||||
|
SYNTAX_ERROR
|
||||||
|
);
|
||||||
|
|
||||||
|
fprintf(
|
||||||
|
stderr,
|
||||||
|
"buf = \"%s\"\n",
|
||||||
|
buf
|
||||||
|
);
|
||||||
|
|
||||||
pos = sm.pos;
|
pos = sm.pos;
|
||||||
token = get_next_token(&sm);
|
token = get_next_token(&sm);
|
||||||
if(token != VERSION){
|
if(token != VERSION){
|
||||||
@ -379,7 +402,13 @@ int http_parse_response(char *buf, ssize_t buf_len, http_response_t *response)
|
|||||||
|
|
||||||
if(token != HEADER && token != PHRASE && token != STATUS_CODE) break;
|
if(token != HEADER && token != PHRASE && token != STATUS_CODE) break;
|
||||||
}while(token != SYNTAX_ERROR);
|
}while(token != SYNTAX_ERROR);
|
||||||
|
|
||||||
|
fprintf(
|
||||||
|
stderr,
|
||||||
|
"HEADERS = %d\n",
|
||||||
|
nt
|
||||||
|
);
|
||||||
|
|
||||||
if(nt != 0){
|
if(nt != 0){
|
||||||
response->headers = (http_header_t *)malloc(sizeof(http_header_t)*nt);
|
response->headers = (http_header_t *)malloc(sizeof(http_header_t)*nt);
|
||||||
if(response->headers == NULL){
|
if(response->headers == NULL){
|
||||||
@ -441,7 +470,6 @@ int http_parse_response(char *buf, ssize_t buf_len, http_response_t *response)
|
|||||||
return ERROR;
|
return ERROR;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
response->body = (char *)malloc((buf_len - sm.pos + 1) * sizeof(char));
|
response->body = (char *)malloc((buf_len - sm.pos + 1) * sizeof(char));
|
||||||
if(response->body == NULL){
|
if(response->body == NULL){
|
||||||
perror("Could not allocate memory for body");
|
perror("Could not allocate memory for body");
|
||||||
|
@ -96,6 +96,7 @@ SWITCH_STANDARD_API(http_api_main)
|
|||||||
char *buf;
|
char *buf;
|
||||||
char *method;
|
char *method;
|
||||||
char *url;
|
char *url;
|
||||||
|
char *headers_dec;
|
||||||
char *headers_str;
|
char *headers_str;
|
||||||
char *value;
|
char *value;
|
||||||
char *body;
|
char *body;
|
||||||
@ -132,11 +133,19 @@ SWITCH_STANDARD_API(http_api_main)
|
|||||||
|
|
||||||
if(argc != HTTP_PARAMS && argc != (HTTP_PARAMS - 1)){
|
if(argc != HTTP_PARAMS && argc != (HTTP_PARAMS - 1)){
|
||||||
switch_safe_free(ccmd);
|
switch_safe_free(ccmd);
|
||||||
stream->write_function(stream, "-ERR\n");
|
stream->write_function(stream, "-ERR\n1\n");
|
||||||
return SWITCH_STATUS_SUCCESS;
|
return SWITCH_STATUS_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
method = argv[0];
|
method = argv[0];
|
||||||
|
if(strcasecmp( "GET", method) == 0) request.method = GET;
|
||||||
|
else if(strcasecmp("POST", method) == 0) request.method = POST;
|
||||||
|
else if(strcasecmp("HEAD", method) == 0) request.method = HEAD;
|
||||||
|
else if(strcasecmp("DELETE", method) == 0) request.method = DELETE;
|
||||||
|
else if(strcasecmp("PUT", method) == 0) request.method = PUT;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
url = argv[1];
|
url = argv[1];
|
||||||
headers_str = argv[2];
|
headers_str = argv[2];
|
||||||
if(argc == HTTP_PARAMS){
|
if(argc == HTTP_PARAMS){
|
||||||
@ -145,7 +154,7 @@ SWITCH_STANDARD_API(http_api_main)
|
|||||||
body = (char *)malloc(1 * sizeof(char));
|
body = (char *)malloc(1 * sizeof(char));
|
||||||
if(body == NULL){
|
if(body == NULL){
|
||||||
switch_safe_free(ccmd);
|
switch_safe_free(ccmd);
|
||||||
stream->write_function(stream, "-ERR\n");
|
stream->write_function(stream, "-ERR\n2\n");
|
||||||
return SWITCH_STATUS_SUCCESS;
|
return SWITCH_STATUS_SUCCESS;
|
||||||
}
|
}
|
||||||
body[0] = '\0';
|
body[0] = '\0';
|
||||||
@ -155,7 +164,7 @@ SWITCH_STANDARD_API(http_api_main)
|
|||||||
buf = (char *)malloc(HTTP_BUFFER_SIZE * sizeof(char));
|
buf = (char *)malloc(HTTP_BUFFER_SIZE * sizeof(char));
|
||||||
if(buf == NULL){
|
if(buf == NULL){
|
||||||
switch_safe_free(ccmd);
|
switch_safe_free(ccmd);
|
||||||
stream->write_function(stream, "-ERR\n");
|
stream->write_function(stream, "-ERR\n3\n");
|
||||||
GARBAGE_CLEANUP();
|
GARBAGE_CLEANUP();
|
||||||
return SWITCH_STATUS_SUCCESS;
|
return SWITCH_STATUS_SUCCESS;
|
||||||
}
|
}
|
||||||
@ -167,50 +176,63 @@ SWITCH_STANDARD_API(http_api_main)
|
|||||||
request.url = (char *)malloc((l + 1) * sizeof(char));
|
request.url = (char *)malloc((l + 1) * sizeof(char));
|
||||||
if(request.url == NULL){
|
if(request.url == NULL){
|
||||||
switch_safe_free(ccmd);
|
switch_safe_free(ccmd);
|
||||||
stream->write_function(stream, "-ERR\n");
|
stream->write_function(stream, "-ERR\n4\n");
|
||||||
GARBAGE_CLEANUP();
|
GARBAGE_CLEANUP();
|
||||||
return SWITCH_STATUS_SUCCESS;
|
return SWITCH_STATUS_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
GARBAGE_ADD(request.url);
|
GARBAGE_ADD(request.url);
|
||||||
strcpy(request.url, url);
|
strcpy(request.url, url);
|
||||||
json_http_headers = json_tokener_parse(headers_str);
|
|
||||||
|
|
||||||
|
l = strlen(headers_str);
|
||||||
|
|
||||||
|
headers_dec = url_decode(headers_str, l);
|
||||||
|
GARBAGE_ADD(headers_dec);
|
||||||
|
|
||||||
|
json_http_headers = json_tokener_parse(headers_dec);
|
||||||
if(is_error(json_http_headers)){
|
if(is_error(json_http_headers)){
|
||||||
switch_safe_free(ccmd);
|
switch_safe_free(ccmd);
|
||||||
stream->write_function(stream, "-ERR\n");
|
stream->write_function(stream, "-ERR\n5\n");
|
||||||
GARBAGE_CLEANUP();
|
GARBAGE_CLEANUP();
|
||||||
return SWITCH_STATUS_SUCCESS;
|
return SWITCH_STATUS_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
i = 0;
|
i = 0;
|
||||||
json_object_object_foreach(json_http_headers, key, val){
|
json_object_object_foreach(json_http_headers, key, val){
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
|
|
||||||
request.header_len = i;
|
request.header_len = i;
|
||||||
headers = (http_header_t *)malloc(i * sizeof(http_header_t));
|
headers = (http_header_t *)malloc(i * sizeof(http_header_t));
|
||||||
|
request.headers = headers;
|
||||||
GARBAGE_ADD(headers);
|
GARBAGE_ADD(headers);
|
||||||
|
|
||||||
i = 0;
|
i = 0;
|
||||||
json_object_object_foreach(json_http_headers, key, val){
|
json_object_object_foreach(json_http_headers, key, val){
|
||||||
l = strlen(key);
|
l = strlen(key);
|
||||||
|
|
||||||
request.headers[i].field_name = (char *)malloc((l + 1) * sizeof(char));
|
request.headers[i].field_name = (char *)malloc((l + 1) * sizeof(char));
|
||||||
if(request.headers[i].field_name == NULL){
|
if(request.headers[i].field_name == NULL){
|
||||||
switch_safe_free(ccmd);
|
switch_safe_free(ccmd);
|
||||||
stream->write_function(stream, "-ERR\n");
|
stream->write_function(stream, "-ERR\n6\n");
|
||||||
GARBAGE_CLEANUP();
|
GARBAGE_CLEANUP();
|
||||||
return SWITCH_STATUS_SUCCESS;
|
return SWITCH_STATUS_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
GARBAGE_ADD(request.headers[i].field_name);
|
GARBAGE_ADD(request.headers[i].field_name);
|
||||||
|
|
||||||
strcpy(request.headers[i].field_name, key);
|
strcpy(request.headers[i].field_name, key);
|
||||||
a += strlen(key);
|
a += strlen(key);
|
||||||
|
|
||||||
value = json_object_to_json_string(val);
|
value = json_object_get_string(val);
|
||||||
|
/* value = json_object_to_json_string(val); */
|
||||||
l = strlen(value);
|
l = strlen(value);
|
||||||
request.headers[i].value = (char *)malloc((l + 1) * sizeof(char));
|
request.headers[i].value = (char *)malloc((l + 1) * sizeof(char));
|
||||||
if(request.headers[i].value == NULL){
|
if(request.headers[i].value == NULL){
|
||||||
switch_safe_free(ccmd);
|
switch_safe_free(ccmd);
|
||||||
stream->write_function(stream, "-ERR\n");
|
stream->write_function(stream, "-ERR\n7\n");
|
||||||
GARBAGE_CLEANUP();
|
GARBAGE_CLEANUP();
|
||||||
return SWITCH_STATUS_SUCCESS;
|
return SWITCH_STATUS_SUCCESS;
|
||||||
}
|
}
|
||||||
@ -230,6 +252,7 @@ SWITCH_STANDARD_API(http_api_main)
|
|||||||
}else request.body_len = 0;
|
}else request.body_len = 0;
|
||||||
|
|
||||||
ret = http_req(&request, &response);
|
ret = http_req(&request, &response);
|
||||||
|
|
||||||
if(response.version != NULL) GARBAGE_ADD(response.version);
|
if(response.version != NULL) GARBAGE_ADD(response.version);
|
||||||
if(response.phrase != NULL) GARBAGE_ADD(response.phrase);
|
if(response.phrase != NULL) GARBAGE_ADD(response.phrase);
|
||||||
if(response.headers != NULL) GARBAGE_ADD(response.headers);
|
if(response.headers != NULL) GARBAGE_ADD(response.headers);
|
||||||
@ -242,7 +265,7 @@ SWITCH_STANDARD_API(http_api_main)
|
|||||||
|
|
||||||
if(ret == ERROR){
|
if(ret == ERROR){
|
||||||
switch_safe_free(ccmd);
|
switch_safe_free(ccmd);
|
||||||
stream->write_function(stream, "-ERR\n");
|
stream->write_function(stream, "-ERR\n8\n");
|
||||||
GARBAGE_CLEANUP();
|
GARBAGE_CLEANUP();
|
||||||
return SWITCH_STATUS_SUCCESS;
|
return SWITCH_STATUS_SUCCESS;
|
||||||
}
|
}
|
||||||
@ -262,7 +285,7 @@ SWITCH_STANDARD_API(http_api_main)
|
|||||||
json_response = (char *)malloc(l * sizeof(char));
|
json_response = (char *)malloc(l * sizeof(char));
|
||||||
if(json_response == NULL){
|
if(json_response == NULL){
|
||||||
switch_safe_free(ccmd);
|
switch_safe_free(ccmd);
|
||||||
stream->write_function(stream, "-ERR\n");
|
stream->write_function(stream, "-ERR\n9\n");
|
||||||
GARBAGE_CLEANUP();
|
GARBAGE_CLEANUP();
|
||||||
return SWITCH_STATUS_SUCCESS;
|
return SWITCH_STATUS_SUCCESS;
|
||||||
}
|
}
|
||||||
@ -272,7 +295,7 @@ SWITCH_STANDARD_API(http_api_main)
|
|||||||
t = (char *)malloc((response.body_len + 1) * sizeof(char));
|
t = (char *)malloc((response.body_len + 1) * sizeof(char));
|
||||||
if(t == NULL){
|
if(t == NULL){
|
||||||
switch_safe_free(ccmd);
|
switch_safe_free(ccmd);
|
||||||
stream->write_function(stream, "-ERR\n");
|
stream->write_function(stream, "-ERR\n10\n");
|
||||||
GARBAGE_CLEANUP();
|
GARBAGE_CLEANUP();
|
||||||
return SWITCH_STATUS_SUCCESS;
|
return SWITCH_STATUS_SUCCESS;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user