/// <summary> /// Registers all of the hostnames configured in the database. /// </summary> public static void RegisterDefaultHostNames() { // Make sure we don't try to register the host names before // the database has been installed. if (SysParam.GetByName("SITE_VERSION") != null) { if (hostNames == null) { hostNames = new Dictionary <string, Entities.SiteTree>(); } HostNames.Clear(); // We need to check version so we don't try to access the column sitetree_hostnames // before it's been created in the database. if (Data.Database.InstalledVersion > 26) { using (var db = new DataContext()) { var sites = db.SiteTrees.ToList(); foreach (var site in sites) { if (HttpContext.Current != null) { Page.InvalidateStartpage(site.Id); } if (!String.IsNullOrEmpty(site.HostNames)) { var hostnames = site.HostNames.Split(new char[] { ',' }); foreach (var host in hostnames) { if (HostNames.ContainsKey(host)) { throw new Exception("Duplicates of the hostname [" + host + "] was found configured"); } HostNames.Add(host.ToLower(), site); } } if (site.Id == Config.DefaultSiteTreeId) { DefaultSite = site; } } } } } }
/// <summary> /// Handles the URL Rewriting for the application /// </summary> /// <param name="context">Http context</param> public static void BeginRequest(HttpContext context) { try { string path = context.Request.Path.Substring(context.Request.ApplicationPath.Length > 1 ? context.Request.ApplicationPath.Length : 0); string[] args = path.Split(new char[] { '/' }).Subset(1); if (args.Length > 0) { int pos = 0; // Ensure database if (args[0] == "" && SysParam.GetByName("SITE_VERSION") == null) { context.Response.Redirect("~/manager", false); context.Response.EndClean(); } // Check for culture prefix if (Cultures.ContainsKey(args[0])) { System.Threading.Thread.CurrentThread.CurrentCulture = System.Threading.Thread.CurrentThread.CurrentUICulture = Cultures[args[0]]; pos = 1; } else { var def = (GlobalizationSection)WebConfigurationManager.GetSection("system.web/globalization"); if (def != null && !String.IsNullOrWhiteSpace(def.Culture) && !def.Culture.StartsWith("auto:")) { System.Threading.Thread.CurrentThread.CurrentCulture = System.Threading.Thread.CurrentThread.CurrentUICulture = new CultureInfo(def.UICulture); } } // Check for hostname extension. This feature can't be combined with culture prefixes if (pos == 0) { var segments = context.Request.RawUrl.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries); if (segments.Length > 0) { var hostExt = context.Request.Url.Host + "/" + segments[0]; if (HostNames.ContainsKey(hostExt)) { RequestedSite = new ExtendedHostName() { HostName = context.Request.Url.Host, Extension = segments[0], SiteTree = HostNames[hostExt] }; if (segments[0] == args[0]) { pos = 1; // If this was the last argument, add an empty one if (args.Length == 1) { args = args.Concat(new string[] { "" }).ToArray(); } } } } } var handled = false; // Find the correct request handler foreach (var hr in Application.Current.Handlers) { if (hr.UrlPrefix.ToLower() == args[pos].ToLower()) { if (hr.Id != "PERMALINK" || !Config.PrefixlessPermalinks) { // Don't execute permalink routing in passive mode if ((hr.Id != "PERMALINK" && hr.Id != "STARTPAGE") || !Config.PassiveMode) { // Execute the handler hr.Handler.HandleRequest(context, args.Subset(pos + 1)); handled = true; break; } } } } if (!handled && args[pos].ToLower() == "res.ashx") { Application.Current.Resources.HandleRequest(context, args.Subset(pos + 1)); handled = true; } // If no handler was found and we are using prefixless permalinks, // route traffic to the permalink handler. if (!Config.PassiveMode) { if (!handled && Config.PrefixlessPermalinks && args[pos].ToLower() != "manager" && String.IsNullOrEmpty(context.Request["permalink"])) { if (Permalink.GetByName(Config.SiteTreeNamespaceId, args[pos]) != null || Permalink.GetByName(Config.DefaultNamespaceId, args[pos]) != null) { var handler = Application.Current.Handlers["PERMALINK"]; handler.HandleRequest(context, args.Subset(pos)); } } } } } catch (ThreadAbortException) { // We simply swallow this exception as we don't want unhandled // exceptions flying around causing the app pool to die. } catch (Exception e) { // One catch to rule them all, and in the log file bind them. Application.Current.LogProvider.Error("WebPiranha.BeginRequest", "Unhandled exception", e); context.Response.StatusCode = 500; context.Response.EndClean(); } }