<commit_before>﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Reflection;
using System.Threading.Tasks;
using Redwood.Framework.Controls;
using Redwood.Framework.Parser;
using Redwood.Framework.ViewModel;
using Redwood.Framework.Runtime;
using Redwood.Framework.Runtime.Filters;
using Redwood.Framework.Security;
using Microsoft.Owin;

namespace Redwood.Framework.Hosting
{
    public class RedwoodPresenter : IRedwoodPresenter
    {
        public IRedwoodViewBuilder RedwoodViewBuilder { get; private set; }

        public IViewModelLoader ViewModelLoader { get; private set; }

        public IViewModelSerializer ViewModelSerializer { get; private set; }

        public IOutputRenderer OutputRenderer { get; private set; }

        public ICsrfProtector CsrfProtector { get; private set; }

        /// <summary>
        /// Initializes a new instance of the <see cref="RedwoodPresenter"/> class.
        /// </summary>
        public RedwoodPresenter(
            IRedwoodViewBuilder redwoodViewBuilder,
            IViewModelLoader viewModelLoader,
            IViewModelSerializer viewModelSerializer,
            IOutputRenderer outputRenderer,
            ICsrfProtector csrfProtector
        )
        {
            RedwoodViewBuilder = redwoodViewBuilder;
            ViewModelLoader = viewModelLoader;
            ViewModelSerializer = viewModelSerializer;
            OutputRenderer = outputRenderer;
            CsrfProtector = csrfProtector;
        }

        /// <summary>
        /// Processes the request.
        /// </summary>
        public async Task ProcessRequest(RedwoodRequestContext context)
        {
            try
            {
                await ProcessRequestCore(context);
            }
            catch (RedwoodInterruptRequestExecutionException)
            {
                // the response has already been generated, do nothing
            }
            catch (UnauthorizedAccessException ex)
            {
                context.OwinContext.Response.StatusCode = (int)HttpStatusCode.Unauthorized;
                await RedwoodErrorPageMiddleware.RenderErrorResponse(context.OwinContext, ex);
            }
        }

        public async Task ProcessRequestCore(RedwoodRequestContext context)
        {
            if (context.OwinContext.Request.Method != "GET" && context.OwinContext.Request.Method != "POST")
            {
                // unknown HTTP method
                context.OwinContext.Response.StatusCode = (int)HttpStatusCode.MethodNotAllowed;
                throw new RedwoodHttpException("Only GET and POST methods are supported!");<commit_msg>Await removed from Catch block. This is not yet possible to do.<commit_after>﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Reflection;
using System.Threading.Tasks;
using Redwood.Framework.Controls;
using Redwood.Framework.Parser;
using Redwood.Framework.ViewModel;
using Redwood.Framework.Runtime;
using Redwood.Framework.Runtime.Filters;
using Redwood.Framework.Security;
using Microsoft.Owin;

namespace Redwood.Framework.Hosting
{
    public class RedwoodPresenter : IRedwoodPresenter
    {
        public IRedwoodViewBuilder RedwoodViewBuilder { get; private set; }

        public IViewModelLoader ViewModelLoader { get; private set; }

        public IViewModelSerializer ViewModelSerializer { get; private set; }

        public IOutputRenderer OutputRenderer { get; private set; }

        public ICsrfProtector CsrfProtector { get; private set; }

        /// <summary>
        /// Initializes a new instance of the <see cref="RedwoodPresenter"/> class.
        /// </summary>
        public RedwoodPresenter(
            IRedwoodViewBuilder redwoodViewBuilder,
            IViewModelLoader viewModelLoader,
            IViewModelSerializer viewModelSerializer,
            IOutputRenderer outputRenderer,
            ICsrfProtector csrfProtector
        )
        {
            RedwoodViewBuilder = redwoodViewBuilder;
            ViewModelLoader = viewModelLoader;
            ViewModelSerializer = viewModelSerializer;
            OutputRenderer = outputRenderer;
            CsrfProtector = csrfProtector;
        }

        /// <summary>
        /// Processes the request.
        /// </summary>
        public async Task ProcessRequest(RedwoodRequestContext context)
        {
            bool failedAsUnauthorized = false;
            Exception exception = null;

            try
            {
                await ProcessRequestCore(context);
            }
            catch (RedwoodInterruptRequestExecutionException)
            {
                // the response has already been generated, do nothing
            }
            catch (UnauthorizedAccessException ex)
            {
                failedAsUnauthorized = true;
                exception = ex;
            }

            if (failedAsUnauthorized)
            {
                // unauthorized error
                context.OwinContext.Response.StatusCode = (int)HttpStatusCode.Unauthorized;
                await RedwoodErrorPageMiddleware.RenderErrorResponse(context.OwinContext, exception);
            }
        }

        public async Task ProcessRequestCore(RedwoodRequestContext context)
        {
            if (context.OwinContext.Request.Method != "GET" && context.OwinContext.Request.Method != "POST")
            {
                // unknown HTTP method
                context.OwinContext.Response.StatusCode = (int)HttpStatusCode.MethodNotAllowed;
                throw new RedwoodHttpException("Only GET and POST methods are supported!");
{'fraction_non_alphanumeric': 0.06995957110212692, 'fraction_numerical': 0.0, 'mean_word_length': 2.249571673329526, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, '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': '2552881', 'n_tokens_mistral': 1363, 'n_tokens_neox': 1336, 'n_words': 328}