Bagikan melalui


Metode IHttpContext::CloneContext

Membuat kloning konteks permintaan saat ini.

Sintaks

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

Parameter

dwCloneFlags
[IN] DWORD yang berisi bendera kloning.

ppHttpContext
[OUT] Penunjuk dereferensi ke IHttpContext.

Tampilkan Nilai

Sebuah HRESULT. Nilai yang mungkin termasuk, tetapi tidak terbatas pada, yang ada dalam tabel berikut.

Nilai Deskripsi
S_OK Menunjukkan bahwa operasi berhasil.
ERROR_INVALID_PARAMETER Menunjukkan bahwa parameter yang ditentukan tidak valid.
ERROR_NOT_ENOUGH_MEMORY Menunjukkan bahwa tidak ada cukup memori untuk melakukan operasi.

Keterangan

Metode ini CloneContext membuat klon konteks permintaan saat ini. Anda dapat mengontrol perilaku kloning dengan menentukan bendera yang sesuai dalam dwCloneFlags parameter . Tabel berikut mencantumkan nilai yang mungkin untuk bendera ini.

Nilai Deskripsi
CLONE_FLAG_BASICS KlonING URL, string kueri, dan metode HTTP.
CLONE_FLAG_HEADERS Kloning header permintaan.
CLONE_FLAG_ENTITY Kloning badan entitas.
CLONE_FLAG_NO_PRECONDITION Jangan sertakan header "rentang" dan "if-" untuk permintaan.
CLONE_FLAG_NO_DAV Jangan sertakan header WebDAV apa pun untuk permintaan tersebut.

Setelah membuat konteks kloning, Anda dapat menggunakan kloning seperti yang akan Anda gunakan konteks induk. Misalnya, untuk menjalankan permintaan anak untuk URL yang berbeda dari URL induk, Anda akan menggunakan metode IHttpRequest::SetUrl untuk konteks kloning untuk mengubah URL konteks kloning sebelum memanggil metode IHttpContext::ExecuteRequest konteks induk.

Contoh

Contoh kode berikut menunjukkan cara membuat modul HTTP yang melakukan tugas berikut:

  1. Modul mendaftar untuk pemberitahuan RQ_MAP_PATH .

  2. Modul membuat kelas CHttpModule yang berisi metode OnMapPath dan OnAsyncCompletion .

  3. Saat klien Web meminta URL, IIS memanggil metode modul OnMapPath . Metode ini melakukan tugas-tugas berikut:

    1. Pengujian untuk melihat apakah URL untuk permintaan saat ini memiliki garis miring berikutnya atau diakhir dengan /default.aspx. Jika URL berakhir dengan salah satu elemen, modul menggunakan CloneContext metode untuk membuat klon permintaan saat ini.

    2. Memanggil metode kloning IHttpRequest::SetUrl untuk mengatur URL untuk kloning ke /example/default.aspx.

    3. IHttpContext::ExecuteRequest Memanggil metode untuk menjalankan permintaan anak.

    4. Pengujian untuk penyelesaian asinkron. Jika penyelesaian asinkron tertunda, modul mengembalikan pemrosesan ke alur pemrosesan permintaan terintegrasi. Jika tidak, modul merilis konteks kloning.

  4. Jika penyelesaian asinkron diperlukan, IIS memanggil metode modul OnAsyncCompletion . Metode ini merilis konteks kloning.

  5. Modul menghapus CHttpModule kelas dari memori lalu keluar.

#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
    );
}

Modul Anda harus mengekspor fungsi RegisterModule . Anda dapat mengekspor fungsi ini dengan membuat file definisi modul (.def) untuk proyek Anda, atau Anda dapat mengkompilasi modul dengan menggunakan sakelar /EXPORT:RegisterModule . Untuk informasi selengkapnya, lihat Panduan: Membuat Modul HTTP Request-Level Dengan Menggunakan Kode Asli.

Anda dapat secara opsional mengkompilasi kode dengan menggunakan __stdcall (/Gz) konvensi panggilan alih-alih secara eksplisit mendeklarasikan konvensi panggilan untuk setiap fungsi.

Persyaratan

Jenis Deskripsi
Klien - IIS 7.0 di Windows Vista
- IIS 7.5 di Windows 7
- IIS 8.0 di Windows 8
- IIS 10.0 pada Windows 10
Server - IIS 7.0 di Windows Server 2008
- IIS 7.5 di Windows Server 2008 R2
- IIS 8.0 di Windows Server 2012
- IIS 8.5 di Windows Server 2012 R2
- IIS 10.0 di Windows Server 2016
Produk - 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
Header Httpserv.h

Lihat juga

Antarmuka IHttpContext
Metode IHttpContext::ExecuteRequest
Metode IHttpContext::ReleaseClonedContext