예제 #1
0
        /// <summary>
        /// 添加路由及对应控制器
        /// </summary>
        /// <param name="path">路由</param>
        /// <param name="controller">控制器</param>
        /// <param name="view">视图目录</param>
        /// <param name="isInterceptor">是否拦截(默认true拦截)</param>
        public void Add(string path, Controller controller, string view, bool isInterceptor = true)
        {
            if (path == "")
            {
                path = "/";
            }
            else
            {
                path = "/" + path + "/";
            }
            if (!String.IsNullOrEmpty(Config.AppName.Trim()))
            {
                path = "/" + Config.AppName.Trim() + path;
            }

            string    s  = controller.ToString();
            RouleItem it = new RouleItem();

            it.action        = s;
            it.path          = path;
            it.view          = view;
            it.isInterceptor = isInterceptor;
            items.Add(it);
            items = items.OrderByDescending(c => c.path).ToList();
        }
예제 #2
0
        public void RunRoule(HttpListenerContext context, RouleMap roule, Interceptor interceptor_)
        {
            if (context == null)
            {
                Log.Print("链接还未建立");
                return;
            }
            try
            {
                string path = context.Request.RawUrl.Trim();
                path = Uri.UnescapeDataString(path);
                //  Log.Print(path);
                string url        = path;
                string methodname = "";
                string url_suffix = "";
                if (!check_directory_permission(path))
                {
                    Error404(context, url);
                    return;
                }

                if (isMimeType(path) == null)
                {
                    string[] m = path.Split('/');

                    RouleItem action = null;
                    action = roule.getRoule(path, ref methodname, ref url_suffix);

                    if (action != null)
                    {
                        if (methodname.ToLower().Equals("index") && url[url.Length - 1] != '/')
                        {
                            context.Response.StatusCode       = 301;
                            context.Response.RedirectLocation = url + "/";
                            context.Response.AddHeader("Cache-Control", "no-cache,no-store");
                            Write(context, "");
                            return;
                        }

                        HttpSession Session       = null;
                        bool        isInterceptor = false;
                        if (Config.Session_open)
                        {
                            Session = new HttpSession(context);
                        }
                        if (action.isInterceptor)
                        {
                            object     Interceptor_obj    = assembly.CreateInstance(interceptor_.ToString()); // 创建类的实例
                            Type       Interceptor_type   = Interceptor_obj.GetType();
                            MethodInfo Interceptor_method = Interceptor_type.GetMethod("Init", new Type[] { typeof(HttpListenerContext), typeof(HttpSession) });
                            object[]   parameters1        = new object[] { context, Session };
                            Interceptor_method.Invoke(Interceptor_obj, parameters1);
                            Interceptor_method = Interceptor_type.GetMethod("isInterceptor");
                            isInterceptor      = (bool)Interceptor_method.Invoke(Interceptor_obj, null);
                        }

                        if (!isInterceptor)
                        {
                            object     obj         = assembly.CreateInstance(action.action); // 创建类的实例
                            Type       type        = obj.GetType();
                            MethodInfo method_main = type.GetMethod(methodname);
                            if (method_main == null)
                            {
                                method_main = type.GetMethod(methodname.ToLower());
                            }
                            MethodInfo method     = type.GetMethod("Init", new Type[] { typeof(HttpListenerContext), typeof(HttpSession), typeof(string), typeof(string), typeof(Compress) });
                            object[]   parameters = new object[] { context, Session, action.view, url_suffix, compress };

                            try
                            {
                                method.Invoke(obj, parameters);
                            }
                            catch (Exception e)
                            {
                                Log.Print("[Init]方法执行异常:" + path + "|" + e.Message + "|" + method.ToString());
                                Error404(context, url);
                            }

                            if (method_main != null)
                            {
                                object[] parameters1 = null;
                                try
                                {
                                    parameters1 = formatParam(context, method_main, url_suffix);
                                }
                                catch (Exception e)
                                {
                                    Log.Print("[formatParam]异常:" + path + "|" + e.Message);
                                    parameters1 = null;
                                }
                                try
                                {
                                    method_main.Invoke(obj, parameters1);
                                    return;
                                }
                                catch (Exception e)
                                {
                                    Log.Print("方法执行异常:" + path + "|" + e.Message + "|" + method_main.ToString());
                                }


                                if (context.Response.ContentLength64 == 0)
                                {
                                    Error404(context, url);
                                }
                            }
                            else
                            {
                                Error404(context, url);
                            }
                        }
                    }
                    else
                    {
                        Error404(context, url);
                    }
                }
                else
                {
                    if (Config.open_cache && !Config.open_debug)
                    {
                        DateTime n             = DateTime.Now;
                        string   Modified_time = n.ToUniversalTime().ToString("r");
                        string   Expires_time  = n.AddHours(24).ToUniversalTime().ToString("r");
                        context.Response.AddHeader("Cache-Control", "max-age=" + Config.cache_max_age);
                        context.Response.AddHeader("Pragma", "Pragma");
                        context.Response.AddHeader("Last-Modified", Modified_time);
                        context.Response.AddHeader("Expires", Expires_time);
                    }
                    else
                    {
                        context.Response.AddHeader("Cache-Control", "no-cache,no-store");
                    }
                    string path2 = path;
                    if (!string.IsNullOrEmpty(Config.AppName))
                    {
                        string tmp = "/" + Config.AppName + "/";
                        if (!path.Substring(0, tmp.Length).Equals(tmp) && !path.Equals("/favicon.ico"))
                        {
                            Error404(context, url);
                            return;
                        }
                        else
                        {
                            path2 = path.Replace(tmp, "/");
                        }
                    }
                    string root = "";
                    if (!Config.WebRoot.Equals(""))
                    {
                        root = "/" + Config.WebRoot;
                    }

                    string htmlfile = System.IO.Directory.GetCurrentDirectory() + root + path2;
                    if (htmlfile.IndexOf('?') > -1)
                    {
                        htmlfile = htmlfile.Substring(0, htmlfile.IndexOf('?'));
                    }
                    if (File.Exists(htmlfile))
                    {
                        string extension = System.IO.Path.GetExtension(htmlfile).Replace(".", "");

                        byte[] data = File.ReadAllBytes(htmlfile);
                        string mime = getMimeType(extension);
                        context.Response.ContentType = mime;
                        context.Response.StatusCode  = 200;
                        WriteByte(context, data);
                    }
                    else
                    {
                        Error404(context, path);
                    }
                }
            }
            catch (Exception e)
            {
                Log.Print("路由方法[RunRoule]:" + e.Message);
                Error500(context, e.Message);
            }
        }