Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IIS 7.5 ASP.NET HttpModule - Setting Response.Filter results in chunked encoding

I'm trying to create a HttpModule that changes the Response.Filter like so (for this demonstration just set the filter back to itself):

public class ContentTrafficMonitor : IHttpModule
{
  public void Init( HttpApplication context )
  {
     context.BeginRequest += OnBeginRequest;
  }

  public void Dispose()
  {
  }

  private static void OnBeginRequest( object sender, EventArgs e )
  {
     var application = (HttpApplication) sender;
     application.Response.Filter = application.Response.Filter;
  }

}

Doing so sets the transfer encoding of the response to chunked, rather than using the Content-Length header.

If I remove the line where the Response.Filter is set, the response does have the Content-Length header. Our application depends on the Content-Length header, is there any way to prevent this behavior?

like image 365
Evan Avatar asked Feb 08 '11 20:02

Evan


1 Answers

My guess is, setting the filter interferes with the normal buffering of the output, hence the output is now chunked.

Maybe you could imitate the behaviour by having your filter read till end, ie get all the output & set the content length header based on what you've read before you write everything read out.

It's only a guess though I'm afraid.

Simon

like image 184
Simon Halsey Avatar answered Oct 30 '22 20:10

Simon Halsey