コード例 #1
0
        private void Register(HttpOptions config, Type controllerType, object controller, string rooturl, HttpApiServer server, ControllerAttribute ca,
                              Action <EventActionRegistingArgs> callback)
        {
            DataConvertAttribute controllerDataConvert      = controllerType.GetCustomAttribute <DataConvertAttribute>(false);
            OptionsAttribute     controllerOptionsAttribute = controllerType.GetCustomAttribute <OptionsAttribute>(false);

            if (string.IsNullOrEmpty(rooturl))
            {
                rooturl = "/";
            }
            else
            {
                if (rooturl[0] != '/')
                {
                    rooturl = "/" + rooturl;
                }
                if (rooturl[rooturl.Length - 1] != '/')
                {
                    rooturl += "/";
                }
            }
            RequestMaxRPS          control_maxRPS = controllerType.GetCustomAttribute <RequestMaxRPS>();
            List <FilterAttribute> filters        = new List <FilterAttribute>();

            if (!ca.SkipPublicFilter)
            {
                filters.AddRange(config.Filters);
            }
            IEnumerable <FilterAttribute> fas = controllerType.GetCustomAttributes <FilterAttribute>(false);

            filters.AddRange(fas);
            LoadBaseFilter(controllerType, filters);
            IEnumerable <SkipFilterAttribute> skipfilters = controllerType.GetCustomAttributes <SkipFilterAttribute>(false);

            foreach (SkipFilterAttribute item in skipfilters)
            {
                RemoveFilter(filters, item.Types);
            }
            object obj = controller;

            if (obj is IController)
            {
                string path = System.IO.Path.GetDirectoryName(controllerType.Assembly.Location) + System.IO.Path.DirectorySeparatorChar;
                ((IController)obj).Init(server, path);
                if (server.EnableLog(EventArgs.LogType.Info))
                {
                    server.Log(EventArgs.LogType.Info, $"init {controllerType} controller path {path}");
                }
            }
            foreach (MethodInfo mi in controllerType.GetMethods(BindingFlags.Instance | BindingFlags.Public))
            {
                if (string.Compare("Equals", mi.Name, true) == 0 ||
                    string.Compare("GetHashCode", mi.Name, true) == 0 ||
                    string.Compare("GetType", mi.Name, true) == 0 ||
                    string.Compare("ToString", mi.Name, true) == 0 || mi.Name.IndexOf("set_") >= 0 ||
                    mi.Name.IndexOf("get_") >= 0)
                {
                    continue;
                }
                if (mi.GetCustomAttribute <NotActionAttribute>(false) != null)
                {
                    continue;
                }
                bool          noconvert = false;
                RequestMaxRPS maxRPS    = mi.GetCustomAttribute <RequestMaxRPS>();
                if (maxRPS == null)
                {
                    maxRPS = control_maxRPS;
                }
                DataConvertAttribute actionConvert          = mi.GetCustomAttribute <DataConvertAttribute>();
                OptionsAttribute     methodOptionsAttribute = mi.GetCustomAttribute <OptionsAttribute>();
                if (mi.GetCustomAttribute <NoDataConvertAttribute>(false) != null)
                {
                    noconvert     = true;
                    actionConvert = null;
                }
                else
                {
                    if (actionConvert == null)
                    {
                        actionConvert = controllerDataConvert;
                    }
                }
                string       sourceUrl = rooturl + mi.Name;
                string       url       = sourceUrl;
                string       method    = HttpParse.GET_TAG + "/" + HttpParse.POST_TAG;
                string       route     = null;
                GetAttribute get       = mi.GetCustomAttribute <GetAttribute>(false);
                if (get != null)
                {
                    method = HttpParse.GET_TAG;
                    route  = get.Route;
                }
                PostAttribute post = mi.GetCustomAttribute <PostAttribute>(false);
                if (post != null)
                {
                    method = HttpParse.POST_TAG;
                    route  = post.Route;
                }
                DelAttribute del = mi.GetCustomAttribute <DelAttribute>(false);
                if (del != null)
                {
                    method = HttpParse.DELETE_TAG;
                    route  = del.Route;
                }
                PutAttribute put = mi.GetCustomAttribute <PutAttribute>(false);
                if (put != null)
                {
                    method = HttpParse.PUT_TAG;
                    route  = put.Route;
                }

                //if (server.Options.UrlIgnoreCase)
                //{
                //    url = sourceUrl.ToLower();
                //}
                RouteTemplateAttribute ra = null;
                if (!string.IsNullOrEmpty(route))
                {
                    ra = new RouteTemplateAttribute(route);
                    string reurl;
                    if (route[0] == '/')
                    {
                        reurl = ra.Analysis(null);
                    }
                    else if (route[0] == '{')
                    {
                        reurl = ra.Analysis(url + "/");
                    }
                    else
                    {
                        reurl = ra.Analysis(route.IndexOf('/', 0) > 0 ? rooturl : url + "/");
                    }
                    if (reurl == null)
                    {
                        if (route[0] == '/')
                        {
                            reurl = route;
                        }
                        else
                        {
                            reurl = rooturl + route;
                        }
                    }
                    server.UrlRewrite.Add(null, reurl, url);
                }
                ActionHandler handler = GetAction(url);
                if (handler != null)
                {
                    if (server.EnableLog(EventArgs.LogType.Error))
                    {
                        server.Log(EventArgs.LogType.Error, $"{url} already exists! replaced with {controllerType.Name}.{mi.Name}!");
                    }
                }
                handler          = new ActionHandler(obj, mi, this.Server);
                handler.AuthMark = controllerType.GetCustomAttribute <AuthMarkAttribute>(false);
                var authmark = mi.GetCustomAttribute <AuthMarkAttribute>(false);
                if (authmark != null)
                {
                    handler.AuthMark = authmark;
                }
                if (handler.AuthMark == null)
                {
                    handler.AuthMark = new AuthMarkAttribute(AuthMarkType.None);
                }
                if (mi.ReturnType == typeof(Task) || mi.ReturnType.BaseType == typeof(Task))
                {
                    handler.Async = true;
                    PropertyInfo pi = mi.ReturnType.GetProperty("Result", BindingFlags.Public | BindingFlags.Instance);
                    if (pi != null)
                    {
                        handler.PropertyHandler = new PropertyHandler(pi);
                    }
                }
                handler.Path = rooturl;
                if (methodOptionsAttribute == null)
                {
                    handler.OptionsAttribute = controllerOptionsAttribute;
                }
                else
                {
                    handler.OptionsAttribute = methodOptionsAttribute;
                }
                if (handler.OptionsAttribute == null && !ca.SkipPublicFilter)
                {
                    handler.OptionsAttribute = handler.HttpApiServer.Options.CrossDomain;
                }
                handler.ThreadQueue = controllerType.GetCustomAttribute <ThreadQueueAttribute>(false);
                var queue = mi.GetCustomAttribute <ThreadQueueAttribute>(false);
                if (queue != null)
                {
                    handler.ThreadQueue = queue;
                }
                handler.NoConvert     = noconvert;
                handler.InstanceType  = ca.InstanceType;
                handler.DataConverter = actionConvert;
                handler.Route         = ra;
                handler.Method        = method;
                handler.SourceUrl     = sourceUrl;
                handler.Filters.AddRange(filters);
                fas = mi.GetCustomAttributes <FilterAttribute>(false);
                handler.Filters.AddRange(fas);
                handler.Url = url;
                if (maxRPS != null)
                {
                    handler.MaxRPS = maxRPS.Value;
                }
                int rpsSetting = server.Options.GetActionMaxrps(handler.SourceUrl);
                if (rpsSetting > 0)
                {
                    handler.MaxRPS = rpsSetting;
                }
                skipfilters = mi.GetCustomAttributes <SkipFilterAttribute>(false);
                foreach (SkipFilterAttribute item in skipfilters)
                {
                    RemoveFilter(handler.Filters, item.Types);
                }
                EventActionRegistingArgs registing = new EventActionRegistingArgs();
                registing.Url     = url;
                registing.Handler = handler;
                registing.Cancel  = false;
                registing.Server  = this.Server;
                callback?.Invoke(registing);
                if (!registing.Cancel)
                {
                    Server.OnActionRegisting(registing);
                }

                if (!registing.Cancel)
                {
                    AddHandlers(url, handler);
                    server.ActionSettings(handler);
                    if (server.EnableLog(EventArgs.LogType.Info))
                    {
                        server.Log(EventArgs.LogType.Info, $"register { controllerType.Name}.{mi.Name} to [{handler.Method}:{url}]");
                    }
                }
                else
                {
                    if (server.EnableLog(EventArgs.LogType.Info))
                    {
                        server.Log(EventArgs.LogType.Info, $"register { controllerType.Name}.{mi.Name} cancel ");
                    }
                }
            }
        }
