<commit_before>﻿using System;
using System.Net;

namespace xetohub.core.eventhub
{
    public class Publisher
    {
        public static readonly DateTime DT_START = new DateTime(1970, 1, 1);

        public static string GenerateSignature(
            string policyName,
            string sasKey,
            Uri uri,
            TimeSpan duration)
        {
            string urlEncoded = System.Net.WebUtility.UrlEncode(uri.ToString());

            DateTime dtEpiry = DateTime.Now.Add(duration);
            string expiry = ((int)(dtEpiry - DT_START).TotalSeconds).ToString();

            string strToSign = string.Format("{0:S}\n{1:S}", urlEncoded, expiry);
            var bytesToSign = System.Text.Encoding.UTF8.GetBytes(strToSign);

            System.Security.Cryptography.HMACSHA256 SHA256 = new System.Security.Cryptography.HMACSHA256(System.Text.Encoding.UTF8.GetBytes(sasKey));
            string strHash2Base64 = Convert.ToBase64String(SHA256.ComputeHash(bytesToSign));

            string strSig = System.Net.WebUtility.UrlEncode(strHash2Base64);

            string sAuth = string.Format("SharedAccessSignature sr={0:S}&sig={1:S}&se={2:S}&skn={3:S}",
                urlEncoded,
                strSig,
                expiry,
                policyName);

            return sAuth;
        }

        public static void PushToEventHub(
            string sbNamespace,
            string eventHubName,
            string policyName,
            string sasKey,
            TimeSpan duration,
            byte[] payload)
        {
            string url = string.Format("https://{0:S}.servicebus.windows.net/{1:S}/messages",
                      sbNamespace,
                      eventHubName);

            Uri uri = new Uri(url);

            string signature = GenerateSignature(
                policyName,
                sasKey,
                uri,
                duration);

            System.Net.HttpWebRequest req = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(url);
            req.Method = "POST";
            req.Timeout = 60000;
            req.ContentLength = payload.Length;
            req.Headers.Add("Authorization", signature);

            using (var reqStream = req.GetRequestStream())
            {
                reqStream.Write(payload, 0, payload.Length);
            }
            var resp = req.GetResponse();
        }

        public static void PushToEventHub(
            string sbNamespace,
            string eventHubName,
            string policyName,
            string sasKey,
            TimeSpan duration,
            string content)
        {

            byte[] payload = System.Text.Encoding.UTF8.GetBytes(content);
            PushToEventHub(sbNamespace, eventHubName, policyName, sasKey, duration, payload);
        }
    }
}
<commit_msg>Fixed a publisher bug: the Response stream was not closed leading to thread starvation<commit_after>﻿using System;
using System.Net;

namespace xetohub.core.eventhub
{
    public class Publisher
    {
        public static readonly DateTime DT_START = new DateTime(1970, 1, 1);

        public static string GenerateSignature(
            string policyName,
            string sasKey,
            Uri uri,
            TimeSpan duration)
        {
            string urlEncoded = System.Net.WebUtility.UrlEncode(uri.ToString());

            DateTime dtEpiry = DateTime.Now.Add(duration);
            string expiry = ((int)(dtEpiry - DT_START).TotalSeconds).ToString();

            string strToSign = string.Format("{0:S}\n{1:S}", urlEncoded, expiry);
            var bytesToSign = System.Text.Encoding.UTF8.GetBytes(strToSign);

            System.Security.Cryptography.HMACSHA256 SHA256 = new System.Security.Cryptography.HMACSHA256(System.Text.Encoding.UTF8.GetBytes(sasKey));
            string strHash2Base64 = Convert.ToBase64String(SHA256.ComputeHash(bytesToSign));

            string strSig = System.Net.WebUtility.UrlEncode(strHash2Base64);

            string sAuth = string.Format("SharedAccessSignature sr={0:S}&sig={1:S}&se={2:S}&skn={3:S}",
                urlEncoded,
                strSig,
                expiry,
                policyName);

            return sAuth;
        }

        public static void PushToEventHub(
            string sbNamespace,
            string eventHubName,
            string policyName,
            string sasKey,
            TimeSpan duration,
            byte[] payload)
        {
            string url = string.Format("https://{0:S}.servicebus.windows.net/{1:S}/messages",
                      sbNamespace,
                      eventHubName);

            Uri uri = new Uri(url);

            string signature = GenerateSignature(
                policyName,
                sasKey,
                uri,
                duration);

            System.Net.HttpWebRequest req = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(url);
            req.Method = "POST";
            req.Timeout = 60000;
            req.ContentLength = payload.Length;
            req.Headers.Add("Authorization", signature);

            using (var reqStream = req.GetRequestStream())
            {
                reqStream.Write(payload, 0, payload.Length);
            }
            using (var resp = req.GetResponse())
            { }
        }

        public static void PushToEventHub(
            string sbNamespace,
            string eventHubName,
            string policyName,
            string sasKey,
            TimeSpan duration,
            string content)
        {

            byte[] payload = System.Text.Encoding.UTF8.GetBytes(content);
            PushToEventHub(sbNamespace, eventHubName, policyName, sasKey, duration, payload);
        }
    }
}

{'fraction_non_alphanumeric': 0.09341233879400487, 'fraction_numerical': 0.014987800627396306, 'mean_word_length': 1.7818710615608337, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, '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': '5812591', 'n_tokens_mistral': 1585, 'n_tokens_neox': 1472, 'n_words': 306}