<commit_before>﻿// Copyright 2021 Cloud Native Foundation.
// Licensed under the Apache 2.0 license.
// See LICENSE file in the project root for full license information.

using CloudNative.CloudEvents.Core;
using System;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

namespace CloudNative.CloudEvents.Http
{
    /// <summary>
    /// Common functionality used by all HTTP code. This is public to enable reuse by other packages,
    /// e.g. ASP.NET Core code.
    /// </summary>
    public static class HttpUtilities
    {
        /// <summary>
        /// The prefix used by all CloudEvents HTTP headers.
        /// </summary>
        public const string HttpHeaderPrefix = "ce-";

        /// <summary>
        /// The name of the HTTP header used to specify the CloudEvents specification version in an HTTP message.
        /// </summary>
        public const string SpecVersionHttpHeader = HttpHeaderPrefix + "specversion";

        /// <summary>
        /// Checks whether the given HTTP header name starts with "ce-", and if so, converts it into
        /// a lower-case attribute name.
        /// </summary>
        /// <param name="headerName">The name of the header to check. Must not be null.</param>
        /// <returns>The corresponding attribute name if the header name matches the CloudEvents header prefix;
        /// null otherwise.</returns>
        public static string GetAttributeNameFromHeaderName(string headerName) =>
            Validation.CheckNotNull(headerName, nameof(headerName)).StartsWith(HttpHeaderPrefix, StringComparison.InvariantCultureIgnoreCase)
            ? headerName.Substring(HttpHeaderPrefix.Length).ToLowerInvariant()
            : null;

        internal static async Task<(HttpStatusCode statusCode, string allowedOriginHeader, string allowedRateHeader)> HandleWebHookValidationAsync<TRequest>(
            TRequest request, Func<TRequest, string, string> headerSelector,
            Func<string, bool> validateOrigin, Func<string, string> validateRate)
        {
            var origin = headerSelector(request, "WebHook-Request-Origin");
            var rate = headerSelector(request, "WebHook-Request-Rate");

            if (origin is null || validateOrigin?.Invoke(origin) == false)
            {
                return (HttpStatusCode.MethodNotAllowed, null, null);
            }

            if (rate is object)
            {
                rate = validateRate?.Invoke(rate) ?? "*";
            }<commit_msg>Make strings in HttpUtilities properties instead of constants<commit_after>﻿// Copyright 2021 Cloud Native Foundation.
// Licensed under the Apache 2.0 license.
// See LICENSE file in the project root for full license information.

using CloudNative.CloudEvents.Core;
using System;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

namespace CloudNative.CloudEvents.Http
{
    /// <summary>
    /// Common functionality used by all HTTP code. This is public to enable reuse by other packages,
    /// e.g. ASP.NET Core code.
    /// </summary>
    public static class HttpUtilities
    {
        /// <summary>
        /// The prefix used by all CloudEvents HTTP headers.
        /// </summary>
        public static string HttpHeaderPrefix { get; } = "ce-";

        /// <summary>
        /// The name of the HTTP header used to specify the CloudEvents specification version in an HTTP message.
        /// </summary>
        public static string SpecVersionHttpHeader { get; } = HttpHeaderPrefix + "specversion";

        /// <summary>
        /// Checks whether the given HTTP header name starts with "ce-", and if so, converts it into
        /// a lower-case attribute name.
        /// </summary>
        /// <param name="headerName">The name of the header to check. Must not be null.</param>
        /// <returns>The corresponding attribute name if the header name matches the CloudEvents header prefix;
        /// null otherwise.</returns>
        public static string GetAttributeNameFromHeaderName(string headerName) =>
            Validation.CheckNotNull(headerName, nameof(headerName)).StartsWith(HttpHeaderPrefix, StringComparison.InvariantCultureIgnoreCase)
            ? headerName.Substring(HttpHeaderPrefix.Length).ToLowerInvariant()
            : null;

        internal static async Task<(HttpStatusCode statusCode, string allowedOriginHeader, string allowedRateHeader)> HandleWebHookValidationAsync<TRequest>(
            TRequest request, Func<TRequest, string, string> headerSelector,
            Func<string, bool> validateOrigin, Func<string, string> validateRate)
        {
            var origin = headerSelector(request, "WebHook-Request-Origin");
            var rate = headerSelector(request, "WebHook-Request-Rate");

            if (origin is null || validateOrigin?.Invoke(origin) == false)
            {
                return (HttpStatusCode.MethodNotAllowed, null, null);
            }

            if (rate is object)
            {
                rate = validateRate?.Invoke(rate) ?? "*";
            }
{'fraction_non_alphanumeric': 0.0974076983503535, 'fraction_numerical': 0.002356637863315004, 'mean_word_length': 2.5368055555555555, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 39, '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': '5929792', 'n_tokens_mistral': 1241, 'n_tokens_neox': 1216, 'n_words': 425}