共用方式為


CHttpModule 類別

定義要求層級 HTTP 模組的基類。

Syntax

class CHttpModule  

方法

下表列出 類別所 CHttpModule 公開的方法。

名稱 描述
~CHttpModule 解除配置 類別的 CHttpModule 實例。
CHttpModule 初始化 CHttpModule 類別的新執行個體。
Dispose 釋放 CHttpModule 類別目前的執行個體所使用的全部資源。
OnAcquireRequestState 表示將處理 AcquireRequestState 事件的方法,這個方法會在 IIS 擷取目前要求的狀態時發生。
OnAsyncCompletion 表示將處理非同步完成事件的方法,這個事件會在非同步作業完成處理之後發生。
OnAuthenticateRequest 表示將處理 AuthenticateRequest 事件的方法,此事件會在 IIS 建立使用者的身分識別時發生。
OnAuthorizeRequest 表示將處理 AuthorizeRequest 事件的方法,此事件會在 IIS 驗證使用者授權時發生。
OnBeginRequest 表示將處理 BeginRequest 事件的方法,這個方法會在目前要求的 HTTP 整合式要求處理管線中當做第一個事件發生。
OnCustomRequestNotification 表示將處理自訂事件的方法,此事件會在模組引發使用者定義通知時發生。
OnEndRequest 表示將處理 EndRequest 事件的方法,這個方法會在目前要求的 HTTP 整合式要求處理管線中當做最後一個事件發生。
OnExecuteRequestHandler 表示將處理 ExecuteRequestHandler 事件的方法,這個方法會在 IIS 執行目前要求的處理常式時發生。
OnLogRequest 表示將處理 LogRequest 事件的方法,這個事件會在 IIS 準備記錄目前的要求時發生。
OnMapPath 表示將處理 MapPath 事件的方法,當作業要求對應至目前要求的實體路徑時,就會發生此事件。
OnMapRequestHandler 表示將處理 MapRequestHandler 事件的方法,這個方法會在 IIS 將目前的要求對應至事件處理常式時發生。
OnPostAcquireRequestState 表示將處理 AcquireRequestState 後事件的方法,這個方法會在 IIS 擷取目前要求的狀態之後發生。
OnPostAuthenticateRequest 表示將處理 AuthenticateRequest 事件後置事件的方法,此事件會在 IIS 建立使用者的身分識別之後發生。
OnPostAuthorizeRequest 表示將處理 AuthorizeRequest 事件後置事件的方法,此事件會在 IIS 驗證使用者授權之後發生。
OnPostBeginRequest 表示將處理 BeginRequest 後事件的方法,此事件會在 HTTP 整合式要求處理管線中的第一個事件之後發生。
OnPostEndRequest 表示將處理事件後置事件的方法,這個方法會在目前要求的 HTTP 整合式要求處理管線中最後一個 EndRequest 事件之後發生。
OnPostExecuteRequestHandler 表示將處理 ExecuteRequestHandler 後續事件的方法,這個方法會在 IIS 執行目前要求的處理常式之後發生。
OnPostLogRequest 表示將處理 LogRequest 後事件的方法,這個方法會在 IIS 記錄目前的要求之後發生。
OnPostMapRequestHandler 表示將處理 MapRequestHandler 後事件的方法,這個方法會在 IIS 將目前的要求對應至適當的事件處理常式之後發生。
OnPostPreExecuteRequestHandler 表示將處理 PreExecuteRequestHandler 事件後置事件的方法,此事件會在 IIS 執行要求處理常式之前發生。
OnPostReleaseRequestState 表示將處理 ReleaseRequestState 事件後置事件的方法,這個事件會在目前狀態釋放之後發生。
OnPostResolveRequestCache 表示將處理 ResolveRequestCache 事件後置事件的方法,此事件會在 IIS 解析快取的要求之後發生。
OnPostUpdateRequestCache 表示將處理 UpdateRequestCache 事件後置事件的方法,此事件會在 IIS 將要求儲存在快取之後發生。
OnPreExecuteRequestHandler 表示將處理 PreExecuteRequestHandler 事件的方法,該事件會在 IIS 執行要求處理常式之前發生。
OnReadEntity 表示將處理 ReadEntity 事件的方法,此事件會在作業從要求緩衝區讀取資料時發生。
OnReleaseRequestState 表示將處理 ReleaseRequestState 事件的方法,這個事件會在釋放目前狀態時發生。
OnResolveRequestCache 表示將處理 ResolveRequestCache 事件的方法,這個事件會在 IIS 解析快取中的要求時發生。
OnSendResponse 表示將處理 SendResponse 事件的方法,這個方法會在 IIS 傳送回應緩衝區時發生。
OnUpdateRequestCache 表示將處理 UpdateRequestCache 事件的方法,此事件會在 IIS 將要求儲存在快取中時發生。

