<commit_before>// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Security.Cryptography.X509Certificates;
using Microsoft.AspNetCore.Http.Features;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Primitives;

namespace Microsoft.AspNetCore.HttpOverrides;

internal sealed class CertificateForwardingFeature : ITlsConnectionFeature
{
    private readonly ILogger _logger;
    private readonly StringValues _header;
    private readonly CertificateForwardingOptions _options;
    private X509Certificate2? _certificate;

    public CertificateForwardingFeature(ILogger logger, StringValues header, CertificateForwardingOptions options)
    {
        _logger = logger;
        _options = options;
        _header = header;
    }

    public X509Certificate2? ClientCertificate
    {
        get
        {
            if (_certificate == null)
            {
                try
                {
                    _certificate = _options.HeaderConverter(_header.ToString());
                }
                catch (Exception e)
                {
                    _logger.NoCertificate(e);
                }
            }
            return _certificate;
        }
        set => _certificate = value;
    }

    public Task<X509Certificate2?> GetClientCertificateAsync(CancellationToken cancellationToken)
        => Task.FromResult(ClientCertificate);
}
<commit_msg>Store and reuse client certificate task (#43270)<commit_after>// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Security.Cryptography.X509Certificates;
using Microsoft.AspNetCore.Http.Features;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Primitives;

namespace Microsoft.AspNetCore.HttpOverrides;

internal sealed class CertificateForwardingFeature : ITlsConnectionFeature
{
    private readonly ILogger _logger;
    private readonly StringValues _header;
    private readonly CertificateForwardingOptions _options;
    private Task<X509Certificate2?>? _certificateTask;

    public CertificateForwardingFeature(ILogger logger, StringValues header, CertificateForwardingOptions options)
    {
        _logger = logger;
        _options = options;
        _header = header;
    }

    public X509Certificate2? ClientCertificate
    {
        get => GetClientCertificateAsync(CancellationToken.None).Result;
        set => _certificateTask = value is not null ? Task.FromResult<X509Certificate2?>(value) : null;
    }

    public Task<X509Certificate2?> GetClientCertificateAsync(CancellationToken cancellationToken)
    {
        if (_certificateTask == null)
        {
            try
            {
                var certificate = _options.HeaderConverter(_header.ToString());
                _certificateTask = Task.FromResult<X509Certificate2?>(certificate);
                return _certificateTask;
            }
            catch (Exception e)
            {
                _logger.NoCertificate(e);
                return Task.FromResult<X509Certificate2?>(null);
            }
        }
        else
        {
            return _certificateTask;
        }
    }
}

{'fraction_non_alphanumeric': 0.06682941714983216, 'fraction_numerical': 0.014342386328959414, 'mean_word_length': 2.5903614457831323, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 13, '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': '257323', 'n_tokens_mistral': 823, 'n_tokens_neox': 781, 'n_words': 199}