Exemple #1
0
DWORD 
StartHttpListener(
	PHTTP_LISTENER _listener,
	IN int urlCount, 
	IN _TCHAR* urls[]
)
{
	int UrlAdded = 0;	
	DWORD result;
	
	ZeroMemory(&_listener->UrlGroupId, sizeof(HTTP_URL_GROUP_ID));
	result = HttpCreateUrlGroup(
				_listener->SessionId, 
				&_listener->UrlGroupId, 
				NULL);
	if(result)
	{		
		DEBUG_ASSERT(false);
		return result;
	}

	HTTP_BINDING_INFO httpBinding; 
	ZeroMemory(&httpBinding, sizeof(httpBinding));
	httpBinding.RequestQueueHandle = _listener->hRequestQueue;
	httpBinding.Flags.Present = true;
	result = HttpSetUrlGroupProperty(
						_listener->UrlGroupId, 
						HttpServerBindingProperty, 
						&httpBinding, 
						sizeof(httpBinding));

	// Add the urls to the UrlGroup
	for (int i = 1; i < urlCount; i++)
    {
        wprintf(L"we are listening for requests on the following url: %s\n", urls[i]);

        result = HttpAddUrlToUrlGroup(
									_listener->UrlGroupId,
									urls[i],
									NULL,
									NULL);

        if (result != NO_ERROR)
        {
			_listener->errorCode = result;
			wprintf(L"HttpAddUrl failed for %s with %lu \n\t", urls[i], result);			
            return result;
        }
        else
        {
            UrlAdded ++;
        }
    }	
	_listener->urls = urls;
	_listener->urlsCount = UrlAdded;

	// We will always have 10 oustanding requests.
	for(int j = 0 ;j < HTTP_LISTENER_MAX_PENDING_RECEIVES; j++)
	{
		result = EnsurePump(_listener);	
		if(result != NO_ERROR)
		{
			break;
		}
	}

	_listener->TimerQueue = CreateTimerQueue();
	_listener->LookAsideFlushPeriod = 5 *1000;
	if(_listener->TimerQueue != INVALID_HANDLE_VALUE)
	{
		if(CreateTimerQueueTimer(&_listener->FlushTimer, 
						_listener->TimerQueue, 
						HttpListenerFlushLookasideThreadProc, 
						_listener, 
						_listener->LookAsideFlushPeriod, 
						_listener->LookAsideFlushPeriod, 
						NULL) != FALSE)
		{
			result = NO_ERROR;
		}
		else
		{
			result = GetLastError();
		}
	}
	else
	{
		result = GetLastError();
	}

	if(result == NO_ERROR)
	{
		_listener->State = HTTP_LISTENER_STATE_STARTED;		
	}

	return result;
}
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;
}