diff --git a/src/CompressR.Sample/Controllers/ValuesController.cs b/src/CompressR.Sample/Controllers/ValuesController.cs index 3048bda..b5a893f 100644 --- a/src/CompressR.Sample/Controllers/ValuesController.cs +++ b/src/CompressR.Sample/Controllers/ValuesController.cs @@ -4,6 +4,7 @@ using System.Collections.Generic; using System.Linq; using System.Net; using System.Net.Http; +using System.Threading.Tasks; using System.Web.Http; namespace CompressR.Sample.Controllers @@ -23,19 +24,29 @@ namespace CompressR.Sample.Controllers return "value"; } - // POST api/values - public void Post([FromBody]string value) + [Compress] + [HttpGet, Route("TestJsonSerialization")] + public async Task TestJsonSerialization() { + return Ok(new + { + A = 1, + B = new string[] { "1", "A", "B" } + + }); } - // PUT api/values/5 - public void Put(int id, [FromBody]string value) + [Compress(requireCompression: true)] + [HttpGet, Route("TestJsonSerialization2")] + public async Task TestJsonSerialization2() { + return Request.CreateResponse(new + { + A = 1, + B = new string[] { "1", "A", "B" } + + }); } - // DELETE api/values/5 - public void Delete(int id) - { - } } } \ No newline at end of file diff --git a/src/CompressR.WebApi/CompressAttribute.cs b/src/CompressR.WebApi/CompressAttribute.cs index e996064..120cd7d 100644 --- a/src/CompressR.WebApi/CompressAttribute.cs +++ b/src/CompressR.WebApi/CompressAttribute.cs @@ -23,6 +23,12 @@ namespace CompressR.WebApi } public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext) + { + OnActionExecutedAsync(actionExecutedContext, CancellationToken.None).Wait(); + + } + + public override async Task OnActionExecutedAsync(HttpActionExecutedContext actionExecutedContext, CancellationToken cancellationToken) { if(actionExecutedContext.Response.Content == null) { @@ -47,31 +53,9 @@ namespace CompressR.WebApi { return; } - - } - actionExecutedContext.Response.Content = new CompressedContent(actionExecutedContext.Response.Content, acceptedEncoding); - } - public override async Task OnActionExecutedAsync(HttpActionExecutedContext actionExecutedContext, CancellationToken cancellationToken) - { - if(actionExecutedContext.Response.Content == null) - { - return; } - var acceptedEncoding = actionExecutedContext - .Response - .RequestMessage - .Headers - .AcceptEncoding - .Select(a => a.Value) - .Intersect(Constants.Compressors, StringComparer.OrdinalIgnoreCase) - .FirstOrDefault(); - if (string.IsNullOrWhiteSpace(acceptedEncoding)) - { - return; - } - actionExecutedContext.Response.Content = new CompressedContent(actionExecutedContext.Response.Content, acceptedEncoding); } } diff --git a/src/CompressR.WebApi/DeflateAttribute.cs b/src/CompressR.WebApi/DeflateAttribute.cs index 87274c9..e90fc4b 100644 --- a/src/CompressR.WebApi/DeflateAttribute.cs +++ b/src/CompressR.WebApi/DeflateAttribute.cs @@ -1,4 +1,5 @@ -using System; +using CompressR.Exceptions; +using System; using System.Collections.Generic; using System.IO.Compression; using System.Linq; @@ -23,23 +24,7 @@ namespace CompressR.WebApi public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext) { - if(actionExecutedContext.Response.Content == null) - { - return; - } - var acceptedEncoding = actionExecutedContext - .Response - .RequestMessage - .Headers - .AcceptEncoding - .Select(a => a.Value) - .Any(a => a.Equals(Constants.Deflate, StringComparison.OrdinalIgnoreCase)); - - if (!acceptedEncoding) - { - return; - } - actionExecutedContext.Response.Content = new CompressedContent(actionExecutedContext.Response.Content, Constants.Deflate); + OnActionExecutedAsync(actionExecutedContext, CancellationToken.None).Wait(); } public override async Task OnActionExecutedAsync(HttpActionExecutedContext actionExecutedContext, CancellationToken cancellationToken) @@ -56,10 +41,16 @@ namespace CompressR.WebApi .Select(a => a.Value) .Any(a => a.Equals(Constants.Deflate, StringComparison.OrdinalIgnoreCase)); + if (!acceptedEncoding && RequireCompression) + { + throw new CompressRException("Compression required but client did not send accept header"); + } + if (!acceptedEncoding) { return; } + actionExecutedContext.Response.Content = new CompressedContent(actionExecutedContext.Response.Content, Constants.Deflate); } } diff --git a/src/CompressR.WebApi/GzipAttribute.cs b/src/CompressR.WebApi/GzipAttribute.cs index deec613..f263f50 100644 --- a/src/CompressR.WebApi/GzipAttribute.cs +++ b/src/CompressR.WebApi/GzipAttribute.cs @@ -1,4 +1,5 @@ -using System; +using CompressR.Exceptions; +using System; using System.IO.Compression; using System.Linq; using System.Net.Http; @@ -22,23 +23,7 @@ namespace CompressR.WebApi public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext) { - if(actionExecutedContext.Response.Content == null) - { - return; - } - var acceptedEncoding = actionExecutedContext - .Response - .RequestMessage - .Headers - .AcceptEncoding - .Select(a => a.Value) - .Any(a => a.Equals(Constants.Gzip, StringComparison.OrdinalIgnoreCase)); - - if (!acceptedEncoding) - { - return; - } - actionExecutedContext.Response.Content = new CompressedContent(actionExecutedContext.Response.Content, Constants.Gzip); + OnActionExecutedAsync(actionExecutedContext, CancellationToken.None).Wait(); } public override async Task OnActionExecutedAsync(HttpActionExecutedContext actionExecutedContext, CancellationToken cancellationToken) @@ -55,10 +40,17 @@ namespace CompressR.WebApi .Select(a => a.Value) .Any(a => a.Equals(Constants.Gzip, StringComparison.OrdinalIgnoreCase)); + + if (!acceptedEncoding && RequireCompression) + { + throw new CompressRException("Compression required but client did not send accept header"); + } + if (!acceptedEncoding) { return; } + actionExecutedContext.Response.Content = new CompressedContent(actionExecutedContext.Response.Content, Constants.Gzip); } }