/// <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());
        }
        /// <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());
        }