<commit_before>﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Internal;
using Porthor.Models;
using Porthor.ResourceRequestValidators.ContentValidators;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;

namespace Porthor.ResourceRequestValidators
{
    /// <summary>
    /// Request validator for content.
    /// </summary>
    public class ContentDefinitionValidator : IResourceRequestValidator
    {
        private readonly IDictionary<string, ContentValidatorBase> _validators = new Dictionary<string, ContentValidatorBase>();

        /// <summary>
        /// Constructs a new instance of <see cref="ContentDefinitionValidator"/>.
        /// </summary>
        /// <param name="contentDefinitions">Collection of <see cref="ContentDefinition"/>.</param>
        /// <param name="options">Content configuration options.</param>
        public ContentDefinitionValidator(
            IEnumerable<ContentDefinition> contentDefinitions,
            ContentOptions options)
        {
            foreach (var definition in contentDefinitions)
            {
                if (string.IsNullOrEmpty(definition.Template))
                {
                    _validators.Add(definition.MediaType, null);
                }
                else
                {
                    var validator = options.Get(definition.MediaType, definition.Template);
                    if (validator == null)
                    {
                        throw new NotSupportedException(definition.MediaType);
                    }
                    _validators.Add(definition.MediaType, validator);
                }
            }
        }

        /// <summary>
        /// Validates the content of the current <see cref="HttpContext"/>.
        /// </summary>
        /// <param name="context">Current context.</param>
        /// <returns>
        /// The <see cref="Task{HttpRequestMessage}"/> that represents the asynchronous query string validation process.
        /// Returns null if the content is valid.
        /// </returns>
        public async Task<HttpResponseMessage> ValidateAsync(HttpContext context)
        {
            try
            {
                var validator = _validators.Single(v => context.Request.ContentType.Contains(v.Key)).Value;
                if (validator == null)
                {
                    return null;
                }

                context.Request.EnableRewind();
                return await validator.ValidateAsync(context);
            }
            catch
            {
                return new HttpResponseMessage(HttpStatusCode.UnsupportedMediaType);
            }
        }
    }
}<commit_msg>Remove try catch from content definition validator<commit_after>﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Internal;
using Porthor.Models;
using Porthor.ResourceRequestValidators.ContentValidators;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;

namespace Porthor.ResourceRequestValidators
{
    /// <summary>
    /// Request validator for content.
    /// </summary>
    public class ContentDefinitionValidator : IResourceRequestValidator
    {
        private readonly IDictionary<string, ContentValidatorBase> _validators = new Dictionary<string, ContentValidatorBase>();

        /// <summary>
        /// Constructs a new instance of <see cref="ContentDefinitionValidator"/>.
        /// </summary>
        /// <param name="contentDefinitions">Collection of <see cref="ContentDefinition"/>.</param>
        /// <param name="options">Content configuration options.</param>
        public ContentDefinitionValidator(
            IEnumerable<ContentDefinition> contentDefinitions,
            ContentOptions options)
        {
            foreach (var definition in contentDefinitions)
            {
                if (string.IsNullOrEmpty(definition.Template))
                {
                    _validators.Add(definition.MediaType, null);
                }
                else
                {
                    var validator = options.Get(definition.MediaType, definition.Template);
                    if (validator == null)
                    {
                        throw new NotSupportedException(definition.MediaType);
                    }
                    _validators.Add(definition.MediaType, validator);
                }
            }
        }

        /// <summary>
        /// Validates the content of the current <see cref="HttpContext"/>.
        /// </summary>
        /// <param name="context">Current context.</param>
        /// <returns>
        /// The <see cref="Task{HttpRequestMessage}"/> that represents the asynchronous query string validation process.
        /// Returns null if the content is valid.
        /// </returns>
        public async Task<HttpResponseMessage> ValidateAsync(HttpContext context)
        {
            var mediaTypeValidator = _validators.SingleOrDefault(v => context.Request.ContentType.Contains(v.Key));
            if (mediaTypeValidator.Key == null)
            {
                return new HttpResponseMessage(HttpStatusCode.UnsupportedMediaType);
            }

            var validator = mediaTypeValidator.Value;
            if (validator == null)
            {
                return null;
            }

            context.Request.EnableRewind();
            return await validator.ValidateAsync(context);
        }
    }
}
{'fraction_non_alphanumeric': 0.09652578796561605, 'fraction_numerical': 0.0, 'mean_word_length': 1.9012987012987013, 'pattern_counts': {'":': 0, '<': 47, '<?xml version=': 0, '>': 49, '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': '6272658', 'n_tokens_mistral': 1288, 'n_tokens_neox': 1257, 'n_words': 294}