private void HandleError(RewriteContext context) { // Return the status code. ContextFacade.SetStatusCode((int)context.StatusCode); // Get the error handler if there is one. IRewriteErrorHandler handler = _configuration.ErrorHandlers[(int)context.StatusCode] as IRewriteErrorHandler; if (handler != null) { try { _configuration.Logger.Debug(MessageProvider.FormatString(Message.CallingErrorHandler)); // Execute the error handler. ContextFacade.HandleError(handler); } catch (HttpException) { throw; } catch (Exception exc) { _configuration.Logger.Fatal(exc.Message, exc); throw new HttpException( (int)HttpStatusCode.InternalServerError, HttpStatusCode.InternalServerError.ToString()); } } else { throw new HttpException((int)context.StatusCode, context.StatusCode.ToString()); } }
/// <summary> /// Performs the rewriting. /// </summary> public void Rewrite() { string originalUrl = ContextFacade.GetRawUrl().Replace("+", " "); RawUrl = originalUrl; // Create the context RewriteContext context = new RewriteContext(this, originalUrl, ContextFacade.GetHttpMethod(), ContextFacade.MapPath, ContextFacade.GetServerVariables(), ContextFacade.GetHeaders(), ContextFacade.GetCookies()); // Process each rule. ProcessRules(context); // Append any headers defined. AppendHeaders(context); // Append any cookies defined. AppendCookies(context); // Rewrite the path if the location has changed. ContextFacade.SetStatusCode((int)context.StatusCode); if ((context.Location != originalUrl) && ((int)context.StatusCode < 400)) { if ((int)context.StatusCode < 300) { // Successful status if less than 300 _configuration.Logger.Info(MessageProvider.FormatString(Message.RewritingXtoY, ContextFacade.GetRawUrl(), context.Location)); // Verify that the url exists on this server. HandleDefaultDocument(context);// VerifyResultExists(context); ContextFacade.RewritePath(context.Location); } else { // Redirection _configuration.Logger.Info(MessageProvider.FormatString(Message.RedirectingXtoY, ContextFacade.GetRawUrl(), context.Location)); ContextFacade.SetRedirectLocation(context.Location); } } else if ((int)context.StatusCode >= 400) { HandleError(context); } else if (HandleDefaultDocument(context)) { ContextFacade.RewritePath(context.Location); } // Sets the context items. SetContextItems(context); }
/// <summary> /// Performs the rewriting. /// </summary> public void Rewrite() { string originalUrl = ContextFacade.GetRawUrl().Replace("+", " "); RawUrl = originalUrl; // Create the context RewriteContext context = new RewriteContext( this, originalUrl, ContextFacade.GetHttpMethod(), ContextFacade.MapPath, ContextFacade.GetServerVariables(), ContextFacade.GetHeaders(), ContextFacade.GetCookies()); // Process each rule. ProcessRules(context); // Append any headers defined. AppendHeaders(context); // Append any cookies defined. AppendCookies(context); // Rewrite the path if the location has changed. ContextFacade.SetStatusCode((int)context.StatusCode); if ((context.Location != originalUrl) && ((int)context.StatusCode < 400)) { if ((int)context.StatusCode < 300) { // Successful status if less than 300 _configuration.Logger.Info( MessageProvider.FormatString(Message.RewritingXtoY, ContextFacade.GetRawUrl(), context.Location)); // Verify that the url exists on this server. HandleDefaultDocument(context); // VerifyResultExists(context); if (context.Location.Contains(@"&")) { var queryStringCollection = HttpUtility.ParseQueryString(new Uri(this.ContextFacade.GetRequestUrl(), context.Location).Query); StringBuilder builder = new StringBuilder(); foreach (var value in queryStringCollection.AllKeys.Distinct()) { var argument = queryStringCollection.GetValues(value).FirstOrDefault(); if (value == "g") { if (context.Location != argument) { builder.AppendFormat( "{0}={1}&", value, argument); } } else { builder.AppendFormat( "{0}={1}&", value, argument); } } context.Location = context.Location.Remove(context.Location.IndexOf("?") + 1); context.Location = context.Location + builder; if (context.Location.EndsWith(@"&")) { context.Location = context.Location.Remove(context.Location.Length - 1); } } ContextFacade.RewritePath(context.Location); } else { // Redirection _configuration.Logger.Info( MessageProvider.FormatString( Message.RedirectingXtoY, ContextFacade.GetRawUrl(), context.Location)); ContextFacade.SetRedirectLocation(context.Location); } } else if ((int)context.StatusCode >= 400) { HandleError(context); } else if (HandleDefaultDocument(context)) { ContextFacade.RewritePath(context.Location); } // Sets the context items. SetContextItems(context); }