求C#写ISAPI Filter的教程

Eri 2005-04-04 01:26:56
在MSDN上面找到C++的例子,C#应该也能写吧。

/*
Copyright (c) Microsoft Corporation
Module Name: upcase.c
*/

#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <httpfilt.h>

BOOL WINAPI __stdcall GetFilterVersion(HTTP_FILTER_VERSION *pVer)
{
/* Specify the types and order of notification */

pVer->dwFlags = (SF_NOTIFY_NONSECURE_PORT | SF_NOTIFY_URL_MAP | SF_NOTIFY_SEND_RAW_DATA | SF_NOTIFY_ORDER_DEFAULT);

pVer->dwFilterVersion = HTTP_FILTER_REVISION;

strcpy(pVer->lpszFilterDesc, "Upper case conversion filter, Version 1.0");

return TRUE;
}

DWORD WINAPI __stdcall HttpFilterProc(HTTP_FILTER_CONTEXT *pfc, DWORD NotificationType, VOID *pvData)
{
CHAR *pchIn, *pPhysPath;
DWORD cbBuffer, cbtemp;
PHTTP_FILTER_URL_MAP pURLMap;
PHTTP_FILTER_RAW_DATA pRawData;

switch (NotificationType) {

case SF_NOTIFY_URL_MAP :

/* Check the URL for a subdirectory in the form of /UC/ or /uc/ */

pURLMap = (PHTTP_FILTER_URL_MAP)pvData;

pPhysPath = pURLMap->pszPhysicalPath;

pfc->pFilterContext = 0;

for ( ; *pPhysPath; pPhysPath++)
{
/* Ensure that there are at least 4 characters (checking for "\UC\") left in the URL before checking */

if (strlen(pPhysPath) > 3)
{
if (*pPhysPath == '\\' && (*(pPhysPath + 1) == 'u' || *(pPhysPath + 1) == 'U') && (*(pPhysPath + 2) == 'c' || *(pPhysPath + 2) == 'C') && *(pPhysPath + 3) == '\\')
{
/* Now that we've found it, remove it by collapsing everything down */

for ( ; *(pPhysPath + 3) ; pPhysPath++)
*pPhysPath = *(pPhysPath + 3);

/* NULL terminate the string */

*pPhysPath = '\0';

/* And set the flag to let the SF_NOTIFY_SEND_RAW_DATA handler know to uppercase the content */

pfc->pFilterContext = (VOID *)1;

/* Break us out of the loop - note that this will only find the first instance of /UC/ in the URL */

break;
}
}
}

break;

case SF_NOTIFY_SEND_RAW_DATA :

if (pfc->pFilterContext)
{
pRawData = (PHTTP_FILTER_RAW_DATA)pvData;

pchIn = (BYTE *)pRawData->pvInData;

cbBuffer = 0;

if (pfc->pFilterContext == (VOID *)1)
{
/*
As this is the first block, scan through it until 2 CRLFs are seen, to pass
all of the headers.
*/

for ( ; cbBuffer < pRawData->cbInData - 2; cbBuffer++)
{
if (pchIn[cbBuffer] == '\n' && pchIn[cbBuffer + 2] == '\n')
{
cbBuffer += 3;

break;
}

cbBuffer++;
}

for (cbtemp = 0; cbtemp < (cbBuffer - 3); cbtemp++)
{
if (pchIn[cbtemp] == '/' && pchIn[cbtemp + 1] == 'h' && pchIn[cbtemp + 2] == 't' && pchIn[cbtemp + 3] == 'm')
{
pfc->pFilterContext = (VOID *)2;

break;
}
}

if (cbtemp == cbBuffer)
pfc->pFilterContext = 0; /* not an html file */
}

/* Now uppercase everything */

if (pfc->pFilterContext)
for ( ; cbBuffer < pRawData->cbInData; cbBuffer++)
pchIn[cbBuffer] = (pchIn[cbBuffer] >= 'a' && pchIn[cbBuffer] <= 'z') ? (pchIn[cbBuffer] - 'a' + 'A') : pchIn[cbBuffer];
}

break;

default :

break;
}

return SF_STATUS_REQ_NEXT_NOTIFICATION;
}
...全文
202 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
Eri 2005-06-06
  • 打赏
  • 举报
回复
这么久都没有高手来?
Eri 2005-04-09
  • 打赏
  • 举报
回复
高手走了?
Eri 2005-04-05
  • 打赏
  • 举报
回复
自定义Log,就是不用IIS的Log纪录功能,
自己写一个ISAPI Filter自定义实现Log纪录,在IIS6上面。
C#应该能写吧?
saucer 2005-04-04
  • 打赏
  • 举报
回复
>>>来自定义IIS的Log


什么意思?写自定义的Log?HttpModules只对ASP.NET管用

http://msdn.microsoft.com/msdnmag/issues/02/08/HTTPFilters/
Eri 2005-04-04
  • 打赏
  • 举报
回复
我想明白的一个就是,我想用C#写一个ISAPI,
来自定义IIS的Log
HttpModules能做到么?
saucer 2005-04-04
  • 打赏
  • 举报
回复
if you mean for ASP.NET, you should be writing HttpModules, see

How To Create an ASP.NET HTTP Module Using Visual C# .NET
http://support.microsoft.com/kb/307996/EN-US/

Implementing an IIS Application Filter Using .NET HttpModules and Response Filtering
http://www.devx.com/vb2themax/Article/19901/0/page/1
UrlRewriter.NET是一个开源的、轻量级、高配置的URL重组件,支持ASP.NET 1.1 和ASP.NET 2.0。UrlRewriter.NET提供地址重的功能,它类似于Apache服务器中使用.htaccecc进行地址重。而且使用本组件,您无需安装任何类似于ISAPI Rewrite filterISAPI组件。本组件是免费开源的,遵循MIT-style licence开源协议。 UrlRewriter.NET是一个非常棒的搜索引擎优化(Search Engine Optimization,简称SEO)工具。使用它,您可以创建包含指定关键字的Url,从而提高您网站在搜索引擎的排名。 使用UrlRewriter.NET您可以做到: 为实际的Url提供一个面向用户和搜索引擎的更加友好的Url格式(例如隐藏Url里面的.aspx扩展名,甚至去掉所有的扩展名,实现语意化的Url地址)。 当您的网站改版之后,可以自动将旧的访问地址转到新的地址,而用户无需参与任何多余的操作。这有利于搜索引擎对您的网站执行更好的收录。 阻拦一些来自于非法的或者您不想让其访问的一些来访请。比如可以根据User-Agent来判断是否搜索引擎,强制其无法收录您不想让其收录的内容(可以不必设置robots.txt)。 根据来访者的IP地址来为其提供不同的服务,比如禁止某IP段的访问请。 还有很多,都需要您的潜心挖掘…… UrlRewriter.NET是一个纯粹使用C#的.NET组件,并且它无需任何其他ISAPI组件的支持。您可以使用XML文档格式来为其配置重规则,默认包含在web.config文件里面,当然,您也可以自定义一个规则文件。

110,567

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