Samples: Change Server Header

Creating managed filters, also called HttpFilters in Filter.NET, is now easy. Lets say you want to change the Server header of every http response.

There are about 11 events on the ISAPI Filters API (taken from MSDN):
  • SF_NOTIFY_READ_RAW_DATA - (managed event ReadRawData) Occurs when data is being read from the client. May occur more than once per request.
  • SF_NOTIFY_PREPROC_HEADERS - (managed event PreProcHeaders) Occurs immediately after IIS has pre-processed headers, but before IIS has begun to process header content.
  • SF_NOTIFY_URL_MAP - (managed event UrlMap) Occurs after IIS has translated a URL to a physical path on the server.
  • SF_NOTIFY_AUTHENTICATION - (managed event Authentication) Occurs just before IIS authenticates the client.
  • SF_NOTIFY_ACCESS_DENIED - (managed event AccessDenied) Occurs just after IIS has determined that access is denied for the request resource, but before IIS has sent a response to the client.
  • SF_NOTIFY_SEND_RESPONSE - (managed event SendResponse) Occurs after the request has been processed by IIS, but before any headers are sent back to the client.
  • SF_NOTIFY_SEND_RAW_DATA - (managed event SendRawData) Occurs as IIS sends raw data back to the client. May occur more than once per request.
  • SF_NOTIFY_END_OF_REQUEST - (managed event EndOfRequest) Occurs at the end of the request.
  • SF_NOTIFY_LOG - (managed event Log) Occurs at the end of a request, just before IIS writes the transaction to the IIS log.
  • SF_NOTIFY_END_OF_NET_SESSION - (managed event EndOfNetSession) Occurs when the network session with the client is ending.
  • SF_NOTIFY_AUTH_COMPLETE - (managed event AuthComplete) Occurs after the client's identity has been negotiated with the client.

We only need SF_NOTIFY_SEND_RESPONSE since this event is always called once and only once for each request arriving at IIS.

Let see the code:

using System;
using KodeIT.Web;

namespace MySample
   public class ServerHeaderChange : IHttpFilter
       void IHttpFilter.Init(IFilterEvents events)
           events.SendResponse += new EventHandler<SendResponseEventArgs>(OnSendResponse);

       void OnSendResponse(object sender, SendResponseEventArgs e)
           ResponseHeadersEvent context = e.Context;
           context.Headers[HttpHeader.Server] = "Powered by Filter.NET";

       void IHttpFilter.Dispose()

Simple, isn't it?

Now, compile it referencing the assembly KodeIT.Web.dll (comes with the installation binary) and name the output assembly MySample.dll.

We need to set it up, so it runs on every single request. Assuming Filter.NET is already setup lets change the Filter.NET configuration file (comes with the installation binary) filter.config like this:

<?xml version="1.0" encoding="utf-8" ?>
      <section name="httpFilters"
         type="KodeIT.Web.Configuration.HttpFiltersSection, KodeIT.Web" />
      <add name="myManagedFilter" type="MySample.ServerHeaderChange, MySample" />

Restart IIS and create a vbs with this contents:

Set oHTTP = WScript.CreateObject("Microsoft.XMLHTTP")
Call oHTTP.Open("HEAD", "http://localhost", False)
Call oHTTP.Send()
WScript.Echo oHTTP.GetAllResponseHeaders()
Set oHTTP = Nothing

Just run the vbs and you'll see the server header changed.

Last edited Sep 2, 2007 at 5:02 PM by thalm, version 12


No comments yet.