Click here to Skip to main content
13,298,842 members (38,852 online)
Click here to Skip to main content
Add your own
alternative version


34 bookmarked
Posted 4 Jul 2006

Forbidden Direct Download HTTP Filter

, 4 Jul 2006
Rate this:
Please Sign up or sign in to vote.
This HTTP filter DLL project will enable IIS websites to redirect all external referral downloads to the home page.

Sample Image


This DLL will enable an IIS website to redirect all requests to the home page when there are requests for EXE, RAR, Zip files that have no reference or have a reference from outside of your own web site. For example, if someone tries to download by clicking on that link from another site, they will be redirected to


A few days ago, I discovered that 80% of the traffic to my website is to download an EXE file which I give as a free download on the Internet. After check with the page view traffic, I found that users were downloading the EXE file without even looking at my web page. :( I did some search on the Internet and realised there are no free tools to stop this and to find the difference between direct download from a link in another site and download from my own website. I decided to write an HTTP filter to do this.

Key knowledge: When downloading a file from your website by clicking on a link from another website, the page will have an HTTP_Reference value which has the name of the other website. For example, if someone tries to download by clicking on that link from, then the HTTP request to your website will come with a HTTP_REFERER:

If someone tries to download files directly (not from another web page), the HTTP_REFERRE will be empty. And, if someone tries to download by clicking on a link from your own website, then the HTTP_REFERER will have your website's URL.

This can be detected by programming on the HTTP FILTER DLL project.

So, a few hours later, this project was released. It is very simple: a one line fix to the source code of the default ISAPI project of VC++ 2003. Actually, only one virtual function has been overwritten. No configuration is needed to use this tool. I had this tool enabled on one of my sites with in a second after its release. And, it has been running very well. Here is a demo. Click on the link, and you will be taken to, and from there, click on the download link, and you can then download the file.

Using the code

There are two parts that were modified in the default CHttpFilter project.

  1. I addrf a SF_NOTIFY_URL_MAP to dwflags, so our OnUrlMap will be run.
  2. //
    BOOL CNoDirectExeFilter::GetFilterVersion(PHTTP_FILTER_VERSION pVer)
       pVer->dwFlags &= ~SF_NOTIFY_ORDER_MASK;
                           SF_NOTIFY_END_OF_NET_SESSION | SF_NOTIFY_URL_MAP;
       pVer->dwFlags |= SF_NOTIFY_ORDER_LOW;
             IDS_FILTER, sz, SF_MAX_FILTER_DESC_LEN));
       _tcscpy(pVer->lpszFilterDesc, sz);
       return TRUE;
  3. We overwrite a virtual function OnUrlMap. This function checks all the HTTP requests, and deals with all requests for EXE, zip, and RAR files (you can add more file extensions to the source code by simply fixing it).
  4. DWORD CNoDirectExeFilter::OnUrlMap(CHttpFilterContext* pfc, 
                              PHTTP_FILTER_URL_MAP pUrlMap)
       DWORD dwSize;
       CString sPath;
       //if add more file ext here.
       bool bHaveReferer=false;
       char szAddress[255];
       dwSize = sizeof(szAddress);
       CString Referer;   
       //Get HTTP_REFERER Value
       if (pfc->GetServerVariable("HTTP_REFERER", szAddress, &dwSize))
       dwSize = sizeof(szAddress);
       //GET SERVER VALUE   
       CString ServerName;
       if(pfc->GetServerVariable("SERVER_NAME", szAddress, &dwSize))
          CHAR szRedirect [256];
         sprintf(szRedirect, "Location: http://%s\r\n\r\n", ServerName.GetBuffer(0));
         pfc->ServerSupportFunction ( SF_REQ_SEND_RESPONSE_HEADER, 
                (LPVOID) "302 Redirect", 
                (DWORD *) szRedirect, 
                0 );
          // we are done with this request
       return CHttpFilter::OnUrlMap(pfc, pUrlMap);

How to use

  1. Download the ready-to-use DLL file, and unzip the files into any folder accessible by IIS, like C:\winnt\system32\.
  2. Go to the ISAPI filter setting of the website which you want to enable these features.
  3. Click on the Add button and select nodirectexe.dll.
  4. Restart W3SVC.

Now, all your EXE, zip, and RAR files are under the protection of this DLL. All direct downloads and downloads by click on links other than that on your own website will be redirected to your front page URL.

Points of interest

Do it yourself sometime is the only and the best way. Oh, you will need C++ !!. :)


This is first time release. My home Page will have a more recent version released.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Web Developer
China China
I enjoy those tiny code which changs the fact of the world.
I start do Perl for write a DB engine with HTML template , which kind of like php. which was 1998.
used to work with security software company for design and coding .
now I do freelance in at my home
most of my interest is Forex trade, and IP technology.
VC++ for 5 years. one of my favour tool I wrote is NetCut.

You may also be interested in...


Comments and Discussions

QuestionIt doesn't work... Pin
TheMattster25-May-12 3:54
memberTheMattster25-May-12 3:54 
GeneralThis is wrong - referer header is optional per W3C specs Pin
rlivelyppk11-Jul-06 8:21
memberrlivelyppk11-Jul-06 8:21 
GeneralRe: This is wrong - referer header is optional per W3C specs Pin
rlivelyppk11-Jul-06 8:25
memberrlivelyppk11-Jul-06 8:25 
GeneralRe: This is wrong - referer header is optional per W3C specs Pin
cceye12-Jul-06 6:06
membercceye12-Jul-06 6:06 
GeneralRe: This is wrong - referer header is optional per W3C specs Pin
rlivelyppk12-Jul-06 6:50
memberrlivelyppk12-Jul-06 6:50 
Jokechina Pin
zhaojicheng5-Jul-06 22:57
memberzhaojicheng5-Jul-06 22:57 
GeneralGreat Pin
2ASoft5-Jul-06 10:25
member2ASoft5-Jul-06 10:25 
GeneralI hate you Pin
Ennis Ray Lynch, Jr.5-Jul-06 6:30
memberEnnis Ray Lynch, Jr.5-Jul-06 6:30 
GeneralI like the idea. Pin
ANewCoder5-Jul-06 4:24
memberANewCoder5-Jul-06 4:24 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.171207.1 | Last Updated 5 Jul 2006
Article Copyright 2006 by cceye
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid