5,276,406 members and growing! (17,263 online)
Email Password   helpLost your password?
Web Development » ISAPI » General     Intermediate

How to make an ISAPI redirection filter

By bryce

How to build a simple ISAPI Filter
VC6, C++Windows, NT4, Win2K, WinXP, Win2003, MFC, VS6, VS, Dev

Posted: 10 Sep 2003
Updated: 10 Sep 2003
Views: 118,909
Announcements
Want a new Job?



Search    
Advanced Search
Sitemap
17 votes for this Article.
Popularity: 5.02 Rating: 4.08 out of 5
1 vote, 5.9%
1
0 votes, 0.0%
2
1 vote, 5.9%
3
5 votes, 29.4%
4
10 votes, 58.8%
5

Introduction

Redirecting web traffic is a fairly common task, we can redirect from html, from ASP (or other scripting engines) and from ISAPI. This tutorial will demonstrate how to reduce server workload by redirecting from ISAPI using MFC using a “redirection filter”. This example will redirect a requested page, while retaining the URL the user sees in their browser

Html Redirect:

The code to redirect a page looks something like the following…
<META http-equiv="refresh" content="5; URL=http://somewhere.com/page.asp">

ASP Redirect

The ASP code to redirect looks like…
response.redirect “chrismaunderfanclub.com”

The first relies on you having each page set up to redirect, the second is done via ASP which means it requires use of the scripting engine (server hit).

ISAPI Redirection Filter

We can reduce the server load by using ISAPI filters in C++. This will have the same effect – the downside is that you need to know how to use Visual Studio to do it. The upside is that you don’t use ASP. This is filter will change the URL of the file IIS thinks it wants to return, without changing the URL the user sees in their browser

The Project

Open Visual Studio (I use version 6.0) Make a new project and select “ISAPI Extension Wizard”.

New ISAPI Project

Give your project a name. I’m using “redirector”, click "OK". You should now be presented with the following.

Tick “Generate Filter Object” and un-tick “Generate a Server Extension Object”. In case I’ll also link statically.

Click Next...

Because we only want to redirect the actual page and not the page the user thinks it is, we’ll tick “Post-processing of the request headers” and select the notification priority we want the filter to have, in this case we’ll select “high” to make sure it gets attention before any other filters. Click “Finish” and Visual Studio will now prepare the project for you.

The Code

We will now add our code to redirect all requests from .cfm to .asp The function of interest is in redirector.cpp and is called OnPreprocHeaders and it looks something like the following;

DWORD CRedirectorFilter::OnPreprocHeaders(
                             CHttpFilterContext* pCtxt, 
                             PHTTP_FILTER_PREPROC_HEADERS pHeaderInfo
                         )
{
    return SF_STATUS_REQ_NEXT_NOTIFICATION;
}

This function is called when the server has preprocessed the client headers. In this example we want to modify the URL that IIS thinks its wants to return. We’ll try something simple - replacing all Coldfusion (.cfm) extensions with .asp, Why? Perhaps we’re using ASP instead of ColdFusion and don’t want to change the links on the pages themselves.

We need to find out which page has been requested, and then replace .cfm with .asp if necessary. To do this we need to use the HTTP_FILTER_PREPROC_HEADERS pointer, pHeaderInfo, and the filter context pointer pCtxt.

We want to see the URL requested, to do this we can ask IIS what the URL was by way of the GetHeader function

DWORD CRedirectorFilter::OnPreprocHeaders(
                             CHttpFilterContext* pCtxt, 
                             PHTTP_FILTER_PREPROC_HEADERS pHeaderInfo
                         )
{
    char buffer[256]
    DWORD buffSize = sizeof(buffer);
    BOOL  bHeader=pHeaderInfo->GetHeader(pCtxt->m_pFC, "url",
                                             buffer, &buffSize); 
    ...
}
Once we have the URL we can decide whether to redirect it. I’m going to cheat a little here and use CString to deal with the string code, you can use whatever string code you like.
DWORD CRedirectorFilter::OnPreprocHeaders(
                             CHttpFilterContext* pCtxt,
                             PHTTP_FILTER_PREPROC_HEADERS pHeaderInfo
                         )
{
    char buffer[256];
    DWORD buffSize = sizeof(buffer);
    BOOL bHeader = pHeaderInfo->GetHeader(pCtxt->m_pFC, "url",
                                              buffer, &buffSize); 
    CString urlString(buffer);
    urlString.MakeLower(); // for this exercise 

    if (urlString.Find(".cfm") != -1) //we want to redirect this file

    {
        urlString.Replace(".cfm",".asp");
        char *newUrlString= urlString.GetBuffer(urlString.GetLength());
        pHeaderInfo->SetHeader(pCtxt->m_pFC, "url", newUrlString);
        return SF_STATUS_REQ_HANDLED_NOTIFICATION;
    }
    //we want to leave this alone and let IIS handle it

    return SF_STATUS_REQ_NEXT_NOTIFICATION;
}

