Update HTTP/HTTPS stack (BUGGY version).

This commit is contained in:
bossiel 2010-03-10 00:16:39 +00:00
parent 03abbe8eec
commit 7452e9c929
9 changed files with 93 additions and 87 deletions

View File

@ -87,72 +87,72 @@ thttp_operation_handle_t* thttp_stack_get_op(thttp_stack_handle_t *self, tnet_fd
static int thttp_transport_layer_stream_cb(const tnet_transport_event_t* e)
{
int ret = -1;
tsk_ragel_state_t state;
thttp_message_t *message = THTTP_NULL;
int endOfheaders = -1;
const thttp_t *stack = e->callback_data;
switch(e->type){
case event_data: {
break;
}
case event_closed:
case event_connected:
default:{
return 0;
}
}
/* Check if buffer is too big to be valid (have we missed some chuncks?) */
if(TSK_BUFFER_SIZE(transport->buff_stream) >= 0xFFFF){
tsk_buffer_cleanup(transport->buff_stream);
}
/* Append new content. */
tsk_buffer_append(transport->buff_stream, e->data, e->size);
/* Check if we have all HTTP headers. */
if((endOfheaders = tsk_strindexOf(TSK_BUFFER_DATA(transport->buff_stream),TSK_BUFFER_SIZE(transport->buff_stream), "\r\n\r\n"/*2CRLF*/)) < 0){
TSK_DEBUG_INFO("No all HTTP headers in the TCP buffer.");
goto bail;
}
/* If we are there this mean that we have all HTTP headers.
* ==> Parse the HTTP message without the content.
*/
tsk_ragel_state_init(&state, TSK_BUFFER_DATA(transport->buff_stream), endOfheaders + 4/*2CRLF*/);
if(thttp_message_parse(&state, &message, THTTP_FALSE/* do not extract the content */) == THTTP_TRUE
&& message->firstVia && message->Call_ID && message->CSeq && message->From && message->To)
{
size_t clen = THTTP_MESSAGE_CONTENT_LENGTH(message); /* MUST have content-length header (see RFC 3261 - 7.5). If no CL header then the macro return zero. */
if(clen == 0){ /* No content */
tsk_buffer_remove(transport->buff_stream, 0, (endOfheaders + 4/*2CRLF*/)); /* Remove HTTP headers and CRLF */
}
else{ /* There is a content */
if((endOfheaders + 4/*2CRLF*/ + clen) > TSK_BUFFER_SIZE(transport->buff_stream)){ /* There is content but not all the content. */
TSK_DEBUG_INFO("No all HTTP content in the TCP buffer.");
goto bail;
}
else{
/* Add the content to the message. */
thttp_message_add_content(message, THTTP_NULL, TSK_BUFFER_TO_U8(transport->buff_stream) + endOfheaders + 4/*2CRLF*/, clen);
/* Remove HTTP headers, CRLF and the content. */
tsk_buffer_remove(transport->buff_stream, 0, (endOfheaders + 4/*2CRLF*/ + clen));
}
}
}
if(message){
/* Handle the incoming message. */
ret = thttp_transport_layer_handle_incoming_msg(transport, message);
/* Set fd */
message->sockfd = e->fd;
}
else ret = -15;
bail:
TSK_OBJECT_SAFE_FREE(message);
// tsk_ragel_state_t state;
// thttp_message_t *message = THTTP_NULL;
// int endOfheaders = -1;
// const thttp_t *stack = e->callback_data;
//
// switch(e->type){
// case event_data: {
// break;
// }
// case event_closed:
// case event_connected:
// default:{
// return 0;
// }
// }
//
//
// /* Check if buffer is too big to be valid (have we missed some chuncks?) */
// if(TSK_BUFFER_SIZE(transport->buff_stream) >= 0xFFFF){
// tsk_buffer_cleanup(transport->buff_stream);
// }
//
// /* Append new content. */
// tsk_buffer_append(transport->buff_stream, e->data, e->size);
//
// /* Check if we have all HTTP headers. */
// if((endOfheaders = tsk_strindexOf(TSK_BUFFER_DATA(transport->buff_stream),TSK_BUFFER_SIZE(transport->buff_stream), "\r\n\r\n"/*2CRLF*/)) < 0){
// TSK_DEBUG_INFO("No all HTTP headers in the TCP buffer.");
// goto bail;
// }
//
// /* If we are there this mean that we have all HTTP headers.
// * ==> Parse the HTTP message without the content.
// */
// tsk_ragel_state_init(&state, TSK_BUFFER_DATA(transport->buff_stream), endOfheaders + 4/*2CRLF*/);
// if(thttp_message_parse(&state, &message, THTTP_FALSE/* do not extract the content */) == THTTP_TRUE
// && message->firstVia && message->Call_ID && message->CSeq && message->From && message->To)
// {
// size_t clen = THTTP_MESSAGE_CONTENT_LENGTH(message); /* MUST have content-length header (see RFC 3261 - 7.5). If no CL header then the macro return zero. */
// if(clen == 0){ /* No content */
// tsk_buffer_remove(transport->buff_stream, 0, (endOfheaders + 4/*2CRLF*/)); /* Remove HTTP headers and CRLF */
// }
// else{ /* There is a content */
// if((endOfheaders + 4/*2CRLF*/ + clen) > TSK_BUFFER_SIZE(transport->buff_stream)){ /* There is content but not all the content. */
// TSK_DEBUG_INFO("No all HTTP content in the TCP buffer.");
// goto bail;
// }
// else{
// /* Add the content to the message. */
// thttp_message_add_content(message, THTTP_NULL, TSK_BUFFER_TO_U8(transport->buff_stream) + endOfheaders + 4/*2CRLF*/, clen);
// /* Remove HTTP headers, CRLF and the content. */
// tsk_buffer_remove(transport->buff_stream, 0, (endOfheaders + 4/*2CRLF*/ + clen));
// }
// }
// }
//
// if(message){
// /* Handle the incoming message. */
// ret = thttp_transport_layer_handle_incoming_msg(transport, message);
// /* Set fd */
// message->sockfd = e->fd;
// }
// else ret = -15;
//
//bail:
// TSK_OBJECT_SAFE_FREE(message);
return ret;
}
@ -366,9 +366,8 @@ thttp_operation_handle_t* thttp_stack_get_op(thttp_stack_handle_t *self, tnet_fd
thttp_operation_handle_t* ret = 0;
thttp_stack_t *stack = self;
tsk_list_item_t *item;
thttp_operation_id_t id;
if(!stack || !stack->ops || !op){
if(!stack || !stack->ops){
return 0;
}
@ -396,8 +395,12 @@ int thttp_stack_add_op(thttp_stack_handle_t *self, thttp_operation_handle_t* op)
}
tsk_safeobj_lock(stack);
/* ref() called by the operation's ctor,
unref will be called when removed from the list. */
tsk_list_push_back_data(stack->ops, &op);
tsk_safeobj_unlock(stack);
return 0;
}
int thttp_stack_remove_op(thttp_stack_handle_t *self, thttp_operation_handle_t* op)
@ -449,7 +452,7 @@ static void* _thttp_stack_create(void * self, va_list * app)
if(stack){
tsk_safeobj_init(stack);
stack->ops = TSK_LIST_CREATE();
stack->ops = TSK_LIST_CREATE_AS_NOT_OWNER();
}
return self;
}
@ -472,9 +475,9 @@ static void* thttp_stack_destroy(void * self)
TSK_FREE(stack->local_ip);
TSK_OBJECT_SAFE_FREE(stack->transport);
/* Operations */
tsk_safeobj_lock(stack);
TSK_OBJECT_SAFREE(stack->ops);
TSK_OBJECT_SAFE_FREE(stack->ops);
tsk_safeobj_unlock(stack);
tsk_safeobj_deinit(stack);

