Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.IO.Compression;
- using System.Web;
- using System.Web.Security;
- /// <summary>
- /// Implement two way HTTP compression for the SOAP API
- /// Code taken from: http://netpl.blogspot.co.uk/2009/07/aspnet-webservices-two-way-response-and.html
- /// Fix: Set Content-encoding: gzip header when an Exception occurs on the server.
- /// Fix: Do not attempt to decrypt GZIP request stream when request was not GZIPed.
- /// </summary>
- public class HttpCompressionModule : IHttpModule
- {
- private bool isDisposed = false;
- ~HttpCompressionModule()
- {
- Dispose(false);
- }
- public void Init(HttpApplication context)
- {
- context.BeginRequest += new EventHandler(Context_BeginRequest);
- context.PreSendRequestHeaders += new EventHandler(context_PreSendRequestHeaders);
- }
- void context_PreSendRequestHeaders(object sender, EventArgs e)
- {
- // Fix headers having been lost if an exception occurred.
- HttpApplication app = sender as HttpApplication;
- HttpContext ctx = app.Context;
- if (app.Response.Filter is GZipStream) SetEncoding("gzip");
- else if (app.Response.Filter is DeflateStream) SetEncoding("deflate");
- // Fix double header
- if (ctx.Response.Headers["Content-encoding"] == "gzip,gzip")
- ctx.Response.Headers.Set("Content-encoding", "gzip");
- }
- /// <summary>
- /// Fires at the start of each new web request
- /// </summary>
- /// <param name="sender">The Sender</param>
- /// <param name="e">Event Arguments</param>
- public void Context_BeginRequest(object sender, EventArgs e)
- {
- HttpApplication app = sender as HttpApplication;
- HttpContext ctx = app.Context;
- // Currently only enable for the Uploader API webservice
- //if (!ctx.Request.Url.PathAndQuery.ToLower().Contains("uploaderapi.asmx"))
- //{
- // return;
- //}
- // Add request filter if request was GZIP encoded
- string requestEncoding = ctx.Request.Headers["Content-encoding"];
- if (requestEncoding != null && requestEncoding == "gzip")
- {
- app.Request.Filter =
- new System.IO.Compression.GZipStream(app.Request.Filter, CompressionMode.Decompress);
- }
- // Add response compression filter if the client accepts compressed responses
- if (IsEncodingAccepted("gzip"))
- {
- app.Response.Filter = new GZipStream(app.Response.Filter, CompressionMode.Compress);
- SetEncoding("gzip");
- }
- else if (IsEncodingAccepted("deflate"))
- {
- app.Response.Filter = new DeflateStream(app.Response.Filter, CompressionMode.Compress);
- SetEncoding("deflate");
- }
- }
- private bool IsEncodingAccepted(string encoding)
- {
- return HttpContext.Current.Request.Headers["Accept-encoding"] != null &&
- HttpContext.Current.Request.Headers["Accept-encoding"].Contains(encoding);
- }
- private void SetEncoding(string encoding)
- {
- HttpContext ctx = HttpContext.Current;
- string responseEncodings = ctx.Response.Headers.Get("Content-encoding");
- if (responseEncodings == null || !responseEncodings.Contains(encoding))
- HttpContext.Current.Response.AppendHeader("Content-encoding", encoding);
- }
- public void Dispose()
- {
- Dispose(true);
- }
- private void Dispose(bool dispose)
- {
- isDisposed = dispose;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement