Example #1
0
        /// <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();
            }
        }
Example #2
0
		/// <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"])) {
							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();
			}
		}