예제 #1
0
DWORD
CreateHttpListener(
	OUT PHTTP_LISTENER* httpListener
	)
{

	ULONG           result;

	PHTTP_LISTENER _listener = (PHTTP_LISTENER)ALLOC_MEM(sizeof(HTTP_LISTENER)); 
	ZeroMemory(_listener, sizeof(HTTP_LISTENER));
	(*httpListener) = _listener;

	_listener->hRequestQueue = NULL;
	_listener->RequestQueueLength = 5000; // Default request queue length;
	_listener->errorCode = 0;
	_listener->urls = NULL;
	_listener->pthreadPoolIO = NULL;
	_listener->State = HTTP_LISTENER_STATE_FAULTED;	
	_listener->stats = (PLISTENER_STATS)_aligned_malloc(sizeof(LISTENER_STATS),MEMORY_ALLOCATION_ALIGNMENT);
	HTTPAPI_VERSION HttpApiVersion = HTTPAPI_VERSION_2;	

	//
    // Initialize HTTP APIs.
    //
    result = HttpInitialize( 
                HttpApiVersion,
                HTTP_INITIALIZE_SERVER,    // Flags
                NULL                       // Reserved
                );

    if (result != NO_ERROR)
    {
		DEBUG_ASSERT(false);
		LOG_ERROR(L"\nHttpInitialize failed with %lu", result);		
    }
	
	if(result == NO_ERROR)
	{
		result = HttpCreateServerSession(HttpApiVersion,
										&_listener->SessionId, 
										NULL); 
		if(result)
		{		
			LOG_ERROR(L"\nHttpCreateServerSession failed with %lu", result);			
		}
	}

	if(result == NO_ERROR)
	{
		result = HttpCreateRequestQueue(HttpApiVersion, 
										NULL, 
										NULL, 
										0,
										&_listener->hRequestQueue);
		if(result)
		{
			LOG_ERROR(L"\nHttpCreateRequestQueue failed with %lu", result);				
		}
	}

	if(result == NO_ERROR)
	{
		result = HttpSetRequestQueueProperty(_listener->hRequestQueue, 
											HttpServerQueueLengthProperty, 
											&_listener->RequestQueueLength,
											sizeof(_listener->RequestQueueLength),
											NULL,
											NULL);
		if(result)
		{
			LOG_ERROR(L"\nHttpSetRequestQueueProperty failed with %lu", result);				
		}
	}
	
	if(result == NO_ERROR)
	{
		if(SetFileCompletionNotificationModes(_listener->hRequestQueue, 
											FILE_SKIP_COMPLETION_PORT_ON_SUCCESS | 
											FILE_SKIP_SET_EVENT_ON_HANDLE) == FALSE)
		{
			result = GetLastError();			
		}
	}

	if(result == NO_ERROR)
	{	
		result = HttpListenerInitializeThreadPool(_listener);
	}

	if(result == NO_ERROR)
	{
		_listener->pthreadPoolIO =  CreateThreadpoolIo(_listener->hRequestQueue, 
														HttpListenerDemuxer, 
														(void*)_listener, 
														&_listener->tpEnvironment);
	}


	InitializeIOContextCache();
	InitializeHttpInputQueue(_listener);
	_listener->errorCode = result;
	return _listener->errorCode;
}
예제 #2
0
BOOL InitializeHttpServer(
    PWCHAR pwszUrlPathToListenFor,
    PWCHAR pwszRootDirectory,
    PSERVER_CONTEXT pServerContext
)
{
    ULONG ulResult;
    HRESULT hResult;

    hResult = StringCbCopyW(
                  pServerContext->wszRootDirectory,
                  MAX_STR_SIZE,
                  pwszRootDirectory);

    if (FAILED(hResult))
    {
        fprintf(stderr, "Invalid command line arguments. Application stopped.\n");
        return FALSE;
    }

    ulResult = HttpInitialize(
                   g_HttpApiVersion,
                   HTTP_INITIALIZE_SERVER,
                   NULL);

    if (ulResult != NO_ERROR)
    {
        fprintf(stderr, "HttpInitialized failed\n");
        return FALSE;
    }

    pServerContext->bHttpInit = TRUE;

    ulResult = HttpCreateServerSession(
                   g_HttpApiVersion,
                   &(pServerContext->sessionId),
                   0);

    if (ulResult != NO_ERROR)
    {
        fprintf(stderr, "HttpCreateServerSession failed\n");
        return FALSE;
    }

    ulResult = HttpCreateUrlGroup(
                   pServerContext->sessionId,
                   &(pServerContext->urlGroupId),
                   0);

    if (ulResult != NO_ERROR)
    {
        fprintf(stderr, "HttpCreateUrlGroup failed\n");
        return FALSE;
    }

    ulResult = HttpAddUrlToUrlGroup(
                   pServerContext->urlGroupId,
                   pwszUrlPathToListenFor,
                   (HTTP_URL_CONTEXT) NULL,
                   0);

    if (ulResult != NO_ERROR)
    {
        fwprintf(stderr, L"HttpAddUrlToUrlGroup failed with code 0x%x for url %s\n",
                 ulResult, pwszUrlPathToListenFor);
        return FALSE;
    }

    return TRUE;
}