Example #1
0
        public object Clone()
        {
            HttpConfig config = new HttpConfig();

            config.CertificateFile     = this.CertificateFile;
            config.CertificatePassword = this.CertificatePassword;
            config.Debug            = this.Debug;
            config.DefaultPage      = this.DefaultPage;
            config.Encoding         = this.Encoding;
            config.Filters          = this.Filters;
            config.Host             = this.Host;
            config.MaxBodyLength    = this.MaxBodyLength;
            config.OutputStackTrace = this.OutputStackTrace;
            config.Port             = this.Port;
            config.SSL = this.SSL;
            config.StaticResourcePath = this.StaticResourcePath;
            config.StaticResurceType  = this.StaticResurceType;

            return(config);
        }
Example #2
0
        public static void ReadUrlPathAndExt(ReadOnlySpan <char> url, QueryString queryString, HttpRequest request, HttpConfig config)
        {
            bool urlIgnoreCase = config.UrlIgnoreCase;

            if (urlIgnoreCase)
            {
                request.BaseUrl = CharToLower(url);
            }
            else
            {
                request.BaseUrl = new string(url);
            }
            for (int i = url.Length - 1; i >= 0; i--)
            {
                if (url[i] == '.')
                {
                    if (urlIgnoreCase)
                    {
                        request.Ext = CharToLower(url.Slice(i + 1, url.Length - i - 1));
                    }
                    else
                    {
                        request.Ext = new string(url.Slice(i + 1, url.Length - i - 1));
                    }
                    continue;
                }
                if (url[i] == '/')
                {
                    if (urlIgnoreCase)
                    {
                        request.Path = CharToLower(url.Slice(0, i + 1));
                    }
                    else
                    {
                        request.Path = new string(url.Slice(0, i + 1));
                    }
                    return;
                }
            }
        }
        private void Register(HttpConfig config, Type controllerType, object controller, string rooturl, HttpApiServer server)
        {
            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)
                {
                    continue;
                }
                if (mi.GetCustomAttribute <NotActionAttribute>(false) != null)
                {
                    continue;
                }
                string        sourceUrl = rooturl + mi.Name;
                string        url       = sourceUrl.ToLower();
                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.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);
            }
        }
Example #4
0
 public HttpPacket(IBodySerializer bodySerializer, HttpConfig serverConfig, IDataFrameSerializer dataPacketSerializer)
 {
     Serializer            = bodySerializer;
     mServerConfig         = serverConfig;
     mDataPacketSerializer = dataPacketSerializer;
 }
        private void Register(HttpConfig config, Type controllerType, object controller, string rooturl, HttpApiServer server)
        {
            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;
                if (server.ServerConfig.UrlIgnoreCase)
                {
                    url = sourceUrl.ToLower();
                }
                RouteTemplateAttribute ra = mi.GetCustomAttribute <RouteTemplateAttribute>(false);
                if (ra != null)
                {
                    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.DataConvert = actionConvert;
                handler.Route       = ra;
                if (mi.GetCustomAttribute <PostAttribute>(false) != null)
                {
                    handler.Method = "POST";
                }
                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);
            }
        }
Example #6
0
 public HttpPacket(IBodySerializer bodySerializer, HttpConfig serverConfig)
 {
     Serializer    = bodySerializer;
     mServerConfig = serverConfig;
 }
Example #7
0
        private void Register(HttpConfig 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 += "/";
                }
            }
            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;
                }
                bool noconvert = false;
                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.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.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;
                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}]");
            }
        }
Example #8
0
 public HttpPacket(HttpConfig serverConfig, IDataFrameSerializer dataPacketSerializer)
 {
     mServerConfig         = serverConfig;
     mDataPacketSerializer = dataPacketSerializer;
 }