Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

HttpCompressionModule bug fixed code

By: nmg196 on May 23rd, 2013  |  syntax: C#  |  size: 3.12 KB  |  views: 478  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. using System;
  2. using System.IO.Compression;
  3. using System.Web;
  4. using System.Web.Security;
  5.  
  6. /// <summary>
  7. /// Implement two way HTTP compression for the SOAP API
  8. /// Code taken from: http://netpl.blogspot.co.uk/2009/07/aspnet-webservices-two-way-response-and.html
  9. /// Fix: Set Content-encoding: gzip header when an Exception occurs on the server.
  10. /// Fix: Do not attempt to decrypt GZIP request stream when request was not GZIPed.
  11. /// </summary>
  12. public class HttpCompressionModule : IHttpModule
  13. {
  14.         private bool isDisposed = false;
  15.  
  16.         ~HttpCompressionModule()
  17.         {
  18.                 Dispose(false);
  19.         }
  20.  
  21.         public void Init(HttpApplication context)
  22.         {
  23.                 context.BeginRequest += new EventHandler(Context_BeginRequest);
  24.                 context.PreSendRequestHeaders += new EventHandler(context_PreSendRequestHeaders);
  25.         }
  26.  
  27.         void context_PreSendRequestHeaders(object sender, EventArgs e)
  28.         {
  29.                 // Fix headers having been lost if an exception occurred.
  30.                 HttpApplication app = sender as HttpApplication;
  31.                 HttpContext ctx = app.Context;
  32.                 if (app.Response.Filter is GZipStream) SetEncoding("gzip");
  33.                 else if (app.Response.Filter is DeflateStream) SetEncoding("deflate");
  34.  
  35.                 // Fix double header
  36.                 if (ctx.Response.Headers["Content-encoding"] == "gzip,gzip")
  37.                         ctx.Response.Headers.Set("Content-encoding", "gzip");
  38.         }
  39.  
  40.         /// <summary>
  41.         /// Fires at the start of each new web request
  42.         /// </summary>
  43.         /// <param name="sender">The Sender</param>
  44.         /// <param name="e">Event Arguments</param>
  45.         public void Context_BeginRequest(object sender, EventArgs e)
  46.         {
  47.                 HttpApplication app = sender as HttpApplication;
  48.                 HttpContext ctx = app.Context;
  49.  
  50.                 // Currently only enable for the Uploader API webservice
  51.                 if (!ctx.Request.Url.PathAndQuery.ToLower().Contains("uploaderapi.asmx"))
  52.                 {
  53.                         return;
  54.                 }
  55.  
  56.                 // Add request filter if request was GZIP encoded
  57.                 string requestEncoding = ctx.Request.Headers["Content-encoding"];
  58.                 if (requestEncoding != null && requestEncoding == "gzip")
  59.                 {
  60.                    app.Request.Filter =
  61.                         new System.IO.Compression.GZipStream(app.Request.Filter, CompressionMode.Decompress);
  62.                 }
  63.  
  64.                 // Add response compression filter if the client accepts compressed responses
  65.                 if (IsEncodingAccepted("gzip"))
  66.                 {
  67.                         app.Response.Filter = new GZipStream(app.Response.Filter, CompressionMode.Compress);
  68.                         SetEncoding("gzip");
  69.                 }
  70.                 else if (IsEncodingAccepted("deflate"))
  71.                 {
  72.                         app.Response.Filter = new DeflateStream(app.Response.Filter, CompressionMode.Compress);
  73.                         SetEncoding("deflate");
  74.                 }
  75.         }
  76.  
  77.         private bool IsEncodingAccepted(string encoding)
  78.         {
  79.                 return HttpContext.Current.Request.Headers["Accept-encoding"] != null &&
  80.                         HttpContext.Current.Request.Headers["Accept-encoding"].Contains(encoding);
  81.         }
  82.  
  83.         private void SetEncoding(string encoding)
  84.         {
  85.                 HttpContext ctx = HttpContext.Current;
  86.                 string responseEncodings = ctx.Response.Headers.Get("Content-encoding");
  87.                 if (responseEncodings == null || !responseEncodings.Contains(encoding))
  88.                         HttpContext.Current.Response.AppendHeader("Content-encoding", encoding);
  89.         }
  90.  
  91.         public void Dispose()
  92.         {
  93.                 Dispose(true);
  94.         }
  95.  
  96.         private void Dispose(bool dispose)
  97.         {
  98.                 isDisposed = dispose;
  99.         }
  100. }