コード例 #2
0
        private void Register(HttpOptions config, Type controllerType, object controller, string rooturl, HttpApiServer server, ControllerAttribute ca)
        {
            DataConvertAttribute controllerDataConvert      = controllerType.GetCustomAttribute <DataConvertAttribute>(false);
            OptionsAttribute     controllerOptionsAttribute = controllerType.GetCustomAttribute <OptionsAttribute>(false);

            if (string.IsNullOrEmpty(rooturl))
            {
                rooturl = "/";
            }
            else
            {
                if (rooturl[0] != '/')
                {
                    rooturl = "/" + rooturl;
                }
                if (rooturl[rooturl.Length - 1] != '/')
                {
                    rooturl += "/";
                }
            }
            RequestMaxRPS          control_maxRPS = controllerType.GetCustomAttribute <RequestMaxRPS>();
            List <FilterAttribute> filters        = new List <FilterAttribute>();

            filters.AddRange(config.Filters);
            IEnumerable <FilterAttribute> fas = controllerType.GetCustomAttributes <FilterAttribute>(false);

            filters.AddRange(fas);
            IEnumerable <SkipFilterAttribute> skipfilters = controllerType.GetCustomAttributes <SkipFilterAttribute>(false);

            foreach (SkipFilterAttribute item in skipfilters)
            {
                RemoveFilter(filters, item.Types);
            }
            object obj = controller;

            if (obj is IController)
            {
                string path = System.IO.Path.GetDirectoryName(controllerType.Assembly.Location) + System.IO.Path.DirectorySeparatorChar;
                ((IController)obj).Init(server, path);
                server.Log(EventArgs.LogType.Info, $"init {controllerType} controller path {path}");
            }
            foreach (MethodInfo mi in controllerType.GetMethods(BindingFlags.Instance | BindingFlags.Public))
            {
                if (string.Compare("Equals", mi.Name, true) == 0 ||
                    string.Compare("GetHashCode", mi.Name, true) == 0 ||
                    string.Compare("GetType", mi.Name, true) == 0 ||
                    string.Compare("ToString", mi.Name, true) == 0 || mi.Name.IndexOf("set_") >= 0 ||
                    mi.Name.IndexOf("get_") >= 0)
                {
                    continue;
                }
                if (mi.GetCustomAttribute <NotActionAttribute>(false) != null)
                {
                    continue;
                }
                bool          noconvert = false;
                RequestMaxRPS maxRPS    = mi.GetCustomAttribute <RequestMaxRPS>();
                if (maxRPS == null)
                {
                    maxRPS = control_maxRPS;
                }
                DataConvertAttribute actionConvert          = mi.GetCustomAttribute <DataConvertAttribute>();
                OptionsAttribute     methodOptionsAttribute = mi.GetCustomAttribute <OptionsAttribute>();
                if (mi.GetCustomAttribute <NoDataConvertAttribute>(false) != null)
                {
                    noconvert     = true;
                    actionConvert = null;
                }
                else
                {
                    if (actionConvert == null)
                    {
                        actionConvert = controllerDataConvert;
                    }
                }
                string       sourceUrl = rooturl + mi.Name;
                string       url       = sourceUrl;
                string       method    = HttpParse.GET_TAG;
                string       route     = null;
                GetAttribute get       = mi.GetCustomAttribute <GetAttribute>(false);
                if (get != null)
                {
                    method = HttpParse.GET_TAG;
                    route  = get.Route;
                }
                PostAttribute post = mi.GetCustomAttribute <PostAttribute>(false);
                if (post != null)
                {
                    method = HttpParse.POST_TAG;
                    route  = post.Route;
                }
                DelAttribute del = mi.GetCustomAttribute <DelAttribute>(false);
                if (del != null)
                {
                    method = HttpParse.DELETE_TAG;
                    route  = del.Route;
                }
                PutAttribute put = mi.GetCustomAttribute <PutAttribute>(false);
                if (put != null)
                {
                    method = HttpParse.PUT_TAG;
                    route  = put.Route;
                }

                if (server.Options.UrlIgnoreCase)
                {
                    url = sourceUrl.ToLower();
                }
                RouteTemplateAttribute ra = null;
                if (!string.IsNullOrEmpty(route))
                {
                    ra = new RouteTemplateAttribute(route);
                    string reurl = ra.Analysis(url);
                    if (reurl != null)
                    {
                        server.UrlRewrite.Add(reurl, url);
                    }
                }
                ActionHandler handler = GetAction(url);
                if (handler != null)
                {
                    server.Log(EventArgs.LogType.Warring, "{0} already exists!replaced with {1}.{2}!", url, controllerType.Name,
                               mi.Name);
                }

                handler = new ActionHandler(obj, mi);
                if (mi.ReturnType == typeof(Task) || mi.ReturnType.BaseType == typeof(Task))
                {
                    handler.Async = true;
                    PropertyInfo pi = mi.ReturnType.GetProperty("Result", BindingFlags.Public | BindingFlags.Instance);
                    if (pi != null)
                    {
                        handler.PropertyHandler = new PropertyHandler(pi);
                    }
                }
                handler.Path = rooturl;
                if (methodOptionsAttribute == null)
                {
                    handler.OptionsAttribute = controllerOptionsAttribute;
                }
                else
                {
                    handler.OptionsAttribute = methodOptionsAttribute;
                }
                handler.NoConvert      = noconvert;
                handler.SingleInstance = ca.SingleInstance;
                handler.DataConvert    = actionConvert;
                handler.Route          = ra;
                handler.Method         = method;
                handler.SourceUrl      = sourceUrl;
                handler.Filters.AddRange(filters);
                fas = mi.GetCustomAttributes <FilterAttribute>(false);
                handler.Filters.AddRange(fas);
                handler.Url = url;
                if (maxRPS != null)
                {
                    handler.MaxRPS = maxRPS.Value;
                }
                int rpsSetting = server.Options.GetActionMaxrps(handler.SourceUrl);
                if (rpsSetting > 0)
                {
                    handler.MaxRPS = rpsSetting;
                }
                skipfilters = mi.GetCustomAttributes <SkipFilterAttribute>(false);
                foreach (SkipFilterAttribute item in skipfilters)
                {
                    RemoveFilter(handler.Filters, item.Types);
                }
                AddHandlers(url, handler);
                server.Log(EventArgs.LogType.Info, $"register { controllerType.Name}.{mi.Name} to [{handler.Method}:{url}]");
            }
        }
