Condividi tramite


Metodo IHttpContext::CloneContext

Crea un clone del contesto di richiesta corrente.

Sintassi

virtual HRESULT CloneContext(  
   IN DWORD dwCloneFlags,  
   OUT IHttpContext** ppHttpContext  
) = 0;  

Parametri

dwCloneFlags
[IN] Oggetto DWORD contenente i flag di clonazione.

ppHttpContext
[OUT] Puntatore dereferenced a un oggetto IHttpContext.

Valore restituito

Oggetto HRESULT. I valori possibili includono, ma non sono limitati a, quelli indicati nella tabella seguente.

Valore Descrizione
S_OK Indica che l'operazione ha avuto esito positivo.
ERROR_INVALID_PARAMETER Indica che un parametro specificato non è valido.
ERROR_NOT_ENOUGH_MEMORY Indica che la memoria non è sufficiente per eseguire l'operazione.

Commenti

Il CloneContext metodo crea un clone del contesto di richiesta corrente. È possibile controllare il comportamento di clonazione specificando i flag appropriati nel dwCloneFlags parametro. Nella tabella seguente sono elencati i valori possibili per questi flag.

Valore Descrizione
CLONE_FLAG_BASICS Clonare l'URL, la stringa di query e il metodo HTTP.
CLONE_FLAG_HEADERS Clonare le intestazioni della richiesta.
CLONE_FLAG_ENTITY Clonare il corpo dell'entità.
CLONE_FLAG_NO_PRECONDITION Non includere intestazioni "range" e "if-" per la richiesta.
CLONE_FLAG_NO_DAV Non includere intestazioni WebDAV per la richiesta.

Dopo aver creato un contesto clonato, è possibile usare il clone come si userebbe il contesto padre. Ad esempio, per eseguire una richiesta figlio per un URL diverso dall'URL padre, usare il metodo IHttpRequest::SetUrl per il contesto clonato per modificare l'URL del contesto clonato prima di chiamare il metodo IHttpContext::ExecuteRequest del contesto padre.

Esempio

Nell'esempio di codice seguente viene illustrato come creare un modulo HTTP che esegue le attività seguenti:

  1. Il modulo registra per la notifica di RQ_MAP_PATH .

  2. Il modulo crea una classe CHttpModule che contiene metodi OnMapPath e OnAsyncCompletion .

  3. Quando un client Web richiede un URL, IIS chiama il metodo del OnMapPath modulo. Questo metodo esegue le attività seguenti:

    1. Verifica se l'URL per la richiesta corrente ha una barra finale o termina con /default.aspx. Se l'URL termina con un elemento, il modulo usa il CloneContext metodo per creare un clone della richiesta corrente.

    2. Chiama il metodo del IHttpRequest::SetUrl clone per impostare l'URL per il clone su /example/default.aspx.

    3. Chiama il IHttpContext::ExecuteRequest metodo per eseguire la richiesta figlio.

    4. Test per il completamento asincrono. Se il completamento asincrono è in sospeso, il modulo restituisce l'elaborazione alla pipeline di elaborazione della richiesta integrata. In caso contrario, il modulo rilascia il contesto clonato.

  4. Se è necessario il completamento asincrono, IIS chiama il metodo del OnAsyncCompletion modulo. Questo metodo rilascia il contesto clonato.

  5. Il modulo rimuove la CHttpModule classe dalla memoria e quindi chiude.

#define _WINSOCKAPI_
#include <windows.h>
#include <sal.h>
#include <httpserv.h>

// Create the module class.
class MyHttpModule : public CHttpModule
{

private:

    // Create a pointer for a child request.
    IHttpContext * m_pChildRequestContext;

public:

    MyHttpModule(void)
    {
        m_pChildRequestContext = NULL;
    }

