Basic Filter Framework

Like ISAPI extensions, ISAPI filters must provide two common entry points, and can optionally provide a termination entry point. The required entry points are GetFilterVersion and HttpFilterProc. Support for a third optional entry point, the TerminateFilter function has been added in ISAPI 4.0. If your filter exposes TerminateFilter, IIS will call this function before it removes the filter from memory. TerminiateFilter is similar to TerminateExtension; it is intended as a way for you to perform any resource maintenance required before shutting down.

The basic structure of an ISAPI filter consists of the functions that accomplish each of the following three tasks:

Initializing a filter

When IIS loads an ISAPI filter, it calls the GetFilterVersion function. This function returns the HTTP_FILTER_VERSION data structure to IIS. The structure includes a short description of the filter and the server events for which the filter needs to be notified. Server events are specified by the data structure's dwFlags member.

The following code registers a filter for notification before sending raw data to the client and before updating the log file.

BOOL WINAPI GetFilterVersion( PHTTP_FILTER_VERSION pVer )
{

// The next line sets the version of the filter.

    pVer->dwFilterVersion = HTTP_FILTER_REVISION;

// The next line sets the description of the filter.

    lstrcpy( pVer->lpszFilterDesc, "Reading Raw Data and Logging filter" );

// This filter will be notified for the following notifications
    pVer->dwFlags = SF_NOTIFY_SEND_RAW_DATA | SF_NOTIFY_LOG;

    return TRUE;
}

Processing the event

The function responsible for processing the event information is the HTTPFilterProc. IIS calls your filter for every event that you have registered the filter for. When IIS calls HTTPFilterProc, it passes in an HTTP_FILTER_CONTEXT data structure. This structure is similar to the ECB for an ISAPI extension, in that it is associated with a particular request, and is used by IIS to coordinate filter processing. When IIS calls the filter's HTTPFilterProc, it also passes in a DWORD identifying which event has occurred and a pointer to a memory area defined by IIS that contains the event information.

The two examples, Obtaining Server Variables, and Adding to a Response Header, demonstrate the use of HTTPFilterProc.

One important factor in processing events is the order in which they are processed. While there are a number of factors that can have an impact on this order, Summary of Event Ordering provides a general outline of the issues involved.

Terminating the Filter

You can provide the TerminateFilter entry point function as a way to gracefully remove any attachments to system resources that your filter may have developed while running. If you do not provide the TerminateFilter entry point, your filter must remove attachments