I'm trying to implement API Gzip Compration OwinMiddleware component.
public class ApiResponseCompression : OwinMiddleware
{
private const string AcceptEncodingHeader = "Accept-Encoding";
private const string ContentEncodingHeader = "Content-Encoding";
private const string ContentTypeHeader = "Content-Type";
private const string GzipEncoding = "gzip";
private const string JsonContentType = "application/json";
private readonly OwinMiddleware _next;
public ApiResponseCompression(OwinMiddleware next)
: base(next)
{
_next = next;
}
public override async Task Invoke(IOwinContext context)
{
var request = context.Request;
if (request.Headers.ContainsKey(AcceptEncodingHeader) &&
request.Headers[AcceptEncodingHeader].Contains(GzipEncoding))
{
/* I need somehow to obtain response content from context then compress it (gzip) and put back*/
context.Response.Headers.Remove(ContentTypeHeader);
context.Response.Headers.Add(ContentEncodingHeader, new[] { GzipEncoding });
context.Response.Headers.Add(ContentTypeHeader, new[] { JsonContentType });
}
await _next.Invoke(context);
}
}
I feel that the answer's in there somewhere, but a really had little exprience with OwinMiddleware. I usually used to work with normal asp.net httpContext, and it has content property, but IOwinContext doesn't.
Q: How can i resolve my issue? what best practise with OwinMiddleware i should use?
Thank you in advance