提交 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 @@
#include "bits.h"
#include "arraylist.h"
struct array_list*
array_list_new(array_list_free_fn *free_fn)
struct array_list *array_list_new(array_list_free_fn * free_fn)
{
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->length = 0;
this->free_fn = free_fn;
if(!(this->array = calloc(sizeof(void*), this->size))) {
if (!(this->array = calloc(sizeof(void *), this->size))) {
free(this);
return NULL;
}
return this;
}
extern void
array_list_free(struct array_list *this)
extern void array_list_free(struct array_list *this)
{
int i;
for(i = 0; i < this->length; i++)
if(this->array[i]) this->free_fn(this->array[i]);
for (i = 0; i < this->length; i++)
if (this->array[i])
this->free_fn(this->array[i]);
free(this->array);
free(this);
}
void*
array_list_get_idx(struct array_list *this, int i)
void *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];
}
......@@ -61,33 +61,35 @@ static int array_list_expand_internal(struct array_list *this, int max)
void *t;
int new_size;
if(max < this->size) return 0;
if (max < this->size)
return 0;
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;
(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;
return 0;
}
int
array_list_put_idx(struct array_list *this, int idx, void *data)
int array_list_put_idx(struct array_list *this, int idx, void *data)
{
if(array_list_expand_internal(this, idx)) return -1;
if(this->array[idx]) this->free_fn(this->array[idx]);
if (array_list_expand_internal(this, idx))
return -1;
if (this->array[idx])
this->free_fn(this->array[idx]);
this->array[idx] = data;
if(this->length <= idx) this->length = idx + 1;
if (this->length <= idx)
this->length = idx + 1;
return 0;
}
int
array_list_add(struct array_list *this, void *data)
int array_list_add(struct array_list *this, void *data)
{
return array_list_put_idx(this, this->length, data);
}
int
array_list_length(struct array_list *this)
int array_list_length(struct array_list *this)
{
return this->length;
}
......@@ -16,30 +16,23 @@
typedef void (array_list_free_fn) (void *data);
struct array_list
{
struct array_list {
void **array;
int length;
int size;
array_list_free_fn *free_fn;
};
extern struct array_list*
array_list_new(array_list_free_fn *free_fn);
extern struct array_list *array_list_new(array_list_free_fn * free_fn);
extern void
array_list_free(struct array_list *al);
extern void array_list_free(struct array_list *al);
extern void*
array_list_get_idx(struct array_list *al, int i);
extern void *array_list_get_idx(struct array_list *al, int i);
extern int
array_list_put_idx(struct array_list *al, int i, void *data);
extern int array_list_put_idx(struct array_list *al, int i, void *data);
extern int
array_list_add(struct array_list *al, void *data);
extern int array_list_add(struct array_list *al, void *data);
extern int
array_list_length(struct array_list *al);
extern int array_list_length(struct array_list *al);
#endif
......@@ -33,8 +33,15 @@
static int _syslog = 0;
static int _debug = 0;
void mc_set_debug(int debug) { _debug = debug; }
int mc_get_debug() { return _debug; }
void mc_set_debug(int debug)
{
_debug = debug;
}
int mc_get_debug()
{
return _debug;
}
extern void mc_set_syslog(int syslog)
{
......@@ -46,7 +53,7 @@ void mc_abort(const char *msg, ...)
va_list ap;
va_start(ap, msg);
#if HAVE_VSYSLOG
if(_syslog) {
if (_syslog) {
vsyslog(LOG_ERR, msg, ap);
} else
#endif
......@@ -58,10 +65,10 @@ void mc_abort(const char *msg, ...)
void mc_debug(const char *msg, ...)
{
va_list ap;
if(_debug) {
if (_debug) {
va_start(ap, msg);
#if HAVE_VSYSLOG
if(_syslog) {
if (_syslog) {
vsyslog(LOG_DEBUG, msg, ap);
} else
#endif
......@@ -74,7 +81,7 @@ void mc_error(const char *msg, ...)
va_list ap;
va_start(ap, msg);
#if HAVE_VSYSLOG
if(_syslog) {
if (_syslog) {
vsyslog(LOG_ERR, msg, ap);
} else
#endif
......@@ -86,7 +93,7 @@ void mc_info(const char *msg, ...)
va_list ap;
va_start(ap, msg);
#if HAVE_VSYSLOG
if(_syslog) {
if (_syslog) {
vsyslog(LOG_INFO, msg, ap);
} else
#endif
......
......@@ -89,7 +89,7 @@ typedef enum accept_states {
} accept_state_t;
typedef struct state_machines {
accept_state_t (*state)(char, struct state_machines *);
accept_state_t(*state) (char, struct state_machines *);
int pos;
char *buf;
ssize_t buf_len;
......@@ -130,9 +130,9 @@ typedef struct http_responses {
int body_len;
} http_response_t;
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_req(http_request_t *req, http_response_t *res);
void free_http_response(http_response_t *response);
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_req(http_request_t * req, http_response_t * res);
void free_http_response(http_response_t * response);
#endif
......@@ -27,5 +27,4 @@ extern "C" {
#ifdef __cplusplus
}
#endif
#endif
......@@ -50,7 +50,7 @@ enum json_type {
* Increment the reference count of json_object
* @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
......@@ -90,7 +90,7 @@ extern enum json_type json_object_get_type(struct json_object *obj);
* @param obj the json_object instance
* @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 */
......@@ -98,13 +98,13 @@ extern char* json_object_to_json_string(struct json_object *obj);
/** Create a new empty 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
* @param obj the json_object instance
* @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
*
......@@ -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 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,
struct json_object *val);
extern void json_object_object_add(struct json_object *obj, char *key, struct json_object *val);
/** Get the json_object associate with a given object field
* @param obj the json_object instance
* @param key the object field name
* @returns the json_object associated with the given field name
*/
extern struct json_object* json_object_object_get(struct json_object* obj,
char *key);
extern struct json_object *json_object_object_get(struct json_object *obj, char *key);
/** Delete the given json_object field
*
......@@ -134,7 +132,7 @@ extern struct json_object* json_object_object_get(struct json_object* obj,
* @param obj the json_object instance
* @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
* @param obj the json_object instance
......@@ -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
* @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
* @param obj the json_object instance
* @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
* @param obj the json_object instance
......@@ -188,8 +186,7 @@ extern int json_object_array_length(struct json_object *obj);
* @param obj the json_object instance
* @param val the json_object to be added
*/
extern int json_object_array_add(struct json_object *obj,
struct json_object *val);
extern int json_object_array_add(struct json_object *obj, struct json_object *val);
/** 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,
* @param idx the index to insert the element at
* @param val the json_object to be added
*/
extern int json_object_array_put_idx(struct json_object *obj, int idx,
struct json_object *val);
extern int json_object_array_put_idx(struct json_object *obj, int idx, struct json_object *val);
/** Get the element at specificed index of the array (a json_object of type json_type_array)
* @param obj the json_object instance
* @param idx the index to get the element at
* @returns the json_object at the specified index (or NULL)
*/
extern struct json_object* json_object_array_get_idx(struct json_object *obj,
int idx);
extern struct json_object *json_object_array_get_idx(struct json_object *obj, int idx);
/* boolean type methods */
......@@ -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)
* @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
*
......@@ -245,7 +240,7 @@ extern boolean json_object_get_boolean(struct json_object *obj);
* @param i the integer
* @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
*
......@@ -265,7 +260,7 @@ extern int json_object_get_int(struct json_object *obj);
* @param d the 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
*
......@@ -288,9 +283,9 @@ extern double json_object_get_double(struct json_object *obj);
* @param s the 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
*
......@@ -303,6 +298,6 @@ extern struct json_object* json_object_new_string_len(char *s, int len);
* @param obj the json_object instance
* @returns a string
*/
extern char* json_object_get_string(struct json_object *obj);
extern char *json_object_get_string(struct json_object *obj);
#endif
......@@ -12,12 +12,10 @@
#ifndef _json_object_private_h_
#define _json_object_private_h_
typedef void (json_object_delete_fn)(struct json_object *o);
typedef int (json_object_to_json_string_fn)(struct json_object *o,
struct printbuf *pb);
typedef void (json_object_delete_fn) (struct json_object * o);
typedef int (json_object_to_json_string_fn) (struct json_object * o, struct printbuf * pb);
struct json_object
{
struct json_object {
enum json_type o_type;
json_object_delete_fn *_delete;
json_object_to_json_string_fn *_to_json_string;
......@@ -34,8 +32,7 @@ struct json_object
};
/* CAW: added for ANSI C iteration correctness */
struct json_object_iter
{
struct json_object_iter {
char *key;
struct json_object *val;
struct lh_entry *entry;
......
......@@ -56,8 +56,7 @@ enum json_tokener_state {
json_tokener_state_object_sep
};
struct json_tokener_srec
{
struct json_tokener_srec {
enum json_tokener_state state, saved_state;
struct json_object *obj;
struct json_object *current;
......@@ -66,8 +65,7 @@ struct json_tokener_srec
#define JSON_TOKENER_MAX_DEPTH 32
struct json_tokener
{
struct json_tokener {
char *str;
struct printbuf *pb;
int depth, is_double, st_pos, char_offset;
......@@ -77,13 +75,12 @@ struct json_tokener
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_reset(struct json_tokener *tok);
extern struct json_object* json_tokener_parse(char *str);
extern struct json_object* json_tokener_parse_ex(struct json_tokener *tok,
char *str, int len);
extern struct json_object *json_tokener_parse(char *str);
extern struct json_object *json_tokener_parse_ex(struct json_tokener *tok, char *str, int len);
#endif
......@@ -51,29 +51,27 @@
#include "json_tokener.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 json_object *obj;
char buf[JSON_FILE_BUF_SIZE];
int fd, ret;
if((fd = open(filename, O_RDONLY)) < 0) {
mc_error("json_object_from_file: error reading file %s: %s\n",
filename, strerror(errno));
if ((fd = open(filename, O_RDONLY)) < 0) {
mc_error("json_object_from_file: error reading file %s: %s\n", filename, strerror(errno));
return error_ptr(-1);
}
if(!(pb = printbuf_new())) {
if (!(pb = printbuf_new())) {
mc_error("json_object_from_file: printbuf_new failed\n");
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);
}
close(fd);
if(ret < 0) {
mc_abort("json_object_from_file: error reading file %s: %s\n",
filename, strerror(errno));
if (ret < 0) {
mc_abort("json_object_from_file: error reading file %s: %s\n", filename, strerror(errno));
printbuf_free(pb);
return error_ptr(-1);
}
......@@ -88,32 +86,32 @@ int json_object_to_file(char *filename, struct json_object *obj)
int fd, ret;
unsigned int wpos, wsize;
if(!obj) {
if (!obj) {
mc_error("json_object_to_file: object is null\n");
return -1;
}
if((fd = open(filename, O_WRONLY | O_TRUNC | O_CREAT, 0644)) < 0) {
mc_error("json_object_to_file: error opening file %s: %s\n",
filename, strerror(errno));
if ((fd = open(filename, O_WRONLY | O_TRUNC | O_CREAT, 0644)) < 0) {
mc_error("json_object_to_file: error opening file %s: %s\n", filename, strerror(errno));
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;
while(wpos < wsize) {
if((ret = write(fd, json_str + wpos, wsize-wpos)) < 0) {
while (wpos < wsize) {
if ((ret = write(fd, json_str + wpos, wsize - wpos)) < 0) {
close(fd);
mc_error("json_object_to_file: error writing file %s: %s\n",
filename, strerror(errno));
mc_error("json_object_to_file: error writing file %s: %s\n", filename, strerror(errno));
return -1;
}
/* because of the above check for ret < 0, we can safely cast and add */
wpos += (unsigned int)ret;
wpos += (unsigned int) ret;
}
close(fd);
......
......@@ -17,7 +17,7 @@
#define JSON_FILE_BUF_SIZE 4096
/* 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);
#endif
......@@ -31,7 +31,7 @@ void lh_abort(const char *msg, ...)
uint32_t lh_ptr_hash(void *k)
{
/* 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)
......@@ -42,48 +42,47 @@ int lh_ptr_equal(void *k1, void *k2)
uint32_t lh_char_hash(void *k)
{
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;
}
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,
lh_entry_free_fn *free_fn,
lh_hash_fn *hash_fn,
lh_equal_fn *equal_fn)
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)
{
int i;
struct lh_table *t;
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->size = size;
t->name = name;
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->hash_fn = hash_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;
}
struct lh_table* lh_kchar_table_new(int size, char *name,
lh_entry_free_fn *free_fn)
struct lh_table *lh_kchar_table_new(int size, char *name, lh_entry_free_fn * free_fn)
{
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,
lh_entry_free_fn *free_fn)
struct lh_table *lh_kptr_table_new(int size, char *name, lh_entry_free_fn * free_fn)
{
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)
new_t = lh_table_new(new_size, t->name, NULL, t->hash_fn, t->equal_fn);
ent = t->head;
while(ent) {
while (ent) {
lh_table_insert(new_t, ent->k, ent->v);
ent = ent->next;
}
......@@ -111,8 +110,8 @@ void lh_table_resize(struct lh_table *t, int new_size)
void lh_table_free(struct lh_table *t)
{
struct lh_entry *c;
for(c = t->head; c != NULL; c = c->next) {
if(t->free_fn) {
for (c = t->head; c != NULL; c = c->next) {
if (t->free_fn) {
t->free_fn(c);
}
}
......@@ -126,22 +125,25 @@ int lh_table_insert(struct lh_table *t, void *k, void *v)
uint32_t h, n;
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);
n = h % t->size;
while( 1 ) {
if(t->table[n].k == LH_EMPTY || t->table[n].k == LH_FREED) break;
while (1) {
if (t->table[n].k == LH_EMPTY || t->table[n].k == LH_FREED)
break;
t->collisions++;
if(++n == t->size) n = 0;
if (++n == t->size)
n = 0;
}
t->table[n].k = k;
t->table[n].v = v;
t->count++;
if(t->head == NULL) {
if (t->head == NULL) {
t->head = t->tail = &t->table[n];
t->table[n].next = t->table[n].prev = NULL;
} else {
......@@ -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 n = h % t->size;
t->lookups++;
while( 1 ) {
if(t->table[n].k == LH_EMPTY) return NULL;
if(t->table[n].k != LH_FREED &&
t->equal_fn(t->table[n].k, k)) return &t->table[n];
if(++n == t->size) n = 0;
while (1) {
if (t->table[n].k == LH_EMPTY)
return NULL;
if (t->table[n].k != LH_FREED && t->equal_fn(t->table[n].k, k))
return &t->table[n];
if (++n == t->size)
n = 0;
}
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);
if(e) return e->v;
if (e)
return e->v;
return NULL;
}
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... */
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--;
if(t->free_fn) t->free_fn(e);
if (t->free_fn)
t->free_fn(e);
t->table[n].v = NULL;
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;
} else if (t->head == &t->table[n]) {
t->head->next->prev = NULL;
......@@ -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)
{
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);
}
......@@ -33,11 +33,11 @@ struct lh_entry;
/**
* 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
*/
typedef uint32_t (lh_hash_fn) (void *k);
typedef uint32_t(lh_hash_fn) (void *k);
/**
* callback function prototypes
*/
......@@ -171,10 +171,7 @@ for(entry = table->head; entry && ((tmp = entry->next) || 1); entry = tmp)
* and C strings respectively.
* @return a pointer onto the linkhash table.
*/
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);
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);
/**
* Convenience function to create a new linkhash
......@@ -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.
* @return a pointer onto the linkhash table.
*/
extern struct lh_table* lh_kchar_table_new(int size, char *name,
lh_entry_free_fn *free_fn);
extern struct lh_table *lh_kchar_table_new(int size, char *name, lh_entry_free_fn * free_fn);
/**
......@@ -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.
* @return a pointer onto the linkhash table.
*/
extern struct lh_table* lh_kptr_table_new(int size, char *name,
lh_entry_free_fn *free_fn);
extern struct lh_table *lh_kptr_table_new(int size, char *name, lh_entry_free_fn * free_fn);
/**
......@@ -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
* @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
......@@ -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
* @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)
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);
return SWITCH_STATUS_SUCCESS;
}
......@@ -132,28 +132,33 @@ SWITCH_STANDARD_API(http_api_main)
ccmd = strdup(cmd);
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);
stream->write_function(stream, "-ERR\n");
return SWITCH_STATUS_SUCCESS;
}
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;
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];
headers_str = argv[2];
if(argc == HTTP_PARAMS){
if (argc == HTTP_PARAMS) {
body = argv[3];
}else{
body = (char *)malloc(1 * sizeof(char));
if(body == NULL){
} else {
body = (char *) malloc(1 * sizeof(char));
if (body == NULL) {
switch_safe_free(ccmd);
stream->write_function(stream, "-ERR\n");
return SWITCH_STATUS_SUCCESS;
......@@ -162,8 +167,8 @@ SWITCH_STANDARD_API(http_api_main)
GARBAGE_ADD(body);
}
buf = (char *)malloc(HTTP_BUFFER_SIZE * sizeof(char));
if(buf == NULL){
buf = (char *) malloc(HTTP_BUFFER_SIZE * sizeof(char));
if (buf == NULL) {
switch_safe_free(ccmd);
stream->write_function(stream, "-ERR\n");
GARBAGE_CLEANUP();
......@@ -174,8 +179,8 @@ SWITCH_STANDARD_API(http_api_main)
request.version = DEFAULT_HTTP_VERSION;
l = strlen(url);
request.url = (char *)malloc((l + 1) * sizeof(char));
if(request.url == NULL){
request.url = (char *) malloc((l + 1) * sizeof(char));
if (request.url == NULL) {
switch_safe_free(ccmd);
stream->write_function(stream, "-ERR\n");
GARBAGE_CLEANUP();
......@@ -192,7 +197,7 @@ SWITCH_STANDARD_API(http_api_main)
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);
stream->write_function(stream, "-ERR\n");
GARBAGE_CLEANUP();
......@@ -200,7 +205,7 @@ SWITCH_STANDARD_API(http_api_main)
}
jsontype = json_object_get_type(json_http_headers);
if(jsontype != json_type_object){
if (jsontype != json_type_object) {
switch_safe_free(ccmd);
stream->write_function(stream, "-ERR\n");
GARBAGE_CLEANUP();
......@@ -208,21 +213,21 @@ SWITCH_STANDARD_API(http_api_main)
}
i = 0;
json_object_object_foreach(json_http_headers, key, val){
json_object_object_foreach(json_http_headers, key, val) {
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);
i = 0;
json_object_object_foreach(json_http_headers, key, val){
json_object_object_foreach(json_http_headers, key, val) {
l = strlen(key);
request.headers[i].field_name = (char *)malloc((l + 1) * sizeof(char));
if(request.headers[i].field_name == NULL){
request.headers[i].field_name = (char *) malloc((l + 1) * sizeof(char));
if (request.headers[i].field_name == NULL) {
switch_safe_free(ccmd);
stream->write_function(stream, "-ERR\n");
GARBAGE_CLEANUP();
......@@ -235,7 +240,7 @@ SWITCH_STANDARD_API(http_api_main)
a += strlen(key);
jsontype = json_object_get_type(val);
if(jsontype != json_type_string){
if (jsontype != json_type_string) {
switch_safe_free(ccmd);
stream->write_function(stream, "-ERR\n");
GARBAGE_CLEANUP();
......@@ -245,8 +250,8 @@ SWITCH_STANDARD_API(http_api_main)
value = json_object_get_string(val);
/* value = json_object_to_json_string(val); */
l = strlen(value);
request.headers[i].value = (char *)malloc((l + 1) * sizeof(char));
if(request.headers[i].value == NULL){
request.headers[i].value = (char *) malloc((l + 1) * sizeof(char));
if (request.headers[i].value == NULL) {
switch_safe_free(ccmd);
stream->write_function(stream, "-ERR\n");
GARBAGE_CLEANUP();
......@@ -258,28 +263,33 @@ SWITCH_STANDARD_API(http_api_main)
i++;
}
if(argc == HTTP_PARAMS){
if (argc == HTTP_PARAMS) {
l = strlen(body);
body_dec = url_decode(body, l);
GARBAGE_ADD(body_dec);
l = strlen(body_dec);
request.body_len = l;
request.body = body_dec;
}else request.body_len = 0;
} else
request.body_len = 0;
ret = http_req(&request, &response);
if(response.version != NULL) GARBAGE_ADD(response.version);
if(response.phrase != NULL) GARBAGE_ADD(response.phrase);
if(response.headers != NULL) GARBAGE_ADD(response.headers);
if(response.body != NULL) GARBAGE_ADD(response.body);
for(i = 0; i < response.header_len; i++){
if (response.version != NULL)
GARBAGE_ADD(response.version);
if (response.phrase != NULL)
GARBAGE_ADD(response.phrase);
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].value);
}
if(ret == ERROR){
if (ret == ERROR) {
switch_safe_free(ccmd);
stream->write_function(stream, "-ERR\n");
GARBAGE_CLEANUP();
......@@ -289,17 +299,15 @@ SWITCH_STANDARD_API(http_api_main)
/* This is evil and should be changed in the future */
l = 128 + (256 * response.header_len) + (a * 2)
+ strlen("version") + strlen(response.version)
+ strlen("status_code") + 3
+ strlen("phrase") + strlen(response.phrase)
+ strlen("body") + (response.body_len * 3) + 1
+ strlen("headers")
+ strlen("status_code") + 3 + strlen("phrase") + strlen(response.phrase)
+ strlen("body") + (response.body_len * 3) + 1 + strlen("headers")
+ 1;
/* to be safe */
l <<= 2;
json_response = (char *)malloc(l * sizeof(char));
if(json_response == NULL){
json_response = (char *) malloc(l * sizeof(char));
if (json_response == NULL) {
switch_safe_free(ccmd);
stream->write_function(stream, "-ERR\n");
GARBAGE_CLEANUP();
......@@ -307,16 +315,16 @@ SWITCH_STANDARD_API(http_api_main)
}
GARBAGE_ADD(json_response);
if(response.body_len != 0){
t = (char *)malloc((response.body_len + 1) * sizeof(char));
if(t == NULL){
if (response.body_len != 0) {
t = (char *) malloc((response.body_len + 1) * sizeof(char));
if (t == NULL) {
switch_safe_free(ccmd);
stream->write_function(stream, "-ERR\n");
GARBAGE_CLEANUP();
return SWITCH_STATUS_SUCCESS;
}
GARBAGE_ADD(t);
(void)memcpy(t, response.body, response.body_len);
(void) memcpy(t, response.body, response.body_len);
t[response.body_len] = '\0';
response.body = url_encode(t, response.body_len);
GARBAGE_ADD(response.body);
......@@ -329,26 +337,17 @@ SWITCH_STANDARD_API(http_api_main)
"\"status_code\": \"%3d\","
"\"phrase\": \"%s\","
"\"body\": \"%s\","
"\"headers\": [",
response.version,
response.status_code,
response.phrase,
((response.body_len <= 0)? "":response.body)
"\"headers\": [", response.version, response.status_code, response.phrase, ((response.body_len <= 0) ? "" : response.body)
);
for(f = HTTP_FALSE, j = 0; j < response.header_len; j++){
if(f != HTTP_FALSE){
m += snprintf(json_response + m, l - m,
","
);
}else f = HTTP_TRUE;
for (f = HTTP_FALSE, j = 0; j < response.header_len; j++) {
if (f != HTTP_FALSE) {
m += snprintf(json_response + m, l - m, ",");
} else
f = HTTP_TRUE;
m += snprintf(json_response + m, l - m,
"{\"key\": \"%s\",\"value\": \"%s\"}",
response.headers[j].field_name,
response.headers[j].value
);
m += snprintf(json_response + m, l - m, "{\"key\": \"%s\",\"value\": \"%s\"}", response.headers[j].field_name, response.headers[j].value);
}
......@@ -356,14 +355,7 @@ SWITCH_STANDARD_API(http_api_main)
json_response[m] = '\0';
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_log_printf(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);
......@@ -380,23 +372,11 @@ SWITCH_MODULE_LOAD_FUNCTION(mod_http_load)
{
switch_api_interface_t *api_interface;
*module_interface =
switch_loadable_module_create_module_interface(pool, modname);
*module_interface = switch_loadable_module_create_module_interface(pool, modname);
switch_log_printf(
SWITCH_CHANNEL_LOG,
SWITCH_LOG_NOTICE,
"HTTP request mod enabled\n"
);
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "HTTP request mod enabled\n");
SWITCH_ADD_API(
api_interface,
"http",
"Make HTTP requests",
http_api_main,
HTTP_SYNTAX
);
SWITCH_ADD_API(api_interface, "http", "Make HTTP requests", http_api_main, HTTP_SYNTAX);
return SWITCH_STATUS_SUCCESS;
}
......@@ -25,14 +25,15 @@
#include "debug.h"
#include "printbuf.h"
struct printbuf* printbuf_new()
struct printbuf *printbuf_new()
{
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->bpos = 0;
if(!(p->buf = malloc(p->size))) {
if (!(p->buf = malloc(p->size))) {
free(p);
return NULL;
}
......@@ -43,20 +44,19 @@ struct printbuf* printbuf_new()
int printbuf_memappend(struct printbuf *p, char *buf, int size)
{
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);
#ifdef PRINTBUF_DEBUG
mc_debug("printbuf_memappend: realloc "
"bpos=%d wrsize=%d old_size=%d new_size=%d\n",
p->bpos, size, p->size, new_size);
mc_debug("printbuf_memappend: realloc " "bpos=%d wrsize=%d old_size=%d new_size=%d\n", p->bpos, size, p->size, new_size);
#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->buf = t;
}
memcpy(p->buf + p->bpos, buf, size);
p->bpos += size;
p->buf[p->bpos]= '\0';
p->buf[p->bpos] = '\0';
return size;
}
......@@ -76,22 +76,26 @@ static int vasprintf(char **buf, const char *fmt, va_list ap)
int chars;
char *b;
if(!buf) { return -1; }
if (!buf) {
return -1;
}
#ifdef WIN32
chars = _vscprintf(fmt, ap)+1;
chars = _vscprintf(fmt, ap) + 1;
#else /* !defined(WIN32) */
/* 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 */
chars = vsnprintf(&_T_emptybuffer, 0, fmt, ap)+1;
if(chars < 0) { chars *= -1; } /* CAW: old glibc versions have this problem */
chars = vsnprintf(&_T_emptybuffer, 0, fmt, ap) + 1;
if (chars < 0) {
chars *= -1;
} /* CAW: old glibc versions have this problem */
#endif /* defined(WIN32) */
b = (char*)malloc(sizeof(char)*chars);
if(!b) { return -1; }
b = (char *) malloc(sizeof(char) * chars);
if (!b) {
return -1;
}
if((chars = vsprintf(b, fmt, ap)) < 0)
{
if ((chars = vsprintf(b, fmt, ap)) < 0) {
free(b);
} else {
*buf = b;
......@@ -116,10 +120,11 @@ int sprintbuf(struct printbuf *p, const char *msg, ...)
with vasprintf. Note: some implementation of vsnprintf return -1
if output is truncated whereas some return the number of bytes that
would have been writen - this code handles both cases. */
if(size == -1 || size > 127) {
if (size == -1 || size > 127) {
int ret;
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);
ret = printbuf_memappend(p, t, size);
free(t);
......@@ -137,7 +142,7 @@ void printbuf_reset(struct printbuf *p)
void printbuf_free(struct printbuf *p)
{
if(p) {
if (p) {
free(p->buf);
free(p);
}
......
......@@ -20,19 +20,14 @@ struct printbuf {
int size;
};
extern struct printbuf*
printbuf_new();
extern struct printbuf *printbuf_new();
extern int
printbuf_memappend(struct printbuf *p, char *buf, int size);
extern int printbuf_memappend(struct printbuf *p, char *buf, int size);
extern int
sprintbuf(struct printbuf *p, const char *msg, ...);
extern int sprintbuf(struct printbuf *p, const char *msg, ...);
extern void
printbuf_reset(struct printbuf *p);
extern void printbuf_reset(struct printbuf *p);
extern void
printbuf_free(struct printbuf *p);
extern void printbuf_free(struct printbuf *p);
#endif
......@@ -53,24 +53,23 @@ char *url_encode(char *url, size_t l)
char *buf;
unsigned char c;
buf = (char *)malloc((l * 3) + 1);
if(buf == NULL){
buf = (char *) malloc((l * 3) + 1);
if (buf == NULL) {
perror("Could not allocate memory url encoding");
return NULL;
}
for(i = 0, j = 0; i < l; i++){
c = (unsigned char)url[i];
if(c <= 31 || c >= 127
for (i = 0, j = 0; i < l; i++) {
c = (unsigned char) url[i];
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 == '`') {
(void)sprintf(buf + j, "%%%X%X", c >> 4, c & 0x0F);
(void) sprintf(buf + j, "%%%X%X", c >> 4, c & 0x0F);
j += 3;
}else{
} else {
buf[j] = url[i];
j++;
}
......@@ -90,28 +89,32 @@ char *url_decode(char *url, size_t l)
char d0;
char d1;
buf = (char *)malloc((l + 1) * sizeof(char));
if(buf == NULL){
buf = (char *) malloc((l + 1) * sizeof(char));
if (buf == NULL) {
perror("Could not allocate memory for decoding");
return NULL;
}
for(i = 0, j = 0; i < l; j++){
for (i = 0, j = 0; i < l; j++) {
c = url[i];
if(c == '%'){
if (c == '%') {
d0 = url[i + 2];
d1 = url[i + 1];
d0 = toupper(d0);
d1 = toupper(d1);
if(d0 >= 'A' && d0 <= 'F') d0 = d0 - 'A' + 10;
else if(d0 >= '0' && d0 <= '9') d0 = d0 - '0';
if(d1 >= 'A' && d1 <= 'F') d1 = d1 - 'A' + 10;
else if(d1 >= '0' && d1 <= '9') d1 = d1 - '0';
if (d0 >= 'A' && d0 <= 'F')
d0 = d0 - 'A' + 10;
else if (d0 >= '0' && d0 <= '9')
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;
i += 3;
}else{
} else {
buf[j] = url[i];
i++;
}
......@@ -121,5 +124,3 @@ char *url_decode(char *url, size_t l)
return buf;
}
......@@ -39,4 +39,3 @@ char *url_encode(char *url, size_t l);
char *url_decode(char *url, size_t l);
#endif
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论