衍生類別

這個類別不包含衍生類別。

備註

類別 CHttpModule 是要求層級 HTTP 模組的基類。 若要建立 CHttpModule 衍生類別,您必須建立要求層級 HTTP 模組,其中包含繼承自 CHttpModule 的類別,以及衍生自 IHttpModuleFactory 介面的類別。 如需建立 HTTP 模組的詳細資訊,請參閱 設計Native-Code HTTP 模組

類別 CHttpModule 提供受保護的建構函式和解構函式方法和公用 Dispose 方法。 在要求結束時, Dispose 會呼叫 方法來刪除衍生類別的 CHttpModule 實例。

類別 CHttpModule 也會定義 IIS 7 在整合式要求處理管線內處理要求層級事件時所呼叫的通知特定方法。 HTTP 模組可以藉由在模組匯出的 RegisterModule 函式中定義通知清單來註冊特定事件。

確定性要求事件

在整合式管線內要求層級事件的正常流程期間,大部分的要求層級通知方法會依時間連續處理。 每個具決定性的要求層級通知方法都有相符的事件後通知,可讓 HTTP 模組在事件發生或事件發生後立即處理。

下表列出時間順序要求層級事件和事件後通知方法,以其在整合式管線內出現的順序。

事件通知方法 事件後通知方法
OnBeginRequest OnPostBeginRequest
OnAuthenticateRequest OnPostAuthenticateRequest
OnAuthorizeRequest OnPostAuthorizeRequest
OnResolveRequestCache OnPostResolveRequestCache
OnMapRequestHandler OnPostMapRequestHandler
OnAcquireRequestState OnPostAcquireRequestState
OnPreExecuteRequestHandler OnPostPreExecuteRequestHandler
OnExecuteRequestHandler OnPostExecuteRequestHandler
OnReleaseRequestState OnPostReleaseRequestState
OnUpdateRequestCache OnPostUpdateRequestCache
OnLogRequest OnPostLogRequest
OnEndRequest OnPostEndRequest

例如,發生在 之前 OnAuthenticateRequestOnBeginRequestOnMapRequestHandler 在 之前 OnAcquireRequestState ,依此類錯。

注意

事件後通知會在下一個時間要求層級通知之前發生。 例如,發生在 之前 OnAuthorizeRequestOnPostAuthenticateRequestOnPostUpdateRequestCache 在 之前 OnLogRequest ,依此類錯。

非決定性的要求事件

其餘的要求層級通知方法不會以任何特定連續處理;相反地,IIS 會在發生特定的非決定性事件時處理這些事件。 下表列出非決定性的要求層級事件和任何相關的事件後通知方法。

事件通知方法 事件後通知方法
OnAsyncCompletion (None) 1
OnCustomRequestNotification (無) 2
OnMapPath (無)
OnReadEntity (無)
OnSendResponse (無)

1OnAsyncCompletion HTTP 模組內發生非同步事件時,會呼叫 方法。 因此,您無法使用模組匯出的 RegisterModule 函式來註冊非同步通知。 相反地,您的模組會提供 OnAsyncCompletion 方法來處理在呼叫 (以非同步方式傳回的方法之後發生的通知,例如 IHttpCoNtext::ExecuteRequestIHttpResponse::WriteEntityChunks 方法) 。 當 IIS 呼叫 OnAsyncCompletion 時,方法會傳遞參數,指出通知類型,以及通知是否用於事件或事件後。

