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.

27 lines
1017 B

  1. 
  2. using DefenseWeb.Helper;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Net.Http;
  7. using System.Web;
  8. using System.Web.Http.Filters;
  9. namespace DefenseWeb.Attribute
  10. {
  11. public class DeflateCompressionAttribute : ActionFilterAttribute
  12. {
  13. public override void OnActionExecuted(HttpActionExecutedContext actContext)
  14. {
  15. var content = actContext.Response.Content;
  16. var bytes = content == null ? null : content.ReadAsByteArrayAsync().Result;
  17. var zlibbedContent = bytes == null ? new byte[0] :
  18. CompressionHelper.DeflateByte(bytes);
  19. actContext.Response.Content = new ByteArrayContent(zlibbedContent);
  20. actContext.Response.Content.Headers.Remove("Content-Type");
  21. actContext.Response.Content.Headers.Add("Content-encoding", "deflate");
  22. actContext.Response.Content.Headers.Add("Content-Type", "application/json");
  23. base.OnActionExecuted(actContext);
  24. }
  25. }
  26. }