次の方法で共有


IHttpContext::CloneContext メソッド

現在の要求コンテキストの複製を作成します。

構文

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

パラメーター

dwCloneFlags
[IN] DWORD 複製フラグを含む 。

ppHttpContext
[OUT] IHttpContext への逆参照ポインター。

戻り値

HRESULT。 有効な値を次の表に示しますが、これ以外にもあります。

説明
S_OK 操作が成功したことを示します。
ERROR_INVALID_PARAMETER 指定したパラメーターが無効であることを示します。
ERROR_NOT_ENOUGH_MEMORY 操作を実行するのに十分なメモリがないことを示します。

解説

メソッドは CloneContext 、現在の要求コンテキストの複製を作成します。 パラメーターに適切なフラグを指定することで、複製の動作を dwCloneFlags 制御できます。 次の表に、これらのフラグに使用できる値を示します。

説明
CLONE_FLAG_BASICS URL、クエリ文字列、HTTP メソッドを複製します。
CLONE_FLAG_HEADERS 要求ヘッダーを複製します。
CLONE_FLAG_ENTITY エンティティ本体を複製します。
CLONE_FLAG_NO_PRECONDITION 要求に "range" ヘッダーと "if-" ヘッダーを含めないでください。
CLONE_FLAG_NO_DAV 要求に WebDAV ヘッダーを含めないでください。

複製されたコンテキストを作成したら、親コンテキストと同様に複製を使用できます。 たとえば、親 URL とは異なる URL に対して子要求を実行するには、複製されたコンテキストに IHttpRequest::SetUrl メソッドを使用して、親コンテキストの IHttpContext::ExecuteRequest メソッドを呼び出す前に、複製されたコンテキストの URL を変更します。

次のコード例は、次のタスクを実行する HTTP モジュールを作成する方法を示しています。

  1. モジュールは 、RQ_MAP_PATH 通知に登録します。

  2. モジュールは、OnMapPath メソッドと OnAsyncCompletion メソッドを含む CHttpModule クラスを作成します。

  3. Web クライアントが URL を要求すると、IIS はモジュールの OnMapPath メソッドを呼び出します。 このメソッドは、次のタスクを実行します。

    1. 現在の要求の URL に末尾のスラッシュがあるか、または /default.aspx で終わるかを確認するテスト。 URL がいずれかの要素で終わる場合、モジュールは メソッドを CloneContext 使用して現在の要求の複製を作成します。

    2. 複製 IHttpRequest::SetUrl のメソッドを呼び出して、クローンの URL を /example/default.aspx に設定します。

    3. メソッドを IHttpContext::ExecuteRequest 呼び出して子要求を実行します。

    4. 非同期完了をテストします。 非同期完了が保留中の場合、モジュールは統合された要求処理パイプラインに処理を返します。 そうでない場合、モジュールは複製されたコンテキストを解放します。

  4. 非同期入力候補が必要な場合、IIS はモジュールの OnAsyncCompletion メソッドを呼び出します。 このメソッドは、複製されたコンテキストを解放します。

  5. モジュールは、 クラスを CHttpModule メモリから削除してから終了します。

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

モジュールは RegisterModule 関数をエクスポートする必要があります。 この関数をエクスポートするには、プロジェクトのモジュール定義 (.def) ファイルを作成するか、スイッチを使用してモジュールを /EXPORT:RegisterModule コンパイルします。 詳細については、「 チュートリアル: ネイティブ コードを使用したRequest-Level HTTP モジュールの作成」を参照してください。

必要に応じて、各関数の呼び出し規約を __stdcall (/Gz) 明示的に宣言するのではなく、呼び出し規約を使用してコードをコンパイルできます。

要件

Type 説明
Client - Windows Vista 上の IIS 7.0
- Windows 7 上の IIS 7.5
- Windows 8 の IIS 8.0
- Windows 10の IIS 10.0
サーバー - Windows Server 2008 の IIS 7.0
- Windows Server 2008 R2 上の IIS 7.5
- Windows Server 2012 上の IIS 8.0
- Windows Server 2012 R2 上の IIS 8.5
- Windows Server 2016上の IIS 10.0
製品 - 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

参照

IHttpContext インターフェイス
IHttpContext::ExecuteRequest メソッド
IHttpContext::ReleaseClonedContext メソッド