IIS - Sample ISAPI Filter doing Redirection to another website

I know I'm in a very old world of writing ISAPI Filters to do the redirection instead of just creating an IHttpModule and plug it directly in the IIS7 request pipeline. But, one of my customer wanted this ISAPI filter and I made a fairly simple ISAPI Filter to do the redirection.

Below sample doesn't do any checks, or maintains any lists of mapped URLs, but just a simple redirection of all the requests to http://www.live.com. Feel free to modify it accommodating your need.

 

Code Snippet
#include <stdio.h> 
#include <stdlib.h> 
#include <afx.h>
#include <afxisapi.h>

BOOL WINAPI __stdcall GetFilterVersion(HTTP_FILTER_VERSION *pVer) 
{ 
  pVer->dwFlags = (SF_NOTIFY_PREPROC_HEADERS ); 
  CFile myFile("C:\\ISAPILOG\\URLs.html", CFile::modeCreate);
  myFile.Close();
  pVer->dwFilterVersion = HTTP_FILTER_REVISION; 
  strcpy(pVer->lpszFilterDesc, "Sample Redirection ISAPI"); 
  return TRUE; 
} 

DWORD WINAPI __stdcall HttpFilterProc(HTTP_FILTER_CONTEXT *pfc, DWORD NotificationType, VOID *pvData) 
{ 
   char buffer[256];
   DWORD buffSize = sizeof(buffer);
   HTTP_FILTER_PREPROC_HEADERS *p;
   CHttpFilterContext *chc;
   chc = (CHttpFilterContext *)pfc;
   char *newUrl;
   CFile myFile("C:\\ISAPILOG\\URLs.html", CFile::modeWrite);

   switch (NotificationType)  { 

   case SF_NOTIFY_PREPROC_HEADERS :

   p = (HTTP_FILTER_PREPROC_HEADERS *)pvData;

   char newUrl[50];
   wsprintf(newUrl, "http://www.live.com/");

   char szTemp[50];
   wsprintf(szTemp, "Location: %s\r\n\r\n",newUrl);

   pfc->ServerSupportFunction (pfc,
                            SF_REQ_SEND_RESPONSE_HEADER,
                            (PVOID) "302 Redirect",
                            (DWORD) szTemp,0); 

   myFile.SeekToEnd();
   myFile.Write("<BR><B> Orignial URL : </B>",strlen("<BR><B> Orignial URL : </B>"));
   BOOL bHeader = p->GetHeader(pfc,"url",buffer,&buffSize); 
   CString myURL(buffer);
   myURL.MakeLower(); 
   myFile.Write(buffer,buffSize);
   
   myFile.Write(" <B>New URL : </B> ",strlen(" <B>New URL : </B> "));
   myFile.Write(newUrl,strlen(newUrl));
   myFile.Close();

   return SF_STATUS_REQ_HANDLED_NOTIFICATION; 
   }
  return SF_STATUS_REQ_NEXT_NOTIFICATION; 
} 

Above is my sample, You might want to check my earlier ISAPI blog post to get the .def file and steps to create the DLL.


Hope this helps!

No Comments