<commit_before>﻿using System.IO.Compression;
using System.Threading.Tasks;
using Cireson.HttpResponseCompression.Common;
using Microsoft.Owin;

namespace Cireson.HttpResponseCompression.Owin.Middleware
{
    public class ResponseCompressingMiddleware : OwinMiddleware
    {
        public ResponseCompressingMiddleware(OwinMiddleware next) : base(next)
        {
        }

        public override async Task Invoke(IOwinContext context)
        {
            var requestCompressionSupport = context.Request.DetectedCompressionSupport();
            switch (requestCompressionSupport)
            {
                case HttpCompression.GZip:
                    context.Response.Body = new GZipStream(context.Response.Body, CompressionLevel.Fastest);
                    context.Response.Headers.Append(HttpHeaders.ContentEncodingHttpHeaderKey,
                        HttpHeaders.GzipContentEncodingValue);
                    break;
                case HttpCompression.Deflate:
                    context.Response.Body = new DeflateStream(context.Response.Body, CompressionLevel.Fastest);
                    context.Response.Headers.Append(HttpHeaders.ContentEncodingHttpHeaderKey,
                        HttpHeaders.DeflateContentEncodingValue);
                    break;
                default:
                    await Next.Invoke(context);
                    break;
            }
        }
    }
}<commit_msg>updated take on the OWIN implementation.<commit_after>﻿using System.IO.Compression;
using System.Threading.Tasks;
using Cireson.HttpResponseCompression.Common;
using Microsoft.Owin;

namespace Cireson.HttpResponseCompression.Owin.Middleware
{
    public class ResponseCompressingMiddleware : OwinMiddleware
    {
        public ResponseCompressingMiddleware(OwinMiddleware next) : base(next)
        {
        }

        public override async Task Invoke(IOwinContext context)
        {
            if (context.Response.Body.Length >= 4096)
            {
                var requestCompressionSupport = context.Request.DetectedCompressionSupport();
                switch (requestCompressionSupport)
                {
                    case HttpCompression.GZip:
                        context.Response.Body = new GZipStream(context.Response.Body, CompressionLevel.Fastest);
                        context.Response.Headers.Append(HttpHeaders.ContentEncodingHttpHeaderKey,
                            HttpHeaders.GzipContentEncodingValue);
                        break;
                    case HttpCompression.Deflate:
                        context.Response.Body = new DeflateStream(context.Response.Body, CompressionLevel.Fastest);
                        context.Response.Headers.Append(HttpHeaders.ContentEncodingHttpHeaderKey,
                            HttpHeaders.DeflateContentEncodingValue);
                        break;
                }
            }

            await Next.Invoke(context);
        }
    }
}
{'fraction_non_alphanumeric': 0.06639566395663957, 'fraction_numerical': 0.0013550135501355014, 'mean_word_length': 1.9678391959798995, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3426918', 'n_tokens_mistral': 697, 'n_tokens_neox': 668, 'n_words': 108}