Browse Source

Added Compression Module to handle GZip and Deflate

pull/111/head
Teknikode 5 years ago
parent
commit
32965400d6
  1. 40
      Teknik/Modules/CompressionModule.cs
  2. 1
      Teknik/Teknik.csproj
  3. 1
      Teknik/Web.config

40
Teknik/Modules/CompressionModule.cs

@ -0,0 +1,40 @@ @@ -0,0 +1,40 @@
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);
}
else if (encodingsAccepted.Contains("deflate"))
{
response.AppendHeader("Content-Encoding", "deflate");
response.Filter = new DeflateStream(response.Filter, CompressionMode.Compress);
}
};
}
}
}

1
Teknik/Teknik.csproj

@ -314,6 +314,7 @@ @@ -314,6 +314,7 @@
<Compile Include="Attributes\TeknikAuthorizeAttribute.cs" />
<Compile Include="Hubs\IRCClientHub.cs" />
<Compile Include="Modules\BlacklistModule.cs" />
<Compile Include="Modules\CompressionModule.cs" />
<Compile Include="Modules\CORSModule.cs" />
<Compile Include="Modules\CSPModule.cs" />
<Compile Include="Modules\UserAuthModule.cs" />

1
Teknik/Web.config

@ -59,6 +59,7 @@ @@ -59,6 +59,7 @@
<add name="BlacklistModule" type="Teknik.Modules.BlacklistModule, Teknik" />
<add name="CORSModule" type="Teknik.Modules.CORSModule, Teknik" />
<add name="CSPModule" type="Teknik.Modules.CSPModule, Teknik" />
<add name="CompressionModule" type="Teknik.Modules.CompressionModule, Teknik" />
<remove name="UrlRoutingModule-4.0" />
<add name="UrlRoutingModule-4.0" type="System.Web.Routing.UrlRoutingModule" preCondition="" />
</modules>

Loading…
Cancel
Save