Exemplo n.º 1
0
CgHttpRequest *cg_http_request_new()
{
    CgHttpRequest *httpReq;

    cg_log_debug_l4("Entering...\n");

    httpReq = (CgHttpRequest *)malloc(sizeof(CgHttpRequest));

    if ( NULL != httpReq )
    {
        cg_http_packet_init((CgHttpPacket *)httpReq);
        httpReq->method = cg_string_new();
        httpReq->version = cg_string_new();
        httpReq->uri = cg_string_new();
        httpReq->userAgent = cg_string_new();
        httpReq->httpRes = cg_http_response_new();
        httpReq->postURL = cg_net_url_new();

        cg_http_request_setversion(httpReq, CG_HTTP_VER11);
        cg_http_request_setuseragent(httpReq, CG_HTTP_USERAGENT_DEFAULT);

        cg_http_request_setsocket(httpReq, NULL);
        cg_http_request_setuserdata(httpReq, NULL);

        cg_http_request_settimeout(httpReq, CG_HTTP_CONN_TIMEOUT);
    }

    cg_log_debug_l4("Leaving...\n");

    return httpReq;
}
Exemplo n.º 2
0
static void cg_http_server_clientthread(CgThread *thread)
{
	CgHttpServerClientData *clientData;
	CgHttpServer *httpServer;
	CgSocket *clientSock;
	void *httpServerUserData;
	CgHttpRequest *httpReq;
	char *version = NULL;

	cg_log_debug_l4("Entering...\n");

	clientData = (CgHttpServerClientData *)cg_thread_getuserdata(thread);
	httpServer = clientData->httpServer;
	clientSock = clientData->clientSock;
	httpServerUserData = cg_http_server_getuserdata(httpServer);

	httpReq = cg_http_request_new();
	cg_http_request_setsocket(httpReq, clientSock);

	/**** Thanks for Makela Aapo (10/31/05) ****/
	while (cg_http_request_read(httpReq, clientSock) == TRUE && cg_thread_isrunnable(thread) == TRUE) {
		/* Check some validity of the request */
		version = cg_http_request_getversion(httpReq);
		if (cg_strcmp(version, CG_HTTP_VER11) == 0)
		{
			/* According to HTTP/1.1 spec, we must not tolerate
			   HTTP/1.1 request without HOST-header */
			if (cg_http_request_gethost(httpReq) == NULL)
			{
				cg_http_request_postbadrequest(httpReq);
				continue;
			}
		}

		if (httpServer->listener != NULL) {
            cg_http_request_setuserdata(httpReq, httpServerUserData);
			httpServer->listener(httpReq);
		}

		/* Close connection according to HTTP version and headers */
		if (cg_strcmp(version, CG_HTTP_VER10) == 0)
		{
			/* Terminate connection after HTTP/1.0 request */
			break;
		}

		/* We are having HTTP/1.1 or better => terminate, if requested */
		if (cg_http_request_iskeepaliveconnection(httpReq) == FALSE)
		{
			break;
		}
	}

	cg_log_debug_s("Dropping HTTP client\n");
	cg_http_request_delete(httpReq);

	cg_socket_close(clientSock);
	cg_socket_delete(clientSock);

	cg_http_server_clientdata_delete(clientData);
	cg_thread_setuserdata(thread, NULL);

    // This code frequently crashes. mutex lock referencing free'd memory.
	cg_http_server_lock(httpServer);
	cg_thread_remove(thread);
	cg_http_server_unlock(httpServer);

	cg_log_debug_l4("Leaving...\n");

	cg_thread_delete(thread);
}