IHttpModule Response.Filter 不关闭 HTML 写入
我编写了一个自定义 IHttpModule,但当源代码中没有结束标记时,它会导致问题。我在 CMS 中运行过几个页面,我正在运行这个页面,其中 .aspx 页面更像是一个处理程序,并且放弃关闭 html 以通过 ajax 向用户提供响应。
这是我的源代码:
public class HideModule : IHttpModule
{
public void Dispose()
{
//Empty
}
public void Init(HttpApplication app)
{
app.ReleaseRequestState += new EventHandler(InstallResponseFilter);
}
// ---------------------------------------------
private void InstallResponseFilter(object sender, EventArgs e)
{
HttpResponse response = HttpContext.Current.Response;
string filePath = HttpContext.Current.Request.FilePath;
string fileExtension = VirtualPathUtility.GetExtension(filePath);
if (response.ContentType == "text/html" && fileExtension.ToLower() == ".aspx")
response.Filter = new PageFilter(response.Filter);
}
}
public class PageFilter : Stream
{
Stream responseStream;
long position;
StringBuilder responseHtml;
public PageFilter (Stream inputStream)
{
responseStream = inputStream;
responseHtml = new StringBuilder ();
}
//Other overrides here
public override void Write(byte[] buffer, int offset, int count)
{
string strBuffer = System.Text.UTF8Encoding.UTF8.GetString (buffer, offset, count);
Regex eof = new Regex ("</html>", RegexOptions.IgnoreCase);
if (!eof.IsMatch (strBuffer))
{
responseHtml.Append (strBuffer);
}
else
{
responseHtml.Append (strBuffer);
string finalHtml = responseHtml.ToString();
//Do replace here
byte[] data = System.Text.UTF8Encoding.UTF8.GetBytes(finalHtml);
responseStream.Write(data, 0, data.Length);
}
}
#endregion
}
正如您所看到的,这很棒,因为它只在上次调用 Write 时进行替换,但如果输出没有结束 HTML 标记,blammo。
我最好的选择是,如果没有找到关闭 html,甚至不添加新的过滤器。但我认为我无法那么早拦截完整的流。如果失败的话,除了寻找结束 html 标签之外,是否还有另一种方法来检测 Write 是否位于流的末尾?
提前致谢。
I wrote a custom IHttpModule but it is causing issues when there is no closing tag in the source. I have ran across a few pages in the CMS I am running this for where the .aspx page is used more like a handler and forgoes closing the html to give a response via ajax back to the user.
Here is my source:
public class HideModule : IHttpModule
{
public void Dispose()
{
//Empty
}
public void Init(HttpApplication app)
{
app.ReleaseRequestState += new EventHandler(InstallResponseFilter);
}
// ---------------------------------------------
private void InstallResponseFilter(object sender, EventArgs e)
{
HttpResponse response = HttpContext.Current.Response;
string filePath = HttpContext.Current.Request.FilePath;
string fileExtension = VirtualPathUtility.GetExtension(filePath);
if (response.ContentType == "text/html" && fileExtension.ToLower() == ".aspx")
response.Filter = new PageFilter(response.Filter);
}
}
public class PageFilter : Stream
{
Stream responseStream;
long position;
StringBuilder responseHtml;
public PageFilter (Stream inputStream)
{
responseStream = inputStream;
responseHtml = new StringBuilder ();
}
//Other overrides here
public override void Write(byte[] buffer, int offset, int count)
{
string strBuffer = System.Text.UTF8Encoding.UTF8.GetString (buffer, offset, count);
Regex eof = new Regex ("</html>", RegexOptions.IgnoreCase);
if (!eof.IsMatch (strBuffer))
{
responseHtml.Append (strBuffer);
}
else
{
responseHtml.Append (strBuffer);
string finalHtml = responseHtml.ToString();
//Do replace here
byte[] data = System.Text.UTF8Encoding.UTF8.GetBytes(finalHtml);
responseStream.Write(data, 0, data.Length);
}
}
#endregion
}
As you can see this is great because it only does the replace the last time Write is called, but if the output doesn't have a closing HTML tag, blammo.
My best option would be to not even add the new filter if a closing html is not found. But I don't think I can intercept the full stream that early. Failing that is there another way to detect Write is at the end of the stream besides looking for the closing html tag?
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,如果是 WebForms,您应该能够在 InstallResponseFilter 函数中执行类似的操作:
Well, if it's WebForms, you should be able to do something like this in your InstallResponseFilter function: