using System;
using System.IO ;
using System.Text ;
using System.Text.RegularExpressions ;
namespace dhzCry
{
public class FilterModule:System.Web.IHttpModule
{
public FilterModule()
{}
protected void OnPreSendRequestContent(Object sender, EventArgs e)
{
System.Web.HttpApplication myContext = (System.Web.HttpApplication)sender;
//毒藥就是從Response.Filter這裡放進來,至於毒藥下一步再作
myContext.Response.Filter = new UpperCaseFilter(myContext.Response.Filter);
}
public void Init(System.Web.HttpApplication context)
{
context.PostRequestHandlerExecute+= new EventHandler(this.OnPreSendRequestContent);
// context.PreSendRequestContent+= new EventHandler(this.OnPreSendRequestContent);
// context.BeginRequest+=new EventHandler(this.OnPreSendRequestContent);
}
public void Dispose()
{
}
}
/// <summary>
/// UpperCaseFilter 毒藥
/// </summary>
public class UpperCaseFilter : Stream
// This filter changes all characters passed through it to uppercase.
{
private Stream _sink;
private long _position;
private System.Text.Encoding end=System.Text.Encoding.GetEncoding("GB18030");
private System.Text.StringBuilder oOutput = new StringBuilder();
public UpperCaseFilter(Stream sink)
{
_sink = sink;
}
// The following members of Stream must be overriden.
public override bool CanRead
{
get { return true; }
}
public override bool CanSeek
{
get { return true; }
}
public override bool CanWrite
{
get { return true; }
}
public override long Length
{
get { return 0; }
}
public override long Position
{
get { return _position; }
set { _position = value; }
}
public override long Seek(long offset, System.IO.SeekOrigin direction)
{
return _sink.Seek(offset, direction);
}
public override void SetLength(long length)
{
_sink.SetLength(length);
}
public override void Close()
{
_sink.Close();
}
public override void Flush()
{
_sink.Flush();
}
public override int Read(byte[] buffer, int offset, int count)
{
return _sink.Read(buffer, offset, count);
}
// The Write method actually does the filtering.
public override void Write(byte[] buffer, int offset, int count)
{
string szBuffer = System.Text.UTF8Encoding.UTF8.GetString(buffer, offset, count);
//Look for the end of the HTML file
//查找HTML文件的结尾部分
Regex oEndFile = new Regex("</head>", RegexOptions.IgnoreCase);
if (oEndFile.IsMatch(szBuffer))
{
//Append the last buffer of data
//附加上缓冲区中的最后一部分数据
oOutput.Append(szBuffer);
//Get back the complete response for the client
//传回完整的客户端返回数据
string szCompleteBuffer = oOutput.ToString();
Regex oRegEx = new Regex(@"<Title>\w*</title>", RegexOptions.IgnoreCase);
if (oRegEx.IsMatch(szCompleteBuffer))
{
//Found string, so replace all occurences
//with the new value (use a non-case sensitive
// match)
//已经找到字符串,所以讲所有的匹配项目用新的值来替换(不区分大小写)
string newBuffer = Regex.Replace(szCompleteBuffer,@"<Title>.*</title>", "<TITLE>dhz</TITLE>", RegexOptions.IgnoreCase);
//Set the reference so can use same code below...
//设置引用,因此可以重复使用相同的代码...
szCompleteBuffer = newBuffer;
}
//No match, so write out original data
//没有匹配,因此写入源代码
byte[] data = System.Text.UTF8Encoding.UTF8.GetBytes(szCompleteBuffer);
_sink.Write(data, 0, data.Length);
}
else
{
oOutput.Append(szBuffer);
}
}
}
}
/////////////
<httpModules>
<add type="dhzCry.FilterModule,dhzCry" name="FilterModule"/>
</httpModules>