Fixed require compression (#3)
This commit is contained in:
committed by
Tommy Parnell
parent
2a7036ff5a
commit
854e8ec626
@@ -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<IHttpActionResult> 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<HttpResponseMessage> TestJsonSerialization2()
|
||||
{
|
||||
return Request.CreateResponse(new
|
||||
{
|
||||
A = 1,
|
||||
B = new string[] { "1", "A", "B" }
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
// DELETE api/values/5
|
||||
public void Delete(int id)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user