<commit_before>﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using Kayak;
using Kayak.Http;

namespace Gate.Kayak
{
    class GateRequestDelegate : IHttpRequestDelegate
    {
        AppDelegate appDelegate;
        IDictionary<string, object> context;

        public GateRequestDelegate(AppDelegate appDelegate, IDictionary<string, object> context)
        {
            this.appDelegate = appDelegate;
            this.context = context;
        }

        public void OnRequest(HttpRequestHead head, IDataProducer body, IHttpResponseDelegate response)
        {
            var env = new Environment();

            if (context != null)
                foreach (var kv in context)
                    env[kv.Key] = kv.Value;

            env.Headers = head.Headers;
            env.Method = head.Method;
            env.Path = head.Path;
            env.PathBase = "";
            env.QueryString = head.QueryString;
            env.Scheme = "http"; // XXX
            env.Version = "1.0";
            
            if (body == null)
                env.Body = null;
            else
                env.Body = (onData, onError, onEnd) =>
                {
                    var d = body.Connect(new DataConsumer(onData, onError, onEnd));
                    return () => { if (d != null) d.Dispose(); };
                };

            appDelegate(env, HandleResponse(response), HandleError(response));
        }

        ResultDelegate HandleResponse(IHttpResponseDelegate response)<commit_msg>Don't produce null where OWIN requires empty string<commit_after>﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using Kayak;
using Kayak.Http;

namespace Gate.Kayak
{
    class GateRequestDelegate : IHttpRequestDelegate
    {
        AppDelegate appDelegate;
        IDictionary<string, object> context;

        public GateRequestDelegate(AppDelegate appDelegate, IDictionary<string, object> context)
        {
            this.appDelegate = appDelegate;
            this.context = context;
        }

        public void OnRequest(HttpRequestHead head, IDataProducer body, IHttpResponseDelegate response)
        {
            var env = new Environment();

            if (context != null)
                foreach (var kv in context)
                    env[kv.Key] = kv.Value;

            env.Headers = head.Headers;
            env.Method = head.Method ?? "";
            env.Path = head.Path ?? "";
            env.PathBase = "";
            env.QueryString = head.QueryString ?? "";
            env.Scheme = "http"; // XXX
            env.Version = "1.0";
            
            if (body == null)
                env.Body = null;
            else
                env.Body = (onData, onError, onEnd) =>
                {
                    var d = body.Connect(new DataConsumer(onData, onError, onEnd));
                    return () => { if (d != null) d.Dispose(); };
                };

            appDelegate(env, HandleResponse(response), HandleError(response));
        }

        ResultDelegate HandleResponse(IHttpResponseDelegate response)
{'fraction_non_alphanumeric': 0.09777070063694268, 'fraction_numerical': 0.0012738853503184713, 'mean_word_length': 1.7552631578947369, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 11, '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': '1680074', 'n_tokens_mistral': 812, 'n_tokens_neox': 781, 'n_words': 196}