<commit_before>﻿using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;

namespace Exceptionless.Api.Utility {
    public class EncodingDelegatingHandler : DelegatingHandler {
        protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) {
            if (request.Method != HttpMethod.Get && request.Content != null
                && !(request.RequestUri.AbsolutePath.EndsWith("/events") && request.Method == HttpMethod.Post)
                && !(request.RequestUri.AbsolutePath.EndsWith("/error") && request.Method == HttpMethod.Post)
                && request.Content.Headers.ContentEncoding.Any()) {
                string encodingType = request.Content.Headers.ContentEncoding.First().ToLowerInvariant();
                if (encodingType == "gzip" || encodingType == "deflate")
                    request.Content = new CompressedContent(request.Content, encodingType);
            }

            return base.SendAsync(request, cancellationToken).ContinueWith(responseToCompleteTask => {
                HttpResponseMessage response = responseToCompleteTask.Result;

                if (response.RequestMessage != null
                    && response.RequestMessage.Headers != null
                    && response.RequestMessage.Headers.AcceptEncoding != null
                    && response.RequestMessage.Headers.AcceptEncoding.Count > 0) {
                    string encodingType = response.RequestMessage.Headers.AcceptEncoding.First().Value;

                    if (response.Content != null)
                        response.Content = new CompressedContent(response.Content, encodingType);
                }

                return response;
            }, TaskContinuationOptions.OnlyOnRanToCompletion);
        }
    }

    public class CompressedContent : HttpContent {
        private readonly HttpContent _originalContent;
        private readonly string _encodingType;

        public CompressedContent(HttpContent content, string encodingType) {
            if (content == null)
                throw new ArgumentNullException("content");

            if (encodingType == null)
                throw new ArgumentNullException("encodingType");

            _originalContent = content;
            _encodingType = encodingType.ToLowerInvariant();
<commit_msg>Fixed an issue where an invalid accept encoding would throw an exception.<commit_after>﻿using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;

namespace Exceptionless.Api.Utility {
    public class EncodingDelegatingHandler : DelegatingHandler {
        protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) {
            if (request.Method != HttpMethod.Get && request.Content != null
                && !(request.RequestUri.AbsolutePath.EndsWith("/events") && request.Method == HttpMethod.Post)
                && !(request.RequestUri.AbsolutePath.EndsWith("/error") && request.Method == HttpMethod.Post)
                && request.Content.Headers.ContentEncoding.Any()) {
                string encodingType = request.Content.Headers.ContentEncoding.First().ToLowerInvariant();
                if (encodingType == "gzip" || encodingType == "deflate")
                    request.Content = new CompressedContent(request.Content, encodingType);
            }

            return base.SendAsync(request, cancellationToken).ContinueWith(responseToCompleteTask => {
                HttpResponseMessage response = responseToCompleteTask.Result;

                if (response.RequestMessage != null
                    && response.RequestMessage.Headers != null
                    && response.RequestMessage.Headers.AcceptEncoding != null
                    && response.RequestMessage.Headers.AcceptEncoding.Count > 0) {
                    string encodingType = response.RequestMessage.Headers.AcceptEncoding.First().Value;

                    if (response.Content != null && (encodingType == "gzip" || encodingType == "deflate"))
                        response.Content = new CompressedContent(response.Content, encodingType);
                }

                return response;
            }, TaskContinuationOptions.OnlyOnRanToCompletion);
        }
    }

    public class CompressedContent : HttpContent {
        private readonly HttpContent _originalContent;
        private readonly string _encodingType;

        public CompressedContent(HttpContent content, string encodingType) {
            if (content == null)
                throw new ArgumentNullException("content");

            if (encodingType == null)
                throw new ArgumentNullException("encodingType");

            _originalContent = content;
            _encodingType = encodingType.ToLowerInvariant();

{'fraction_non_alphanumeric': 0.09147894221351616, 'fraction_numerical': 0.0003917727717923604, 'mean_word_length': 2.5831578947368423, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, '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': '6978887', 'n_tokens_mistral': 1169, 'n_tokens_neox': 1132, 'n_words': 247}