<commit_before>﻿using Chromium;
using Neutronium.Core;
using System;
using System.IO;
using System.Windows.Resources;

namespace Neutronium.WebBrowserEngine.ChromiumFx.WPF
{
    public class PackUriResourceHandler : CfxResourceHandler
    {
        public PackUriResourceHandler(Uri packUri, IWebSessionLogger logger)
        {
            var resInfo = default(StreamResourceInfo);
            try
            {
                resInfo = System.Windows.Application.GetResourceStream(packUri);
            }
            catch (Exception exception)
            {
                logger?.Error(() => $"Unable to find pack ressource:{packUri} exception:{exception}");
            }

            GetResponseHeaders += (_, responeHeader) =>
            {
                var response = responeHeader.Response;
                if (resInfo == null)
                {
                    response.Status = 404;
                    response.StatusText = "Not Found";
                    return;
                }

                responeHeader.ResponseLength = resInfo.Stream.Length;         
                response.MimeType = GetMineType(packUri);
                response.Status = 200;
                response.StatusText = "OK";
            };

            ReadResponse += (_, readResponse) =>
            {
                if (resInfo == null)
                {
                    readResponse.SetReturnValue(false);
                    return;
                }

                var buffer = new byte[readResponse.BytesToRead];
                var bytesRead = resInfo.Stream.Read(buffer, 0, readResponse.BytesToRead);
                System.Runtime.InteropServices.Marshal.Copy(buffer, 0, readResponse.DataOut, bytesRead);
                readResponse.BytesRead = bytesRead;
                readResponse.SetReturnValue(true);
            };

            ProcessRequest += (_, processRequest) =>
            {
                processRequest.Callback.Continue();
                processRequest.SetReturnValue(true);
            };
        }

        private static string GetMineType(Uri uri)
        {
            var extension = Path.GetExtension(uri.AbsoluteUri).Replace(".", "");
            return CfxRuntime.GetMimeType(extension);
        }
    }
}<commit_msg>WIP: refactor PackUriResourceHandler<commit_after>﻿using Chromium;
using Chromium.Event;
using Neutronium.Core;
using System;
using System.IO;
using System.Windows.Resources;

namespace Neutronium.WebBrowserEngine.ChromiumFx.WPF
{
    public class PackUriResourceHandler : CfxResourceHandler
    {
        private readonly Uri _Uri;
        private readonly StreamResourceInfo _StreamResourceInfo;

        public PackUriResourceHandler(Uri packUri, IWebSessionLogger logger)
        {
            _Uri = packUri;
            try
            {
                _StreamResourceInfo = System.Windows.Application.GetResourceStream(packUri);
            }
            catch (Exception exception)
            {
                logger?.Error(() => $"Unable to find pack ressource:{packUri} exception:{exception}");
            }

            GetResponseHeaders += PackUriResourceHandler_GetResponseHeaders;
            ReadResponse += PackUriResourceHandler_ReadResponse;
            ProcessRequest += PackUriResourceHandler_ProcessRequest;
            CanGetCookie += PackUriResourceHandler_CanGetCookie;
            CanSetCookie += PackUriResourceHandler_CanSetCookie;
        }

        private void PackUriResourceHandler_GetResponseHeaders(object sender, CfxGetResponseHeadersEventArgs responeHeader)
        {
            var response = responeHeader.Response;
            if (_StreamResourceInfo == null)
            {
                response.Status = 404;
                response.StatusText = "Not Found";
                return;
            }

            responeHeader.ResponseLength = _StreamResourceInfo.Stream.Length;
            response.MimeType = GetMineType();
            response.Status = 200;
            response.StatusText = "OK";
        }

        private void PackUriResourceHandler_ReadResponse(object sender, CfxReadResponseEventArgs readResponse)
        {
            if (_StreamResourceInfo == null)
            {
                readResponse.SetReturnValue(false);
                return;
            }

            var buffer = new byte[readResponse.BytesToRead];
            var bytesRead = _StreamResourceInfo.Stream.Read(buffer, 0, readResponse.BytesToRead);
            System.Runtime.InteropServices.Marshal.Copy(buffer, 0, readResponse.DataOut, bytesRead);
            readResponse.BytesRead = bytesRead;
            readResponse.SetReturnValue(bytesRead > 0);
        }

        private void PackUriResourceHandler_ProcessRequest(object sender, CfxProcessRequestEventArgs processRequest)
        {
            processRequest.Callback.Continue();
            processRequest.SetReturnValue(true);
        }
        private void PackUriResourceHandler_CanSetCookie(object sender, CfxResourceHandlerCanSetCookieEventArgs e)
        {
            e.SetReturnValue(true);
        }

        private void PackUriResourceHandler_CanGetCookie(object sender, CfxCanGetCookieEventArgs e)
        {
            e.SetReturnValue(true);
        }

        public override string ToString()
        {
            return _Uri.ToString();
        }

        private string GetMineType()
        {
            var extension = Path.GetExtension(_Uri.AbsoluteUri).Replace(".", "");
            return CfxRuntime.GetMimeType(extension);
        }
    }
}
{'fraction_non_alphanumeric': 0.07846682335924787, 'fraction_numerical': 0.0030735852467908154, 'mean_word_length': 1.906988964792433, 'pattern_counts': {'":': 0, '<': 3, '<?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': '7592736', 'n_tokens_mistral': 1370, 'n_tokens_neox': 1295, 'n_words': 244}