Skip to content
Browse files

Created onion_request_process, that processes a request calling the r…

…equired handlers and returning the close status. Added mutex to oterm_t structure. Removed some commented code.
  • Loading branch information...
1 parent 34be3e9 commit 335ff7c97536826e36e797068567b35e20794e62 @davidmoreno committed Nov 16, 2010
View
BIN examples/oterm/favicon.ico
Binary file not shown.
View
17 examples/oterm/oterm_handler.c
@@ -31,6 +31,8 @@
#include <onion_response.h>
#include <onion_handler.h>
+#include <pthread.h>
+
#ifdef __DEBUG__
#include <onion_handler_directory.h>
#else
@@ -60,6 +62,7 @@ typedef struct process_t{
* @short Information about all the processes.
*/
typedef struct{
+ pthread_mutex_t head_mutex;
process *head;
onion_handler *data;
}oterm_t;
@@ -74,13 +77,17 @@ static int oterm_out(process *o, onion_request *req);
/// Returns the term from the list of known terms. FIXME, make this structure a tree or something faster than linear search.
process *oterm_get_process(oterm_t *o, const char *id){
+ pthread_mutex_lock( &o->head_mutex );
process *p=o->head;
int pid=atoi(id);
while(p){
- if (p->pid==pid)
+ if (p->pid==pid){
+ pthread_mutex_unlock( &o->head_mutex );
return p;
+ }
p=p->next;
}
+ pthread_mutex_unlock( &o->head_mutex );
return NULL;
}
@@ -149,8 +156,10 @@ process *oterm_new(oterm_t *o){
exit(1);
}
// I set myself at head
+ pthread_mutex_lock( &o->head_mutex );
oterm->next=o->head;
o->head=oterm;
+ pthread_mutex_unlock( &o->head_mutex );
return oterm;
}
@@ -162,6 +171,7 @@ int oterm_status(oterm_t *o, onion_request *req){
onion_response_write_headers(res);
onion_response_write0(res,"{");
+
process *n=o->head;
while(n->next){
@@ -235,13 +245,16 @@ void oterm_oterm_free(oterm_t *o){
free(t);
}
onion_handler_free(o->data);
+ pthread_mutex_destroy(&o->head_mutex);
free(o);
}
/// Prepares the oterm handler
onion_handler *oterm_handler_data(){
- oterm_t *oterm=malloc(sizeof(oterm));
+ oterm_t *oterm=malloc(sizeof(oterm_t));
+ pthread_mutex_init(&oterm->head_mutex, NULL);
+ oterm->head=NULL;
oterm->head=oterm_new(oterm);
#ifdef __DEBUG__
onion_handler *data=onion_handler_directory(".");
View
4 src/onion/onion.c
@@ -296,11 +296,9 @@ static gnutls_session_t onion_prepare_gnutls_session(onion *o, int clientfd){
}
static void onion_enable_tls(onion *o){
-#ifdef HAVE_GNUTLS
#ifdef HAVE_PTHREADS
gcry_control (GCRYCTL_SET_THREAD_CBS, &gcry_threads_pthread);
#endif
-#endif
if (!(o->flags&O_USE_DEV_RANDOM)){
gcry_control(GCRYCTL_ENABLE_QUICK_RANDOM, 0);
}
@@ -384,7 +382,7 @@ void *onion_request_thread(void *d){
if (0!=close(td->clientfd)){
perror("Error closing connection");
}
- fprintf(stderr,"%s:%d Close connection %d\n",__FILE__,__LINE__,td->clientfd);
+ fprintf(stderr,"%s:%d Closed connection %d\n",__FILE__,__LINE__,td->clientfd);
pthread_mutex_lock (&o->mutex);
td->o->active_threads_count--;
View
45 src/onion/onion_request.c
@@ -174,11 +174,34 @@ int onion_request_parse_query(onion_request *req){
return 1;
}
+/**
+ * @short Performs the processing of the request. FIXME. Make really close the connection on Close-Connection. It does not do it now.
+ *
+ * Returns the OR_KEEP_ALIVE or OR_CLOSE_CONNECTION value. If on keep alive the struct is already reinitialized.
+ */
+int onion_request_process(onion_request *req){
+ int status=onion_handler_handle(req->server->root_handler, req);
+ if (status==OR_KEEP_ALIVE){ // if keep alive, reset struct to get the new petition.
+ onion_dict_free(req->headers);
+ req->headers=onion_dict_new();
+ req->flags=0;
+ if (req->fullpath){
+ free(req->fullpath);
+ req->path=req->fullpath=NULL;
+ }
+ if (req->query){
+ onion_dict_free(req->query);
+ req->query=NULL;
+ }
+ return OR_KEEP_ALIVE;
+ }
+ return OR_CLOSE_CONNECTION;
+}
/**
* @short Write some data into the request, and performs the query if necesary.
*
- * This is where alomst all logic has place: it reads from the given data until it has all the headers
+ * This is where almost all logic has place: it reads from the given data until it has all the headers
* and launchs the root handler to perform the petition.
*/
int onion_request_write(onion_request *req, const char *data, unsigned int length){
@@ -188,25 +211,9 @@ int onion_request_write(onion_request *req, const char *data, unsigned int lengt
char c=data[i];
if (c=='\n'){
if (req->buffer_pos==0){ // If true, then headers are over. Do the processing.
- int status=onion_handler_handle(req->server->root_handler, req);
- if (status==OR_CLOSE_CONNECTION){
- //fprintf(stderr,"%s:%d Close connection %p\n",__FILE__,__LINE__,req);
+ int s=onion_request_process(req);
+ if (s==OR_CLOSE_CONNECTION) // close the connection.
return -i;
- }
- else{ // I guess keep alive. This side wants to keep alive, but maybe other side closes it.
- //fprintf(stderr,"%s:%d Keep alive %p\n",__FILE__,__LINE__,req);
- onion_dict_free(req->headers);
- req->headers=onion_dict_new();
- req->flags=0;
- if (req->fullpath){
- free(req->fullpath);
- req->path=req->fullpath=NULL;
- }
- if (req->query){
- onion_dict_free(req->query);
- req->query=NULL;
- }
- }
// I do not stop as it might have more data: keep alive.
}
else{
View
13 src/onion/onion_response.c
@@ -126,36 +126,27 @@ int onion_response_write(onion_response *res, const char *data, unsigned int len
length-=wb;
data+=wb;
}
- //fprintf(stderr,"at tail %d\n",length);
+
memcpy(&res->buffer[res->buffer_pos], data, length);
res->buffer_pos+=length;
- //res->buffer[res->buffer_pos]=0;
- //fprintf(stderr,"head is %s\n",res->buffer);
+
return length;
}
/// Writes all buffered output waiting for sending.
static void onion_response_write_buffer(onion_response *res){
- //fprintf(stderr,"%s:%d Write buffer. %d bytes.\n",__FILE__,__LINE__,res->buffer_pos);
void *fd=onion_response_get_socket(res);
onion_write write=res->request->server->write;
int w;
int pos=0;
- //res->buffer[res->buffer_pos]=0;
- //fprintf(stderr,"%s:%d write %s\n",__FILE__,__LINE__,res->buffer);
while ( (w=write(fd, &res->buffer[pos], res->buffer_pos)) != res->buffer_pos){
- //fprintf(stderr, "wrote %d/%d bytes\n",w,length);
if (w<=0){
fprintf(stderr,"%s:%d Error writing. Maybe closed connection. Code %d. ",basename(__FILE__),__LINE__,w);
-#ifdef HAVE_GNUTLS
- //fprintf(stderr,"%s:%d %s\n",basename(__FILE__),__LINE__, gnutls_strerror (w));
-#endif
perror("");
res->buffer_pos=0;
return;
}
pos+=w;
- //fprintf(stderr,"Buffer_pos %d, w %d\n",res->buffer_pos,w);
res->buffer_pos-=w;
}
res->buffer_pos=0;

0 comments on commit 335ff7c

Please sign in to comment.
Something went wrong with that request. Please try again.