2 方法 OnCustomRequestNotification 沒有對應的事件後通知方法。 HTTP 模組可以使用模組的 RegisterModule 匯出函式來註冊自訂通知,但模組無法註冊在自訂通知發生之後所發生的通知。

範例

下列範例示範如何建立簡單的 「Hello World」 HTTP 模組。 模組會定義匯出 RegisterModule 的函式,將介面的 IHttpModuleFactory 實例傳遞至 IHttpModuleRegistrationInfo::SetRequestNotifications 方法,並註冊 RQ_BEGIN_REQUEST 通知。 IIS 會使用 IHttpModuleFactory::GetHttpModule 方法來建立類別的 CHttpModule 實例,並傳回成功狀態。 IIS 也會使用 IHttpModuleFactory::Terminate 方法從記憶體中移除處理站。

RQ_BEGIN_REQUEST發生通知時,IIS 會呼叫模組 OnBeginRequest 的 方法來處理目前的要求。 OnBeginRequest 清除回應緩衝區,並修改回應的 MIME 類型。 然後,方法會建立包含 「Hello World」 字串的資料區塊,並將字串傳回至 Web 用戶端。 最後,模組會傳回狀態指示器,通知 IIS 所有通知都已完成,然後結束。

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

// Create the module class.
class CHelloWorld : public CHttpModule
{
public:
    REQUEST_NOTIFICATION_STATUS
    OnBeginRequest(
        IN IHttpContext * pHttpContext,
        IN IHttpEventProvider * pProvider
    )
    {
        UNREFERENCED_PARAMETER( pProvider );

        // Create an HRESULT to receive return values from methods.
        HRESULT hr;
        
        // Retrieve a pointer to the response.
        IHttpResponse * pHttpResponse = pHttpContext->GetResponse();

        // Test for an error.
        if (pHttpResponse != NULL)
        {
            // Clear the existing response.
            pHttpResponse->Clear();
            // Set the MIME type to plain text.
            pHttpResponse->SetHeader(
                HttpHeaderContentType,"text/plain",
                (USHORT)strlen("text/plain"),TRUE);

            // Create a string with the response.
            PCSTR pszBuffer = "Hello World!";
            // Create a data chunk.
            HTTP_DATA_CHUNK dataChunk;
            // Set the chunk to a chunk in memory.
            dataChunk.DataChunkType = HttpDataChunkFromMemory;
            // Buffer for bytes written of data chunk.
            DWORD cbSent;
            
            // Set the chunk to the buffer.
            dataChunk.FromMemory.pBuffer =
                (PVOID) pszBuffer;
            // Set the chunk size to the buffer size.
            dataChunk.FromMemory.BufferLength =
                (USHORT) strlen(pszBuffer);
            // Insert the data chunk into the response.
            hr = pHttpResponse->WriteEntityChunks(
                &dataChunk,1,FALSE,TRUE,&cbSent);

            // Test for an error.
            if (FAILED(hr))
            {
                // Set the HTTP status.
                pHttpResponse->SetStatus(500,"Server Error",0,hr);
            }

            // End additional processing.
            return RQ_NOTIFICATION_FINISH_REQUEST;
        }

        // Return processing to the pipeline.
        return RQ_NOTIFICATION_CONTINUE;
    }
};

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

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

        // Test for an error.
        if (!pModule)
        {
            // Return an error if the factory 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 );

    // Set the request notifications and exit.
    return pModuleInfo->SetRequestNotifications(
        new CHelloWorldFactory,
        RQ_BEGIN_REQUEST,
        0
    );
}

您的模組必須匯出函 RegisterModule 式。 您可以為專案建立模組定義 (.def) 檔案,或使用 參數編譯模組 /EXPORT:RegisterModule 來匯出此函式。 如需詳細資訊,請參閱逐步解說 :使用機器碼建立Request-Level HTTP 模組

您可以選擇性地使用呼叫慣例編譯器代碼, __stdcall (/Gz) 而不是明確宣告每個函式的呼叫慣例。

規格需求

類型 描述
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
標頭 Httpserv.h

另請參閱

建立 Native-Code HTTP 模組
CGlobalModule 類別