<commit_before>﻿using System;
using System.IO;
using System.Reflection;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Web;
using Nancy.Extensions;
using Nancy.Routing;

namespace Nancy.Hosting.Wcf
{
    [ServiceContract]
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
    public class NancyWcfGenericService
    {
        private readonly NancyEngine engine;

        public NancyWcfGenericService(Assembly modulesAssembly)
        {
            engine = new NancyEngine(new NancyModuleLocator(modulesAssembly), new RouteResolver());
        }

        [WebInvoke(UriTemplate = "*")]
        public Message HandleOther(Stream body)
        {
            return HandleAll(body);
        }

        [WebGet(UriTemplate = "*")]
        public Message HandleGet()
        {
            return HandleAll(null);
        }

        private Message HandleAll(Stream body)
        {
            WebOperationContext context = WebOperationContext.Current;
            IRequest request = CreateNancyRequestFromIncomingRequest(context.IncomingRequest, body);
            Response response = engine.HandleRequest(request);

            SetNancyResponseToOutgoingResponse(context.OutgoingResponse, response);

            return context.CreateStreamResponse(response.Contents, response.ContentType);
        }

        private static IRequest CreateNancyRequestFromIncomingRequest(IncomingWebRequestContext request, Stream body)
        {
            Uri relativeUri =
                request.UriTemplateMatch.BaseUri.MakeRelativeUri(request.UriTemplateMatch.RequestUri);

            return new Request(request.Method,
                               string.Concat("/", relativeUri),
                               request.Headers.ToDictionary(),
                               body ?? new MemoryStream());
        }

        private static void SetNancyResponseToOutgoingResponse(OutgoingWebResponseContext resp, Response response)
        {
            resp.ContentType = response.ContentType;
            resp.StatusCode = response.StatusCode;
        }<commit_msg>Minor code clean up<commit_after>﻿namespace Nancy.Hosting.Wcf
{
    using System.IO;
    using System.Reflection;
    using System.ServiceModel;
    using System.ServiceModel.Channels;
    using System.ServiceModel.Web;
    using Nancy.Extensions;
    using Nancy.Routing;

    [ServiceContract]
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
    public class NancyWcfGenericService
    {
        private readonly NancyEngine engine;

        public NancyWcfGenericService(Assembly modulesAssembly)
        {
            engine = new NancyEngine(new NancyModuleLocator(modulesAssembly), new RouteResolver());
        }

        [WebInvoke(UriTemplate = "*")]
        public Message HandleOther(Stream body)
        {
            return HandleAll(body);
        }

        [WebGet(UriTemplate = "*")]
        public Message HandleGet()
        {
            return HandleAll(null);
        }

        private Message HandleAll(Stream body)
        {
            var context = WebOperationContext.Current;
            var request = CreateNancyRequestFromIncomingRequest(context.IncomingRequest, body);
            var response = engine.HandleRequest(request);

            SetNancyResponseToOutgoingResponse(context.OutgoingResponse, response);

            return context.CreateStreamResponse(response.Contents, response.ContentType);
        }

        private static IRequest CreateNancyRequestFromIncomingRequest(IncomingWebRequestContext request, Stream body)
        {
            var relativeUri =
                request.UriTemplateMatch.BaseUri.MakeRelativeUri(request.UriTemplateMatch.RequestUri);

            return new Request(
                request.Method,
                string.Concat("/", relativeUri),
                request.Headers.ToDictionary(),
                body ?? new MemoryStream());
        }

        private static void SetNancyResponseToOutgoingResponse(OutgoingWebResponseContext resp, Response response)
        {
            resp.ContentType = response.ContentType;
            resp.StatusCode = response.StatusCode;
        }
{'fraction_non_alphanumeric': 0.07390684410646388, 'fraction_numerical': 0.0, 'mean_word_length': 2.472772277227723, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '3130614', 'n_tokens_mistral': 985, 'n_tokens_neox': 965, 'n_words': 200}