IHttpServer::GetFileInfo 方法

傳回特定檔案路徑的 IHttpFileInfo 介面。

語法

virtual HRESULT GetFileInfo(  
   IN PCWSTR pszPhysicalPath,  
   IN HANDLE hUserToken,  
   IN PSID pSid,  
   IN PCWSTR pszVrPath,  
   IN HANDLE hVrToken,  
   IN BOOL fCache,  
   OUT IHttpFileInfo** ppFileInfo,  
   IN IHttpTraceContext* pHttpTraceContext = NULL  
) = 0;  

參數

pszPhysicalPath
[IN]字串的指標,其中包含檔案的實體路徑。

hUserToken
[IN], HANDLE 包含模擬使用者的權杖,否則為 Null。

pSid
[IN]安全性識別碼的指標 (SID) ,其中包含模擬使用者的安全性識別碼;否則為 Null。

pszVrPath
[IN]字串的指標,其中包含要註冊變更通知的虛擬路徑;否則為 Null。

hVrToken
[IN], HANDLE 其中包含要註冊變更通知的模擬權杖,否則為 Null。

fCache
[IN] true 表示應該快取檔案;否則為 false 。

ppFileInfo
[OUT]介面的取值指標 IHttpFileInfo 。

pHttpTraceContext
[IN]選擇性 IHttpTraceCoNtext 介面的指標。

傳回值

HRESULT。 可能的值包括 (但不限於) 下表中的這些值。

值 定義
S_OK 表示作業成功。
E_FAIL 表示 GetFileInfo 在模組主機終止時呼叫 。

備註

方法 IHttpServer::GetFileInfo 會 IHttpFileInfo 建立特定路徑的介面。 這個方法與 IHttpCoNtext::GetFileInfo 方法不同,此方法會傳回 IHttpFileInfo IIS 在要求內容中正在處理的檔案介面。

pszPhysicalPath建立介面需要 IHttpFileInfo 和 ppFileInfo 參數。 參數 pszPhysicalPath 會指定檔案的路徑,而 ppFileInfo 參數會定義 IIS 將填入對應 IHttpFileInfo 介面的指標。

pszVrPath和 hVrToken 參數是選擇性的,如果您未使用這些參數,則應該將它們設定為 Null。 這些參數分別指定當模組註冊變更通知 (時要使用的虛擬路徑和模擬權杖,例如,如果您藉由將 參數設定 fCache 為 true) 來要求快取。

注意

其他組態設定可能會防止 IIS 快取檔案,即使您為 fCache 參數指定 true 也一樣。

hUserToken和 pSid 參數也是選擇性的,如果您未使用這些參數,則應該將它們設定為 Null。 這些參數分別指定模擬使用者的權杖和 SID。 其餘的選擇性參數 pHttpTraceContext 會 IHttpTraceContext 指定追蹤的介面。

範例

下列程式碼範例示範如何建立執行下列工作的 HTTP 模組:

  1. 註冊 RQ_BEGIN_REQUEST 通知。

  2. 建立包含OnBeginRequest方法的CHttpModule類別。 當用戶端要求檔案時, OnBeginRequest 方法會執行下列工作:

    1. 使用 IHttpCoNtext::MapPath 方法,將路徑對應至相對 URL。

    2. IHttpFileInfo使用 方法建立檔案路徑的 IHttpServer::GetFileInfo 介面。

    3. 使用 IHttpFileInfo::GetETag 方法擷取檔案的實體標籤。

    4. 使用 IHttpResponse::WriteEntityChunks 方法,將實體標記傳回給用戶端。

  3. CHttpModule從記憶體中移除 類別,然後結束。

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

// Create a pointer for the global server interface.
IHttpServer * g_pHttpServer = NULL;

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

        HRESULT hr;

        PWSTR wszUrl = L"/example/default.asp";
        WCHAR wszPhysicalPath[1024] = L"";
        DWORD cbPhysicalPath = 1024;

        pHttpContext->MapPath(wszUrl,wszPhysicalPath,&cbPhysicalPath);

        if (NULL != wszPhysicalPath)
        {
            IHttpFileInfo * pHttpFileInfo;
            hr = g_pHttpServer->GetFileInfo(wszPhysicalPath,
                NULL,NULL,wszUrl,NULL,TRUE,&pHttpFileInfo);
            if (NULL != pHttpFileInfo)
            {
                // Create a buffer for the Etag.
                USHORT cchETag;
                // Retrieve the Etag.
                PCSTR pszETag = pHttpFileInfo->GetETag(&cchETag);
                //Test for an error.
                if (NULL != pszETag)
                {
                    // Clear the existing response.
                    pHttpContext->GetResponse()->Clear();
                    // Set the MIME type to plain text.
                    pHttpContext->GetResponse()->SetHeader(
                        HttpHeaderContentType,"text/plain",
                        (USHORT)strlen("text/plain"),TRUE);
                    // Return the Etag to the client.
                    WriteResponseMessage(pHttpContext,
                        "ETag: ",pszETag);
                    // End additional processing.
                    return RQ_NOTIFICATION_FINISH_REQUEST;
                }
            }
        }

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

    // Create a utility method that inserts a name/value pair into the response.
    HRESULT WriteResponseMessage(
        IHttpContext * pHttpContext,
        PCSTR pszName,
        PCSTR pszValue
    )
    {
        // Create an HRESULT to receive return values from methods.
        HRESULT hr;
        
        // Create a data chunk. (Defined in the Http.h file.)
        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 first buffer.
        dataChunk.FromMemory.pBuffer =
            (PVOID) pszName;
        // Set the chunk size to the first buffer size.
        dataChunk.FromMemory.BufferLength =
            (USHORT) strlen(pszName);
        // Insert the data chunk into the response.
        hr = pHttpContext->GetResponse()->WriteEntityChunks(
            &dataChunk,1,FALSE,TRUE,&cbSent);
        // Test for an error.
        if (FAILED(hr))
        {
            // Return the error status.
            return hr;
        }

        // Set the chunk to the second buffer.
        dataChunk.FromMemory.pBuffer =
            (PVOID) pszValue;
        // Set the chunk size to the second buffer size.
        dataChunk.FromMemory.BufferLength =
            (USHORT) strlen(pszValue);
        // Insert the data chunk into the response.
        hr = pHttpContext->GetResponse()->WriteEntityChunks(
            &dataChunk,1,FALSE,TRUE,&cbSent);
        // Test for an error.
        if (FAILED(hr))
        {
            // Return the error status.
            return hr;
        }

        // Return a success status.
        return S_OK;
    }
};

// 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 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 );

    // Store the pointer for the global server interface.
    g_pHttpServer = pGlobalInfo;

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

如需如何建立及部署原生 DLL 模組的詳細資訊,請參閱逐步解說 :使用機器碼建立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

另請參閱

IHttpServer 介面
IHttpCoNtext::GetFileInfo 方法