Basic Filter Framework

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

The basic structure for an ISAPI filter is described in Filter Event Processing.

Initializing Filters

The following code fragment provides a sample implementation of GetFilterVersion. The code sets the filter version and description information, and sets dwFlags of the HTTP_FILTER_VERSION structure to indicate that it can handle SF_NOTIFY_SEND_RAW_DATA and SF_NOTIFY_LOG event notifications.

BOOL WINAPI GetFilterVersion( 
  PHTTP_FILTER_VERSION pVer 
)
{
  // Sets the filter version number.
  pVer->dwFilterVersion = HTTP_FILTER_REVISION;

  // Sets the filter description.
  lstrcpy( pVer->lpszFilterDesc, "Reading Raw Data and Logging filter" );

  // Registers for two 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 for which you have registered the filter. When IIS calls HttpFilterProc, it passes in an HTTP_FILTER_CONTEXT data structure. This structure is similar to the EXTENSION_CONTROL_BLOCK 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. A number of factors can have an impact on this order; Event Notification Order provides a general outline of the issues involved.

Terminating the Filter

You can provide the TerminateFilter entry-point function as a way to gracefully free any allocated resources. If your filter does not implement and use a worker thread pool of its own, cleanup operations can be performed in DllMain, as an alternative to TerminateFilter. If your filter does use worker threads, TerminateFilter should be used for cleanup.