<commit_before>﻿namespace Easy.Logger.Tests.Integration
{
    using System;
    using System.IO;
    using System.Net;
    using System.Text;
    using System.Text.RegularExpressions;
    using Easy.Logger.Tests.Integration.Models;
    using System.Threading;
    using Newtonsoft.Json;
    using Newtonsoft.Json.Serialization;

    internal sealed class EasyLogListener : IDisposable
    {
        private static readonly byte[] InvalidRequestMethodMessage = Encoding.UTF8.GetBytes("You can only POST a valid JSON payload to this server.");
        private static readonly byte[] InvalidPayloadMessage = Encoding.UTF8.GetBytes("Invalid payload. Payload should be a valid JSON.");

        private static readonly Regex JSONMediaTypeRegex =
            new Regex(@"([aA][pP][pP][lL][iI][cC][aA][tT][iI][oO][nN]/[jJ][sS][oO][nN]|[tT][eE][xX][tT]/[jJ][sS][oO][nN]|[aA][pP][pP][lL][iI][cC][aA][tT][iI][oO][nN]/[vV][nN][dD]\.\S*\+[jJ][sS][oO][nN])", RegexOptions.Compiled);

        private readonly HttpListener _listener;
        private readonly JsonSerializer _serializer;
        private int _isDisposing;

        /// <summary>
        /// Invoked when a new <see cref="LogPayload"/> is <c>POST</c>ed.
        /// </summary>
        public EventHandler<LogPayload> OnPayload;
        
        /// <summary>
        /// Invoked when there is an error during the deserialization of <see cref="LogPayload"/>.
        /// </summary>
        public EventHandler<Exception> OnError;

        /// <summary>
        /// Creates an instance of <see cref="EasyLogListener"/>.
        /// </summary>
        /// <param name="prefix">The prefix the listener will be listening on.</param>
        public EasyLogListener(Uri prefix)
        {
            _listener = new HttpListener();
            _listener.Prefixes.Add(prefix.AbsoluteUri);

            _serializer = new JsonSerializer
            {
                ContractResolver = new CamelCasePropertyNamesContractResolver(),
                DateFormatHandling = DateFormatHandling.IsoDateFormat,
                DateTimeZoneHandling = DateTimeZoneHandling.Utc,
                Formatting = Formatting.None
            };
        }

        /// <summary>
        /// Starts listening for payloads.
        /// </summary>
        public void Start() => StartListening();

        /// <summary>
        /// Stops and releases resources used by the instance.<commit_msg>Minor bug fix<commit_after>﻿namespace Easy.Logger.Tests.Integration
{
    using System;
    using System.IO;
    using System.Net;
    using System.Text;
    using System.Text.RegularExpressions;
    using Easy.Logger.Tests.Integration.Models;
    using System.Threading;
    using Newtonsoft.Json;
    using Newtonsoft.Json.Serialization;

    /// <summary>
    /// A simple <c>HTTP</c> listener for receiving <see cref="LogPayload"/>s.
    /// </summary>
    public sealed class EasyLogListener : IDisposable
    {
        private static readonly byte[] InvalidRequestMethodMessage = Encoding.UTF8.GetBytes("You can only POST a valid JSON payload to this server.");
        private static readonly byte[] InvalidPayloadMessage = Encoding.UTF8.GetBytes("Invalid payload. Payload should be a valid JSON.");

        private static readonly Regex JSONMediaTypeRegex =
            new Regex(@"([aA][pP][pP][lL][iI][cC][aA][tT][iI][oO][nN]/[jJ][sS][oO][nN]|[tT][eE][xX][tT]/[jJ][sS][oO][nN]|[aA][pP][pP][lL][iI][cC][aA][tT][iI][oO][nN]/[vV][nN][dD]\.\S*\+[jJ][sS][oO][nN])", RegexOptions.Compiled);

        private readonly HttpListener _listener;
        private readonly JsonSerializer _serializer;
        private int _isDisposing;

        /// <summary>
        /// Invoked when a new <see cref="LogPayload"/> is <c>POST</c>ed.
        /// </summary>
        public event EventHandler<LogPayload> OnPayload;
        
        /// <summary>
        /// Invoked when there is an error during the deserialization of <see cref="LogPayload"/>.
        /// </summary>
        public event EventHandler<Exception> OnError;

        /// <summary>
        /// Creates an instance of <see cref="EasyLogListener"/>.
        /// </summary>
        /// <param name="prefix">The prefix the listener will be listening on.</param>
        public EasyLogListener(Uri prefix)
        {
            _listener = new HttpListener();
            _listener.Prefixes.Add(prefix.AbsoluteUri);

            _serializer = new JsonSerializer
            {
                ContractResolver = new CamelCasePropertyNamesContractResolver(),
                DateFormatHandling = DateFormatHandling.IsoDateFormat,
                DateTimeZoneHandling = DateTimeZoneHandling.Utc,
                Formatting = Formatting.None
            };
        }

        /// <summary>
        /// Starts listening for payloads.
        /// </summary>
        public void Start() => StartListening();

        /// <summary>
        /// Stops and releases resources used by the instance.
{'fraction_non_alphanumeric': 0.13004846526655897, 'fraction_numerical': 0.0008077544426494346, 'mean_word_length': 2.4371963913948647, 'pattern_counts': {'":': 0, '<': 44, '<?xml version=': 0, '>': 46, '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': '3010646', 'n_tokens_mistral': 1421, 'n_tokens_neox': 1390, 'n_words': 329}