The next generation of the Teknik Services. Written in ASP.NET. https://www.teknik.io/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CompressionModule.cs 1.0KB

1234567891011121314151617181920212223242526272829303132333435
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO.Compression;
  4. using System.Linq;
  5. using System.Web;
  6. namespace Teknik.Modules
  7. {
  8. public class CompressionModule : IHttpModule
  9. {
  10. public void Dispose()
  11. {
  12. }
  13. public void Init(HttpApplication context)
  14. {
  15. context.BeginRequest += delegate (object sender, EventArgs args)
  16. {
  17. HttpContext requestContext = ((HttpApplication)sender).Context;
  18. var encodingsAccepted = requestContext.Request.Headers["Accept-Encoding"];
  19. if (string.IsNullOrEmpty(encodingsAccepted)) return;
  20. encodingsAccepted = encodingsAccepted.ToLowerInvariant();
  21. var response = requestContext.Response;
  22. if (encodingsAccepted.Contains("gzip"))
  23. {
  24. response.AppendHeader("Content-Encoding", "gzip");
  25. response.Filter = new GZipStream(response.Filter, CompressionMode.Compress);
  26. }
  27. };
  28. }
  29. }
  30. }