src/t_htp_req.c
static int
t_htp_req_parseHeaders( lua_State *L, const char **data, const char *end )
{
	const char    *r  = eat_lws( *data );  ///< runner char
	const char    *k  = r;                 ///< marks start of key
	const char    *c  = r;                 ///< marks colon after key
	const char    *v  = r;                 ///< marks start of value
	enum t_htp_rs  rs = T_HTP_R_KY;        ///< local parse state = New Line Beginning

	lua_getfield( L, 1, "headers" );       // get pre-existing header table -> re-entrent

	// since exit condition is based on r+1 compare for (r+1)
	while (r+1 <= end)
	{
		switch (*r)
		{
			case '\n':
				if (' ' == *(r+1))                            // Value Continuation
					break;
				if (T_HTP_R_KY == rs)
				{
					lua_pushlstring( L, k, r-k-1 );            // didn't find colon; push entire line
					lua_rawseti( L, -2, lua_rawlen( L, -2 ) ); // push entire line as enumerated value
				}
				else
					t_htp_req_handleHeader( L, (char *) k, c, v, r );
				rs = T_HTP_R_KY;
				if ('\n' == *(r+1) || '\r' == *(r+1))         // double newLine -> END OF HEADER
				{
					(*data) = r + (('\n'==*(r+1))? 1 : 3);
					lua_pushstring( L, "content-length" );
					lua_rawget( L, -2 );
					lua_pushstring( L, "state" );              //S: req hdr cl "state"
					if (lua_isnil( L, -2 ) || (lua_isinteger( L, -2 ) && 0 == lua_tointeger( L, -2 )))
						lua_pushinteger( L, T_HTP_REQ_DONE );
					else
						lua_pushinteger( L, T_HTP_REQ_BODY );   //S: req hdr cl "state" ste
					lua_rawset( L, 1 );                        //S: req hdr cl
					lua_pop( L, 2 );                           //S: req
					return 1;
				}
				k = r+1;
				break;
			case  ':':
				if (T_HTP_R_KY == rs)
				{
					c  = r;
					r  = (char *) eat_lws( ++r );
					v  = r;
					rs = T_HTP_R_VL;
				}
				break;
			default:
				break;
		}
		r++;
	}
	lua_pop( L, 1 );  // pop header-table from stack
	return 0;
}
/**--------------------------------------------------------------------------
 * Parse HTTP Headers from the request.
 * Stack: requesttable data
 * \param  lua_State   L.
 * \param  char **data pointer within to data stream.
 * \param  char *end   end of data strem.
 * \return bool        0 means not enough data, 1 means done successfully
 * --------------------------------------------------------------------------*/
Parse HTTP Headers from the request.
Stack: requesttable data
\param  lua_State   L.
\param  char **data pointer within to data stream.
\param  char *end   end of data strem.
\return bool        0 means not enough data, 1 means done successfully
Parse HTTP Headers from the request . Stack : requesttable data \ param lua_State L . \ param char ** data pointer within to data stream . \ param char * end end of data strem . \ return bool 0 means not enough data 1 means done successfully
static int
t_htp_req_parseHeaders( lua_State *L, const char **data, const char *end )
{
	const char    *r  = eat_lws( *data );  
	const char    *k  = r;                 
	const char    *c  = r;                 
	const char    *v  = r;                 
	enum t_htp_rs  rs = T_HTP_R_KY;        
	lua_getfield( L, 1, "headers" );       
	while (r+1 <= end)
	{
		switch (*r)
		{
			case '\n':
				if (' ' == *(r+1))                            
					break;
				if (T_HTP_R_KY == rs)
				{
					lua_pushlstring( L, k, r-k-1 );            
					lua_rawseti( L, -2, lua_rawlen( L, -2 ) ); 
				}
				else
					t_htp_req_handleHeader( L, (char *) k, c, v, r );
				rs = T_HTP_R_KY;
				if ('\n' == *(r+1) || '\r' == *(r+1))         
				{
					(*data) = r + (('\n'==*(r+1))? 1 : 3);
					lua_pushstring( L, "content-length" );
					lua_rawget( L, -2 );
					lua_pushstring( L, "state" );              
					if (lua_isnil( L, -2 ) || (lua_isinteger( L, -2 ) && 0 == lua_tointeger( L, -2 )))
						lua_pushinteger( L, T_HTP_REQ_DONE );
					else
						lua_pushinteger( L, T_HTP_REQ_BODY );    ste
					lua_rawset( L, 1 );                        
					lua_pop( L, 2 );                           
					return 1;
				}
				k = r+1;
				break;
			case  ':':
				if (T_HTP_R_KY == rs)
				{
					c  = r;
					r  = (char *) eat_lws( ++r );
					v  = r;
					rs = T_HTP_R_VL;
				}
				break;
			default:
				break;
		}
		r++;
	}
	lua_pop( L, 1 );  
	return 0;
}
static int t_htp_req_parseHeaders ( lua_State * L , const char * * data , const char * end ) { const char * r = eat_lws ( * data ) ; const char * k = r ; const char * c = r ; const char * v = r ; enum t_htp_rs rs = T_HTP_R_KY ; lua_getfield ( L , 1 , " " ) ; while ( r + 1 <= end ) { switch ( * r ) { case ' \n ' : if ( ' ' == * ( r + 1 ) ) break ; if ( T_HTP_R_KY == rs ) { lua_pushlstring ( L , k , r - k - 1 ) ; lua_rawseti ( L , -2 , lua_rawlen ( L , -2 ) ) ; } else t_htp_req_handleHeader ( L , ( char * ) k , c , v , r ) ; rs = T_HTP_R_KY ; if ( ' \n ' == * ( r + 1 ) || ' \r ' == * ( r + 1 ) ) { ( * data ) = r + ( ( ' \n ' == * ( r + 1 ) ) ? 1 : 3 ) ; lua_pushstring ( L , " " ) ; lua_rawget ( L , -2 ) ; lua_pushstring ( L , " " ) ; if ( lua_isnil ( L , -2 ) || ( lua_isinteger ( L , -2 ) && 0 == lua_tointeger ( L , -2 ) ) ) lua_pushinteger ( L , T_HTP_REQ_DONE ) ; else lua_pushinteger ( L , T_HTP_REQ_BODY ) ; lua_rawset ( L , 1 ) ; lua_pop ( L , 2 ) ; return 1 ; } k = r + 1 ; break ; case ' ' : if ( T_HTP_R_KY == rs ) { c = r ; r = ( char * ) eat_lws ( ++ r ) ; v = r ; rs = T_HTP_R_VL ; } break ; default : break ; } r ++ ; } lua_pop ( L , 1 ) ; return 0 ; }
Parse HTTP Headers from the request.
Parse HTTP Headers from the request .
///< runner char ///< marks start of key ///< marks colon after key ///< marks start of value ///< local parse state = New Line Beginning // get pre-existing header table -> re-entrent // since exit condition is based on r+1 compare for (r+1) // Value Continuation // didn't find colon; push entire line // push entire line as enumerated value // double newLine -> END OF HEADER //S: req hdr cl "state" //S: req hdr cl "state" ste //S: req hdr cl //S: req // pop header-table from stack