<commit_before>using System;
using System.Diagnostics;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using Microsoft.Extensions.Hosting;
using Microsoft.Net.Http.Headers;

namespace Hellang.Middleware.ProblemDetails
{
    public class ProblemDetailsOptionsSetup : IConfigureOptions<ProblemDetailsOptions>
    {
        public void Configure(ProblemDetailsOptions options)
        {
            if (options.IncludeExceptionDetails is null)
            {
                options.IncludeExceptionDetails = IncludeExceptionDetails;
            }

            if (options.ShouldLogUnhandledException is null)
            {
                options.ShouldLogUnhandledException = (ctx, e, d) => IsServerError(d.Status);
            }

            if (options.MapStatusCode is null)
            {
                options.MapStatusCode = ctx => StatusCodeProblemDetails.Create(ctx.Response.StatusCode);
            }

            if (options.IsProblem is null)
            {
                options.IsProblem = IsProblem;
            }

            if (options.GetTraceId is null)
            {
                options.GetTraceId = ctx => Activity.Current?.Id ?? ctx.TraceIdentifier;
            }

            if (string.IsNullOrEmpty(options.ExceptionDetailsPropertyName))
            {
                options.ExceptionDetailsPropertyName = ProblemDetailsOptions.DefaultExceptionDetailsPropertyName;
            }

            if (options.AppendCacheHeaders is null)
            {
                options.AppendCacheHeaders = (ctx, headers) =>
                {
                    headers.Append(HeaderNames.CacheControl, "no-cache, no-store, must-revalidate");
                    headers.Append(HeaderNames.Pragma, "no-cache");
                    headers.Append(HeaderNames.Expires, "0");
                };
            }

            if (options.ContentTypes.Count == 0)
            {
                options.ContentTypes.Add("application/problem+json");
                options.ContentTypes.Add("application/problem+xml");
            }
        }

        private static bool IncludeExceptionDetails(HttpContext context, Exception exception)
        {
            return context.RequestServices.GetRequiredService<IHostEnvironment>().IsDevelopment();
        }

        private static bool IsServerError(int? statusCode)
        {
            // Err on the side of caution and treat missing status code as server error.
            return !statusCode.HasValue || statusCode.Value >= 500;
        }

        private static bool IsProblem(HttpContext context)
        {
            if (context.Response.StatusCode < 400)
            {
                return false;
            }

            if (context.Response.StatusCode >= 600)
            {
                return false;
            }

            if (context.Response.ContentLength.HasValue)
            {
                return false;
            }

            if (string.IsNullOrEmpty(context.Response.ContentType))
            {
                return true;
            }

            return false;
        }
    }
}<commit_msg>Pull out internal helper method to check for problem status code<commit_after>using System;
using System.Diagnostics;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using Microsoft.Extensions.Hosting;
using Microsoft.Net.Http.Headers;

namespace Hellang.Middleware.ProblemDetails
{
    public class ProblemDetailsOptionsSetup : IConfigureOptions<ProblemDetailsOptions>
    {
        public void Configure(ProblemDetailsOptions options)
        {
            if (options.IncludeExceptionDetails is null)
            {
                options.IncludeExceptionDetails = IncludeExceptionDetails;
            }

            if (options.ShouldLogUnhandledException is null)
            {
                options.ShouldLogUnhandledException = (ctx, e, d) => IsServerError(d.Status);
            }

            if (options.MapStatusCode is null)
            {
                options.MapStatusCode = ctx => StatusCodeProblemDetails.Create(ctx.Response.StatusCode);
            }

            if (options.IsProblem is null)
            {
                options.IsProblem = IsProblem;
            }

            if (options.GetTraceId is null)
            {
                options.GetTraceId = ctx => Activity.Current?.Id ?? ctx.TraceIdentifier;
            }

            if (string.IsNullOrEmpty(options.ExceptionDetailsPropertyName))
            {
                options.ExceptionDetailsPropertyName = ProblemDetailsOptions.DefaultExceptionDetailsPropertyName;
            }

            if (options.AppendCacheHeaders is null)
            {
                options.AppendCacheHeaders = (ctx, headers) =>
                {
                    headers.Append(HeaderNames.CacheControl, "no-cache, no-store, must-revalidate");
                    headers.Append(HeaderNames.Pragma, "no-cache");
                    headers.Append(HeaderNames.Expires, "0");
                };
            }

            if (options.ContentTypes.Count == 0)
            {
                options.ContentTypes.Add("application/problem+json");
                options.ContentTypes.Add("application/problem+xml");
            }
        }

        private static bool IncludeExceptionDetails(HttpContext context, Exception exception)
        {
            return context.RequestServices.GetRequiredService<IHostEnvironment>().IsDevelopment();
        }

        private static bool IsServerError(int? statusCode)
        {
            // Err on the side of caution and treat missing status code as server error.
            return !statusCode.HasValue || statusCode.Value >= 500;
        }

        private static bool IsProblem(HttpContext context)
        {
            if (!IsProblemStatusCode(context.Response.StatusCode))
            {
                return false;
            }

            if (context.Response.ContentLength.HasValue)
            {
                return false;
            }

            if (string.IsNullOrEmpty(context.Response.ContentType))
            {
                return true;
            }

            return false;
        }

        internal static bool IsProblemStatusCode(int? statusCode)
        {
            return statusCode switch
            {
                >= 600 => false,
                < 400 => false,
                null => false,
                _ => true
            };
        }
    }
}
{'fraction_non_alphanumeric': 0.07962708237811401, 'fraction_numerical': 0.00336237200061134, 'mean_word_length': 1.6786737617683176, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6432510', 'n_tokens_mistral': 1581, 'n_tokens_neox': 1482, 'n_words': 301}