title: Unable to set both GzipStream and Content-Encoding to gzip on the same response
body: <p>I'm working on a CompressionModule to be used in web.config, I've broken up a bigger issue into this case which I'm stumped on.</p>
  
  <p>For this example I've created a new MVC4 internet application and have made the following modifications to web.config:</p>
  
  <pre><code>&lt;handlers&gt;
    &lt;!--
    &lt;remove name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" /&gt;
    &lt;remove name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" /&gt;
    &lt;remove name="ExtensionlessUrlHandler-Integrated-4.0" /&gt;
    &lt;add name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness32" responseBufferLimit="0" /&gt;
    &lt;add name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness64" responseBufferLimit="0" /&gt;
    &lt;add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" /&gt;
    --&gt;
    &lt;add name="Content" path="*" verb="*" preCondition="integratedMode" type="System.Web.StaticFileHandler" /&gt;
  &lt;/handlers&gt;
  &lt;modules&gt;
          &lt;remove name="monorailRouting" /&gt;
        &lt;add name="compressionModule" type="Platform.Web.CompressionModule" /&gt;
    &lt;!--
    &lt;add name="ErrorHandlerModule" type="System.Web.Mobile.ErrorHandlerModule, System.Web.Mobile, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" /&gt;
        --&gt;
  &lt;/modules&gt;
  </code></pre>
  
  <p>Where compression module is </p>
  
  <pre><code>namespace Platform.Web {
  public class CompressionModule : IHttpModule {
  
      #region HTTP Header Constants
      private const string DEFLATE = "deflate";
      private const string GZIP = "gzip";
      private const string CONTENT_ENCODING = "Content-Encoding";
      private const string ACCEPT_ENCODING = "Accept-Encoding";
      private const string VARY = "Vary";
      #endregion
  
      #region IHttpModule Members
      public void Dispose() {
          //noop
      }
  
      public void Init(HttpApplication context) {
          context.BeginRequest += new EventHandler(context_CompressResponse);
      }
  
  
      void context_CompressResponse(object sender, EventArgs e) {
          HttpApplication app = (HttpApplication)sender;
  
          string encodings = app.Request.Headers.Get(ACCEPT_ENCODING);
  
          if (encodings == null)
          {
              return;
          }
  
          encodings = encodings.ToLower();
  
          if (encodings.Contains(GZIP))
          {
              //1
              app.Response.Filter = new GZipStream(app.Response.Filter, CompressionMode.Compress);
  
              //2
              app.Response.AppendHeader(CONTENT_ENCODING, GZIP);
  
          }
  
          app.Response.AppendHeader(VARY, CONTENT_ENCODING);
  
  
  
      }
  
      #endregion
  }
  }
  </code></pre>
  
  <p>And I am experiencing the following issues:</p>
  
  <ol>
  <li><p>If line 1 and 2 exist the app loads fine. But nothing is apparently
  gzipped in the headers, nor the body. I'm determining this through Fiddler.</p></li>
  <li><p>If line 1 exists but 2 doesn't the app loads but unreadable gzip is
      displayed in the browser.</p></li>
  <li><p>If line 1 doesn't exists but 2 does the app fails to load but
      Content-Encoding: gzip is in the headers.</p></li>
  </ol>
  
  <p>Anyone have some suggestions on what I may be doing wrong?</p>