Recently, I noticed that this site is referring to one of the images on my site, without viewing my site. That is to say, one of the forum contributors put a < IMG src="..." > that refers directly to an image stored on my blog.

Since this blog is based on DotText, and that DotText has a IHttpModule system for allowing HttpModules to hook onto the HTTP request mechanism, I decided to write a DotText plugin / addon, that would redirect all requests to my site to another site. This teaches me how to use the IHttpModule interface of the NET Framework.

The simple test would be this: If the referer contains 4042.com, then, redirect it to another URL. Since it's a first version, I decided to hardcode all the values inside, instead of retrieving it from web.config.

Then, in order to deploy it, I copy it to the Dottext bin directory, and added the following line into the HttpModules section of web.config:
<add  name="BlockReferer" type="Dottext.RefererBlocker.TRefererBlocker, Dottext.RefererBlocker">

In the process, I found out that declaring the "destructor Destroy" pattern doesn't work as described on Allen's blog. And Allen's response is:

"IHttpModule has its own Dispose method irrespective of IDisposable..  The compiler only works its magic on the IDisposable interface and not any descendants.  It handles the mapping of Dispose to the destructor.  For any other interfaces, you're on your own."

Tsk... Tsk... These Microsofties! Can't they just inherit IDisposable when declaring IHttpModule, so that Borland can do their job properly?

unit Dottext.RefererBlocker.RefererBlockerImpl;

interface
  uses System.Web;

type
  TRefererBlocker = class sealed(System.Object, IHttpModule)
  public
    procedure Dispose;
    procedure Init(context: HttpApplication);
    procedure BanReferer(Sender: System.Object; e: EventArgs);
  end;

implementation

{ TRefererBlocker }

procedure TRefererBlocker.BanReferer(Sender: System.Object; e: EventArgs);
var
  context: HttpApplication;
begin
  context := HttpApplication(Sender);
  if Pos('4042.com', context.Request.UrlReferrer.ToString)<>0 then
    context.Response.Redirect('http://www.microsoft.com/presspass/images/features/2002/07-15exchangeqa_l.jpg');
end;

procedure TRefererBlocker.Dispose;
begin
 // Empty
end;

procedure TRefererBlocker.Init(context: HttpApplication);
begin
  Include(context.BeginRequest, BanReferer);
end;

end.