HttpModule 破坏了 WebResource.axd

发布于 2024-11-25 14:12:33 字数 3199 浏览 2 评论 0 原文

我编写了一个 HttpModule,它进行了一些简单的内容重写。它工作正常,但是它有一个令人讨厌的副作用,即 WebResource.axd 的输出为零字节。我在网上看到了一些对此已知问题的参考,但没有使用像我这样的过滤器。

我在这里做的事情(或忽略)是否会导致 WebResource.axd 中断?我在 IIS 7.5 上使用 ASP.NET 4.0。

Imports System.Text
Imports System.Text.RegularExpressions
Imports System.IO
Imports System.Web

Namespace HttpModules

  Public Class ResponseRewritingModule
    Implements IHttpModule

    Public Sub Init(ByVal app As HttpApplication) Implements IHttpModule.Init
      AddHandler app.ReleaseRequestState, AddressOf Me.OnReleaseRequestState
    End Sub

    Public Sub Dispose() Implements IHttpModule.Dispose
    End Sub

    Public Sub OnReleaseRequestState(ByVal s As Object, ByVal e As EventArgs)
      Dim app As HttpApplication = CType(s, HttpApplication)
      If (app.Request.Url.ToString().Contains(".aspx")) Then
        app.Response.Filter = New ImageUrlResponseFilter(app.Response.Filter)
      End If
    End Sub

  End Class

  Public Class ImageUrlResponseFilter
    Inherits MemoryStream

    Private responseStream As Stream
    Private matchpattern As String = "src=""/Images/"
    Private matchpattern2 As String = "url\(/Images"
    Private matchpattern3 As String = "src = ""/Images/"
    Private matchpattern4 As String = "src=""http://www.mydomain.com/Images/"
    Private replacementstring As String = "src=""http://images.mydomain.com/Images/"
    Private replacementstring2 As String = "url(http://images.mydomain.com/Images"

    Public Sub New(inputStream As Stream)
      responseStream = inputStream
    End Sub

    Public Overrides Sub Write(buffer As Byte(), offset As Integer, count As Integer)

      Dim strBuffer As String = System.Text.UTF8Encoding.UTF8.GetString(buffer)

      If (Not HttpContext.Current.Request.IsSecureConnection) Then
        strBuffer = Regex.Replace(strBuffer, matchpattern, replacementstring, RegexOptions.Compiled Or RegexOptions.IgnoreCase)
        strBuffer = Regex.Replace(strBuffer, matchpattern2, replacementstring2, RegexOptions.Compiled Or RegexOptions.IgnoreCase)
        strBuffer = Regex.Replace(strBuffer, matchpattern3, replacementstring, RegexOptions.Compiled Or RegexOptions.IgnoreCase)
        strBuffer = Regex.Replace(strBuffer, matchpattern4, replacementstring, RegexOptions.Compiled Or RegexOptions.IgnoreCase)
        responseStream.Write(UTF8Encoding.UTF8.GetBytes(strBuffer), offset, UTF8Encoding.UTF8.GetByteCount(strBuffer))
      Else
        ' Do nothing for SSL connections
        responseStream.Write(buffer, offset, count)
      End If

    End Sub

  End Class

End Namespace

更新: Daniel Richardson 似乎已经发现了根本原因,并在这里提供了解决方案:http://daniel-richardson.blogspot.com/2008/11/how-to-apply-filter-to-content-returned.html

微软显然已经知道这个问题很多年了并且不会修复它:http://connect.microsoft.com/VisualStudio/feedback/details/105150/webresource-axd-output-always-truncated-when-used-with-a-response-filter

I wrote an HttpModule which does some simple content rewriting. It works fine, however it has a nasty side effect where the output of WebResource.axd is zero bytes. I've seen some references to this known issue online, but not with a filter quite like mine.

Am I doing something (or neglecting to) here that would cause WebResource.axd to break? I'm using ASP.NET 4.0 on IIS 7.5.

Imports System.Text
Imports System.Text.RegularExpressions
Imports System.IO
Imports System.Web

Namespace HttpModules

  Public Class ResponseRewritingModule
    Implements IHttpModule

    Public Sub Init(ByVal app As HttpApplication) Implements IHttpModule.Init
      AddHandler app.ReleaseRequestState, AddressOf Me.OnReleaseRequestState
    End Sub

    Public Sub Dispose() Implements IHttpModule.Dispose
    End Sub

    Public Sub OnReleaseRequestState(ByVal s As Object, ByVal e As EventArgs)
      Dim app As HttpApplication = CType(s, HttpApplication)
      If (app.Request.Url.ToString().Contains(".aspx")) Then
        app.Response.Filter = New ImageUrlResponseFilter(app.Response.Filter)
      End If
    End Sub

  End Class

  Public Class ImageUrlResponseFilter
    Inherits MemoryStream

    Private responseStream As Stream
    Private matchpattern As String = "src=""/Images/"
    Private matchpattern2 As String = "url\(/Images"
    Private matchpattern3 As String = "src = ""/Images/"
    Private matchpattern4 As String = "src=""http://www.mydomain.com/Images/"
    Private replacementstring As String = "src=""http://images.mydomain.com/Images/"
    Private replacementstring2 As String = "url(http://images.mydomain.com/Images"

    Public Sub New(inputStream As Stream)
      responseStream = inputStream
    End Sub

    Public Overrides Sub Write(buffer As Byte(), offset As Integer, count As Integer)

      Dim strBuffer As String = System.Text.UTF8Encoding.UTF8.GetString(buffer)

      If (Not HttpContext.Current.Request.IsSecureConnection) Then
        strBuffer = Regex.Replace(strBuffer, matchpattern, replacementstring, RegexOptions.Compiled Or RegexOptions.IgnoreCase)
        strBuffer = Regex.Replace(strBuffer, matchpattern2, replacementstring2, RegexOptions.Compiled Or RegexOptions.IgnoreCase)
        strBuffer = Regex.Replace(strBuffer, matchpattern3, replacementstring, RegexOptions.Compiled Or RegexOptions.IgnoreCase)
        strBuffer = Regex.Replace(strBuffer, matchpattern4, replacementstring, RegexOptions.Compiled Or RegexOptions.IgnoreCase)
        responseStream.Write(UTF8Encoding.UTF8.GetBytes(strBuffer), offset, UTF8Encoding.UTF8.GetByteCount(strBuffer))
      Else
        ' Do nothing for SSL connections
        responseStream.Write(buffer, offset, count)
      End If

    End Sub

  End Class

End Namespace

UPDATE: Daniel Richardson seems to have discovered the root cause and has a solution here: http://daniel-richardson.blogspot.com/2008/11/how-to-apply-filter-to-content-returned.html

Microsoft has apparently known about this issue for years and WILL NOT FIX IT: http://connect.microsoft.com/VisualStudio/feedback/details/105150/webresource-axd-output-always-truncated-when-used-with-a-response-filter

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文