/// <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);
        }
Esempio n. 2
0
        private void SetContextItems(RewriteContext context)
        {
            OriginalQueryString = new Uri(ContextFacade.GetRequestUrl(), ContextFacade.GetRawUrl()).Query.Replace("?", "");
            QueryString         = new Uri(ContextFacade.GetRequestUrl(), context.Location).Query.Replace("?", "");

            // Add in the properties as context items, so these will be accessible to the handler
            foreach (string key in context.Properties.Keys)
            {
                ContextFacade.SetItem(String.Format("Rewriter.{0}", key), context.Properties[key]);
            }
        }
 private void VerifyResultExists(RewriteContext context)
 {
     if ((String.Compare(context.Location, ContextFacade.GetRawUrl()) != 0) && ((int)context.StatusCode < 300))
     {
         Uri uri = new Uri(ContextFacade.GetRequestUrl(), context.Location);
         if (uri.Host == ContextFacade.GetRequestUrl().Host)
         {
             string filename = ContextFacade.MapPath(uri.AbsolutePath);
             if (!File.Exists(filename))
             {
                 _configuration.Logger.Debug(MessageProvider.FormatString(Message.ResultNotFound, filename));
                 context.StatusCode = HttpStatusCode.NotFound;
             }
             else
             {
                 HandleDefaultDocument(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);
        }