<commit_before>﻿using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Peachpie.Web
{
    public static class RequestDelegateExtension
    {
        /// <summary>
        /// Installs PHP request handler within the application.
        /// </summary>
        public static IApplicationBuilder UsePhp(this IApplicationBuilder builder /* , TODO: options, allowed extensions, directory */)
        {
            return builder.Use(new Func<RequestDelegate, RequestDelegate>(next => async context =>
            {
                var script = RequestContextCore.ResolveScript(context.Request);
                if (script.IsValid)
                {
                    await Task.Run(() =>
                    {
                        using (var phpctx = new RequestContextCore(context))
                        {
                            script.MainMethod(phpctx, phpctx.Globals, null);
                        }
                    });
                }
                else
                {
                    await next(context);
                }
            }));
        }
    }
}<commit_msg>ASP.NET Core extension handles "die" and "exit"<commit_after>﻿using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Peachpie.Web
{
    public static class RequestDelegateExtension
    {
        /// <summary>
        /// Installs PHP request handler within the application.
        /// </summary>
        public static IApplicationBuilder UsePhp(this IApplicationBuilder builder /* , TODO: options, allowed extensions, directory */)
        {
            return builder.Use(new Func<RequestDelegate, RequestDelegate>(next => async context =>
            {
                var script = RequestContextCore.ResolveScript(context.Request);
                if (script.IsValid)
                {
                    await Task.Run(() =>
                    {
                        using (var phpctx = new RequestContextCore(context))
                        {
                            // TODO: move following into a ProcessScript() routine

                            try
                            {
                                script.MainMethod(phpctx, phpctx.Globals, null);
                            }
                            catch (Pchp.Core.ScriptDiedException died)
                            {
                                died.ProcessStatus(phpctx);
                            }
                        }
                    });
                }
                else
                {
                    await next(context);
                }
            }));
        }
    }
}
{'fraction_non_alphanumeric': 0.0821917808219178, 'fraction_numerical': 0.0, 'mean_word_length': 1.230227094753328, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 15, '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': '6032517', 'n_tokens_mistral': 639, 'n_tokens_neox': 621, 'n_words': 142}