Ejemplo n.º 1
0
        /// <summary>
        /// A convenience method that creates a new
        /// <see cref="Sharpen.URI">Sharpen.URI</see>
        /// whose scheme, host, port, path,
        /// query are taken from the existing URI, dropping any fragment or user-information.
        /// The path is set to "/" if not explicitly specified. The existing URI is returned
        /// unmodified if it has no fragment or user-information and has a path.
        /// </summary>
        /// <param name="uri">original URI.</param>
        /// <exception cref="Sharpen.URISyntaxException">If the resulting URI is invalid.</exception>
        public static URI RewriteURI(URI uri)
        {
            Args.NotNull(uri, "URI");
            if (uri.IsOpaque())
            {
                return(uri);
            }
            URIBuilder uribuilder = new URIBuilder(uri);

            if (uribuilder.GetUserInfo() != null)
            {
                uribuilder.SetUserInfo(null);
            }
            if (TextUtils.IsEmpty(uribuilder.GetPath()))
            {
                uribuilder.SetPath("/");
            }
            if (uribuilder.GetHost() != null)
            {
                uribuilder.SetHost(uribuilder.GetHost().ToLower(Sharpen.Extensions.GetEnglishCulture()
                                                                ));
            }
            uribuilder.SetFragment(null);
            return(uribuilder.Build());
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Derives the interpreted (absolute) URI that was used to generate the last
        /// request.
        /// </summary>
        /// <remarks>
        /// Derives the interpreted (absolute) URI that was used to generate the last
        /// request. This is done by extracting the request-uri and target origin for
        /// the last request and scanning all the redirect locations for the last
        /// fragment identifier, then combining the result into a
        /// <see cref="Sharpen.URI">Sharpen.URI</see>
        /// .
        /// </remarks>
        /// <param name="originalURI">original request before any redirects</param>
        /// <param name="target">
        /// if the last URI is relative, it is resolved against this target,
        /// or <code>null</code> if not available.
        /// </param>
        /// <param name="redirects">
        /// collection of redirect locations since the original request
        /// or <code>null</code> if not available.
        /// </param>
        /// <returns>interpreted (absolute) URI</returns>
        /// <exception cref="Sharpen.URISyntaxException"></exception>
        public static URI Resolve(URI originalURI, HttpHost target, IList <URI> redirects)
        {
            Args.NotNull(originalURI, "Request URI");
            URIBuilder uribuilder;

            if (redirects == null || redirects.IsEmpty())
            {
                uribuilder = new URIBuilder(originalURI);
            }
            else
            {
                uribuilder = new URIBuilder(redirects[redirects.Count - 1]);
                string frag = uribuilder.GetFragment();
                // read interpreted fragment identifier from redirect locations
                for (int i = redirects.Count - 1; frag == null && i >= 0; i--)
                {
                    frag = redirects[i].GetFragment();
                }
                uribuilder.SetFragment(frag);
            }
            // read interpreted fragment identifier from original request
            if (uribuilder.GetFragment() == null)
            {
                uribuilder.SetFragment(originalURI.GetFragment());
            }
            // last target origin
            if (target != null && !uribuilder.IsAbsolute())
            {
                uribuilder.SetScheme(target.GetSchemeName());
                uribuilder.SetHost(target.GetHostName());
                uribuilder.SetPort(target.GetPort());
            }
            return(uribuilder.Build());
        }
Ejemplo n.º 3
0
        /// <summary>
        /// A convenience method for creating a new
        /// <see cref="Sharpen.URI">Sharpen.URI</see>
        /// whose scheme, host
        /// and port are taken from the target host, but whose path, query and
        /// fragment are taken from the existing URI. The fragment is only used if
        /// dropFragment is false. The path is set to "/" if not explicitly specified.
        /// </summary>
        /// <param name="uri">Contains the path, query and fragment to use.</param>
        /// <param name="target">Contains the scheme, host and port to use.</param>
        /// <param name="dropFragment">True if the fragment should not be copied.</param>
        /// <exception cref="Sharpen.URISyntaxException">If the resulting URI is invalid.</exception>
        public static URI RewriteURI(URI uri, HttpHost target, bool dropFragment)
        {
            Args.NotNull(uri, "URI");
            if (uri.IsOpaque())
            {
                return(uri);
            }
            URIBuilder uribuilder = new URIBuilder(uri);

            if (target != null)
            {
                uribuilder.SetScheme(target.GetSchemeName());
                uribuilder.SetHost(target.GetHostName());
                uribuilder.SetPort(target.GetPort());
            }
            else
            {
                uribuilder.SetScheme(null);
                uribuilder.SetHost(null);
                uribuilder.SetPort(-1);
            }
            if (dropFragment)
            {
                uribuilder.SetFragment(null);
            }
            if (TextUtils.IsEmpty(uribuilder.GetPath()))
            {
                uribuilder.SetPath("/");
            }
            return(uribuilder.Build());
        }