IHttpServer::GetFileInfo Method

Returns an IHttpFileInfo interface for a specific file path.

Syntax

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;  

Parameters

pszPhysicalPath
[IN] A pointer to a string that contains the physical path to a file.

hUserToken
[IN] A HANDLE that contains the token for the impersonation user; otherwise, NULL.

pSid
[IN] A pointer to a security identifier (SID) that contains the security ID for the impersonation user; otherwise, NULL.

pszVrPath
[IN] A pointer to a string that contains the virtual path to register for change notifications; otherwise, NULL.

hVrToken
[IN] A HANDLE that contains the impersonation token to register for change notifications; otherwise, NULL.

fCache
[IN] true to indicate that the file should be cached; otherwise, false.

ppFileInfo
[OUT] A dereferenced pointer to an IHttpFileInfo interface.

pHttpTraceContext
[IN] A pointer to an optional IHttpTraceContext interface.

Return Value

An HRESULT. Possible values include, but are not limited to, those in the following table.

Value Definition
S_OK Indicates that the operation was successful.
E_FAIL Indicates that the call to GetFileInfo was made while the module host was terminating.

Remarks

The IHttpServer::GetFileInfo method creates an IHttpFileInfo interface for a specific path. This method differs from the IHttpContext::GetFileInfo method, which returns an IHttpFileInfo interface for the file that IIS is processing within a request context.

The pszPhysicalPath and ppFileInfo parameters are required to create an IHttpFileInfo interface. The pszPhysicalPath parameter specifies the path to the file, and the ppFileInfo parameter defines the pointer that IIS will populate with the corresponding IHttpFileInfo interface.

The pszVrPath and hVrToken parameters are optional, and you should set them to NULL if you do not use them. These parameters specify, respectively, the virtual path and impersonation token to use when a module registers for change notifications (for example, if you request caching by setting the fCache parameter to true).

Note

Other configuration settings may prevent IIS from caching the file, even if you specify true for the fCache parameter.

The hUserToken and pSid parameters are also optional, and you should set them to NULL if you do not use them. These parameters specify, respectively, the token and SID for the impersonation user. The remaining optional parameter, pHttpTraceContext, specifies the IHttpTraceContext interface for tracing.

Example

The following code example demonstrates how to create an HTTP module that performs the following tasks:

  1. Registers for the RQ_BEGIN_REQUEST notification.

  2. Creates a CHttpModule class that contains an OnBeginRequest method. When a client requests a file, the OnBeginRequest method performs the following tasks:

    1. Maps a path to a relative URL by using the IHttpContext::MapPath method.

    2. Creates an IHttpFileInfo interface for the file path by using the IHttpServer::GetFileInfo method.

    3. Retrieves the entity tag for the file by using the IHttpFileInfo::GetETag method.

    4. Returns the entity tag to the client by using the IHttpResponse::WriteEntityChunks method.

  3. Removes the CHttpModule class from memory and then exits.

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

For more information on how to create and deploy a native DLL module, see Walkthrough: Creating a Request-Level HTTP Module By Using Native Code.

You can optionally compile the code by using the __stdcall (/Gz) calling convention instead of explicitly declaring the calling convention for each function.

Requirements

Type Description
Client - IIS 7.0 on Windows Vista
- IIS 7.5 on Windows 7
- IIS 8.0 on Windows 8
- IIS 10.0 on Windows 10
Server - IIS 7.0 on Windows Server 2008
- IIS 7.5 on Windows Server 2008 R2
- IIS 8.0 on Windows Server 2012
- IIS 8.5 on Windows Server 2012 R2
- IIS 10.0 on Windows Server 2016
Product - 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

See Also

IHttpServer Interface
IHttpContext::GetFileInfo Method