示例#1
0
        /// <summary>
        /// Resolve the path using redirects. No authentication.
        /// </summary>
        protected virtual string ResolvePath(string path)
        {
            HttpRedirect redirect = null;

            string[] sections;
            int      index;

            if (path[0] == '/')
            {
                // split the request path into directory sections
                sections = path.Split(Chars.ForwardSlash);
                index    = -1;
            }
            else
            {
                sections = path.Split(Chars.ForwardSlash);
                index    = -2;
            }

            // persistance of the current path
            path = "/";

            // the first iteration?
            bool first = true;
            // redirected path is absolute?
            bool absolute = false;

            // iterate the request path sections
            while (++index < sections.Length)
            {
                // is the first iteration?
                if (!first)
                {
                    // is the redirect absolute? yes, end the redirects
                    if (absolute)
                    {
                        break;
                    }

                    // is the section empty? yes, skip the section
                    if (sections[index].Length == 0)
                    {
                        continue;
                    }

                    // append the section
                    path = path + Chars.ForwardSlash + sections[index];
                }

                // should the path be redirected?
                if (Redirects.TryGetValue(path, out redirect))
                {
                    // update the current path
                    path     = redirect.Target;
                    absolute = redirect.Absolute;

                    int iteration = 1;
                    // while the current path is to be redirected
                    while (Redirects.TryGetValue(path, out redirect))
                    {
                        // update the current path
                        path     = redirect.Target;
                        absolute = redirect.Absolute;

                        // have there been an excessive number of redirects?
                        if (++iteration == 20)
                        {
                            // yes, start maintaining a collection of redirected paths
                            ArrayRig <string> redirects = new ArrayRig <string>();
                            redirects.Add(path);

                            // while the current path is to be redirected
                            while (Redirects.TryGetValue(path, out redirect))
                            {
                                // update the current path
                                path     = redirect.Target;
                                absolute = redirect.Absolute;

                                // has the redirect path already been hit?
                                if (redirects.Contains(path))
                                {
                                    // yes, return false, a redirect loop was hit
                                    Log.Warning("Redirect loop detected. Request denied.");
                                    return(null);
                                }

                                // add to the redirected collection
                                redirects.Add(path);
                            }
                            break;
                        }
                    }

                    first = false;
                }
                else if (first)
                {
                    path  = string.Empty;
                    first = false;
                }
            }

            // return the authentication value
            return(path);
        }
示例#2
0
        /// <summary>
        /// Check if the WebRequest should be redirected and authenticate the
        /// web request. Returns if the request path was resolved successfully.
        /// </summary>
        protected virtual bool ResolvePath(HttpRequest request)
        {
            HttpRedirect    redirect = null;
            HttpRequirement auth;

            // split the request path into directory sections
            var sections = request.RequestPath.Split(Chars.ForwardSlash);

            // persistance of the current path
            string path = "/";

            // the first iteration?
            bool first = true;
            // redirected path is absolute?
            bool absolute = false;
            // is the request path authenticated?
            bool authentication = true;

            // iterate the request path sections
            for (int i = 0; i < sections.Length; ++i)
            {
                // is the first iteration?
                if (!first)
                {
                    // is the redirect absolute? yes, end the redirects
                    if (absolute)
                    {
                        break;
                    }

                    // is the section empty? yes, skip the section
                    if (sections[i].Length == 0)
                    {
                        continue;
                    }

                    // append the section
                    path = path + Chars.ForwardSlash + sections[i];
                }

                // does authentication exist for the request path?
                if (Authentication.TryGetValue(path, out auth))
                {
                    // update the authentication
                    authentication = auth.Allow(request);
                }

                // should the path be redirected?
                if (Redirects.TryGetValue(path, out redirect))
                {
                    // update the current path
                    path     = redirect.Target;
                    absolute = redirect.Absolute;

                    // does authentication exist for the request path?
                    if (Authentication.TryGetValue(path, out auth))
                    {
                        // update the authentication
                        authentication = auth.Allow(request);
                    }

                    int iteration = 1;
                    // while the current path is to be redirected
                    while (Redirects.TryGetValue(path, out redirect))
                    {
                        // update the current path
                        path     = redirect.Target;
                        absolute = redirect.Absolute;

                        // does authentication exist for the request path?
                        if (Authentication.TryGetValue(path, out auth))
                        {
                            // update the authentication
                            authentication = auth.Allow(request);
                        }

                        // have there been an excessive number of redirects?
                        if (++iteration == 20)
                        {
                            // yes, start maintaining a collection of redirected paths
                            ArrayRig <string> redirects = new ArrayRig <string>();
                            redirects.Add(path);

                            // while the current path is to be redirected
                            while (Redirects.TryGetValue(path, out redirect))
                            {
                                // update the current path
                                path     = redirect.Target;
                                absolute = redirect.Absolute;

                                // has the redirect path already been hit?
                                if (redirects.Contains(path))
                                {
                                    // yes, return false, a redirect loop was hit
                                    Log.Warning("Redirect loop detected. Request denied.");
                                    request.RequestPath = null;
                                    return(false);
                                }

                                // add to the redirected collection
                                redirects.Add(path);

                                // does authentication exist for the request path?
                                if (Authentication.TryGetValue(path, out auth))
                                {
                                    // update the authentication
                                    authentication = auth.Allow(request);
                                }
                            }
                            break;
                        }
                    }

                    first = false;
                }
                else if (first)
                {
                    path  = string.Empty;
                    first = false;
                }
            }

            // has a redirect occured?
            if (!request.RequestPath.Equals(path))
            {
                // has the redirect method been assigned?
                if (OnRedirect != null)
                {
                    OnRedirect.ArgA = request;
                    OnRedirect.ArgB = path;
                    OnRedirect.Run();
                }

                // update the web request path
                request.RequestPath = path;
            }

            // return the authentication value
            return(authentication);
        }