View File

@ -254,6 +254,9 @@ static void* thttp_operation_create(void * self, va_list * app)
else{
operation->id = ++unique_id;
}
/* Add the operation to the stack. The stack will not own the op. */
thttp_stack_add_op(operation->stack, operation);
}
return self;
@ -263,6 +266,10 @@ static void* thttp_operation_destroy(void * self)
{
thttp_operation_t *operation = self;
if(operation){
/* Remove the operation from the stack. The stack do not own the op. */
thttp_stack_remove_op(operation->stack, operation);
TSK_OBJECT_SAFE_FREE(operation->stack);
TSK_OBJECT_SAFE_FREE(operation->params);
TSK_OBJECT_SAFE_FREE(operation->headers);

View File

@ -33,9 +33,9 @@ void test_stack()
thttp_stack_handle_t* stack = thttp_stack_create(test_stack_callback,
THTTP_STACK_SET_NULL());
if(thttp_stack_start(stack)){
goto bail;
}
//if(thttp_stack_start(stack)){
// goto bail;
//}
/*
op = THTTP_OPERATION_CREATE(stack,
THTTP_OPERATION_SET_PARAM("method", "GET"),
@ -60,7 +60,7 @@ void test_stack()
THTTP_OPERATION_SET_HEADER("User-Agent", "XDM-client/OMA1.1"),
THTTP_OPERATION_SET_NULL());
thttp_operation_perform(op);
//thttp_operation_perform(op);
/*thttp_operation_set(op,
THTTP_OPERATION_SET_PARAM("method", "HEAD"),

View File

@ -221,12 +221,10 @@ int tnet_transport_set_callback(const tnet_transport_handle_t *handle, tnet_tran
int tnet_transport_shutdown(tnet_transport_handle_t* handle)
{
if(handle)
{
if(handle){
return tnet_transport_stop(handle);
}
else
{
else{
TSK_DEBUG_ERROR("NULL transport object.");
}

View File

@ -552,8 +552,7 @@ void *tnet_transport_mainthread(void *param)
bail:
transport->active = 0;
TSK_OBJECT_SAFE_FREE(context);
TSK_DEBUG_INFO("Stopping [%s] server with IP {%s} on port {%d}...", transport->description, transport->master->ip, transport->master->port);
return 0;
}

View File

@ -629,8 +629,7 @@ void *tnet_transport_mainthread(void *param)
bail:
transport->active = 0;
TSK_OBJECT_SAFE_FREE(context);
TSK_DEBUG_INFO("Stopping [%s] server with IP {%s} on port {%d}...", transport->description, transport->master->ip, transport->master->port);
return 0;
}

View File

@ -36,7 +36,7 @@
*/
#if defined (_DEBUG) || defined (DEBUG)
# define TSK_DEBUG_OBJECTS 0
# define TSK_DEBUG_OBJECTS 1
static int tsk_objects_count = 0;
#else
# define TSK_DEBUG_OBJECTS 0

View File

@ -33,8 +33,8 @@
#define LOOP 1
#define RUN_TEST_ALL 1
#define RUN_TEST_LISTS 0
#define RUN_TEST_ALL 0
#define RUN_TEST_LISTS 1
#define RUN_TEST_HEAP 0
#define RUN_TEST_STRINGS 0
#define RUN_TEST_URL 0

View File

@ -261,7 +261,7 @@
<a href="http://www.doubango.org/">Home</a> | <a href="http://doubango.blogspot.com/">
Stay Tuned</a> | <a href="http://doubango.googlecode.com/svn/trunk/website/supports">
Supports</a> | <a href="http://doubango.googlecode.com/svn/trunk/website/contacts.html">
Contacts</a> | Copyright 2009-2010 Mamadou DIOP. Designed by <a href="http://www.winkhosting.com/">
Contacts</a> | &copy; 2009-2010 Mamadou DIOP. Designed by <a href="http://www.winkhosting.com/">
Wink Hosting</a>.
</div>
</div>