提交 af824783 authored 作者: Anthony Minessale's avatar Anthony Minessale

indent pass MODAPP-202

git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@11335 d0543943-73ff-0310-b7d9-9358b9ac24b2
上级 d88a0fbe
...@@ -23,36 +23,36 @@ ...@@ -23,36 +23,36 @@
#include "bits.h" #include "bits.h"
#include "arraylist.h" #include "arraylist.h"
struct array_list* struct array_list *array_list_new(array_list_free_fn * free_fn)
array_list_new(array_list_free_fn *free_fn)
{ {
struct array_list *this; struct array_list *this;
if(!(this = calloc(1, sizeof(struct array_list)))) return NULL; if (!(this = calloc(1, sizeof(struct array_list))))
return NULL;
this->size = ARRAY_LIST_DEFAULT_SIZE; this->size = ARRAY_LIST_DEFAULT_SIZE;
this->length = 0; this->length = 0;
this->free_fn = free_fn; this->free_fn = free_fn;
if(!(this->array = calloc(sizeof(void*), this->size))) { if (!(this->array = calloc(sizeof(void *), this->size))) {
free(this); free(this);
return NULL; return NULL;
} }
return this; return this;
} }
extern void extern void array_list_free(struct array_list *this)
array_list_free(struct array_list *this)
{ {
int i; int i;
for(i = 0; i < this->length; i++) for (i = 0; i < this->length; i++)
if(this->array[i]) this->free_fn(this->array[i]); if (this->array[i])
this->free_fn(this->array[i]);
free(this->array); free(this->array);
free(this); free(this);
} }
void* void *array_list_get_idx(struct array_list *this, int i)
array_list_get_idx(struct array_list *this, int i)
{ {
if(i >= this->length) return NULL; if (i >= this->length)
return NULL;
return this->array[i]; return this->array[i];
} }
...@@ -61,33 +61,35 @@ static int array_list_expand_internal(struct array_list *this, int max) ...@@ -61,33 +61,35 @@ static int array_list_expand_internal(struct array_list *this, int max)
void *t; void *t;
int new_size; int new_size;
if(max < this->size) return 0; if (max < this->size)
return 0;
new_size = max(this->size << 1, max); new_size = max(this->size << 1, max);
if(!(t = realloc(this->array, new_size*sizeof(void*)))) return -1; if (!(t = realloc(this->array, new_size * sizeof(void *))))
return -1;
this->array = t; this->array = t;
(void)memset(this->array + this->size, 0, (new_size-this->size)*sizeof(void*)); (void) memset(this->array + this->size, 0, (new_size - this->size) * sizeof(void *));
this->size = new_size; this->size = new_size;
return 0; return 0;
} }
int int array_list_put_idx(struct array_list *this, int idx, void *data)
array_list_put_idx(struct array_list *this, int idx, void *data)
{ {
if(array_list_expand_internal(this, idx)) return -1; if (array_list_expand_internal(this, idx))
if(this->array[idx]) this->free_fn(this->array[idx]); return -1;
if (this->array[idx])
this->free_fn(this->array[idx]);
this->array[idx] = data; this->array[idx] = data;
if(this->length <= idx) this->length = idx + 1; if (this->length <= idx)
this->length = idx + 1;
return 0; return 0;
} }
int int array_list_add(struct array_list *this, void *data)
array_list_add(struct array_list *this, void *data)
{ {
return array_list_put_idx(this, this->length, data); return array_list_put_idx(this, this->length, data);
} }
int int array_list_length(struct array_list *this)
array_list_length(struct array_list *this)
{ {
return this->length; return this->length;
} }
...@@ -16,30 +16,23 @@ ...@@ -16,30 +16,23 @@
typedef void (array_list_free_fn) (void *data); typedef void (array_list_free_fn) (void *data);
struct array_list struct array_list {
{
void **array; void **array;
int length; int length;
int size; int size;
array_list_free_fn *free_fn; array_list_free_fn *free_fn;
}; };
extern struct array_list* extern struct array_list *array_list_new(array_list_free_fn * free_fn);
array_list_new(array_list_free_fn *free_fn);
extern void extern void array_list_free(struct array_list *al);
array_list_free(struct array_list *al);
extern void* extern void *array_list_get_idx(struct array_list *al, int i);
array_list_get_idx(struct array_list *al, int i);
extern int extern int array_list_put_idx(struct array_list *al, int i, void *data);
array_list_put_idx(struct array_list *al, int i, void *data);
extern int extern int array_list_add(struct array_list *al, void *data);
array_list_add(struct array_list *al, void *data);
extern int extern int array_list_length(struct array_list *al);
array_list_length(struct array_list *al);
#endif #endif
...@@ -33,8 +33,15 @@ ...@@ -33,8 +33,15 @@
static int _syslog = 0; static int _syslog = 0;
static int _debug = 0; static int _debug = 0;
void mc_set_debug(int debug) { _debug = debug; } void mc_set_debug(int debug)
int mc_get_debug() { return _debug; } {
_debug = debug;
}
int mc_get_debug()
{
return _debug;
}
extern void mc_set_syslog(int syslog) extern void mc_set_syslog(int syslog)
{ {
...@@ -46,7 +53,7 @@ void mc_abort(const char *msg, ...) ...@@ -46,7 +53,7 @@ void mc_abort(const char *msg, ...)
va_list ap; va_list ap;
va_start(ap, msg); va_start(ap, msg);
#if HAVE_VSYSLOG #if HAVE_VSYSLOG
if(_syslog) { if (_syslog) {
vsyslog(LOG_ERR, msg, ap); vsyslog(LOG_ERR, msg, ap);
} else } else
#endif #endif
...@@ -58,10 +65,10 @@ void mc_abort(const char *msg, ...) ...@@ -58,10 +65,10 @@ void mc_abort(const char *msg, ...)
void mc_debug(const char *msg, ...) void mc_debug(const char *msg, ...)
{ {
va_list ap; va_list ap;
if(_debug) { if (_debug) {
va_start(ap, msg); va_start(ap, msg);
#if HAVE_VSYSLOG #if HAVE_VSYSLOG
if(_syslog) { if (_syslog) {
vsyslog(LOG_DEBUG, msg, ap); vsyslog(LOG_DEBUG, msg, ap);
} else } else
#endif #endif
...@@ -74,7 +81,7 @@ void mc_error(const char *msg, ...) ...@@ -74,7 +81,7 @@ void mc_error(const char *msg, ...)
va_list ap; va_list ap;
va_start(ap, msg); va_start(ap, msg);
#if HAVE_VSYSLOG #if HAVE_VSYSLOG
if(_syslog) { if (_syslog) {
vsyslog(LOG_ERR, msg, ap); vsyslog(LOG_ERR, msg, ap);
} else } else
#endif #endif
...@@ -86,7 +93,7 @@ void mc_info(const char *msg, ...) ...@@ -86,7 +93,7 @@ void mc_info(const char *msg, ...)
va_list ap; va_list ap;
va_start(ap, msg); va_start(ap, msg);
#if HAVE_VSYSLOG #if HAVE_VSYSLOG
if(_syslog) { if (_syslog) {
vsyslog(LOG_INFO, msg, ap); vsyslog(LOG_INFO, msg, ap);
} else } else
#endif #endif
......
...@@ -89,7 +89,7 @@ typedef enum accept_states { ...@@ -89,7 +89,7 @@ typedef enum accept_states {
} accept_state_t; } accept_state_t;
typedef struct state_machines { typedef struct state_machines {
accept_state_t (*state)(char, struct state_machines *); accept_state_t(*state) (char, struct state_machines *);
int pos; int pos;
char *buf; char *buf;
ssize_t buf_len; ssize_t buf_len;
...@@ -130,9 +130,9 @@ typedef struct http_responses { ...@@ -130,9 +130,9 @@ typedef struct http_responses {
int body_len; int body_len;
} http_response_t; } http_response_t;
token_t get_next_token(state_machine_t *sm); token_t get_next_token(state_machine_t * sm);
int http_parse_response(char *buf, ssize_t buf_len, http_response_t *response); int http_parse_response(char *buf, ssize_t buf_len, http_response_t * response);
int http_req(http_request_t *req, http_response_t *res); int http_req(http_request_t * req, http_response_t * res);
void free_http_response(http_response_t *response); void free_http_response(http_response_t * response);
#endif #endif
...@@ -27,5 +27,4 @@ extern "C" { ...@@ -27,5 +27,4 @@ extern "C" {
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif
#endif #endif
...@@ -50,7 +50,7 @@ enum json_type { ...@@ -50,7 +50,7 @@ enum json_type {
* Increment the reference count of json_object * Increment the reference count of json_object
* @param obj the json_object instance * @param obj the json_object instance
*/ */
extern struct json_object* json_object_get(struct json_object *obj); extern struct json_object *json_object_get(struct json_object *obj);
/** /**
* Decrement the reference count of json_object and free if it reaches zero * Decrement the reference count of json_object and free if it reaches zero
...@@ -90,7 +90,7 @@ extern enum json_type json_object_get_type(struct json_object *obj); ...@@ -90,7 +90,7 @@ extern enum json_type json_object_get_type(struct json_object *obj);
* @param obj the json_object instance * @param obj the json_object instance
* @returns a string in JSON format * @returns a string in JSON format
*/ */
extern char* json_object_to_json_string(struct json_object *obj); extern char *json_object_to_json_string(struct json_object *obj);
/* object type methods */ /* object type methods */
...@@ -98,13 +98,13 @@ extern char* json_object_to_json_string(struct json_object *obj); ...@@ -98,13 +98,13 @@ extern char* json_object_to_json_string(struct json_object *obj);
/** Create a new empty object /** Create a new empty object
* @returns a json_object of type json_type_object * @returns a json_object of type json_type_object
*/ */
extern struct json_object* json_object_new_object(); extern struct json_object *json_object_new_object();
/** Get the hashtable of a json_object of type json_type_object /** Get the hashtable of a json_object of type json_type_object
* @param obj the json_object instance * @param obj the json_object instance
* @returns a linkhash * @returns a linkhash
*/ */
extern struct lh_table* json_object_get_object(struct json_object *obj); extern struct lh_table *json_object_get_object(struct json_object *obj);
/** Add an object field to a json_object of type json_type_object /** Add an object field to a json_object of type json_type_object
* *
...@@ -116,16 +116,14 @@ extern struct lh_table* json_object_get_object(struct json_object *obj); ...@@ -116,16 +116,14 @@ extern struct lh_table* json_object_get_object(struct json_object *obj);
* @param key the object field name (a private copy will be duplicated) * @param key the object field name (a private copy will be duplicated)
* @param val a json_object or NULL member to associate with the given field * @param val a json_object or NULL member to associate with the given field
*/ */
extern void json_object_object_add(struct json_object* obj, char *key, extern void json_object_object_add(struct json_object *obj, char *key, struct json_object *val);
struct json_object *val);
/** Get the json_object associate with a given object field /** Get the json_object associate with a given object field
* @param obj the json_object instance * @param obj the json_object instance
* @param key the object field name * @param key the object field name
* @returns the json_object associated with the given field name * @returns the json_object associated with the given field name
*/ */
extern struct json_object* json_object_object_get(struct json_object* obj, extern struct json_object *json_object_object_get(struct json_object *obj, char *key);
char *key);
/** Delete the given json_object field /** Delete the given json_object field
* *
...@@ -134,7 +132,7 @@ extern struct json_object* json_object_object_get(struct json_object* obj, ...@@ -134,7 +132,7 @@ extern struct json_object* json_object_object_get(struct json_object* obj,
* @param obj the json_object instance * @param obj the json_object instance
* @param key the object field name * @param key the object field name
*/ */
extern void json_object_object_del(struct json_object* obj, char *key); extern void json_object_object_del(struct json_object *obj, char *key);
/** Iterate through all keys and values of an object /** Iterate through all keys and values of an object
* @param obj the json_object instance * @param obj the json_object instance
...@@ -165,13 +163,13 @@ extern void json_object_object_del(struct json_object* obj, char *key); ...@@ -165,13 +163,13 @@ extern void json_object_object_del(struct json_object* obj, char *key);
/** Create a new empty json_object of type json_type_array /** Create a new empty json_object of type json_type_array
* @returns a json_object of type json_type_array * @returns a json_object of type json_type_array
*/ */
extern struct json_object* json_object_new_array(); extern struct json_object *json_object_new_array();
/** Get the arraylist of a json_object of type json_type_array /** Get the arraylist of a json_object of type json_type_array
* @param obj the json_object instance * @param obj the json_object instance
* @returns an arraylist * @returns an arraylist
*/ */
extern struct array_list* json_object_get_array(struct json_object *obj); extern struct array_list *json_object_get_array(struct json_object *obj);
/** Get the length of a json_object of type json_type_array /** Get the length of a json_object of type json_type_array
* @param obj the json_object instance * @param obj the json_object instance
...@@ -188,8 +186,7 @@ extern int json_object_array_length(struct json_object *obj); ...@@ -188,8 +186,7 @@ extern int json_object_array_length(struct json_object *obj);
* @param obj the json_object instance * @param obj the json_object instance
* @param val the json_object to be added * @param val the json_object to be added
*/ */
extern int json_object_array_add(struct json_object *obj, extern int json_object_array_add(struct json_object *obj, struct json_object *val);
struct json_object *val);
/** Insert or replace an element at a specified index in an array (a json_object of type json_type_array) /** Insert or replace an element at a specified index in an array (a json_object of type json_type_array)
* *
...@@ -206,16 +203,14 @@ extern int json_object_array_add(struct json_object *obj, ...@@ -206,16 +203,14 @@ extern int json_object_array_add(struct json_object *obj,
* @param idx the index to insert the element at * @param idx the index to insert the element at
* @param val the json_object to be added * @param val the json_object to be added
*/ */
extern int json_object_array_put_idx(struct json_object *obj, int idx, extern int json_object_array_put_idx(struct json_object *obj, int idx, struct json_object *val);
struct json_object *val);
/** Get the element at specificed index of the array (a json_object of type json_type_array) /** Get the element at specificed index of the array (a json_object of type json_type_array)
* @param obj the json_object instance * @param obj the json_object instance
* @param idx the index to get the element at * @param idx the index to get the element at
* @returns the json_object at the specified index (or NULL) * @returns the json_object at the specified index (or NULL)
*/ */
extern struct json_object* json_object_array_get_idx(struct json_object *obj, extern struct json_object *json_object_array_get_idx(struct json_object *obj, int idx);
int idx);
/* boolean type methods */ /* boolean type methods */
...@@ -223,7 +218,7 @@ extern struct json_object* json_object_array_get_idx(struct json_object *obj, ...@@ -223,7 +218,7 @@ extern struct json_object* json_object_array_get_idx(struct json_object *obj,
* @param b a boolean TRUE or FALSE (0 or 1) * @param b a boolean TRUE or FALSE (0 or 1)
* @returns a json_object of type json_type_boolean * @returns a json_object of type json_type_boolean
*/ */
extern struct json_object* json_object_new_boolean(boolean b); extern struct json_object *json_object_new_boolean(boolean b);
/** Get the boolean value of a json_object /** Get the boolean value of a json_object
* *
...@@ -245,7 +240,7 @@ extern boolean json_object_get_boolean(struct json_object *obj); ...@@ -245,7 +240,7 @@ extern boolean json_object_get_boolean(struct json_object *obj);
* @param i the integer * @param i the integer
* @returns a json_object of type json_type_int * @returns a json_object of type json_type_int
*/ */
extern struct json_object* json_object_new_int(int i); extern struct json_object *json_object_new_int(int i);
/** Get the int value of a json_object /** Get the int value of a json_object
* *
...@@ -265,7 +260,7 @@ extern int json_object_get_int(struct json_object *obj); ...@@ -265,7 +260,7 @@ extern int json_object_get_int(struct json_object *obj);
* @param d the double * @param d the double
* @returns a json_object of type json_type_double * @returns a json_object of type json_type_double
*/ */
extern struct json_object* json_object_new_double(double d); extern struct json_object *json_object_new_double(double d);
/** Get the double value of a json_object /** Get the double value of a json_object
* *
...@@ -288,9 +283,9 @@ extern double json_object_get_double(struct json_object *obj); ...@@ -288,9 +283,9 @@ extern double json_object_get_double(struct json_object *obj);
* @param s the string * @param s the string
* @returns a json_object of type json_type_string * @returns a json_object of type json_type_string
*/ */
extern struct json_object* json_object_new_string(char *s); extern struct json_object *json_object_new_string(char *s);
extern struct json_object* json_object_new_string_len(char *s, int len); extern struct json_object *json_object_new_string_len(char *s, int len);
/** Get the string value of a json_object /** Get the string value of a json_object
* *
...@@ -303,6 +298,6 @@ extern struct json_object* json_object_new_string_len(char *s, int len); ...@@ -303,6 +298,6 @@ extern struct json_object* json_object_new_string_len(char *s, int len);
* @param obj the json_object instance * @param obj the json_object instance
* @returns a string * @returns a string
*/ */
extern char* json_object_get_string(struct json_object *obj); extern char *json_object_get_string(struct json_object *obj);
#endif #endif
...@@ -12,12 +12,10 @@ ...@@ -12,12 +12,10 @@
#ifndef _json_object_private_h_ #ifndef _json_object_private_h_
#define _json_object_private_h_ #define _json_object_private_h_
typedef void (json_object_delete_fn)(struct json_object *o); typedef void (json_object_delete_fn) (struct json_object * o);
typedef int (json_object_to_json_string_fn)(struct json_object *o, typedef int (json_object_to_json_string_fn) (struct json_object * o, struct printbuf * pb);
struct printbuf *pb);
struct json_object struct json_object {
{
enum json_type o_type; enum json_type o_type;
json_object_delete_fn *_delete; json_object_delete_fn *_delete;
json_object_to_json_string_fn *_to_json_string; json_object_to_json_string_fn *_to_json_string;
...@@ -34,8 +32,7 @@ struct json_object ...@@ -34,8 +32,7 @@ struct json_object
}; };
/* CAW: added for ANSI C iteration correctness */ /* CAW: added for ANSI C iteration correctness */
struct json_object_iter struct json_object_iter {
{
char *key; char *key;
struct json_object *val; struct json_object *val;
struct lh_entry *entry; struct lh_entry *entry;
......
...@@ -56,8 +56,7 @@ enum json_tokener_state { ...@@ -56,8 +56,7 @@ enum json_tokener_state {
json_tokener_state_object_sep json_tokener_state_object_sep
}; };
struct json_tokener_srec struct json_tokener_srec {
{
enum json_tokener_state state, saved_state; enum json_tokener_state state, saved_state;
struct json_object *obj; struct json_object *obj;
struct json_object *current; struct json_object *current;
...@@ -66,8 +65,7 @@ struct json_tokener_srec ...@@ -66,8 +65,7 @@ struct json_tokener_srec
#define JSON_TOKENER_MAX_DEPTH 32 #define JSON_TOKENER_MAX_DEPTH 32
struct json_tokener struct json_tokener {
{
char *str; char *str;
struct printbuf *pb; struct printbuf *pb;
int depth, is_double, st_pos, char_offset; int depth, is_double, st_pos, char_offset;
...@@ -77,13 +75,12 @@ struct json_tokener ...@@ -77,13 +75,12 @@ struct json_tokener
struct json_tokener_srec stack[JSON_TOKENER_MAX_DEPTH]; struct json_tokener_srec stack[JSON_TOKENER_MAX_DEPTH];
}; };
extern const char* json_tokener_errors[]; extern const char *json_tokener_errors[];
extern struct json_tokener* json_tokener_new(); extern struct json_tokener *json_tokener_new();
extern void json_tokener_free(struct json_tokener *tok); extern void json_tokener_free(struct json_tokener *tok);
extern void json_tokener_reset(struct json_tokener *tok); extern void json_tokener_reset(struct json_tokener *tok);
extern struct json_object* json_tokener_parse(char *str); extern struct json_object *json_tokener_parse(char *str);
extern struct json_object* json_tokener_parse_ex(struct json_tokener *tok, extern struct json_object *json_tokener_parse_ex(struct json_tokener *tok, char *str, int len);
char *str, int len);
#endif #endif
...@@ -51,29 +51,27 @@ ...@@ -51,29 +51,27 @@
#include "json_tokener.h" #include "json_tokener.h"
#include "json_util.h" #include "json_util.h"
struct json_object* json_object_from_file(char *filename) struct json_object *json_object_from_file(char *filename)
{ {
struct printbuf *pb; struct printbuf *pb;
struct json_object *obj; struct json_object *obj;
char buf[JSON_FILE_BUF_SIZE]; char buf[JSON_FILE_BUF_SIZE];
int fd, ret; int fd, ret;
if((fd = open(filename, O_RDONLY)) < 0) { if ((fd = open(filename, O_RDONLY)) < 0) {
mc_error("json_object_from_file: error reading file %s: %s\n", mc_error("json_object_from_file: error reading file %s: %s\n", filename, strerror(errno));
filename, strerror(errno));
return error_ptr(-1); return error_ptr(-1);
} }
if(!(pb = printbuf_new())) { if (!(pb = printbuf_new())) {
mc_error("json_object_from_file: printbuf_new failed\n"); mc_error("json_object_from_file: printbuf_new failed\n");
return error_ptr(-1); return error_ptr(-1);
} }
while((ret = read(fd, buf, JSON_FILE_BUF_SIZE)) > 0) { while ((ret = read(fd, buf, JSON_FILE_BUF_SIZE)) > 0) {
printbuf_memappend(pb, buf, ret); printbuf_memappend(pb, buf, ret);
} }
close(fd); close(fd);
if(ret < 0) { if (ret < 0) {
mc_abort("json_object_from_file: error reading file %s: %s\n", mc_abort("json_object_from_file: error reading file %s: %s\n", filename, strerror(errno));
filename, strerror(errno));
printbuf_free(pb); printbuf_free(pb);
return error_ptr(-1); return error_ptr(-1);
} }
...@@ -88,32 +86,32 @@ int json_object_to_file(char *filename, struct json_object *obj) ...@@ -88,32 +86,32 @@ int json_object_to_file(char *filename, struct json_object *obj)
int fd, ret; int fd, ret;
unsigned int wpos, wsize; unsigned int wpos, wsize;
if(!obj) { if (!obj) {
mc_error("json_object_to_file: object is null\n"); mc_error("json_object_to_file: object is null\n");
return -1; return -1;
} }
if((fd = open(filename, O_WRONLY | O_TRUNC | O_CREAT, 0644)) < 0) { if ((fd = open(filename, O_WRONLY | O_TRUNC | O_CREAT, 0644)) < 0) {
mc_error("json_object_to_file: error opening file %s: %s\n", mc_error("json_object_to_file: error opening file %s: %s\n", filename, strerror(errno));
filename, strerror(errno));
return -1; return -1;
} }
if(!(json_str = json_object_to_json_string(obj))) { return -1; } if (!(json_str = json_object_to_json_string(obj))) {
return -1;
}
wsize = (unsigned int)(strlen(json_str) & UINT_MAX); /* CAW: probably unnecessary, but the most 64bit safe */ wsize = (unsigned int) (strlen(json_str) & UINT_MAX); /* CAW: probably unnecessary, but the most 64bit safe */
wpos = 0; wpos = 0;
while(wpos < wsize) { while (wpos < wsize) {
if((ret = write(fd, json_str + wpos, wsize-wpos)) < 0) { if ((ret = write(fd, json_str + wpos, wsize - wpos)) < 0) {
close(fd); close(fd);
mc_error("json_object_to_file: error writing file %s: %s\n", mc_error("json_object_to_file: error writing file %s: %s\n", filename, strerror(errno));
filename, strerror(errno));
return -1; return -1;
} }
/* because of the above check for ret < 0, we can safely cast and add */ /* because of the above check for ret < 0, we can safely cast and add */
wpos += (unsigned int)ret; wpos += (unsigned int) ret;
} }
close(fd); close(fd);
......
...@@ -17,7 +17,7 @@ ...@@ -17,7 +17,7 @@
#define JSON_FILE_BUF_SIZE 4096 #define JSON_FILE_BUF_SIZE 4096
/* utlitiy functions */ /* utlitiy functions */
extern struct json_object* json_object_from_file(char *filename); extern struct json_object *json_object_from_file(char *filename);
extern int json_object_to_file(char *filename, struct json_object *obj); extern int json_object_to_file(char *filename, struct json_object *obj);
#endif #endif
...@@ -31,7 +31,7 @@ void lh_abort(const char *msg, ...) ...@@ -31,7 +31,7 @@ void lh_abort(const char *msg, ...)
uint32_t lh_ptr_hash(void *k) uint32_t lh_ptr_hash(void *k)
{ {
/* CAW: refactored to be 64bit nice */ /* CAW: refactored to be 64bit nice */
return (uint32_t)((((ptrdiff_t)k * LH_PRIME) >> 4) & ULONG_MAX); return (uint32_t) ((((ptrdiff_t) k * LH_PRIME) >> 4) & ULONG_MAX);
} }
int lh_ptr_equal(void *k1, void *k2) int lh_ptr_equal(void *k1, void *k2)
...@@ -42,48 +42,47 @@ int lh_ptr_equal(void *k1, void *k2) ...@@ -42,48 +42,47 @@ int lh_ptr_equal(void *k1, void *k2)
uint32_t lh_char_hash(void *k) uint32_t lh_char_hash(void *k)
{ {
unsigned int h = 0; unsigned int h = 0;
const char* data = k; const char *data = k;
while( *data!=0 ) h = h*129 + (unsigned int)(*data++) + LH_PRIME; while (*data != 0)
h = h * 129 + (unsigned int) (*data++) + LH_PRIME;
return h; return h;
} }
int lh_char_equal(void *k1, void *k2) int lh_char_equal(void *k1, void *k2)
{ {
return (strcmp((char*)k1, (char*)k2) == 0); return (strcmp((char *) k1, (char *) k2) == 0);
} }
struct lh_table* lh_table_new(int size, char *name, struct lh_table *lh_table_new(int size, char *name, lh_entry_free_fn * free_fn, lh_hash_fn * hash_fn, lh_equal_fn * equal_fn)
lh_entry_free_fn *free_fn,
lh_hash_fn *hash_fn,
lh_equal_fn *equal_fn)
{ {
int i; int i;
struct lh_table *t; struct lh_table *t;
t = calloc(1, sizeof(struct lh_table)); t = calloc(1, sizeof(struct lh_table));
if(!t) lh_abort("lh_table_new: calloc failed\n"); if (!t)
lh_abort("lh_table_new: calloc failed\n");
t->count = 0; t->count = 0;
t->size = size; t->size = size;
t->name = name; t->name = name;
t->table = calloc(size, sizeof(struct lh_entry)); t->table = calloc(size, sizeof(struct lh_entry));
if(!t->table) lh_abort("lh_table_new: calloc failed\n"); if (!t->table)
lh_abort("lh_table_new: calloc failed\n");
t->free_fn = free_fn; t->free_fn = free_fn;
t->hash_fn = hash_fn; t->hash_fn = hash_fn;
t->equal_fn = equal_fn; t->equal_fn = equal_fn;
for(i = 0; i < size; i++) t->table[i].k = LH_EMPTY; for (i = 0; i < size; i++)
t->table[i].k = LH_EMPTY;
return t; return t;
} }
struct lh_table* lh_kchar_table_new(int size, char *name, struct lh_table *lh_kchar_table_new(int size, char *name, lh_entry_free_fn * free_fn)
lh_entry_free_fn *free_fn)
{ {
return lh_table_new(size, name, free_fn, lh_char_hash, lh_char_equal); return lh_table_new(size, name, free_fn, lh_char_hash, lh_char_equal);
} }
struct lh_table* lh_kptr_table_new(int size, char *name, struct lh_table *lh_kptr_table_new(int size, char *name, lh_entry_free_fn * free_fn)
lh_entry_free_fn *free_fn)
{ {
return lh_table_new(size, name, free_fn, lh_ptr_hash, lh_ptr_equal); return lh_table_new(size, name, free_fn, lh_ptr_hash, lh_ptr_equal);
} }
...@@ -95,7 +94,7 @@ void lh_table_resize(struct lh_table *t, int new_size) ...@@ -95,7 +94,7 @@ void lh_table_resize(struct lh_table *t, int new_size)
new_t = lh_table_new(new_size, t->name, NULL, t->hash_fn, t->equal_fn); new_t = lh_table_new(new_size, t->name, NULL, t->hash_fn, t->equal_fn);
ent = t->head; ent = t->head;
while(ent) { while (ent) {
lh_table_insert(new_t, ent->k, ent->v); lh_table_insert(new_t, ent->k, ent->v);
ent = ent->next; ent = ent->next;
} }
...@@ -111,8 +110,8 @@ void lh_table_resize(struct lh_table *t, int new_size) ...@@ -111,8 +110,8 @@ void lh_table_resize(struct lh_table *t, int new_size)
void lh_table_free(struct lh_table *t) void lh_table_free(struct lh_table *t)
{ {
struct lh_entry *c; struct lh_entry *c;
for(c = t->head; c != NULL; c = c->next) { for (c = t->head; c != NULL; c = c->next) {
if(t->free_fn) { if (t->free_fn) {
t->free_fn(c); t->free_fn(c);
} }
} }
...@@ -126,22 +125,25 @@ int lh_table_insert(struct lh_table *t, void *k, void *v) ...@@ -126,22 +125,25 @@ int lh_table_insert(struct lh_table *t, void *k, void *v)
uint32_t h, n; uint32_t h, n;
t->inserts++; t->inserts++;
if(t->count > t->size * 0.66) lh_table_resize(t, t->size * 2); if (t->count > t->size * 0.66)
lh_table_resize(t, t->size * 2);
h = t->hash_fn(k); h = t->hash_fn(k);
n = h % t->size; n = h % t->size;
while( 1 ) { while (1) {
if(t->table[n].k == LH_EMPTY || t->table[n].k == LH_FREED) break; if (t->table[n].k == LH_EMPTY || t->table[n].k == LH_FREED)
break;
t->collisions++; t->collisions++;
if(++n == t->size) n = 0; if (++n == t->size)
n = 0;
} }
t->table[n].k = k; t->table[n].k = k;
t->table[n].v = v; t->table[n].v = v;
t->count++; t->count++;
if(t->head == NULL) { if (t->head == NULL) {
t->head = t->tail = &t->table[n]; t->head = t->tail = &t->table[n];
t->table[n].next = t->table[n].prev = NULL; t->table[n].next = t->table[n].prev = NULL;
} else { } else {
...@@ -155,43 +157,50 @@ int lh_table_insert(struct lh_table *t, void *k, void *v) ...@@ -155,43 +157,50 @@ int lh_table_insert(struct lh_table *t, void *k, void *v)
} }
struct lh_entry* lh_table_lookup_entry(struct lh_table *t, void *k) struct lh_entry *lh_table_lookup_entry(struct lh_table *t, void *k)
{ {
uint32_t h = t->hash_fn(k); uint32_t h = t->hash_fn(k);
uint32_t n = h % t->size; uint32_t n = h % t->size;
t->lookups++; t->lookups++;
while( 1 ) { while (1) {
if(t->table[n].k == LH_EMPTY) return NULL; if (t->table[n].k == LH_EMPTY)
if(t->table[n].k != LH_FREED && return NULL;
t->equal_fn(t->table[n].k, k)) return &t->table[n]; if (t->table[n].k != LH_FREED && t->equal_fn(t->table[n].k, k))
if(++n == t->size) n = 0; return &t->table[n];
if (++n == t->size)
n = 0;
} }
return NULL; return NULL;
} }
void* lh_table_lookup(struct lh_table *t, void *k) void *lh_table_lookup(struct lh_table *t, void *k)
{ {
struct lh_entry *e = lh_table_lookup_entry(t, k); struct lh_entry *e = lh_table_lookup_entry(t, k);
if(e) return e->v; if (e)
return e->v;
return NULL; return NULL;
} }
int lh_table_delete_entry(struct lh_table *t, struct lh_entry *e) int lh_table_delete_entry(struct lh_table *t, struct lh_entry *e)
{ {
ptrdiff_t n = (ptrdiff_t)(e - t->table); /* CAW: fixed to be 64bit nice, still need the crazy negative case... */ ptrdiff_t n = (ptrdiff_t) (e - t->table); /* CAW: fixed to be 64bit nice, still need the crazy negative case... */
/* CAW: this is bad, really bad, maybe stack goes other direction on this machine... */ /* CAW: this is bad, really bad, maybe stack goes other direction on this machine... */
if(n < 0) { return -2; } if (n < 0) {
return -2;
}
if(t->table[n].k == LH_EMPTY || t->table[n].k == LH_FREED) return -1; if (t->table[n].k == LH_EMPTY || t->table[n].k == LH_FREED)
return -1;
t->count--; t->count--;
if(t->free_fn) t->free_fn(e); if (t->free_fn)
t->free_fn(e);
t->table[n].v = NULL; t->table[n].v = NULL;
t->table[n].k = LH_FREED; t->table[n].k = LH_FREED;
if(t->tail == &t->table[n] && t->head == &t->table[n]) { if (t->tail == &t->table[n] && t->head == &t->table[n]) {
t->head = t->tail = NULL; t->head = t->tail = NULL;
} else if (t->head == &t->table[n]) { } else if (t->head == &t->table[n]) {
t->head->next->prev = NULL; t->head->next->prev = NULL;
...@@ -211,7 +220,7 @@ int lh_table_delete_entry(struct lh_table *t, struct lh_entry *e) ...@@ -211,7 +220,7 @@ int lh_table_delete_entry(struct lh_table *t, struct lh_entry *e)
int lh_table_delete(struct lh_table *t, void *k) int lh_table_delete(struct lh_table *t, void *k)
{ {
struct lh_entry *e = lh_table_lookup_entry(t, k); struct lh_entry *e = lh_table_lookup_entry(t, k);
if(!e) return -1; if (!e)
return -1;
return lh_table_delete_entry(t, e); return lh_table_delete_entry(t, e);
} }
...@@ -33,11 +33,11 @@ struct lh_entry; ...@@ -33,11 +33,11 @@ struct lh_entry;
/** /**
* callback function prototypes * callback function prototypes
*/ */
typedef void (lh_entry_free_fn) (struct lh_entry *e); typedef void (lh_entry_free_fn) (struct lh_entry * e);
/** /**
* callback function prototypes * callback function prototypes
*/ */
typedef uint32_t (lh_hash_fn) (void *k); typedef uint32_t(lh_hash_fn) (void *k);
/** /**
* callback function prototypes * callback function prototypes
*/ */
...@@ -171,10 +171,7 @@ for(entry = table->head; entry && ((tmp = entry->next) || 1); entry = tmp) ...@@ -171,10 +171,7 @@ for(entry = table->head; entry && ((tmp = entry->next) || 1); entry = tmp)
* and C strings respectively. * and C strings respectively.
* @return a pointer onto the linkhash table. * @return a pointer onto the linkhash table.
*/ */
extern struct lh_table* lh_table_new(int size, char *name, extern struct lh_table *lh_table_new(int size, char *name, lh_entry_free_fn * free_fn, lh_hash_fn * hash_fn, lh_equal_fn * equal_fn);
lh_entry_free_fn *free_fn,
lh_hash_fn *hash_fn,
lh_equal_fn *equal_fn);
/** /**
* Convenience function to create a new linkhash * Convenience function to create a new linkhash
...@@ -184,8 +181,7 @@ extern struct lh_table* lh_table_new(int size, char *name, ...@@ -184,8 +181,7 @@ extern struct lh_table* lh_table_new(int size, char *name,
* @param free_fn callback function used to free memory for entries. * @param free_fn callback function used to free memory for entries.
* @return a pointer onto the linkhash table. * @return a pointer onto the linkhash table.
*/ */
extern struct lh_table* lh_kchar_table_new(int size, char *name, extern struct lh_table *lh_kchar_table_new(int size, char *name, lh_entry_free_fn * free_fn);
lh_entry_free_fn *free_fn);
/** /**
...@@ -196,8 +192,7 @@ extern struct lh_table* lh_kchar_table_new(int size, char *name, ...@@ -196,8 +192,7 @@ extern struct lh_table* lh_kchar_table_new(int size, char *name,
* @param free_fn callback function used to free memory for entries. * @param free_fn callback function used to free memory for entries.
* @return a pointer onto the linkhash table. * @return a pointer onto the linkhash table.
*/ */
extern struct lh_table* lh_kptr_table_new(int size, char *name, extern struct lh_table *lh_kptr_table_new(int size, char *name, lh_entry_free_fn * free_fn);
lh_entry_free_fn *free_fn);
/** /**
...@@ -224,7 +219,7 @@ extern int lh_table_insert(struct lh_table *t, void *k, void *v); ...@@ -224,7 +219,7 @@ extern int lh_table_insert(struct lh_table *t, void *k, void *v);
* @param k a pointer to the key to lookup * @param k a pointer to the key to lookup
* @return a pointer to the record structure of the value or NULL if it does not exist. * @return a pointer to the record structure of the value or NULL if it does not exist.
*/ */
extern struct lh_entry* lh_table_lookup_entry(struct lh_table *t, void *k); extern struct lh_entry *lh_table_lookup_entry(struct lh_table *t, void *k);
/** /**
* Lookup a record into the table * Lookup a record into the table
...@@ -232,7 +227,7 @@ extern struct lh_entry* lh_table_lookup_entry(struct lh_table *t, void *k); ...@@ -232,7 +227,7 @@ extern struct lh_entry* lh_table_lookup_entry(struct lh_table *t, void *k);
* @param k a pointer to the key to lookup * @param k a pointer to the key to lookup
* @return a pointer to the found value or NULL if it does not exist. * @return a pointer to the found value or NULL if it does not exist.
*/ */
extern void* lh_table_lookup(struct lh_table *t, void *k); extern void *lh_table_lookup(struct lh_table *t, void *k);
/** /**
......
...@@ -122,9 +122,9 @@ SWITCH_STANDARD_API(http_api_main) ...@@ -122,9 +122,9 @@ SWITCH_STANDARD_API(http_api_main)
GARBAGE_INIT(); GARBAGE_INIT();
(void)memset(&response, 0, sizeof(response)); (void) memset(&response, 0, sizeof(response));
if(cmd == NULL){ if (cmd == NULL) {
stream->write_function(stream, "-USAGE: %s\n", HTTP_SYNTAX); stream->write_function(stream, "-USAGE: %s\n", HTTP_SYNTAX);
return SWITCH_STATUS_SUCCESS; return SWITCH_STATUS_SUCCESS;
} }
...@@ -132,28 +132,33 @@ SWITCH_STANDARD_API(http_api_main) ...@@ -132,28 +132,33 @@ SWITCH_STANDARD_API(http_api_main)
ccmd = strdup(cmd); ccmd = strdup(cmd);
argc = switch_separate_string(ccmd, ' ', argv, HTTP_PARAMS); argc = switch_separate_string(ccmd, ' ', argv, HTTP_PARAMS);
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\n");
return SWITCH_STATUS_SUCCESS; return SWITCH_STATUS_SUCCESS;
} }
method = argv[0]; method = argv[0];
if(strcasecmp( "GET", method) == 0) request.method = GET; if (strcasecmp("GET", method) == 0)
else if(strcasecmp("POST", method) == 0) request.method = POST; request.method = GET;
else if(strcasecmp("HEAD", method) == 0) request.method = HEAD; else if (strcasecmp("POST", method) == 0)
else if(strcasecmp("DELETE", method) == 0) request.method = DELETE; request.method = POST;
else if(strcasecmp("PUT", method) == 0) request.method = PUT; 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) {
body = argv[3]; body = argv[3];
}else{ } else {
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\n");
return SWITCH_STATUS_SUCCESS; return SWITCH_STATUS_SUCCESS;
...@@ -162,8 +167,8 @@ SWITCH_STANDARD_API(http_api_main) ...@@ -162,8 +167,8 @@ SWITCH_STANDARD_API(http_api_main)
GARBAGE_ADD(body); GARBAGE_ADD(body);
} }
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\n");
GARBAGE_CLEANUP(); GARBAGE_CLEANUP();
...@@ -174,8 +179,8 @@ SWITCH_STANDARD_API(http_api_main) ...@@ -174,8 +179,8 @@ SWITCH_STANDARD_API(http_api_main)
request.version = DEFAULT_HTTP_VERSION; request.version = DEFAULT_HTTP_VERSION;
l = strlen(url); l = strlen(url);
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\n");
GARBAGE_CLEANUP(); GARBAGE_CLEANUP();
...@@ -192,7 +197,7 @@ SWITCH_STANDARD_API(http_api_main) ...@@ -192,7 +197,7 @@ SWITCH_STANDARD_API(http_api_main)
GARBAGE_ADD(headers_dec); GARBAGE_ADD(headers_dec);
json_http_headers = json_tokener_parse(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\n");
GARBAGE_CLEANUP(); GARBAGE_CLEANUP();
...@@ -200,7 +205,7 @@ SWITCH_STANDARD_API(http_api_main) ...@@ -200,7 +205,7 @@ SWITCH_STANDARD_API(http_api_main)
} }
jsontype = json_object_get_type(json_http_headers); jsontype = json_object_get_type(json_http_headers);
if(jsontype != json_type_object){ if (jsontype != json_type_object) {
switch_safe_free(ccmd); switch_safe_free(ccmd);
stream->write_function(stream, "-ERR\n"); stream->write_function(stream, "-ERR\n");
GARBAGE_CLEANUP(); GARBAGE_CLEANUP();
...@@ -208,21 +213,21 @@ SWITCH_STANDARD_API(http_api_main) ...@@ -208,21 +213,21 @@ SWITCH_STANDARD_API(http_api_main)
} }
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; 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\n");
GARBAGE_CLEANUP(); GARBAGE_CLEANUP();
...@@ -235,7 +240,7 @@ SWITCH_STANDARD_API(http_api_main) ...@@ -235,7 +240,7 @@ SWITCH_STANDARD_API(http_api_main)
a += strlen(key); a += strlen(key);
jsontype = json_object_get_type(val); jsontype = json_object_get_type(val);
if(jsontype != json_type_string){ if (jsontype != json_type_string) {
switch_safe_free(ccmd); switch_safe_free(ccmd);
stream->write_function(stream, "-ERR\n"); stream->write_function(stream, "-ERR\n");
GARBAGE_CLEANUP(); GARBAGE_CLEANUP();
...@@ -245,8 +250,8 @@ SWITCH_STANDARD_API(http_api_main) ...@@ -245,8 +250,8 @@ SWITCH_STANDARD_API(http_api_main)
value = json_object_get_string(val); value = json_object_get_string(val);
/* value = json_object_to_json_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\n");
GARBAGE_CLEANUP(); GARBAGE_CLEANUP();
...@@ -258,28 +263,33 @@ SWITCH_STANDARD_API(http_api_main) ...@@ -258,28 +263,33 @@ SWITCH_STANDARD_API(http_api_main)
i++; i++;
} }
if(argc == HTTP_PARAMS){ if (argc == HTTP_PARAMS) {
l = strlen(body); l = strlen(body);
body_dec = url_decode(body, l); body_dec = url_decode(body, l);
GARBAGE_ADD(body_dec); GARBAGE_ADD(body_dec);
l = strlen(body_dec); l = strlen(body_dec);
request.body_len = l; request.body_len = l;
request.body = body_dec; request.body = body_dec;
}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)
if(response.phrase != NULL) GARBAGE_ADD(response.phrase); GARBAGE_ADD(response.version);
if(response.headers != NULL) GARBAGE_ADD(response.headers); if (response.phrase != NULL)
if(response.body != NULL) GARBAGE_ADD(response.body); GARBAGE_ADD(response.phrase);
for(i = 0; i < response.header_len; i++){ if (response.headers != NULL)
GARBAGE_ADD(response.headers);
if (response.body != NULL)
GARBAGE_ADD(response.body);
for (i = 0; i < response.header_len; i++) {
GARBAGE_ADD(response.headers[i].field_name); GARBAGE_ADD(response.headers[i].field_name);
GARBAGE_ADD(response.headers[i].value); GARBAGE_ADD(response.headers[i].value);
} }
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\n");
GARBAGE_CLEANUP(); GARBAGE_CLEANUP();
...@@ -289,17 +299,15 @@ SWITCH_STANDARD_API(http_api_main) ...@@ -289,17 +299,15 @@ SWITCH_STANDARD_API(http_api_main)
/* This is evil and should be changed in the future */ /* This is evil and should be changed in the future */
l = 128 + (256 * response.header_len) + (a * 2) l = 128 + (256 * response.header_len) + (a * 2)
+ strlen("version") + strlen(response.version) + strlen("version") + strlen(response.version)
+ strlen("status_code") + 3 + strlen("status_code") + 3 + strlen("phrase") + strlen(response.phrase)
+ strlen("phrase") + strlen(response.phrase) + strlen("body") + (response.body_len * 3) + 1 + strlen("headers")
+ strlen("body") + (response.body_len * 3) + 1
+ strlen("headers")
+ 1; + 1;
/* to be safe */ /* to be safe */
l <<= 2; l <<= 2;
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\n");
GARBAGE_CLEANUP(); GARBAGE_CLEANUP();
...@@ -307,16 +315,16 @@ SWITCH_STANDARD_API(http_api_main) ...@@ -307,16 +315,16 @@ SWITCH_STANDARD_API(http_api_main)
} }
GARBAGE_ADD(json_response); GARBAGE_ADD(json_response);
if(response.body_len != 0){ if (response.body_len != 0) {
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\n");
GARBAGE_CLEANUP(); GARBAGE_CLEANUP();
return SWITCH_STATUS_SUCCESS; return SWITCH_STATUS_SUCCESS;
} }
GARBAGE_ADD(t); GARBAGE_ADD(t);
(void)memcpy(t, response.body, response.body_len); (void) memcpy(t, response.body, response.body_len);
t[response.body_len] = '\0'; t[response.body_len] = '\0';
response.body = url_encode(t, response.body_len); response.body = url_encode(t, response.body_len);
GARBAGE_ADD(response.body); GARBAGE_ADD(response.body);
...@@ -329,26 +337,17 @@ SWITCH_STANDARD_API(http_api_main) ...@@ -329,26 +337,17 @@ SWITCH_STANDARD_API(http_api_main)
"\"status_code\": \"%3d\"," "\"status_code\": \"%3d\","
"\"phrase\": \"%s\"," "\"phrase\": \"%s\","
"\"body\": \"%s\"," "\"body\": \"%s\","
"\"headers\": [", "\"headers\": [", response.version, response.status_code, response.phrase, ((response.body_len <= 0) ? "" : response.body)
response.version,
response.status_code,
response.phrase,
((response.body_len <= 0)? "":response.body)
); );
for(f = HTTP_FALSE, j = 0; j < response.header_len; j++){ for (f = HTTP_FALSE, j = 0; j < response.header_len; j++) {
if(f != HTTP_FALSE){ if (f != HTTP_FALSE) {
m += snprintf(json_response + m, l - m, m += snprintf(json_response + m, l - m, ",");
"," } else
); f = HTTP_TRUE;
}else f = HTTP_TRUE;
m += snprintf(json_response + m, l - m, m += snprintf(json_response + m, l - m, "{\"key\": \"%s\",\"value\": \"%s\"}", response.headers[j].field_name, response.headers[j].value);
"{\"key\": \"%s\",\"value\": \"%s\"}",
response.headers[j].field_name,
response.headers[j].value
);
} }
...@@ -356,14 +355,7 @@ SWITCH_STANDARD_API(http_api_main) ...@@ -356,14 +355,7 @@ SWITCH_STANDARD_API(http_api_main)
json_response[m] = '\0'; json_response[m] = '\0';
switch_log_printf( switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "RESERVED %ld BYTES, USED %ld BYTES, HTTP Response as JSON: %s\n", l, m, json_response);
SWITCH_CHANNEL_LOG,
SWITCH_LOG_NOTICE,
"RESERVED %ld BYTES, USED %ld BYTES, HTTP Response as JSON: %s\n",
l,
m,
json_response
);
stream->write_function(stream, "%s\n", json_response); stream->write_function(stream, "%s\n", json_response);
...@@ -380,23 +372,11 @@ SWITCH_MODULE_LOAD_FUNCTION(mod_http_load) ...@@ -380,23 +372,11 @@ SWITCH_MODULE_LOAD_FUNCTION(mod_http_load)
{ {
switch_api_interface_t *api_interface; switch_api_interface_t *api_interface;
*module_interface = *module_interface = switch_loadable_module_create_module_interface(pool, modname);
switch_loadable_module_create_module_interface(pool, modname);
switch_log_printf( switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "HTTP request mod enabled\n");
SWITCH_CHANNEL_LOG,
SWITCH_LOG_NOTICE,
"HTTP request mod enabled\n"
);
SWITCH_ADD_API( SWITCH_ADD_API(api_interface, "http", "Make HTTP requests", http_api_main, HTTP_SYNTAX);
api_interface,
"http",
"Make HTTP requests",
http_api_main,
HTTP_SYNTAX
);
return SWITCH_STATUS_SUCCESS; return SWITCH_STATUS_SUCCESS;
} }
...@@ -25,14 +25,15 @@ ...@@ -25,14 +25,15 @@
#include "debug.h" #include "debug.h"
#include "printbuf.h" #include "printbuf.h"
struct printbuf* printbuf_new() struct printbuf *printbuf_new()
{ {
struct printbuf *p; struct printbuf *p;
if(!(p = calloc(1, sizeof(struct printbuf)))) return NULL; if (!(p = calloc(1, sizeof(struct printbuf))))
return NULL;
p->size = 32; p->size = 32;
p->bpos = 0; p->bpos = 0;
if(!(p->buf = malloc(p->size))) { if (!(p->buf = malloc(p->size))) {
free(p); free(p);
return NULL; return NULL;
} }
...@@ -43,20 +44,19 @@ struct printbuf* printbuf_new() ...@@ -43,20 +44,19 @@ struct printbuf* printbuf_new()
int printbuf_memappend(struct printbuf *p, char *buf, int size) int printbuf_memappend(struct printbuf *p, char *buf, int size)
{ {
char *t; char *t;
if(p->size - p->bpos <= size) { if (p->size - p->bpos <= size) {
int new_size = max(p->size * 2, p->bpos + size + 8); int new_size = max(p->size * 2, p->bpos + size + 8);
#ifdef PRINTBUF_DEBUG #ifdef PRINTBUF_DEBUG
mc_debug("printbuf_memappend: realloc " mc_debug("printbuf_memappend: realloc " "bpos=%d wrsize=%d old_size=%d new_size=%d\n", p->bpos, size, p->size, new_size);
"bpos=%d wrsize=%d old_size=%d new_size=%d\n",
p->bpos, size, p->size, new_size);
#endif /* PRINTBUF_DEBUG */ #endif /* PRINTBUF_DEBUG */
if(!(t = realloc(p->buf, new_size))) return -1; if (!(t = realloc(p->buf, new_size)))
return -1;
p->size = new_size; p->size = new_size;
p->buf = t; p->buf = t;
} }
memcpy(p->buf + p->bpos, buf, size); memcpy(p->buf + p->bpos, buf, size);
p->bpos += size; p->bpos += size;
p->buf[p->bpos]= '\0'; p->buf[p->bpos] = '\0';
return size; return size;
} }
...@@ -76,22 +76,26 @@ static int vasprintf(char **buf, const char *fmt, va_list ap) ...@@ -76,22 +76,26 @@ static int vasprintf(char **buf, const char *fmt, va_list ap)
int chars; int chars;
char *b; char *b;
if(!buf) { return -1; } if (!buf) {
return -1;
}
#ifdef WIN32 #ifdef WIN32
chars = _vscprintf(fmt, ap)+1; chars = _vscprintf(fmt, ap) + 1;
#else /* !defined(WIN32) */ #else /* !defined(WIN32) */
/* CAW: RAWR! We have to hope to god here that vsnprintf doesn't overwrite /* CAW: RAWR! We have to hope to god here that vsnprintf doesn't overwrite
our buffer like on some 64bit sun systems.... but hey, its time to move on */ our buffer like on some 64bit sun systems.... but hey, its time to move on */
chars = vsnprintf(&_T_emptybuffer, 0, fmt, ap)+1; chars = vsnprintf(&_T_emptybuffer, 0, fmt, ap) + 1;
if(chars < 0) { chars *= -1; } /* CAW: old glibc versions have this problem */ if (chars < 0) {
chars *= -1;
} /* CAW: old glibc versions have this problem */
#endif /* defined(WIN32) */ #endif /* defined(WIN32) */
b = (char*)malloc(sizeof(char)*chars); b = (char *) malloc(sizeof(char) * chars);
if(!b) { return -1; } if (!b) {
return -1;
}
if((chars = vsprintf(b, fmt, ap)) < 0) if ((chars = vsprintf(b, fmt, ap)) < 0) {
{
free(b); free(b);
} else { } else {
*buf = b; *buf = b;
...@@ -116,10 +120,11 @@ int sprintbuf(struct printbuf *p, const char *msg, ...) ...@@ -116,10 +120,11 @@ int sprintbuf(struct printbuf *p, const char *msg, ...)
with vasprintf. Note: some implementation of vsnprintf return -1 with vasprintf. Note: some implementation of vsnprintf return -1
if output is truncated whereas some return the number of bytes that if output is truncated whereas some return the number of bytes that
would have been writen - this code handles both cases. */ would have been writen - this code handles both cases. */
if(size == -1 || size > 127) { if (size == -1 || size > 127) {
int ret; int ret;
va_start(ap, msg); va_start(ap, msg);
if((size = vasprintf(&t, msg, ap)) == -1) return -1; if ((size = vasprintf(&t, msg, ap)) == -1)
return -1;
va_end(ap); va_end(ap);
ret = printbuf_memappend(p, t, size); ret = printbuf_memappend(p, t, size);
free(t); free(t);
...@@ -137,7 +142,7 @@ void printbuf_reset(struct printbuf *p) ...@@ -137,7 +142,7 @@ void printbuf_reset(struct printbuf *p)
void printbuf_free(struct printbuf *p) void printbuf_free(struct printbuf *p)
{ {
if(p) { if (p) {
free(p->buf); free(p->buf);
free(p); free(p);
} }
......
...@@ -20,19 +20,14 @@ struct printbuf { ...@@ -20,19 +20,14 @@ struct printbuf {
int size; int size;
}; };
extern struct printbuf* extern struct printbuf *printbuf_new();
printbuf_new();
extern int extern int printbuf_memappend(struct printbuf *p, char *buf, int size);
printbuf_memappend(struct printbuf *p, char *buf, int size);
extern int extern int sprintbuf(struct printbuf *p, const char *msg, ...);
sprintbuf(struct printbuf *p, const char *msg, ...);
extern void extern void printbuf_reset(struct printbuf *p);
printbuf_reset(struct printbuf *p);
extern void extern void printbuf_free(struct printbuf *p);
printbuf_free(struct printbuf *p);
#endif #endif
...@@ -53,24 +53,23 @@ char *url_encode(char *url, size_t l) ...@@ -53,24 +53,23 @@ char *url_encode(char *url, size_t l)
char *buf; char *buf;
unsigned char c; unsigned char c;
buf = (char *)malloc((l * 3) + 1); buf = (char *) malloc((l * 3) + 1);
if(buf == NULL){ if (buf == NULL) {
perror("Could not allocate memory url encoding"); perror("Could not allocate memory url encoding");
return NULL; return NULL;
} }
for(i = 0, j = 0; i < l; i++){ for (i = 0, j = 0; i < l; i++) {
c = (unsigned char)url[i]; c = (unsigned char) url[i];
if(c <= 31 || c >= 127 if (c <= 31 || c >= 127
|| c == '$' || c == '&' || c == '+' || c == ',' || c == '/' || c == '$' || c == '&' || c == '+' || c == ',' || c == '/'
|| c == ':' || c == ';' || c == '=' || c == '?' || c == '@' || c == ':' || c == ';' || c == '=' || c == '?' || c == '@'
|| c == ' ' || c == '"' || c == '<' || c == '>' || c == '#' || c == ' ' || c == '"' || c == '<' || c == '>' || c == '#'
|| c == '%' || c == '{' || c == '}' || c == '|' || c == '\\' || c == '%' || c == '{' || c == '}' || c == '|' || c == '\\' || c == '^' || c == '~' || c == '[' || c == ']' || c == '`') {
|| c == '^' || c == '~' || c == '[' || c == ']' || c == '`'){
(void)sprintf(buf + j, "%%%X%X", c >> 4, c & 0x0F); (void) sprintf(buf + j, "%%%X%X", c >> 4, c & 0x0F);
j += 3; j += 3;
}else{ } else {
buf[j] = url[i]; buf[j] = url[i];
j++; j++;
} }
...@@ -90,28 +89,32 @@ char *url_decode(char *url, size_t l) ...@@ -90,28 +89,32 @@ char *url_decode(char *url, size_t l)
char d0; char d0;
char d1; char d1;
buf = (char *)malloc((l + 1) * sizeof(char)); buf = (char *) malloc((l + 1) * sizeof(char));
if(buf == NULL){ if (buf == NULL) {
perror("Could not allocate memory for decoding"); perror("Could not allocate memory for decoding");
return NULL; return NULL;
} }
for(i = 0, j = 0; i < l; j++){ for (i = 0, j = 0; i < l; j++) {
c = url[i]; c = url[i];
if(c == '%'){ if (c == '%') {
d0 = url[i + 2]; d0 = url[i + 2];
d1 = url[i + 1]; d1 = url[i + 1];
d0 = toupper(d0); d0 = toupper(d0);
d1 = toupper(d1); d1 = toupper(d1);
if(d0 >= 'A' && d0 <= 'F') d0 = d0 - 'A' + 10; if (d0 >= 'A' && d0 <= 'F')
else if(d0 >= '0' && d0 <= '9') d0 = d0 - '0'; d0 = d0 - 'A' + 10;
if(d1 >= 'A' && d1 <= 'F') d1 = d1 - 'A' + 10; else if (d0 >= '0' && d0 <= '9')
else if(d1 >= '0' && d1 <= '9') d1 = d1 - '0'; d0 = d0 - '0';
if (d1 >= 'A' && d1 <= 'F')
d1 = d1 - 'A' + 10;
else if (d1 >= '0' && d1 <= '9')
d1 = d1 - '0';
buf[j] = (d1 << 4) + d0; buf[j] = (d1 << 4) + d0;
i += 3; i += 3;
}else{ } else {
buf[j] = url[i]; buf[j] = url[i];
i++; i++;
} }
...@@ -121,5 +124,3 @@ char *url_decode(char *url, size_t l) ...@@ -121,5 +124,3 @@ char *url_decode(char *url, size_t l)
return buf; return buf;
} }
...@@ -39,4 +39,3 @@ char *url_encode(char *url, size_t l); ...@@ -39,4 +39,3 @@ char *url_encode(char *url, size_t l);
char *url_decode(char *url, size_t l); char *url_decode(char *url, size_t l);
#endif #endif
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论