<commit_before>﻿using System;
using System.IO;
using Cuemon.IO;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Extensions;

namespace Cuemon.AspNetCore.Mvc.Filters.Diagnostics
{
    /// <summary>
    /// Provides detailed information about a given <seealso cref="HttpRequest"/>.
    /// </summary>
    public class HttpRequestEvidence
    {
        // TODO: REMEMBER TO TEST THIS THOROUGHLY ON ASPNET CORE 3
        internal HttpRequestEvidence(HttpRequest request, Func<Stream, string> bodyParser = null)
        {
            var hasMultipartContentType = request.GetMultipartBoundary().Length > 0;
            if (bodyParser == null) { bodyParser = body => hasMultipartContentType ? null : Decorator.Enclose(body).ToEncodedString(); }
            Location = request.GetDisplayUrl();
            Method = request.Method;
            Headers = request.Headers;
            Query = request.Query;
            if (request.HasFormContentType && !hasMultipartContentType) { Form = request.Form; }
            Cookies = request.Cookies;
            #if NETCOREAPP
            Body = bodyParser(request.BodyReader.AsStream(true));
            #else
            Body = bodyParser(request.Body);
            #endif
        }

        /// <summary>
        /// Gets the request URL in a fully un-escaped form (except for the QueryString).
        /// </summary>
        /// <value>The request URL in a fully un-escaped form (except for the QueryString).</value>
        public string Location { get; }

        /// <summary>
        /// Gets the request HTTP method.
        /// </summary>
        /// <value>The HTTP method.</value>
        public string Method { get; }

        /// <summary>
        /// Gets the request headers.
        /// </summary>
        /// <value>The headers of the request.</value>
        public IHeaderDictionary Headers { get; }

        /// <summary>
        /// Gets the associated keys and values collection parsed from the <see cref="HttpRequest.QueryString"/>.
        /// </summary>
        /// <value>The associated keys and values collection parsed from the <see cref="HttpRequest.QueryString"/>.</value>
        public IQueryCollection Query { get; }

        /// <summary>
        /// Gets the associated keys and values collection from the <see cref="HttpRequest.Form"/>.
        /// </summary>
        /// <value>The associated keys and values collection parsed from the <see cref="HttpRequest.Form"/>.</value>
        public IFormCollection Form { get; }

        /// <summary>
        /// Gets the collection of cookies for the request.
        /// </summary>
        /// <value>The collection of cookies for the request.</value>
        public IRequestCookieCollection Cookies { get; }

        /// <summary>
        /// Gets the body of the request.
        /// </summary>
        /// <value>The body of the request.</value>
        public string Body { get; }
    }
}<commit_msg>Fixed to work on ASPNET Core 3.<commit_after>﻿using System;
using System.IO;
using Cuemon.IO;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Extensions;

namespace Cuemon.AspNetCore.Mvc.Filters.Diagnostics
{
    /// <summary>
    /// Provides detailed information about a given <seealso cref="HttpRequest"/>.
    /// </summary>
    public class HttpRequestEvidence
    {
        internal HttpRequestEvidence(HttpRequest request, Func<Stream, string> bodyParser = null)
        {
            var hasMultipartContentType = request.GetMultipartBoundary().Length > 0;
            if (bodyParser == null) { bodyParser = body => hasMultipartContentType ? null : Decorator.Enclose(body).ToEncodedString(); }
            Location = request.GetDisplayUrl();
            Method = request.Method;
            Headers = request.Headers;
            Query = request.Query;
            if (request.HasFormContentType && !hasMultipartContentType) { Form = request.Form; }
            Cookies = request.Cookies;
            #if NETCOREAPP
            var requestBody = new MemoryStream();
            Decorator.Enclose(request.BodyReader.AsStream(true)).CopyStream(requestBody);
            Body =  bodyParser(requestBody);
            #else
            Body = bodyParser(request.Body);
            #endif
        }

        /// <summary>
        /// Gets the request URL in a fully un-escaped form (except for the QueryString).
        /// </summary>
        /// <value>The request URL in a fully un-escaped form (except for the QueryString).</value>
        public string Location { get; }

        /// <summary>
        /// Gets the request HTTP method.
        /// </summary>
        /// <value>The HTTP method.</value>
        public string Method { get; }

        /// <summary>
        /// Gets the request headers.
        /// </summary>
        /// <value>The headers of the request.</value>
        public IHeaderDictionary Headers { get; }

        /// <summary>
        /// Gets the associated keys and values collection parsed from the <see cref="HttpRequest.QueryString"/>.
        /// </summary>
        /// <value>The associated keys and values collection parsed from the <see cref="HttpRequest.QueryString"/>.</value>
        public IQueryCollection Query { get; }

        /// <summary>
        /// Gets the associated keys and values collection from the <see cref="HttpRequest.Form"/>.
        /// </summary>
        /// <value>The associated keys and values collection parsed from the <see cref="HttpRequest.Form"/>.</value>
        public IFormCollection Form { get; }

        /// <summary>
        /// Gets the collection of cookies for the request.
        /// </summary>
        /// <value>The collection of cookies for the request.</value>
        public IRequestCookieCollection Cookies { get; }

        /// <summary>
        /// Gets the body of the request.
        /// </summary>
        /// <value>The body of the request.</value>
        public string Body { get; }
    }
}
{'fraction_non_alphanumeric': 0.11890346451395896, 'fraction_numerical': 0.0006727211570803902, 'mean_word_length': 2.04974358974359, 'pattern_counts': {'":': 0, '<': 75, '<?xml version=': 0, '>': 79, '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': '5326207', 'n_tokens_mistral': 1508, 'n_tokens_neox': 1490, 'n_words': 465}