Exemplo n.º 1
0
        protected void Application_BeginRequest(object sender, EventArgs e)
        {
            if (Application[StartupOK] == null)
            {
                Application.Lock();
                if (Application[StartupOK] == null)
                {
                    // Setup Resource Exchanger
                    ScrewTurn.Wiki.Exchanger.ResourceExchanger = new ScrewTurn.Wiki.ResourceExchanger();
                    ScrewTurn.Wiki.StartupTools.Startup();

                    // All is OK, proceed with normal startup operations
                    Application[StartupOK] = "OK";
                }
                Application.UnLock();
            }

            string physicalPath = null;

            try {
                physicalPath = HttpContext.Current.Request.PhysicalPath;
            }
            catch (ArgumentException) {
                // Illegal characters in path
                HttpContext.Current.Response.Redirect("~/PageNotFound.aspx");
                return;
            }

            string currentWiki = Tools.DetectCurrentWiki();

            string url = HttpContext.Current.Request.Url.ToString();

            foreach (RequestHandlerRegistryEntry handler in Host.Instance.GetRequestHandlers(currentWiki).Values)
            {
                if (handler.Methods.Any(m => StringComparer.OrdinalIgnoreCase.Compare(m, HttpContext.Current.Request.HttpMethod) == 0))
                {
                    Match match = handler.UrlRegex.Match(url);
                    if (match.Success)
                    {
                        try {
                            var plugin = Collectors.CollectorsBox.FormatterProviderCollector.GetProvider(handler.CallerType.FullName, currentWiki);
                            if (plugin != null && plugin.HandleRequest(HttpContext.Current, match))
                            {
                                HttpContext.Current.Response.End();
                                return;
                            }
                        }
                        catch (Exception ex) {
                            if (ex is ThreadAbortException)
                            {
                                continue;
                            }
                            if (ex.InnerException != null && ex.InnerException is ThreadAbortException)
                            {
                                continue;
                            }

                            LogError(ex);
                        }
                    }
                }
            }

            // Extract the physical page name, e.g. MainPage, Edit or Category
            string pageName = Path.GetFileNameWithoutExtension(physicalPath);
            // Exctract the extension, e.g. .ashx or .aspx
            string ext = (Path.GetExtension(HttpContext.Current.Request.PhysicalPath) + "").ToLowerInvariant();

            // Remove trailing dot, .ashx -> ashx
            if (ext.Length > 0)
            {
                ext = ext.Substring(1);
            }

            // IIS7+Integrated Pipeline handles all requests through the ASP.NET engine
            // All non-interesting files are not processed, such as GIF, CSS, etc.
            if (ext.Length == 0 || ext == "ashx" || ext == "aspx")
            {
                if (!Request.PhysicalPath.ToLowerInvariant().Contains("createmasterpassword.aspx"))
                {
                    if (Application[MasterPasswordOk] == null)
                    {
                        Application.Lock();
                        if (Application[MasterPasswordOk] == null)
                        {
                            // Setup Master Password
                            if (!String.IsNullOrEmpty(GlobalSettings.GetMasterPassword()))
                            {
                                Application[MasterPasswordOk] = "OK";
                            }
                        }
                        Application.UnLock();
                    }

                    if (Application[MasterPasswordOk] == null)
                    {
                        ScrewTurn.Wiki.UrlTools.Redirect("~/CreateMasterPassword.aspx");
                    }
                }
                else if (!string.IsNullOrEmpty(GlobalSettings.GetMasterPassword()))
                {
                    ScrewTurn.Wiki.UrlTools.RedirectHome(currentWiki);
                }
            }
            ScrewTurn.Wiki.UrlTools.RouteCurrentRequest();
        }