@ -7,8 +7,8 @@
# include "interface/class.h"
# include "interface/class.h"
# define REMAINS(pars,done ) \
( ( pars ) - > buffer_used - ( ( done ) - ( pars ) - > buffer ) )
# define REMAINS(pars) \
( ( pars ) - > buffer_used - ( ( pars ) - > cur_data - ( pars ) - > buffer ) )
static
static
@ -43,34 +43,32 @@ void httpRequestParserGetHeader(HttpRequest, char *);
void
void
httpRequestParserParse ( HttpRequestParser this )
httpRequestParserParse ( HttpRequestParser this )
{
{
static HttpRequest request = NULL ;
static char * data ; / / static pointer to unprocessed data
char * line ;
char * line ;
int cont = 1 ;
int cont = 1 ;
while ( cont ) {
while ( cont ) {
switch ( this - > state ) {
switch ( this - > state ) {
case HTTP_REQUEST_GARBAGE :
case HTTP_REQUEST_GARBAGE :
data = this - > buffer ; / / initialize static pointer
httpRequestSkip ( & data ) ;
request = new ( HttpRequest ) ;
this - > cur_ data = this - > buffer ; / / initialize static pointer
httpRequestSkip ( & ( this - > cur_ data) ) ;
this - > cur_ request = new ( HttpRequest ) ;
this - > state = HTTP_REQUEST_START ;
this - > state = HTTP_REQUEST_START ;
break ;
break ;
case HTTP_REQUEST_START :
case HTTP_REQUEST_START :
if ( NULL = = ( line = httpRequestParserGetLine ( & data ) ) ) {
if ( NULL = = ( line = httpRequestParserGetLine ( & ( this - > cur_ data) ) ) ) {
cont = 0 ;
cont = 0 ;
break ;
break ;
}
}
httpRequestParserGetRequestLine ( request , line ) ;
httpRequestParserGetRequestLine ( this - > cur_ request, line ) ;
this - > state = HTTP_REQUEST_REQUEST_LINE_DONE ;
this - > state = HTTP_REQUEST_REQUEST_LINE_DONE ;
break ;
break ;
case HTTP_REQUEST_REQUEST_LINE_DONE :
case HTTP_REQUEST_REQUEST_LINE_DONE :
if ( NULL = = ( line = httpRequestParserGetLine ( & data ) ) ) {
if ( NULL = = ( line = httpRequestParserGetLine ( & ( this - > cur_ data) ) ) ) {
cont = 0 ;
cont = 0 ;
break ;
break ;
}
}
@ -80,19 +78,19 @@ httpRequestParserParse(HttpRequestParser this)
break ;
break ;
}
}
httpRequestParserGetHeader ( request , line ) ;
httpRequestParserGetHeader ( this - > cur_ request, line ) ;
break ;
break ;
case HTTP_REQUEST_HEADERS_DONE :
case HTTP_REQUEST_HEADERS_DONE :
httpHeaderSort ( request - > header , request - > nheader ) ;
httpHeaderSort ( this - > cur_ request- > header , this - > cur_ request- > nheader ) ;
{
{
char * nbody ;
char * nbody ;
if ( 0 = = request - > nbody ) {
if ( 0 = = this - > cur_ request- > nbody ) {
nbody = httpHeaderGet (
nbody = httpHeaderGet (
request - > header ,
request - > nheader ,
this - > cur_ request- > header ,
this - > cur_ request- > nheader ,
" Content-Length " ) ;
" Content-Length " ) ;
if ( NULL = = nbody ) {
if ( NULL = = nbody ) {
@ -100,14 +98,16 @@ httpRequestParserParse(HttpRequestParser this)
break ;
break ;
}
}
else {
else {
request - > nbody = atoi ( nbody ) ;
this - > cur_ request- > nbody = atoi ( nbody ) ;
}
}
}
}
if ( REMAINS ( this , data ) > = request - > nbody ) {
request - > body = calloc ( 1 , request - > nbody + 1 ) ;
memcpy ( request - > body , data , request - > nbody ) ;
data + = request - > nbody ;
if ( REMAINS ( this ) > = this - > cur_request - > nbody ) {
this - > cur_request - > body = calloc ( 1 , this - > cur_request - > nbody + 1 ) ;
memcpy ( this - > cur_request - > body ,
this - > cur_data ,
this - > cur_request - > nbody ) ;
this - > cur_data + = this - > cur_request - > nbody ;
this - > state = HTTP_REQUEST_DONE ;
this - > state = HTTP_REQUEST_DONE ;
}
}
}
}
@ -119,14 +119,14 @@ httpRequestParserParse(HttpRequestParser this)
* enqueue current request
* enqueue current request
*/
*/
this - > request_queue - > requests [ ( this - > request_queue - > nrequests ) + + ] =
this - > request_queue - > requests [ ( this - > request_queue - > nrequests ) + + ] =
request ;
this - > cur_ request;
/**
/**
* remove processed stuff from input buffer .
* remove processed stuff from input buffer .
*/
*/
memmove ( this - > buffer , data , REMAINS ( this , data ) ) ;
memmove ( this - > buffer , this - > cur_ data, REMAINS ( this ) ) ;
this - > buffer_used - = data - this - > buffer ;
this - > buffer_used - = this - > cur_ data - this - > buffer ;
/**
/**
* dont continue loop if input buffer is empty
* dont continue loop if input buffer is empty