An ISAPI Filter converting ASPX to ASP

I have posted about writing a simple ISAPI and also making that ISAPI bad and taking ETW traces to understand what is really going behind the scenes. I was thinking to write this extension conversion filter long back, but just had the time to complete it. This sample filter just reads the incoming request and modifies the .ASPX extension in the URL to .ASP. So users will be requesting for a .ASPX page, but we are serving the legacy ASP pages! Cool.

How to implement this? You want to make your ISAPI subscribe to SF_NOTIFY_PREPROC Filter Notification. And, read the incoming request and then modify it to contain .ASP instead of .ASPX. Same good old CString.Replace J

Here is the code:

#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 | SF_NOTIFY_AUTHENTICATION |

                                                SF_NOTIFY_URL_MAP | SF_NOTIFY_SEND_RAW_DATA | SF_NOTIFY_LOG | SF_NOTIFY_END_OF_NET_SESSION );

  // You can subscribe to the notifications you require!

  CFile myFile("C:\\URLs.html", CFile::modeCreate);

  myFile.Close();

  pVer->dwFilterVersion = HTTP_FILTER_REVISION;

  strcpy(pVer->lpszFilterDesc, "ASPX to ASP");

  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:\\URLs.html", CFile::modeWrite);

            switch (NotificationType) {

            case SF_NOTIFY_PREPROC_HEADERS :

                        p = (HTTP_FILTER_PREPROC_HEADERS *)pvData;

                        myFile.SeekToEnd();

                        myFile.Write("<B> Orignial URL : </B>",strlen("<B> Orignial URL : </B>"));

                        BOOL bHeader = p->GetHeader(pfc,"url",buffer,&buffSize);

                        CString myURL(buffer);

                        myURL.MakeLower();

                        myFile.Write(buffer,buffSize);

                        if (myURL.Find(".aspx") != -1)

                        {

            myURL.Replace(".aspx",".asp");

            newUrl = myURL.GetBuffer(myURL.GetLength());

            p->SetHeader(pfc, "url", newUrl);

            }

                        myFile.Write("<BR><B>New URL : </B>",strlen("<BR><B>New URL : </B>"));

                        myFile.Write(newUrl,strlen(newUrl));

                        myFile.Close();

                        return SF_STATUS_REQ_HANDLED_NOTIFICATION;

            }

            return SF_STATUS_REQ_NEXT_NOTIFICATION;

}

Thanks to Sukesh who blogged a code snippet which will do this long back. And yeah, he is one of my favorite bloggers! He has tons of technical information on his blogs! Subscribe to him, you will know all the latest happenings J