    REQUEST_NOTIFICATION_STATUS
    OnMapPath(
        IN IHttpContext * pHttpContext,
        IN IMapPathProvider * pProvider
    )
    {
        UNREFERENCED_PARAMETER( pProvider );

        HRESULT hr;
        BOOL fCompletionExpected;

        // Retrieve a pointer to the URL.
        PCWSTR pwszUrl = pProvider->GetUrl();

        // Only process requests for the root.
        if (0 == wcscmp(pwszUrl,L"/") || 0 == wcscmp(pwszUrl,L"/default.aspx"))
        {            
            // Clone the current context.
            hr = pHttpContext->CloneContext(
                CLONE_FLAG_BASICS, &m_pChildRequestContext );
            
            // Test for a failure.
            if (FAILED(hr))
            {
                goto Failure;
            }
            
            // Test for an error.
            if ( NULL != m_pChildRequestContext )
            {
                // Set the URL for the child request.
                hr = m_pChildRequestContext->GetRequest()->SetUrl(
                    "/example/default.aspx",
                    (DWORD)strlen("/example/default.aspx"),false);
            
                // Test for a failure.
                if (FAILED(hr))
                {
                    goto Failure;
                }
                
                // Execute the child request.
                hr = pHttpContext->ExecuteRequest(
                    TRUE, m_pChildRequestContext,
                    0, NULL, &fCompletionExpected );
                
                // Test for a failure.
                if (FAILED(hr))
                {
                    goto Failure;
                }
                
                // Test for pending asynchronous operations.
                if (fCompletionExpected)
                {
                    return RQ_NOTIFICATION_PENDING;
                }

            }

 Failure:
            // Test for a child request.
            if (NULL != m_pChildRequestContext)
            {
                // Release the child request.
                m_pChildRequestContext->ReleaseClonedContext();
                m_pChildRequestContext = NULL;
            }
        }
        
        // Return processing to the pipeline.
        return RQ_NOTIFICATION_CONTINUE;
    }
    
    REQUEST_NOTIFICATION_STATUS
        OnAsyncCompletion(
        IN IHttpContext * pHttpContext,
        IN DWORD dwNotification,
        IN BOOL fPostNotification,
        IN IHttpEventProvider * pProvider,
        IN IHttpCompletionInfo * pCompletionInfo
        )
    {
        // Test for a child request.
        if (NULL != m_pChildRequestContext)
        {
            // Release the child request.
            m_pChildRequestContext->ReleaseClonedContext();
            m_pChildRequestContext = NULL;
        }
        // Return processing to the pipeline.
        return RQ_NOTIFICATION_CONTINUE;
    }

};

// Create the module's class factory.
class MyHttpModuleFactory : public IHttpModuleFactory
{
public:
    HRESULT
    GetHttpModule(
        OUT CHttpModule ** ppModule, 
        IN IModuleAllocator * pAllocator
    )
    {
        UNREFERENCED_PARAMETER( pAllocator );

        // Create a new instance.
        MyHttpModule * pModule = new MyHttpModule;

        // Test for an error.
        if (!pModule)
        {
            // Return an error if we cannot create the instance.
            return HRESULT_FROM_WIN32( ERROR_NOT_ENOUGH_MEMORY );
        }
        else
        {
            // Return a pointer to the module.
            *ppModule = pModule;
            pModule = NULL;
            // Return a success status.
            return S_OK;
        }            
    }

    void Terminate()
    {
        // Remove the class from memory.
        delete this;
    }
};

// Create the module's exported registration function.
HRESULT
__stdcall
RegisterModule(
    DWORD dwServerVersion,
    IHttpModuleRegistrationInfo * pModuleInfo,
    IHttpServer * pGlobalInfo
)
{
    UNREFERENCED_PARAMETER( dwServerVersion );
    UNREFERENCED_PARAMETER( pGlobalInfo );

    return pModuleInfo->SetRequestNotifications(
        new MyHttpModuleFactory,
        RQ_MAP_PATH,
        0
    );
}

Il modulo deve esportare la funzione RegisterModule . È possibile esportare questa funzione creando un file di definizione del modulo (con estensione def) per il progetto oppure è possibile compilare il modulo usando l'opzione /EXPORT:RegisterModule . Per altre informazioni, vedere Procedura dettagliata: Creazione di un modulo HTTP Request-Level tramite codice nativo.

Facoltativamente, è possibile compilare il codice usando la __stdcall (/Gz) convenzione chiamante anziché dichiarare esplicitamente la convenzione chiamante per ogni funzione.

Requisiti

Tipo Descrizione
Client - IIS 7.0 in Windows Vista
- IIS 7.5 in Windows 7
- IIS 8.0 in Windows 8
- IIS 10.0 in Windows 10
Server - IIS 7.0 in Windows Server 2008
- IIS 7.5 in Windows Server 2008 R2
- IIS 8.0 in Windows Server 2012
- IIS 8.5 in Windows Server 2012 R2
- IIS 10.0 in Windows Server 2016
Prodotto - IIS 7.0, IIS 7.5, IIS 8.0, IIS 8.5, IIS 10.0
- IIS Express 7,5, IIS Express 8.0, IIS Express 10.0
Intestazione Httpserv.h

Vedere anche

Interfaccia IHttpContext
Metodo IHttpContext::ExecuteRequest
Metodo IHttpContext::ReleaseClonedContext