<commit_before>﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Text;
using System.Web;
using cmstar.Serialization.Json;

namespace cmstar.WebApi.Slim
{
    /// <summary>
    /// 提供Slim WebAPI的入口。继承此类以实现API的注册和使用。
    /// 这是一个抽象类。
    /// </summary>
    public abstract class SlimApiHttpHandler : ApiHttpHandlerBase
    {
        protected static string MetaParamMethodName = "~method";
        protected static string MetaParamFormat = "~format";
        protected static string MetaParamCallback = "~callback";

        protected const string MetaRequestFormatJson = "json";
        protected const string MetaRequestFormatPost = "post";
        protected const string MetaRequestFormatGet = "get";
        protected const string MetaResponseFormatPlain = "plain";
        private const int OutputBodyLimitLower = 1024;
        private const int OutputBodyLimitUpper = 65536;

        /// <summary>
        /// 获取指定的API方法所对应的参数解析器。
        /// </summary>
        /// <param name="method">包含API方法的有关信息。</param>
        /// <returns>key为解析器的名称，value为解析器实例。</returns>
        protected override IDictionary<string, IRequestDecoder> ResolveDecoders(ApiMethodInfo method)
        {
            var param = method.Method.GetParameters();
            var decoderMap = new Dictionary<string, IRequestDecoder>(4);

            if (param.Length == 0)
            {
                decoderMap[MetaRequestFormatGet]
                    = decoderMap[MetaRequestFormatPost]
                    = decoderMap[MetaRequestFormatJson]
                    = decoderMap[string.Empty]
                    = EmptyParamMethodRequestDecoder.Instance;

                return decoderMap;
            }

            var paramInfoMap = method.ParamInfoMap;
            var methodParamStat = TypeHelper.GetMethodParamStat(method.Method);
            if (!methodParamStat.HasCoplexMember)
            {
                var inlineParamHttpParamDecoder = new InlineParamHttpParamDecoder(paramInfoMap);
                decoderMap[MetaRequestFormatGet] = inlineParamHttpParamDecoder;

                if (!methodParamStat.HasStream)
                {
                    decoderMap[MetaRequestFormatPost] = inlineParamHttpParamDecoder;
                    decoderMap[MetaRequestFormatJson] = new InlineParamJsonDecoder(paramInfoMap);
                }
            }
            else if (param.Length == 1)
            {
                var paramType = param[0].ParameterType;
                var paramTypeStat = TypeHelper.GetTypeMemberStat(paramType);

                if (paramTypeStat.HasStream)
                {
                    if (paramTypeStat.HasCoplexMember)
                    {
                        throw NoSupportedDecoderError(method.Method);
                    }

                    decoderMap[MetaRequestFormatGet] = new SingleObjectHttpParamDecoder(paramInfoMap);
                }
                else
                {
                    if (!paramTypeStat.HasCoplexMember)
                    {
                        decoderMap[MetaRequestFormatPost] = new SingleObjectHttpParamDecoder(paramInfoMap);
                    }

                    decoderMap[MetaRequestFormatJson] = new SingleObjectJsonDecoder(paramInfoMap);
                }
            }
            else // param.Length > 1 || methodParamStat.HasCoplexMember
            {
                if (methodParamStat.HasStream)
                {
                    throw NoSupportedDecoderError(method.Method);
                }

                decoderMap[MetaRequestFormatJson] = new InlineParamJsonDecoder(paramInfoMap);
            }

            // 设置默认的Decoder，优先级 get->json->post
            if (decoderMap.ContainsKey(MetaRequestFormatGet))
            {
                decoderMap[string.Empty] = decoderMap[MetaRequestFormatGet];
            }
            else if (decoderMap.ContainsKey(MetaRequestFormatJson))
            {
                decoderMap[string.Empty] = decoderMap[MetaRequestFormatJson];<commit_msg>bug fix：API方法只有一个POCO参数时只支持使用JSON方式请求。<commit_after>﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Text;
using System.Web;
using cmstar.Serialization.Json;

namespace cmstar.WebApi.Slim
{
    /// <summary>
    /// 提供Slim WebAPI的入口。继承此类以实现API的注册和使用。
    /// 这是一个抽象类。
    /// </summary>
    public abstract class SlimApiHttpHandler : ApiHttpHandlerBase
    {
        protected static string MetaParamMethodName = "~method";
        protected static string MetaParamFormat = "~format";
        protected static string MetaParamCallback = "~callback";

        protected const string MetaRequestFormatJson = "json";
        protected const string MetaRequestFormatPost = "post";
        protected const string MetaRequestFormatGet = "get";
        protected const string MetaResponseFormatPlain = "plain";
        private const int OutputBodyLimitLower = 1024;
        private const int OutputBodyLimitUpper = 65536;

        /// <summary>
        /// 获取指定的API方法所对应的参数解析器。
        /// </summary>
        /// <param name="method">包含API方法的有关信息。</param>
        /// <returns>key为解析器的名称，value为解析器实例。</returns>
        protected override IDictionary<string, IRequestDecoder> ResolveDecoders(ApiMethodInfo method)
        {
            var param = method.Method.GetParameters();
            var decoderMap = new Dictionary<string, IRequestDecoder>(4);

            if (param.Length == 0)
            {
                decoderMap[MetaRequestFormatGet]
                    = decoderMap[MetaRequestFormatPost]
                    = decoderMap[MetaRequestFormatJson]
                    = decoderMap[string.Empty]
                    = EmptyParamMethodRequestDecoder.Instance;

                return decoderMap;
            }

            var paramInfoMap = method.ParamInfoMap;
            var methodParamStat = TypeHelper.GetMethodParamStat(method.Method);
            if (!methodParamStat.HasCoplexMember)
            {
                var inlineParamHttpParamDecoder = new InlineParamHttpParamDecoder(paramInfoMap);
                decoderMap[MetaRequestFormatGet] = inlineParamHttpParamDecoder;

                if (!methodParamStat.HasStream)
                {
                    decoderMap[MetaRequestFormatPost] = inlineParamHttpParamDecoder;
                    decoderMap[MetaRequestFormatJson] = new InlineParamJsonDecoder(paramInfoMap);
                }
            }
            else if (param.Length == 1)
            {
                var paramType = param[0].ParameterType;
                var paramTypeStat = TypeHelper.GetTypeMemberStat(paramType);

                if (paramTypeStat.HasStream)
                {
                    if (paramTypeStat.HasCoplexMember)
                    {
                        throw NoSupportedDecoderError(method.Method);
                    }

                    decoderMap[MetaRequestFormatGet] = new SingleObjectHttpParamDecoder(paramInfoMap);
                }
                else
                {
                    if (!paramTypeStat.HasCoplexMember)
                    {
                        var singleObjectHttpParamDecoder = new SingleObjectHttpParamDecoder(paramInfoMap);
                        decoderMap[MetaRequestFormatGet] = singleObjectHttpParamDecoder;
                        decoderMap[MetaRequestFormatPost] = singleObjectHttpParamDecoder;
                    }

                    decoderMap[MetaRequestFormatJson] = new SingleObjectJsonDecoder(paramInfoMap);
                }
            }
            else // param.Length > 1 || methodParamStat.HasCoplexMember
            {
                if (methodParamStat.HasStream)
                {
                    throw NoSupportedDecoderError(method.Method);
                }

                decoderMap[MetaRequestFormatJson] = new InlineParamJsonDecoder(paramInfoMap);
            }

            // 设置默认的Decoder，优先级 get->json->post
            if (decoderMap.ContainsKey(MetaRequestFormatGet))
            {
                decoderMap[string.Empty] = decoderMap[MetaRequestFormatGet];
            }
            else if (decoderMap.ContainsKey(MetaRequestFormatJson))
            {
                decoderMap[string.Empty] = decoderMap[MetaRequestFormatJson];
{'fraction_non_alphanumeric': 0.07628243814121907, 'fraction_numerical': 0.003379601689800845, 'mean_word_length': 1.848401512547267, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 29, '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': '3510974', 'n_tokens_mistral': 2170, 'n_tokens_neox': 2029, 'n_words': 349}