<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using Dolstagis.Web.Auth;
using Dolstagis.Web.Features.Impl;
using Dolstagis.Web.Http;
using Dolstagis.Web.Lifecycle.ResultProcessors;
using Dolstagis.Web.Routes;
using Dolstagis.Web.Sessions;

namespace Dolstagis.Web.Lifecycle
{
    public class RequestProcessor
    {
        private IList<IResultProcessor> _resultProcessors;
        private IEnumerable<IExceptionHandler> _exceptionHandlers;
        private ISessionStore _sessionStore;
        private IAuthenticator _authenticator;
        private FeatureSet _features;
        private IIoCContainer _featureSetContainer;

        private bool _requestContainerIsChecked = false;
        private bool _requestContainerIsValid = false;

        public RequestProcessor(
            IEnumerable<IResultProcessor> resultProcessors,
            IEnumerable<IExceptionHandler> exceptionHandlers,
            ISessionStore sessionStore,
            IAuthenticator authenticator,
            FeatureSet features,
            IIoCContainer featureSetContainer
        )
        {
            _resultProcessors = (resultProcessors ?? Enumerable.Empty<IResultProcessor>()).ToList();
            _exceptionHandlers = exceptionHandlers;
            _sessionStore = sessionStore;
            _authenticator = authenticator;
            _features = features;
            _featureSetContainer = featureSetContainer;
        }

        public async Task ProcessRequestAsync(IRequest request, IResponse response)
        {
            using (var childContainer = _featureSetContainer.GetChildContainer()) {
                childContainer.Use<IRequest>(request);
                childContainer.Use<IResponse>(response);

                foreach (var feature in _features.Features) {
                    feature.ContainerBuilder.SetupRequest(childContainer);
                }

                lock (_featureSetContainer) {
                    // Only validate the request container once.
                    // But throw every time if it fails.
                    if (_requestContainerIsChecked) {
                        if (!_requestContainerIsValid) {
                            throw new InvalidOperationException(
                                "The container configuration for requests for this feature set is invalid. "
                                + "Please refer to previous errors."
                            );
                        }
                    }
                    else {
                        try {
                            childContainer.Validate();
                            _requestContainerIsValid = true;
                        }
                        finally {
                            _requestContainerIsChecked = true;
                        }
                    }
                }

                var context = CreateContext(request, response, childContainer);
                await ProcessRequest(context);
            }
        }


        public async Task ProcessRequest(RequestContext context)
        {
            try {
                object result;
                IResultProcessor processor;

                try {
                    result = await context.InvokeRequest();
                }
                finally {
                    await context.PersistSession();
                }

                if (context.Request.Method.Equals("head", StringComparison.OrdinalIgnoreCase)) {
                    processor = HeadResultProcessor.Instance;
                }
                else {
                    processor = _resultProcessors.LastOrDefault(x => x.CanProcess(result));
                }
                if (processor == null) Status.NotFound.Throw();
                await processor.Process(result, context);
            }
            catch (Exception ex) {<commit_msg>Rename ProcessRequest -> ProcessRequestContextAsync<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using Dolstagis.Web.Auth;
using Dolstagis.Web.Features.Impl;
using Dolstagis.Web.Http;
using Dolstagis.Web.Lifecycle.ResultProcessors;
using Dolstagis.Web.Routes;
using Dolstagis.Web.Sessions;

namespace Dolstagis.Web.Lifecycle
{
    public class RequestProcessor
    {
        private IList<IResultProcessor> _resultProcessors;
        private IEnumerable<IExceptionHandler> _exceptionHandlers;
        private ISessionStore _sessionStore;
        private IAuthenticator _authenticator;
        private FeatureSet _features;
        private IIoCContainer _featureSetContainer;

        private bool _requestContainerIsChecked = false;
        private bool _requestContainerIsValid = false;

        public RequestProcessor(
            IEnumerable<IResultProcessor> resultProcessors,
            IEnumerable<IExceptionHandler> exceptionHandlers,
            ISessionStore sessionStore,
            IAuthenticator authenticator,
            FeatureSet features,
            IIoCContainer featureSetContainer
        )
        {
            _resultProcessors = (resultProcessors ?? Enumerable.Empty<IResultProcessor>()).ToList();
            _exceptionHandlers = exceptionHandlers;
            _sessionStore = sessionStore;
            _authenticator = authenticator;
            _features = features;
            _featureSetContainer = featureSetContainer;
        }

        public async Task ProcessRequestAsync(IRequest request, IResponse response)
        {
            using (var childContainer = _featureSetContainer.GetChildContainer()) {
                childContainer.Use<IRequest>(request);
                childContainer.Use<IResponse>(response);

                foreach (var feature in _features.Features) {
                    feature.ContainerBuilder.SetupRequest(childContainer);
                }

                lock (_featureSetContainer) {
                    // Only validate the request container once.
                    // But throw every time if it fails.
                    if (_requestContainerIsChecked) {
                        if (!_requestContainerIsValid) {
                            throw new InvalidOperationException(
                                "The container configuration for requests for this feature set is invalid. "
                                + "Please refer to previous errors."
                            );
                        }
                    }
                    else {
                        try {
                            childContainer.Validate();
                            _requestContainerIsValid = true;
                        }
                        finally {
                            _requestContainerIsChecked = true;
                        }
                    }
                }

                await ProcessRequestContextAsync(context);
            }
        }


        public async Task ProcessRequestContextAsync(RequestContext context)
        {
            try {
                object result;
                IResultProcessor processor;

                try {
                    result = await context.InvokeRequest();
                }
                finally {
                    await context.PersistSession();
                }

                if (context.Request.Method.Equals("head", StringComparison.OrdinalIgnoreCase)) {
                    processor = HeadResultProcessor.Instance;
                }
                else {
                    processor = _resultProcessors.LastOrDefault(x => x.CanProcess(result));
                }
                if (processor == null) Status.NotFound.Throw();
                await processor.Process(result, context);
            }
            catch (Exception ex) {
{'fraction_non_alphanumeric': 0.06124011227353917, 'fraction_numerical': 0.0, 'mean_word_length': 1.5542521994134897, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 20, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6606143', 'n_tokens_mistral': 1731, 'n_tokens_neox': 1647, 'n_words': 373}