Click here to Skip to main content
15,881,812 members
Articles / Desktop Programming / MFC

How to Make an ISAPI Redirection Filter

Rate me:
Please Sign up or sign in to vote.
4.36/5 (20 votes)
10 Sep 20034 min read 411.7K   2.9K   52  
How to build a simple ISAPI Filter
// REDIRECTOR.CPP - Implementation file for your Internet Server
//    redirector Filter

#include "stdafx.h"
#include "redirector.h"




///////////////////////////////////////////////////////////////////////
// The one and only CRedirectorFilter object

CRedirectorFilter theFilter;


///////////////////////////////////////////////////////////////////////
// CRedirectorFilter implementation

CRedirectorFilter::CRedirectorFilter()
{
}

CRedirectorFilter::~CRedirectorFilter()
{
}

BOOL CRedirectorFilter::GetFilterVersion(PHTTP_FILTER_VERSION pVer)
{
	// Call default implementation for initialization
	CHttpFilter::GetFilterVersion(pVer);

	// Clear the flags set by base class
	pVer->dwFlags &= ~SF_NOTIFY_ORDER_MASK;

	// Set the flags we are interested in
	pVer->dwFlags |= SF_NOTIFY_ORDER_HIGH | SF_NOTIFY_SECURE_PORT | SF_NOTIFY_NONSECURE_PORT
			 | SF_NOTIFY_PREPROC_HEADERS | SF_NOTIFY_END_OF_NET_SESSION;

	// Load description string
	TCHAR sz[SF_MAX_FILTER_DESC_LEN+1];
	ISAPIVERIFY(::LoadString(AfxGetResourceHandle(),
			IDS_FILTER, sz, SF_MAX_FILTER_DESC_LEN));
	_tcscpy(pVer->lpszFilterDesc, sz);
	return TRUE;
}

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;
}

DWORD CRedirectorFilter::OnEndOfNetSession(CHttpFilterContext* pCtxt)
{
	// TODO: React to this notification accordingly and
	// return the appropriate status code
	return SF_STATUS_REQ_NEXT_NOTIFICATION;
}

// Do not edit the following lines, which are needed by ClassWizard.
#if 0
BEGIN_MESSAGE_MAP(CRedirectorFilter, CHttpFilter)
	//{{AFX_MSG_MAP(CRedirectorFilter)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()
#endif	// 0

///////////////////////////////////////////////////////////////////////
// If your extension will not use MFC, you'll need this code to make
// sure the extension objects can find the resource handle for the
// module.  If you convert your extension to not be dependent on MFC,
// remove the comments arounn the following AfxGetResourceHandle()
// and DllMain() functions, as well as the g_hInstance global.

/****

static HINSTANCE g_hInstance;

HINSTANCE AFXISAPI AfxGetResourceHandle()
{
	return g_hInstance;
}

BOOL WINAPI DllMain(HINSTANCE hInst, ULONG ulReason,
					LPVOID lpReserved)
{
	if (ulReason == DLL_PROCESS_ATTACH)
	{
		g_hInstance = hInst;
	}

	return TRUE;
}

****/

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.


Written By
Software Developer (Senior) TMR
Australia Australia
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions