Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/onion/http.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ int onion_http_read_ready(onion_request * req);
struct onion_http_t {
};


/**
* @short Creates an HTTP listen point
* @memberof onion_http_t
Expand All @@ -61,6 +62,7 @@ onion_listen_point *onion_http_new() {
return ret;
}


/**
* @short Reads data from the http connection
* @memberof onion_http_t
Expand All @@ -76,7 +78,7 @@ static ssize_t onion_http_read(onion_request * con, char *data, size_t len) {
* @ingroup http
*/
int onion_http_read_ready(onion_request * con) {
char buffer[1500];
char buffer[9100];
ssize_t len = con->connection.listen_point->read(con, buffer, sizeof(buffer));

if (len <= 0)
Expand Down
73 changes: 73 additions & 0 deletions src/onion/listen_point.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,82 @@ static int onion_listen_point_read_ready(onion_request * req);
*/
onion_listen_point *onion_listen_point_new() {
onion_listen_point *ret = onion_low_calloc(1, sizeof(onion_listen_point));
ret->att_hndl.auth = NULL;
ret->att_hndl.open = NULL;
ret->att_hndl.pread = NULL;
ret->att_hndl.pwrite = NULL;
ret->att_hndl.close = NULL;
ret->att_hndl.unlink = NULL;
ret->cache_size = 4096;

ret->new_hash_ctx = NULL;
ret->init_hash_ctx = NULL;
ret->update_hash_ctx = NULL;
ret->final_hash_ctx = NULL;
ret->free_hash_ctx = NULL;
ret->multi = false;

ret->context = NULL;

return ret;
}

void onion_listen_point_set_attachment_handlers(onion_listen_point* ret,
int (*f_auth)(onion_request*, char*),
int (*f_open)(const char*, int, ...),
ssize_t (*f_pread)(const char*, void*, size_t, off_t),
ssize_t (*f_pwrite)(const char*, const void*, size_t, off_t, onion_request*),
int (*f_close)(const char*),
int (*f_unlink)(const char*) ){
ret->att_hndl.auth = f_auth;
ret->att_hndl.open = f_open;
ret->att_hndl.pread = f_pread;
ret->att_hndl.pwrite = f_pwrite;
ret->att_hndl.close = f_close;
ret->att_hndl.unlink = f_unlink;
}

void* onion_listen_point_att_hndl_open(onion_listen_point* lp){
return lp->att_hndl.open;
}

void* onion_listen_point_att_hndl_pread(onion_listen_point* lp){
return lp->att_hndl.pread;
}

void* onion_listen_point_att_hndl_close(onion_listen_point* lp){
return lp->att_hndl.close;
}

void* onion_listen_point_att_hndl_unlink(onion_listen_point* lp){
return lp->att_hndl.unlink;
}

void onion_listen_point_set_hash_handlers(onion_listen_point* ret,
void* (*f_new)(), int (*f_init)(void*),
int (*f_update)(void* , const void *, size_t ),
int (*f_final)(unsigned char*, void*),
void (*f_free)(void* ctx), bool multi ){
ret->new_hash_ctx = f_new;
ret->init_hash_ctx = f_init;
ret->update_hash_ctx = f_update;
ret->final_hash_ctx = f_final;
ret->free_hash_ctx = f_free;
ret->multi = multi;
}

void onion_listen_point_set_cache_size(onion_listen_point* ret, size_t cache_size){
ret->cache_size = cache_size;
}

void onion_listen_point_set_context(onion_listen_point* ret, void* context){
ret->context = context;
}

void* onion_listen_point_get_context(onion_listen_point* lp){
return lp->context;
}

/**
* @short Free and closes the listen point
* @memberof onion_listen_point_t
Expand Down
24 changes: 24 additions & 0 deletions src/onion/listen_point.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,30 @@ extern "C" {
int onion_listen_point_accept(onion_listen_point *);
int onion_listen_point_request_init_from_socket(onion_request * op);
void onion_listen_point_request_close_socket(onion_request * oc);
void onion_listen_point_set_attachment_handlers(onion_listen_point* lp,
int (*f_auth)(onion_request*, char*),
int (*f_open)(const char*, int, ...),
ssize_t (*f_pread)(const char*, void*, size_t, off_t),
ssize_t (*f_pwrite)(const char*, const void*, size_t, off_t, onion_request*),
int (*f_close)(const char*),
int (*f_unlink)(const char*));
void onion_listen_point_set_hash_handlers(onion_listen_point* lp,
void* (*f_new)(),
int (*f_init)(void* ctx),
int (*f_update)(void* ctx, const void *data, size_t len),
int (*f_final)(unsigned char* data, void* ctx),
void (*f_free)(void* ctx), bool multi);

void* onion_listen_point_att_hndl_open(onion_listen_point*);
void* onion_listen_point_att_hndl_pread(onion_listen_point*);
void* onion_listen_point_att_hndl_close(onion_listen_point*);
void* onion_listen_point_att_hndl_unlink(onion_listen_point*);

void onion_listen_point_set_cache_size(onion_listen_point* lp, size_t);

void onion_listen_point_set_context(onion_listen_point* lp, void* context);
void* onion_listen_point_get_context(onion_listen_point* lp);

#ifdef __cplusplus
}
#endif
Expand Down
62 changes: 62 additions & 0 deletions src/onion/onion.c
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,68 @@ onion *onion_new(int flags) {
return o;
}

void onion_set_attachment_handlers(onion* onion,
int (*f_auth)(onion_request*, char*),
int (*f_open)(const char*, int, ...),
ssize_t (*f_read)(const char*, void*, size_t, off_t),
ssize_t (*f_write)(const char*, const void*, size_t, off_t, onion_request*),
int (*f_close)(const char*),
int (*f_unlink)(const char*) ){
if (onion->listen_points) {
onion_listen_point **p = onion->listen_points;
while (*p != NULL) {
onion_listen_point_set_attachment_handlers(*p++,
f_auth, f_open, f_read, f_write, f_close, f_unlink);
}
}
}

void onion_set_hash_handlers(onion* onion, void* (*f_new)(), int (*f_init)(void*),
int (*f_update)(void*, const void*, size_t), int (*f_final)(unsigned char*, void*),
void (*f_free)(void*), bool multi){
if (onion->listen_points) {
onion_listen_point **p = onion->listen_points;
while (*p != NULL) {
onion_listen_point_set_hash_handlers(*p++, f_new, f_init, f_update, f_final, f_free, multi);
}
}
}

/*
void onion_set_s3_handlers(onion* onion,
int (*f_auth)(onion_request*, char*),
int (*f_open)(const char*),
ssize_t (*f_write)(const char* , const void*, size_t, off_t),
int (*f_close)(const char*),
int (*f_unlink)(const char*)){
if (onion->listen_points) {
onion_listen_point **p = onion->listen_points;
while (*p != NULL) {
onion_listen_point_set_s3_handlers(*p++, f_auth, f_open, f_write, f_close, f_unlink);
}
}
}
*/

void onion_set_cache_size(onion* onion, size_t cache_size){
if (onion->listen_points) {
onion_listen_point **p = onion->listen_points;
while (*p != NULL) {
onion_listen_point_set_cache_size(*p++, cache_size);
}
}
}


void onion_set_context(onion* onion, void* context){
if (onion->listen_points) {
onion_listen_point **p = onion->listen_points;
while (*p != NULL) {
onion_listen_point_set_context(*p++, context);
}
}
}

/**
* @short Removes the allocated data
* @ingroup onion
Expand Down
20 changes: 20 additions & 0 deletions src/onion/onion.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,26 @@ extern "C" {
/// Gets a single listen point, or NULL if not that many.
onion_listen_point *onion_get_listen_point(onion * server, int nlisten_point);

void onion_set_attachment_handlers(onion* server,
int (*f_auth)(onion_request*, char*),
int (*f_open)(const char*, int, ...),
ssize_t (*f_read)(const char*, void*, size_t, off_t),
ssize_t (*f_write)(const char*, const void*, size_t, off_t, onion_request*),
int (*f_close)(const char*),
int (*f_unlink)(const char*));

void onion_set_hash_handlers(onion* server,
void* (*f_new)(),
int (*f_init)(void*),
int (*f_update)(void * , const void *, size_t ),
int (*f_final)(unsigned char* , void*),
void (*f_free)(void* ),
bool multi);

void onion_set_cache_size(onion* server, size_t cache_size);

void onion_set_context(onion* server, void* context);

/// Gets the current flags, for example to check SSL support.
int onion_flags(onion * onion);

Expand Down
53 changes: 48 additions & 5 deletions src/onion/request.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ onion_request *onion_request_new(onion_listen_point * op) {
//req->connection=con;
req->headers = onion_dict_new();
onion_dict_set_flags(req->headers, OD_ICASE);
req->hash_ctx = (op->new_hash_ctx) ? op->new_hash_ctx() : NULL;
ONION_DEBUG0("Create request %p", req);

if (op) {
Expand Down Expand Up @@ -107,18 +108,23 @@ onion_request *onion_request_new_from_socket(onion_listen_point * con, int fd,
* @short Helper to remove temporal files from req->files
* @memberof onion_request_t
* @ingroup request
*/
static void unlink_files(void *p, const char *key, const char *value, int flags) {
static void unlink_files(void *p, const char *key, const char *value, int flags) {
ONION_DEBUG0("Unlinking temporal file %s", value);
unlink(value);
int (*f) (const char *value);
f = p;
f(value);
//unlink(value);
}
*/

/**
* @short Deletes a request and all its data
* @memberof onion_request_t
* @ingroup request
*/
void onion_request_free(onion_request * req) {

free(req->cache);
ONION_DEBUG0("Free request %p", req);
onion_dict_free(req->headers);

Expand All @@ -132,7 +138,8 @@ void onion_request_free(onion_request * req) {
if (req->POST)
onion_dict_free(req->POST);
if (req->FILES) {
onion_dict_preorder(req->FILES, unlink_files, NULL);
//onion_dict_preorder(req->FILES, unlink_files, NULL);
//onion_dict_preorder(req->FILES, unlink_files, req->connection.listen_point->unlink);
onion_dict_free(req->FILES);
}
if (req->session) {
Expand Down Expand Up @@ -162,6 +169,19 @@ void onion_request_free(onion_request * req) {
onion_ptr_list_foreach(req->free_list, onion_low_free);
onion_ptr_list_free(req->free_list);
}

#ifdef HAVE_PTHREADS
if (req->connection.listen_point->multi){
pthread_mutex_lock( &req->mtx );
pthread_mutex_unlock( &req->mtx );
pthread_mutex_destroy( &req->mtx );
free(req->hash_base);
}
#endif
if (req->hash_ctx){
req->connection.listen_point->free_hash_ctx(req->hash_ctx);
req->hash_ctx = NULL;
}
onion_low_free(req);
}

Expand Down Expand Up @@ -193,7 +213,8 @@ void onion_request_clean(onion_request * req) {
req->POST = NULL;
}
if (req->FILES) {
onion_dict_preorder(req->FILES, unlink_files, NULL);
//onion_dict_preorder(req->FILES, unlink_files, NULL);
//onion_dict_preorder(req->FILES, unlink_files, req->connection.listen_point->unlink_att);
onion_dict_free(req->FILES);
req->FILES = NULL;
}
Expand Down Expand Up @@ -730,3 +751,25 @@ const char *onion_request_get_cookie(onion_request * req,
bool onion_request_is_secure(onion_request * req) {
return req->connection.listen_point->secure;
}


void onion_request_get_hash(onion_request * req, unsigned char* value){
if (req->hash_ctx && req->connection.listen_point->final_hash_ctx){
#ifdef HAVE_PTHREADS
if (req->connection.listen_point->multi){
pthread_mutex_lock( &req->mtx );
}
#endif
req->connection.listen_point->final_hash_ctx(value, req->hash_ctx);
#ifdef HAVE_PTHREADS
if (req->connection.listen_point->multi){
pthread_mutex_unlock( &req->mtx );
}
#endif
}
}


onion_listen_point* onion_request_get_listen_point(onion_request* req){
return req->connection.listen_point;
}
5 changes: 5 additions & 0 deletions src/onion/request.h
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,11 @@ extern "C" {

/// Determine if the request was sent over a secure listen point
bool onion_request_is_secure(onion_request * req);

void onion_request_get_hash(onion_request * req, unsigned char* value);

onion_listen_point* onion_request_get_listen_point(onion_request* req);

#ifdef __cplusplus
}
#endif
Expand Down
Loading