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.Linq;
|
||||||
using System.Net;
|
using System.Net;
|
||||||
using System.Net.Http;
|
using System.Net.Http;
|
||||||
|
using System.Threading.Tasks;
|
||||||
using System.Web.Http;
|
using System.Web.Http;
|
||||||
|
|
||||||
namespace CompressR.Sample.Controllers
|
namespace CompressR.Sample.Controllers
|
||||||
@@ -23,19 +24,29 @@ namespace CompressR.Sample.Controllers
|
|||||||
return "value";
|
return "value";
|
||||||
}
|
}
|
||||||
|
|
||||||
// POST api/values
|
[Compress]
|
||||||
public void Post([FromBody]string value)
|
[HttpGet, Route("TestJsonSerialization")]
|
||||||
|
public async Task<IHttpActionResult> TestJsonSerialization()
|
||||||
{
|
{
|
||||||
|
return Ok(new
|
||||||
|
{
|
||||||
|
A = 1,
|
||||||
|
B = new string[] { "1", "A", "B" }
|
||||||
|
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// PUT api/values/5
|
[Compress(requireCompression: true)]
|
||||||
public void Put(int id, [FromBody]string value)
|
[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)
|
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)
|
if(actionExecutedContext.Response.Content == null)
|
||||||
{
|
{
|
||||||
@@ -49,28 +55,6 @@ namespace CompressR.WebApi
|
|||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
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);
|
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.Collections.Generic;
|
||||||
using System.IO.Compression;
|
using System.IO.Compression;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
@@ -23,23 +24,7 @@ namespace CompressR.WebApi
|
|||||||
|
|
||||||
public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
|
public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
|
||||||
{
|
{
|
||||||
if(actionExecutedContext.Response.Content == null)
|
OnActionExecutedAsync(actionExecutedContext, CancellationToken.None).Wait();
|
||||||
{
|
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public override async Task OnActionExecutedAsync(HttpActionExecutedContext actionExecutedContext, CancellationToken cancellationToken)
|
public override async Task OnActionExecutedAsync(HttpActionExecutedContext actionExecutedContext, CancellationToken cancellationToken)
|
||||||
@@ -56,10 +41,16 @@ namespace CompressR.WebApi
|
|||||||
.Select(a => a.Value)
|
.Select(a => a.Value)
|
||||||
.Any(a => a.Equals(Constants.Deflate, StringComparison.OrdinalIgnoreCase));
|
.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)
|
if (!acceptedEncoding)
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
actionExecutedContext.Response.Content = new CompressedContent(actionExecutedContext.Response.Content, Constants.Deflate);
|
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.IO.Compression;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Net.Http;
|
using System.Net.Http;
|
||||||
@@ -22,23 +23,7 @@ namespace CompressR.WebApi
|
|||||||
|
|
||||||
public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
|
public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
|
||||||
{
|
{
|
||||||
if(actionExecutedContext.Response.Content == null)
|
OnActionExecutedAsync(actionExecutedContext, CancellationToken.None).Wait();
|
||||||
{
|
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public override async Task OnActionExecutedAsync(HttpActionExecutedContext actionExecutedContext, CancellationToken cancellationToken)
|
public override async Task OnActionExecutedAsync(HttpActionExecutedContext actionExecutedContext, CancellationToken cancellationToken)
|
||||||
@@ -55,10 +40,17 @@ namespace CompressR.WebApi
|
|||||||
.Select(a => a.Value)
|
.Select(a => a.Value)
|
||||||
.Any(a => a.Equals(Constants.Gzip, StringComparison.OrdinalIgnoreCase));
|
.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)
|
if (!acceptedEncoding)
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
actionExecutedContext.Response.Content = new CompressedContent(actionExecutedContext.Response.Content, Constants.Gzip);
|
actionExecutedContext.Response.Content = new CompressedContent(actionExecutedContext.Response.Content, Constants.Gzip);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user