Welcome to MSDN Blogs Sign in | Join | Help

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!

Published Saturday, May 10, 2008 12:17 AM by rakkim
Filed under: ,

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# re: IIS - Sample ISAPI Filter doing Redirection to another website

We have not gone to IIS7 yet, so to handle a REST type interface we need to implement a ISAPI redirect filter to hide the .svc extention. We have made one as a ISAPI filter...but most reading I see says you should make one as an extension not a filter. Have you done this...or know of a example. What I want to do is send something where the svc is not in the URL path, and we redirect to where the svc is?

Thursday, July 24, 2008 2:43 PM by Chad Gray

# re: IIS - Sample ISAPI Filter doing Redirection to another website

Hi Chad, I'd say IIS7 is really powerful, and more good with the newly released URL rewrite module.

As far as your question on ISAPI filter Vs Extension, ISAPI filter is something which you can use to modify the request URL. so, probably you can use the above code and modify it so that it maps REST type interface call to a .svc service hosted on IIS. This should be easy to implement rather writing an ISAPI extension and mapping it as a wildcard (since your incoming URL won't be with an extension).

Hope this helps!

Thursday, July 24, 2008 2:59 PM by rakkim

# re: IIS - Sample ISAPI Filter doing Redirection to another website

Thank you very much! this was very helpful!

Thursday, April 30, 2009 8:15 AM by Lopa

Leave a Comment

(required) 
required 
(required) 
 
Page view tracker