Using ISAPI filters to enhance the functionality of IIS

Source: Internet
Author: User
Tags filter define bool count header iis include

As a WWW server software, Microsoft's Internet Infomation Server (IIS) is easy to learn, easy to manage and widely used. You can also enhance the functionality of IIS by making your own custom processing through an ISAPI filter. ISAPI filters can customize the following processing: receiving HTTP protocol header preprocessing, sending HTTP protocol header preprocessing, sending raw data preprocessing, obtaining raw data preprocessing, HTTP session ending information processing, custom security authentication mechanism, URL mapping information processing, logging processing and so on. With the flexibility to use these customization processes, you can accomplish many seemingly elusive features and get unexpected results. However, improper use of ISAPI filters can also affect server performance.


The development of ISAPI filters is very simple, only three interface DLL functions can be completed. They are getfilterversion (), HttpFilterProc (), Terminatefilter (), and you can view MSDN for detailed usage. ISAPI filters are DLL files that are typically developed in C/s + + language. To enable the ISAPI filter to run, you need to hkey_local_machine\system\currentcontrolset\ in the registry
Services\w3svc\parameters creates a string entry with the name "Filter Dlls", and the value is the full path name of the ISAPI filter file. If the string entry already exists, simply add its full path name to it and use the ";" between the different ISAPI filter files. Delimited, you can add to the appropriate location based on the precedence of the execution. Once you've set it up, restart the IIS service, and your ISAPI filter works.

The following authors cite a specific application example.

Statistical analysis of the access content:
Usually we put a counter in the page that needs to count, or use ASP file to implement the counting function. This method does not apply to other non-HTML-formatted files, such as README.TXT. If you use the log feature of IIS, it is not convenient to occupy too much space. By customizing the URL mapping information processing, the author tracks the count statistics of several files of interest and records the results in a file.

The following is its source program.

Fcount.def:
LIBRARY Fcount
Exports getfilterversion
HttpFilterProc
Terminatefilter

FCOUNT.C:
#include <stdio.h>
#include <string.h>
#include <windows.h>
#include

#define LOGFILE "C:\\inetpub\\fcount.log"
#define PAGES 5
char* urls[] = {
"/default.htm",
"/banner.gif",
"/product/readme.txt",
"/product/product1.htm",
"/product/product2.htm"
};
int counts[pages];

BOOL WINAPI GetFilterVersion
(Http_filter_version *pver)
{
int i;

Pver->dwfilterversion = http_filter_revision;
strcpy (Pver->lpszfilterdesc, "Fcount");
Pver->dwflags = Sf_notify_url_map; /* Filter Content * *

For (i=0 i<pages; i++) {/* Read the initial count value from the file. * *
Counts[i] = Getprivateprofileint ("Visitcounter",
Urls[i],
0, logfile);
}

return TRUE;
}

DWORD WINAPI HttpFilterProc
(Http_filter_context *PFC,
DWORD Notetype, VOID *pvnote)
{
int i;
Char lurl[512];
Char buf[16];

strcpy (Lurl, (phttp_filter_url_map) pvnote)
->pszurl);
_STRLWR (Lurl);
For (i=0 i<pages; i++) {
if (strcmp (Lurl, urls[i]) ==0) {
Counts[i] + +; /* Count Value Increase * *
if (counts[i]%10==0) {
/* When the count value is full 10 o'clock, the file is entered,
To avoid the loss of all data when the system suddenly dies.
_itoa (Counts[i], buf, 10);
WritePrivateProfileString ("Visitcounter",
Urls[i], buf, logfile);
}
Break
}
}
return sf_status_req_next_notification;
}

BOOL WINAPI Terminatefilter (DWORD dwflags)
{
int i;
Char buf[16];

For (i=0 i<pages; i++) {
/* Write the Count value to the file when the system is stopped * *
_itoa (Counts[i], buf, 10);
WritePrivateProfileString ("Visitcounter",
Urls[i], buf, logfile);
}
return TRUE;
}

The above two examples are compiled using VC 6.0 and are debugged on WINNT2000 + SP3 and IIS 5.0.







Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.