1234567891011121314151617181920212223242526272829303132333435 |
- using System;
- using System.Collections.Generic;
- using System.IO.Compression;
- using System.Linq;
- using System.Web;
-
- namespace Teknik.Modules
- {
- public class CompressionModule : IHttpModule
- {
- public void Dispose()
- {
- }
-
- public void Init(HttpApplication context)
- {
- context.BeginRequest += delegate (object sender, EventArgs args)
- {
- HttpContext requestContext = ((HttpApplication)sender).Context;
-
- var encodingsAccepted = requestContext.Request.Headers["Accept-Encoding"];
- if (string.IsNullOrEmpty(encodingsAccepted)) return;
-
- encodingsAccepted = encodingsAccepted.ToLowerInvariant();
- var response = requestContext.Response;
-
- if (encodingsAccepted.Contains("gzip"))
- {
- response.AppendHeader("Content-Encoding", "gzip");
- response.Filter = new GZipStream(response.Filter, CompressionMode.Compress);
- }
- };
- }
- }
- }
|