Пример #1
0
        public void RuleCheck()
        {
            HttpProxyAttribute attr = clientInterface.GetCustomAttribute <HttpProxyAttribute>();

            if (attr == null)
            {
                throw new Exception();
            }

            var filters = clientInterface.GetCustomAttributes <ProxyFilterAttribute>();

            var methods = clientInterface.GetMethods();

            foreach (var m in methods)
            {
                var rule = ProxyRule.Verificate(m, attr, filters);
                if (rule.IsValid == false)
                {
                    throw new Exception(rule.ErrorMessage);
                }
                contexts.Add(rule.ProxyContext);
            }
        }
Пример #2
0
        public static ProxyRule Verificate(MethodInfo method, HttpProxyAttribute classAttr, IEnumerable <ProxyFilterAttribute> classFilters)
        {
            ProxyRule rule = new ProxyRule();

            HttpProxyContext.Builder contextBuilder = new HttpProxyContext.Builder();
            ResquestMappingAttribute methodAttr     = method.GetCustomAttribute <ResquestMappingAttribute>();

            if (methodAttr == null)
            {
                methodAttr = new ResquestMappingAttribute();
            }

            ///TODO;拼接url

            SimpleUrl url;

            if (!string.IsNullOrEmpty(classAttr.Prefix))
            {
                url = new SimpleUrl(classAttr.Prefix);
                url.AppendPath(methodAttr.Path);
            }
            else
            {
                url = new SimpleUrl(methodAttr.Path);
            }

            if (string.IsNullOrEmpty(url.ToUrl()))
            {
                rule.IsValid      = false;
                rule.ErrorMessage = $"方法{method.DeclaringType.FullName }.{method.Name}的Url无效";
                return(rule);
            }

            //
            if (methodAttr.ContentType != "application/x-www-form-urlencoded" &&
                methodAttr.ContentType != "application/json")
            {
                rule.IsValid      = false;
                rule.ErrorMessage = $"方法{method.DeclaringType.FullName }.{method.Name}的ContentType无效";
                return(rule);
            }

            contextBuilder.SetAccept(methodAttr.Accept)
            .SetContentType(methodAttr.ContentType)
            .AddFilters(classFilters)
            .SetHttpMethod(methodAttr.Method)
            .SetMethodInfo(method)
            .SetTimeOut(methodAttr.TimeOut)
            .SetUrl(url);

            //Parameter
            List <ParameterInfo> queryArgs = new List <ParameterInfo>();
            List <ParameterInfo> pathArgs  = new List <ParameterInfo>();
            List <ParameterInfo> bodyArgs  = new List <ParameterInfo>();

            List <HttpArgument> arguments = new List <HttpArgument>();
            var ps = method.GetParameters();

            foreach (var p in ps)
            {
                HttpArgument          argument;
                HttpArgumentAttribute argAttr = p.GetCustomAttribute <HttpArgumentAttribute>();


                if (p.IsOut)
                {
                    rule.IsValid      = false;
                    rule.ErrorMessage = $"方法{method.DeclaringType.FullName }.{method.Name}参数{p.Name}有out";
                    return(rule);
                }
                if (p.ParameterType.IsByRef)
                {
                    rule.IsValid      = false;
                    rule.ErrorMessage = $"方法{method.DeclaringType.FullName }.{method.Name}参数{p.Name}有ref";
                    return(rule);
                }
                if (p.GetCustomAttributes(typeof(ParamArrayAttribute), false).Count() > 0)
                {
                    rule.IsValid      = false;
                    rule.ErrorMessage = $"方法{method.DeclaringType.FullName }.{method.Name}参数{p.Name}有params";
                    return(rule);
                }


                if (argAttr == null)
                {
                    queryArgs.Add(p);
                    argument = new HttpArgument(p);
                    arguments.Add(argument);
                    continue;
                }

                argument = new HttpArgument(p, argAttr);
                arguments.Add(argument);

                switch (argAttr.ArgType)
                {
                case HttpArgType.QueryString:
                    queryArgs.Add(p);
                    break;

                case HttpArgType.Path:
                    pathArgs.Add(p);
                    break;

                case HttpArgType.Body:
                    bodyArgs.Add(p);
                    break;

                default:
                    queryArgs.Add(p);
                    break;
                }
            }


            if (methodAttr.Method == HttpMethod.POST)
            {
                if ("application/x-www-form-urlencoded" == methodAttr.ContentType)
                {
                    if (bodyArgs.Count > 1)
                    {
                        rule.IsValid      = false;
                        rule.ErrorMessage = $"方法{method.DeclaringType.FullName }.{method.Name}无ContentType与参数不匹配";
                        return(rule);
                    }
                }
            }
            contextBuilder.SetArguments(arguments);
            //Filter
            var filters = method.GetCustomAttributes <ProxyFilterAttribute>();

            contextBuilder.AddFilters(filters);

            rule.IsValid      = true;
            rule.ProxyContext = contextBuilder.Build();
            return(rule);
        }