コード例 #1
0
        /// <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);
        }
コード例 #2
0
        /// <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);
        }