Exemplo n.º 1
0
void *atenderPeticion (void *arguments)
{
    strarg *threadArguments = (strarg*)arguments;
    char *httpMessage = malloc(8092);
    char logMessage[BUFFER_SIZE];
    int salida = 0;
    
    do
    {
        salida = readMessage(threadArguments->socketDescriptor, httpMessage);
        if(salida == -1)
        {
            sprintf(logMessage, "ERROR: Cliente %d desconectado abruptamente. Cerrando thread\n", threadArguments->socketDescriptor);;;
        }
        else if (salida == 0)
        {
            sprintf(logMessage, "Cliente %d desconectado de forma normal.\n", threadArguments->socketDescriptor);
        }
        logger(logMessage);
        processHttpMessage(httpMessage, threadArguments->socketDescriptor);
    }
    while(salida > 0);  //TODO El corte debería ser el error de read_message, por ejemplo que se desconecto el cliente es un 0...
    //FD_CLR(argumentosDelThread->socketDescriptor, &readset);
    return NULL;
}
Exemplo n.º 2
0
int CHttpServer::runClientHandler(int nClientFD)
{

	int result;
	char pBuf[BUF_SIZE];
	void* pvBuf = pBuf;
	http_parser *parser = (http_parser*)malloc(sizeof(http_parser));
	http_parser_settings *settings = (http_parser_settings*)malloc(sizeof(http_parser_settings)); 
	size_t nparsed;
	struct message *m;

	m = (message *)malloc(sizeof(message));
	memset(m, 0, sizeof(message));
	pthread_setspecific(m_threadDatakey, m);
	
	settings->on_url = onURL;
	settings->on_header_field = onHeaderField;
	settings->on_header_value = onHeaderValue;
	settings->on_body = onBody;
	settings->on_message_begin = onMessageBegin;
	settings->on_headers_complete = onHeadersComplete;
	settings->on_message_complete = onMessageComplete;

	http_parser_init(parser, HTTP_REQUEST);
  
	while (1)
	{
		memset(pBuf, 0, sizeof(pBuf));
		result = socketrecv(nClientFD, &pvBuf);
		if (0 >= result)
		{
			nparsed = http_parser_execute(parser, settings, pBuf, 0);
			_DBG("nparsed: %lu state: %d method: %d", nparsed, parser->state, parser->method);
			socketClose(nClientFD);
			_DBG("socket close client: %d", nClientFD);
			break;
		}

		if (0 < result)
		{
			nparsed = http_parser_execute(parser, settings, pBuf, result);
			_DBG("nparsed: %lu state: %d method: %d", nparsed, parser->state, parser->method);
			if (nparsed != (size_t)result)
			{
				/* Error. Close connecttion */
				socketClose(nClientFD);
				_DBG("http_parse error!");
				break;
			}

			if (m->message_complete_cb_called)
			{
				processHttpMessage(nClientFD, m);
			}
		}
	}

	free(parser);
	free(settings);

	sendMessage(EVENT_FILTER_HTTP_SERVER, EVENT_COMMAND_THREAD_EXIT,
			threadHandler->getThreadID(), 0, NULL);

	threadHandler->threadSleep(1);
	threadHandler->threadExit();

	return 0;
}