<commit_before>﻿using System;
using System.Collections.Generic;
using System.Net;
using System.Text;

namespace RockLib.Messaging.Http
{
    public class HttpListenerReceiverMessage : ReceiverMessage
    {
        public HttpListenerReceiverMessage(HttpListenerContext context, IHttpResponseGenerator httpResponseGenerator)
            : base(() => GetPayload(context))
        {
            Context = context ?? throw new ArgumentNullException(nameof(context));
            HttpResponseGenerator = httpResponseGenerator ?? throw new ArgumentNullException(nameof(httpResponseGenerator));
        }

        public override byte? Priority => null;

        public HttpListenerContext Context { get; }

        public IHttpResponseGenerator HttpResponseGenerator { get; }

        public override void Acknowledge()
        {
            var response = HttpResponseGenerator.GetAcknowledgeResponse(this);
            WriteResponse(response);
        }

        public override void Rollback()
        {
            var response = HttpResponseGenerator.GetRollbackResponse(this);
            WriteResponse(response);
        }

        private void WriteResponse(HttpResponse response)
        {
            Context.Response.StatusCode = response.StatusCode;
            Context.Response.StatusDescription = response.StatusDescription;

            switch (response.Content)
            {
                case string stringContent:
                    Context.Response.ContentEncoding = Encoding.UTF8;
                    var buffer = Encoding.UTF8.GetBytes(stringContent);
                    Context.Response.OutputStream.Write(buffer, 0, buffer.Length);
                    break;
                case byte[] binaryContent:
                    Context.Response.OutputStream.Write(binaryContent, 0, binaryContent.Length);
                    break;
            }

            foreach (var header in response.Headers)
                Context.Response.Headers.Add(header.Key, header.Value);

            Context.Response.Close();
        }

        protected override void InitializeHeaders(IDictionary<string, object> headers)
        {
            foreach (var key in Context.Request.Headers.AllKeys)
            {
                headers.Add(key, Context.Request.Headers[key]);
            }
        }

        private static byte[] GetPayload(HttpListenerContext context)
        {
            var buffer = new byte[context.Request.ContentLength64];
            context.Request.InputStream.Read(buffer, 0, buffer.Length);
            return buffer;
        }
    }
}<commit_msg>Add query string parameters to http receiver message headers<commit_after>﻿using System;
using System.Collections.Generic;
using System.Net;
using System.Text;

namespace RockLib.Messaging.Http
{
    public class HttpListenerReceiverMessage : ReceiverMessage
    {
        public HttpListenerReceiverMessage(HttpListenerContext context, IHttpResponseGenerator httpResponseGenerator)
            : base(() => GetPayload(context))
        {
            Context = context ?? throw new ArgumentNullException(nameof(context));
            HttpResponseGenerator = httpResponseGenerator ?? throw new ArgumentNullException(nameof(httpResponseGenerator));
        }

        public override byte? Priority => null;

        public HttpListenerContext Context { get; }

        public IHttpResponseGenerator HttpResponseGenerator { get; }

        public override void Acknowledge()
        {
            var response = HttpResponseGenerator.GetAcknowledgeResponse(this);
            WriteResponse(response);
        }

        public override void Rollback()
        {
            var response = HttpResponseGenerator.GetRollbackResponse(this);
            WriteResponse(response);
        }

        private void WriteResponse(HttpResponse response)
        {
            Context.Response.StatusCode = response.StatusCode;
            Context.Response.StatusDescription = response.StatusDescription;

            switch (response.Content)
            {
                case string stringContent:
                    Context.Response.ContentEncoding = Encoding.UTF8;
                    var buffer = Encoding.UTF8.GetBytes(stringContent);
                    Context.Response.OutputStream.Write(buffer, 0, buffer.Length);
                    break;
                case byte[] binaryContent:
                    Context.Response.OutputStream.Write(binaryContent, 0, binaryContent.Length);
                    break;
            }

            foreach (var header in response.Headers)
                Context.Response.Headers.Add(header.Key, header.Value);

            Context.Response.Close();
        }

        protected override void InitializeHeaders(IDictionary<string, object> headers)
        {
            foreach (var key in Context.Request.Headers.AllKeys)
                headers.Add(key, Context.Request.Headers[key]);

            foreach (var key in Context.Request.QueryString.AllKeys)
                headers.Add(key, Context.Request.QueryString[key]);
        }

        private static byte[] GetPayload(HttpListenerContext context)
        {
            var buffer = new byte[context.Request.ContentLength64];
            context.Request.InputStream.Read(buffer, 0, buffer.Length);
            return buffer;
        }
    }
}
{'fraction_non_alphanumeric': 0.07714016933207903, 'fraction_numerical': 0.002634054562558796, 'mean_word_length': 2.1699463327370303, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, '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': '6955811', 'n_tokens_mistral': 1213, 'n_tokens_neox': 1159, 'n_words': 263}