<commit_before>﻿using System;
using System.IO;
using System.IO.Compression;
using Autofac;
using Hadouken.Common;
using Hadouken.Common.IO;
using Hadouken.Core.Http.Security;
using Nancy;
using Nancy.Bootstrapper;
using Nancy.Bootstrappers.Autofac;
using Nancy.Conventions;
using Nancy.Responses;
using Path = Hadouken.Common.IO.Path;

namespace Hadouken.Core.Http
{
    public class CustomNancyBootstrapper : AutofacNancyBootstrapper
    {
        private readonly ILifetimeScope _lifetimeScope;
        private readonly IEnvironment _environment;
        private readonly IFileSystem _fileSystem;

        public CustomNancyBootstrapper(ILifetimeScope lifetimeScope,
            IEnvironment environment,
            IFileSystem fileSystem)
        {
            if (lifetimeScope == null) throw new ArgumentNullException("lifetimeScope");
            if (environment == null) throw new ArgumentNullException("environment");
            if (fileSystem == null) throw new ArgumentNullException("fileSystem");

            _lifetimeScope = lifetimeScope;
            _environment = environment;
            _fileSystem = fileSystem;
        }

        protected override ILifetimeScope GetApplicationContainer()
        {
            return _lifetimeScope;
        }

        protected override void ApplicationStartup(ILifetimeScope container, IPipelines pipelines)
        {
            var tokenizer = container.Resolve<ITokenizer>();
            var cfg = new TokenAuthenticationConfiguration(tokenizer);

            TokenAuthentication.Enable(pipelines, cfg);
        }

        protected override void ConfigureConventions(NancyConventions nancyConventions)
        {
            var webPath = _environment.GetWebApplicationPath();

            nancyConventions.StaticContentsConventions.Add((context, s) =>
            {
                var requestedFile = (context.Request.Path == "/" ? "/index.html" : context.Request.Path);
                if (requestedFile.StartsWith("/")) requestedFile = requestedFile.Substring(1);

                if (webPath.FullPath.EndsWith(".zip") && webPath is FilePath)
                {
                    var package = ((FilePath) webPath).MakeAbsolute(_environment);

                    using (var archive = ZipFile.Open(package.FullPath, ZipArchiveMode.Read))<commit_msg>Added HTTP pipeline exception logging.<commit_after>﻿using System;
using System.IO;
using System.IO.Compression;
using Autofac;
using Hadouken.Common;
using Hadouken.Common.IO;
using Hadouken.Common.Logging;
using Hadouken.Core.Http.Security;
using Nancy;
using Nancy.Bootstrapper;
using Nancy.Bootstrappers.Autofac;
using Nancy.Conventions;
using Nancy.Extensions;
using Nancy.Responses;
using Path = Hadouken.Common.IO.Path;

namespace Hadouken.Core.Http
{
    public class CustomNancyBootstrapper : AutofacNancyBootstrapper
    {
        private readonly ILifetimeScope _lifetimeScope;
        private readonly ILogger<CustomNancyBootstrapper> _logger;
        private readonly IEnvironment _environment;
        private readonly IFileSystem _fileSystem;

        public CustomNancyBootstrapper(ILifetimeScope lifetimeScope,
            ILogger<CustomNancyBootstrapper> logger,
            IEnvironment environment,
            IFileSystem fileSystem)
        {
            if (lifetimeScope == null) throw new ArgumentNullException("lifetimeScope");
            if (logger == null) throw new ArgumentNullException("logger");
            if (environment == null) throw new ArgumentNullException("environment");
            if (fileSystem == null) throw new ArgumentNullException("fileSystem");

            _lifetimeScope = lifetimeScope;
            _logger = logger;
            _environment = environment;
            _fileSystem = fileSystem;
        }

        protected override ILifetimeScope GetApplicationContainer()
        {
            return _lifetimeScope;
        }

        protected override void ApplicationStartup(ILifetimeScope container, IPipelines pipelines)
        {
            var tokenizer = container.Resolve<ITokenizer>();
            var cfg = new TokenAuthenticationConfiguration(tokenizer);

            TokenAuthentication.Enable(pipelines, cfg);

            pipelines.OnError.AddItemToStartOfPipeline((context, exception) => 
            {
                _logger.Error(exception, "Error in HTTP pipeline.");
                return null;
            });
        }

        protected override void ConfigureConventions(NancyConventions nancyConventions)
        {
            var webPath = _environment.GetWebApplicationPath();

            nancyConventions.StaticContentsConventions.Add((context, s) =>
            {
                var requestedFile = (context.Request.Path == "/" ? "/index.html" : context.Request.Path);
                if (requestedFile.StartsWith("/")) requestedFile = requestedFile.Substring(1);

                if (webPath.FullPath.EndsWith(".zip") && webPath is FilePath)
                {
                    var package = ((FilePath) webPath).MakeAbsolute(_environment);

                    using (var archive = ZipFile.Open(package.FullPath, ZipArchiveMode.Read))
{'fraction_non_alphanumeric': 0.08122813835989118, 'fraction_numerical': 0.000388651379712398, 'mean_word_length': 2.694903086862886, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 10, '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': '2134666', 'n_tokens_mistral': 1294, 'n_tokens_neox': 1257, 'n_words': 292}