Browse Source

Added Compression Module to handle GZip and Deflate

tags/3.0.0
Teknikode 1 year ago
parent
commit
32965400d6
3 changed files with 42 additions and 0 deletions
  1. 40
    0
      Teknik/Modules/CompressionModule.cs
  2. 1
    0
      Teknik/Teknik.csproj
  3. 1
    0
      Teknik/Web.config

+ 40
- 0
Teknik/Modules/CompressionModule.cs View File

@@ -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
- 0
Teknik/Teknik.csproj View File

@@ -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
- 0
Teknik/Web.config View File

@@ -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