Now, build the file and with any luck there will be no errors!

Adding the Filter to IIS

Next you will want to add the filter to IIS, or Personal Web Service if you are using it. I’m now going to describe how to add the filter to Personal Web Service.

So, go to Administrative tools (in Windows 2000) and select Internet Services Manger, something like the following will now appear.

Right click on “Default Web Site” (or whatever web server you want to add it to) and select “properties”, the following windows should then appear.

From the tabs, click on “ISAPI Filters”

Now, assuming there are no filters already present, the box in the middle should be clear (as this example shows) Click the “Add” button…

Give your filter a “name”, and use the browse button to locate the redirection filter .dll file.

Click “OK” and the filter will now appear, if you want your filter to have higher priority than others that may appear in the list, use the buttons on the left hand side to organize the correct order.

Click “OK”. The tabbed dialog will disappear.

Next we want to stop and start the web service. Right click on the “Default Web Site”, select “Stop” then right click again and select “Start”.

Alternatively you can click the black square on the rebar to stop the service, and click the “Start item” (black triangle) to restart it.

Testing it

Make a file test.asp and place it in your wwwroot directory of inetpub. Open your web browser and open location http://localhost/test.cfm. If all goes well then the contents of test.asp will be displayed in the browser.

That’s it, you’re done!

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

bryce


me? not telling, its secret *shhhsshh*

Occupation: Software Developer (Senior)
Location: Australia Australia

Other popular ISAPI articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 25 of 43 (Total in Forum: 43) (Refresh)FirstPrevNext
Subject  Author Date 
GeneralRedirect to specific page based on referrermemberlabrys5:47 5 Jun '08  
GeneralDynamic redirect?membergawain_the_stout11:46 2 Oct '07  
GeneralRe: Dynamic redirect?memberbryce14:30 4 Oct '07  
GeneralDebugging ISAPI Filter?memberLang Deng15:19 3 Aug '07  
GeneralRe: Debugging ISAPI Filter?memberbryce3:52 4 Aug '07  
GeneralRe: Debugging ISAPI Filter?memberLang Deng12:05 6 Aug '07  
GeneralRe: Debugging ISAPI Filter?memberbryce12:21 6 Aug '07  
GeneralRe: Debugging ISAPI Filter?memberLang Deng14:28 6 Aug '07  
GeneralRe: Debugging ISAPI Filter?memberbryce14:39 6 Aug '07  
GeneralRe: Debugging ISAPI Filter?memberLang Deng10:12 7 Aug '07  
GeneralRe: Debugging ISAPI Filter?memberbryce12:33 7 Aug '07  
GeneralRe: Debugging ISAPI Filter?memberLang Deng14:33 7 Aug '07  
GeneralRe: Debugging ISAPI Filter? [modified]memberbryce19:22 7 Aug '07  
GeneralRe: Debugging ISAPI Filter?memberLang Deng21:39 7 Aug '07  
GeneralDetect HTTP Agent - and redirectmemberQuantumDJ0:01 18 Jun '07  
GeneralRe: Detect HTTP Agent - and redirectmemberbryce0:24 18 Jun '07  
QuestionVSTS (VS 2005)memberOfflinesurfer2:37 11 Dec '06  
AnswerRe: VSTS (VS 2005)memberOfflinesurfer11:49 11 Dec '06  
GeneralRe: VSTS (VS 2005)memberbryce21:55 14 Dec '06  
GeneralRe: VSTS (VS 2005)memberOfflinesurfer4:58 15 Dec '06  
GeneralRe: VSTS (VS 2005)memberbryce19:52 4 Jan '07  
GeneralRe: VSTS (VS 2005)memberOfflinesurfer3:50 9 Jan '07  
GeneralRe: VSTS (VS 2005)memberYUserID5:23 3 Apr '07  
GeneralAny filter to add a banner or footer to all static and dynamic pages?membermarcelo_imai5:08 23 Nov '06  
GeneralRe: Any filter to add a banner or footer to all static and dynamic pages?memberbryce11:54 23 Nov '06  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 10 Sep 2003
Editor: Rob Manderson
Copyright 2003 by bryce
Everything else Copyright © CodeProject, 1999-2008
Web15 | Advertise on the Code Project