<commit_before>namespace Nancy.Hosting.Aspnet
{
    using System.Collections.Generic;
    using System.Globalization;
    using System.Linq;
    using System.Web;
    using IO;
    using Nancy.Extensions;

    /// <summary>
    /// Bridges the communication between Nancy and ASP.NET based hosting.
    /// </summary>
    public class NancyHandler
    {        
        private readonly INancyEngine engine;

        /// <summary>
        /// Initializes a new instance of the <see cref="NancyHandler"/> type for the specified <paramref name="engine"/>.
        /// </summary>
        /// <param name="engine">An <see cref="INancyEngine"/> instance, that should be used by the handler.</param>
        public NancyHandler(INancyEngine engine)
        {
            this.engine = engine;
        }

        /// <summary>
        /// Processes the ASP.NET request with Nancy.
        /// </summary>
        /// <param name="context">The <see cref="HttpContextBase"/> of the request.</param>
        public void ProcessRequest(HttpContextBase context)
        {
            var request = CreateNancyRequest(context);

            using (var nancyContext = this.engine.HandleRequest(request))
            {
                SetNancyResponseToHttpResponse(context, nancyContext.Response);
            }
        }

        private static Request CreateNancyRequest(HttpContextBase context)
        {
            var expectedRequestLength =
                GetExpectedRequestLength(context.Request.Headers.ToDictionary());

            var basePath =
                context.Request.ApplicationPath.TrimEnd('/');

            var nancyUrl = new Url
                               {
                                   Scheme = context.Request.Url.Scheme,
                                   HostName = context.Request.Url.Host,
                                   Port = context.Request.Url.Port,
                                   BasePath = basePath,
                                   Path = context.Request.Url.AbsolutePath.Substring(basePath.Length),
                                   Query = context.Request.Url.Query,
                                   Fragment = context.Request.Url.Fragment,
                               };

            return new Request(
                context.Request.HttpMethod.ToUpperInvariant(),
                nancyUrl,
                RequestStream.FromStream(context.Request.InputStream, expectedRequestLength, true),
                context.Request.Headers.ToDictionary(),
                context.Request.UserHostAddress);
        }

        private static long GetExpectedRequestLength(IDictionary<string, IEnumerable<string>> incomingHeaders)
        {
            if (incomingHeaders == null)
            {
                return 0;
            }

            if (!incomingHeaders.ContainsKey("Content-Length"))
            {
                return 0;
            }

            var headerValue =
                incomingHeaders["Content-Length"].SingleOrDefault();
<commit_msg>Fixed bug #820.<commit_after>namespace Nancy.Hosting.Aspnet
{
    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.Globalization;
    using System.Linq;
    using System.Web;
    using IO;
    using Nancy.Extensions;

    /// <summary>
    /// Bridges the communication between Nancy and ASP.NET based hosting.
    /// </summary>
    public class NancyHandler
    {        
        private readonly INancyEngine engine;

        /// <summary>
        /// Initializes a new instance of the <see cref="NancyHandler"/> type for the specified <paramref name="engine"/>.
        /// </summary>
        /// <param name="engine">An <see cref="INancyEngine"/> instance, that should be used by the handler.</param>
        public NancyHandler(INancyEngine engine)
        {
            this.engine = engine;
        }

        /// <summary>
        /// Processes the ASP.NET request with Nancy.
        /// </summary>
        /// <param name="context">The <see cref="HttpContextBase"/> of the request.</param>
        public void ProcessRequest(HttpContextBase context)
        {
            var request = CreateNancyRequest(context);

            using (var nancyContext = this.engine.HandleRequest(request))
            {
                SetNancyResponseToHttpResponse(context, nancyContext.Response);
            }
        }

        private static Request CreateNancyRequest(HttpContextBase context)
        {
            var expectedRequestLength =
                GetExpectedRequestLength(context.Request.Headers.ToDictionary());

            var basePath = context.Request.ApplicationPath;
            Debug.Assert(basePath != null, "Base path must not be null.");

            Uri requestUrl = context.Request.Url;
            Debug.Assert(requestUrl != null, "Request Url must not be null.");

            var path = requestUrl.AbsolutePath.Substring(basePath.Length);
            path = string.IsNullOrWhiteSpace(path) ? "/" : path;

            var nancyUrl = new Url
            {
                Scheme = requestUrl.Scheme,
                HostName = requestUrl.Host,
                Port = requestUrl.Port,
                BasePath = basePath,
                Path = path,
                Query = requestUrl.Query,
                Fragment = requestUrl.Fragment,
            };

            return new Request(
                context.Request.HttpMethod.ToUpperInvariant(),
                nancyUrl,
                RequestStream.FromStream(context.Request.InputStream, expectedRequestLength, true),
                context.Request.Headers.ToDictionary(),
                context.Request.UserHostAddress);
        }

        private static long GetExpectedRequestLength(IDictionary<string, IEnumerable<string>> incomingHeaders)
        {
            if (incomingHeaders == null)
            {
                return 0;
            }

            if (!incomingHeaders.ContainsKey("Content-Length"))
            {
                return 0;
            }

            var headerValue =
                incomingHeaders["Content-Length"].SingleOrDefault();

{'fraction_non_alphanumeric': 0.09235044132069303, 'fraction_numerical': 0.0011441647597254005, 'mean_word_length': 1.6523623753792804, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 7, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5204081', 'n_tokens_mistral': 1463, 'n_tokens_neox': 1393, 'n_words': 331}