<commit_before>﻿using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Primitives;
using Newtonsoft.Json;

namespace EasyRpc.AspNetCore.Middleware
{
    public class RpcHeaderContext : IRpcHeaderContext
    {
        private IHttpContextAccessor _accessor;
        private ConcurrentDictionary<string, object> _values;

        public RpcHeaderContext(IHttpContextAccessor accessor)
        {
            _accessor = accessor;
            _values = new ConcurrentDictionary<string, object>();
        }

        public T GetValue<T>(string key = null)
        {
            var stringKey = typeof(T).Name + key;

            var headerValue = _accessor.HttpContext.Request.Headers["RpcContext-" + stringKey];

            if (headerValue.Count == 0)
            {
                return default(T);
            }

            var base64 = headerValue[0];

            return JsonConvert.DeserializeObject<T>(Encoding.UTF8.GetString(Convert.FromBase64String(base64)));
        }

        public void SetValue<T>(T value, string key = null)
        {
            var stringKey = typeof(T).Name + key;

            string valueString = value != null ? JsonConvert.SerializeObject(value) : "";

            var base64String = Convert.ToBase64String(Encoding.UTF8.GetBytes(valueString));

            _accessor.HttpContext.Response.Headers["RpcContext-" + stringKey] = new StringValues(base64String);
        }
    }
}
<commit_msg>updating rpc context<commit_after>﻿using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Primitives;
using Newtonsoft.Json;

namespace EasyRpc.AspNetCore.Middleware
{
    public class RpcHeaderContext : IRpcHeaderContext
    {
        private readonly IHttpContextAccessor _accessor;

        public RpcHeaderContext(IHttpContextAccessor accessor)
        {
            _accessor = accessor;
        }

        public T GetValue<T>(string key = null)
        {
            var httpContext = _accessor.HttpContext;

            var currentValues = GetCurrentValues(httpContext);

            var stringKey = typeof(T).Name + key;

            object value;

            if (currentValues.TryGetValue(stringKey, out value))
            {
                return (T) value;
            }

            var headerValue = httpContext.Request.Headers["RpcContext-" + stringKey];

            if (headerValue.Count == 0)
            {
                return default(T);
            }

            var base64 = headerValue[0];

            return JsonConvert.DeserializeObject<T>(Encoding.UTF8.GetString(Convert.FromBase64String(base64)));
        }

        public void SetValue<T>(T value, string key = null)
        {
            var httpContext = _accessor.HttpContext;

            var stringKey = typeof(T).Name + key;

            string valueString = value != null ? JsonConvert.SerializeObject(value) : "";

            var base64String = Convert.ToBase64String(Encoding.UTF8.GetBytes(valueString));

            httpContext.Response.Headers["RpcContext-" + stringKey] = new StringValues(base64String);

            var currentValues = GetCurrentValues(httpContext);

            currentValues[stringKey] = value;
        }

        protected ConcurrentDictionary<string, object> GetCurrentValues(HttpContext context)
        {
            var currentValues = context.Items["RpcHeaderValues"] as ConcurrentDictionary<string, object>;

            if (currentValues != null)
            {
                return currentValues;
            }

            currentValues = new ConcurrentDictionary<string, object>();

            context.Items["RpcHeaderValues"] = currentValues;

            return currentValues;
        }
    }
}

{'fraction_non_alphanumeric': 0.08914631075295895, 'fraction_numerical': 0.008058423570888944, 'mean_word_length': 2.3890784982935154, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 14, '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': '6982723', 'n_tokens_mistral': 1052, 'n_tokens_neox': 1013, 'n_words': 214}