IHttpModule Response.Filter 不关闭 HTML 写入

发布于 2024-11-23 16:41:12 字数 2057 浏览 1 评论 0原文

我编写了一个自定义 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

以为你会在 2024-11-30 16:41:12

好吧,如果是 WebForms,您应该能够在 InstallResponseFilter 函数中执行类似的操作:

if(Application.Context.CurrentHandler is System.Web.UI.Page
                    && Application.Request["HTTP_X_MICROSOFTAJAX"] == null
                    && Application.Request.Params["_TSM_CombinedScripts_"] == null)
{
response.Filter=new PageFilter(response.Filter);
}

Well, if it's WebForms, you should be able to do something like this in your InstallResponseFilter function:

if(Application.Context.CurrentHandler is System.Web.UI.Page
                    && Application.Request["HTTP_X_MICROSOFTAJAX"] == null
                    && Application.Request.Params["_TSM_CombinedScripts_"] == null)
{
response.Filter=new PageFilter(response.Filter);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文