コード例 #3
0
        private void Register(HttpConfig config, Type controllerType, object controller, string rooturl, HttpApiServer server, ControllerAttribute ca)
        {
            DataConvertAttribute controllerDataConvert = controllerType.GetCustomAttribute <DataConvertAttribute>(false);

            if (string.IsNullOrEmpty(rooturl))
            {
                rooturl = "/";
            }
            else
            {
                if (rooturl[0] != '/')
                {
                    rooturl = "/" + rooturl;
                }
                if (rooturl[rooturl.Length - 1] != '/')
                {
                    rooturl += "/";
                }
            }
            List <FilterAttribute> filters = new List <FilterAttribute>();

            filters.AddRange(config.Filters);
            IEnumerable <FilterAttribute> fas = controllerType.GetCustomAttributes <FilterAttribute>(false);

            filters.AddRange(fas);
            IEnumerable <SkipFilterAttribute> skipfilters = controllerType.GetCustomAttributes <SkipFilterAttribute>(false);

            foreach (SkipFilterAttribute item in skipfilters)
            {
                RemoveFilter(filters, item.Types);
            }
            object obj = controller;

            if (obj is IController)
            {
                ((IController)obj).Init(server);
            }
            foreach (MethodInfo mi in controllerType.GetMethods(BindingFlags.Instance | BindingFlags.Public))
            {
                if (string.Compare("Equals", mi.Name, true) == 0 ||
                    string.Compare("GetHashCode", mi.Name, true) == 0 ||
                    string.Compare("GetType", mi.Name, true) == 0 ||
                    string.Compare("ToString", mi.Name, true) == 0 || mi.Name.IndexOf("set_") >= 0 ||
                    mi.Name.IndexOf("get_") >= 0)
                {
                    continue;
                }
                if (mi.GetCustomAttribute <NotActionAttribute>(false) != null)
                {
                    continue;
                }
                DataConvertAttribute actionConvert = mi.GetCustomAttribute <DataConvertAttribute>();
                if (mi.GetCustomAttribute <NoDataConvertAttribute>(false) != null)
                {
                    actionConvert = null;
                }
                else
                {
                    if (actionConvert == null)
                    {
                        actionConvert = controllerDataConvert;
                    }
                    if (actionConvert == null)
                    {
                        actionConvert = new JsonDataConvertAttribute();
                    }
                }

                string       sourceUrl = rooturl + mi.Name;
                string       url       = sourceUrl;
                string       method    = HttpParse.GET_TAG;
                string       route     = null;
                GetAttribute get       = mi.GetCustomAttribute <GetAttribute>(false);
                if (get != null)
                {
                    method = HttpParse.GET_TAG;
                    route  = get.Route;
                }
                PostAttribute post = mi.GetCustomAttribute <PostAttribute>(false);
                if (post != null)
                {
                    method = HttpParse.POST_TAG;
                    route  = post.Route;
                }
                DelAttribute del = mi.GetCustomAttribute <DelAttribute>(false);
                if (del != null)
                {
                    method = HttpParse.DELETE_TAG;
                    route  = del.Route;
                }
                PutAttribute put = mi.GetCustomAttribute <PutAttribute>(false);
                if (put != null)
                {
                    method = HttpParse.PUT_TAG;
                    route  = put.Route;
                }

                if (server.ServerConfig.UrlIgnoreCase)
                {
                    url = sourceUrl.ToLower();
                }
                RouteTemplateAttribute ra = null;
                if (!string.IsNullOrEmpty(route))
                {
                    ra = new RouteTemplateAttribute(route);
                    string reurl = ra.Analysis(url);
                    if (reurl != null)
                    {
                        server.UrlRewrite.Add(reurl, url);
                    }
                }
                ActionHandler handler = GetAction(url);
                if (handler != null)
                {
                    server.Log(EventArgs.LogType.Error, "{0} already exists!duplicate definition {1}.{2}!", url, controllerType.Name,
                               mi.Name);
                    continue;
                }
                handler = new ActionHandler(obj, mi);
                handler.SingleInstance = ca.SingleInstance;
                handler.DataConvert    = actionConvert;
                handler.Route          = ra;
                handler.Method         = method;
                handler.SourceUrl      = sourceUrl;
                handler.Filters.AddRange(filters);
                fas = mi.GetCustomAttributes <FilterAttribute>(false);
                handler.Filters.AddRange(fas);
                skipfilters = mi.GetCustomAttributes <SkipFilterAttribute>(false);
                foreach (SkipFilterAttribute item in skipfilters)
                {
                    RemoveFilter(handler.Filters, item.Types);
                }
                mMethods[url] = handler;
                server.Log(EventArgs.LogType.Info, "register {0}.{1} to {2}", controllerType.Name, mi.Name, url);
            }
        }