/// <summary>
        /// Combines the given URI components into a string that is properly encoded for use in HTTP headers.
        /// Note that unicode in the HostString will be encoded as punycode.
        /// </summary>
        /// <param name="scheme">http, https, etc.</param>
        /// <param name="host">The host portion of the uri normally included in the Host header. This may include the port.</param>
        /// <param name="pathBase">The first portion of the request path associated with application root.</param>
        /// <param name="path">The portion of the request path that identifies the requested resource.</param>
        /// <param name="query">The query, if any.</param>
        /// <param name="fragment">The fragment, if any.</param>
        /// <returns></returns>
        public static string BuildAbsolute(
            string scheme,
            HostString host,
            PathString pathBase = new PathString(),
            PathString path = new PathString(),
            QueryString query = new QueryString(),
            FragmentString fragment = new FragmentString())
        {
            var combinedPath = (pathBase.HasValue || path.HasValue) ? (pathBase + path).ToString() : "/";

            var encodedHost = host.ToString();
            var encodedQuery = query.ToString();
            var encodedFragment = fragment.ToString();

            // PERF: Calculate string length to allocate correct buffer size for StringBuilder.
            var length = scheme.Length + SchemeDelimiter.Length + encodedHost.Length
                + combinedPath.Length + encodedQuery.Length + encodedFragment.Length;

            return new StringBuilder(length)
                .Append(scheme)
                .Append(SchemeDelimiter)
                .Append(encodedHost)
                .Append(combinedPath)
                .Append(encodedQuery)
                .Append(encodedFragment)
                .ToString();
        }
 /// <summary>
 /// Combines the given URI components into a string that is properly encoded for use in HTTP headers.
 /// </summary>
 /// <param name="pathBase"></param>
 /// <param name="path"></param>
 /// <param name="query"></param>
 /// <param name="fragment"></param>
 /// <returns></returns>
 public static string Encode(PathString pathBase = new PathString(),
     PathString path = new PathString(),
     QueryString query = new QueryString(),
     FragmentString fragment = new FragmentString())
 {
     string combinePath = (pathBase.HasValue || path.HasValue) ? (pathBase + path).ToString() : "/";
     return combinePath + query + fragment;
 }
 /// <summary>
 /// Combines the given URI components into a string that is properly encoded for use in HTTP headers.
 /// Note that unicode in the HostString will be encoded as punycode.
 /// </summary>
 /// <param name="scheme"></param>
 /// <param name="host"></param>
 /// <param name="pathBase"></param>
 /// <param name="path"></param>
 /// <param name="query"></param>
 /// <param name="fragment"></param>
 /// <returns></returns>
 public static string Encode(string scheme,
     HostString host,
     PathString pathBase = new PathString(),
     PathString path = new PathString(),
     QueryString query = new QueryString(),
     FragmentString fragment = new FragmentString())
 {
     string combinePath = (pathBase.HasValue || path.HasValue) ? (pathBase + path).ToString() : "/";
     return scheme + "://" + host + combinePath + query + fragment;
 }
 public static string Create(PathString pathBase,
     PathString path = new PathString(),
     QueryString query = new QueryString(),
     FragmentString fragment = new FragmentString())
 {
     return new UriHelper()
     {
         PathBase = pathBase,
         Path = path,
         Query = query,
         Fragment = fragment
     }.GetPartialUri();
 }
        public void ImplicitStringConverters_WorksWithAdd()
        {
            var scheme = "http";
            var host = new HostString("localhost:80");
            var pathBase = new PathString("/base");
            var path = new PathString("/path");
            var query = new QueryString("?query");
            var fragment = new FragmentString("#frag");

            var result = scheme + "://" + host + pathBase + path + query + fragment;
            Assert.Equal("http://localhost:80/base/path?query#frag", result);

            result = pathBase + path + query + fragment;
            Assert.Equal("/base/path?query#frag", result);

            result = path + "text";
            Assert.Equal("/pathtext", result);
        }
        public override string GetUriByAddress <TAddress>(
            TAddress address,
            RouteValueDictionary values,
            string scheme,
            HostString host,
            PathString pathBase     = default,
            FragmentString fragment = default,
            LinkOptions options     = null)
        {
            if (string.IsNullOrEmpty(scheme))
            {
                throw new ArgumentException("A scheme must be provided.", nameof(scheme));
            }

            if (!host.HasValue)
            {
                throw new ArgumentException("A host must be provided.", nameof(host));
            }

            var endpoints = GetEndpoints(address);

            if (endpoints.Count == 0)
            {
                return(null);
            }

            return(GetUriByEndpoints(
                       endpoints,
                       values,
                       ambientValues: null,
                       scheme: scheme,
                       host: host,
                       pathBase: pathBase,
                       fragment: fragment,
                       options: options));
        }
        // Also called from DefaultLinkGenerationTemplate
        public string GetUriByEndpoints(
            List <RouteEndpoint> endpoints,
            RouteValueDictionary values,
            RouteValueDictionary ambientValues,
            string scheme,
            HostString host,
            PathString pathBase,
            FragmentString fragment,
            LinkOptions options)
        {
            for (var i = 0; i < endpoints.Count; i++)
            {
                var endpoint = endpoints[i];
                if (TryProcessTemplate(
                        httpContext: null,
                        endpoint: endpoint,
                        values: values,
                        ambientValues: ambientValues,
                        options: options,
                        result: out var result))
                {
                    var uri = UriHelper.BuildAbsolute(
                        scheme,
                        host,
                        pathBase,
                        result.path,
                        result.query,
                        fragment);
                    Log.LinkGenerationSucceeded(_logger, endpoints, uri);
                    return(uri);
                }
            }

            Log.LinkGenerationFailed(_logger, endpoints);
            return(null);
        }
Exemple #8
0
 internal LayoutedRubyFragment(FragmentString baseFragments, FragmentString rubyFragments)
     : base(baseFragments, rubyFragments)
 {
 }
Exemple #9
0
 internal RubyFragmentBase(FragmentString baseFragments, FragmentString rubyFragments)
 {
     _baseFragments = baseFragments;
     _rubyFragments = rubyFragments;
 }
Exemple #10
0
 public override string GetPathByAddress <TAddress>(HttpContext httpContext, TAddress address, RouteValueDictionary values, RouteValueDictionary ambientValues = null, PathString?pathBase = null, FragmentString fragment = default, LinkOptions options = null)
 {
     var(newValues, newAmbientValues) = PromoteAmbientValues(values, ambientValues);
     return(_inner.GetPathByAddress(httpContext,
                                    address,
                                    newValues,
                                    newAmbientValues,
                                    pathBase,
                                    fragment,
                                    options));
 }
Exemple #11
0
 public string UriHelper_BuildRelative(PathString pathBase, PathString path, QueryString query, FragmentString fragment)
 => UriHelper.BuildRelative(pathBase, path, query, fragment);
        public string BuildAbsolute(PathString path, QueryString query = default(QueryString), FragmentString fragment = default(FragmentString))
        {
            var rq = HttpContext.Request;

            return(Microsoft.AspNetCore.Http.Extensions.UriHelper.BuildAbsolute(rq.Scheme, rq.Host, rq.PathBase, path, query, fragment));
        }
Exemple #13
0
 public string Single_Concat(PathString pathBase, PathString path, QueryString query, FragmentString fragment)
 {
     if (pathBase.HasValue || path.HasValue)
     {
         return(pathBase.ToUriComponent() + path.ToUriComponent() + query.ToUriComponent() + fragment.ToUriComponent());
     }
     else
     {
         return("/" + query.ToUriComponent() + fragment.ToUriComponent());
     }
 }
Exemple #14
0
 public override string GetPathByAddress <TAddress>(TAddress address, RouteValueDictionary values, PathString pathBase = default, FragmentString fragment = default, LinkOptions options = null)
 {
     TryCopyCultureTokenToExplicitValues(_httpContextAccessor.HttpContext, address);
     return(_inner.GetPathByAddress(address, values, pathBase, fragment, options));
 }
Exemple #15
0
 public override string GetPathByAddress <TAddress>(HttpContext httpContext, TAddress address, RouteValueDictionary values, RouteValueDictionary ambientValues = null, PathString?pathBase = null, FragmentString fragment = default, LinkOptions options = null)
 {
     throw new NotImplementedException();
 }
        /// <inheritdoc/>
        public override string GetPathByAddress <TAddress>(HttpContext httpContext, TAddress address, RouteValueDictionary values, RouteValueDictionary ambientValues = null, PathString?pathBase = null, FragmentString fragment = default, LinkOptions options = null)
        {
            object odataPathValue;

            if (values.TryGetValue("odataPath", out odataPathValue))
            {
                string odataPath = odataPathValue as string;
                if (odataPath != null)
                {
                    IPerRouteContainer perRouteContainer = httpContext.RequestServices.GetRequiredService <IPerRouteContainer>();
                    string             routePrefix       = perRouteContainer.GetRoutePrefix(httpContext.Request.ODataFeature().RouteName);

                    bool canGenerateDirectLink = routePrefix == null || routePrefix.IndexOf('{') == -1;
                    if (!canGenerateDirectLink)
                    {
                        routePrefix = BindPrefixTemplate(routePrefix, values, ambientValues);
                    }
                    string link = CombinePathSegments(routePrefix, odataPath);
                    link = UriEncode(link);
                    return(link);
                }
            }

            return(_innerGenerator.GetPathByAddress(httpContext, address, values, ambientValues, pathBase, fragment, options));
        }
Exemple #17
0
        public static void FromRelative(string url, out PathString path, out QueryString query, out FragmentString fragment)
        {
            var uri = new Uri(dummyBaseUri, url);

            UriHelper.FromAbsolute(uri.ToString(), out string scheme, out HostString host, out path, out query, out fragment);
        }
        public override string GetPathByAddress <TAddress>(HttpContext httpContext, TAddress address, RouteValueDictionary values, RouteValueDictionary ambientValues = null, PathString?pathBase = null, FragmentString fragment = default, LinkOptions options = null)
        {
            var nonRoundTripUsingQueryStringValues = new Dictionary <string, object>(StringComparer.InvariantCultureIgnoreCase);

            var routeValues = GetValuesDictionary(values);

            foreach (var routeDataStringKey in _options.AmbientRouteDataKeys)
            {
                if (!routeValues.ContainsKey(routeDataStringKey.RouteDataKey) &&
                    ambientValues.TryGetValue(routeDataStringKey.RouteDataKey, out var value))
                {
                    if (!routeDataStringKey.RoundTripUsingQueryString)
                    {
                        nonRoundTripUsingQueryStringValues.Add(routeDataStringKey.RouteDataKey, value);
                    }

                    routeValues[routeDataStringKey.RouteDataKey] = value;
                }
                else if (!routeValues.ContainsKey(routeDataStringKey.RouteDataKey) && httpContext.Request.Query.TryGetValue(routeDataStringKey.RouteDataKey, out var queryStringValues))
                {
                    if (!routeDataStringKey.RoundTripUsingQueryString)
                    {
                        nonRoundTripUsingQueryStringValues.Add(routeDataStringKey.RouteDataKey, queryStringValues.First());
                    }

                    routeValues[routeDataStringKey.RouteDataKey] = queryStringValues.First();
                }
            }

            var url = _generator.GetPathByAddress <TAddress>(httpContext, address, routeValues, ambientValues, pathBase, fragment, options);

            if (url != null)
            {
                var uri = new Uri(url, UriKind.RelativeOrAbsolute);
                if (!uri.IsAbsoluteUri)
                {
                    uri = new Uri($"http://www.domain.com{url}");
                }

                var queryDictionary = QueryHelpers.ParseQuery(uri.Query);

                if (queryDictionary.Keys.Any(k => nonRoundTripUsingQueryStringValues.ContainsKey(k)))
                {
                    foreach (var key in queryDictionary.Keys.Where(k => nonRoundTripUsingQueryStringValues.ContainsKey(k)))
                    {
                        routeValues.Remove(key);
                    }

                    url = _generator.GetPathByAddress <TAddress>(httpContext, address, routeValues, ambientValues, pathBase, fragment, options);
                }
            }

            return(url);
        }
Exemple #19
0
 public static string RouteUrl(this LinkGenerator generator, string routeName, object values = null,
                               PathString pathBase = default, FragmentString fragment = default, LinkOptions options = null)
 {
     //var context = EngineContext.Current.Resolve<IHttpContextAccessor>().HttpContext;
     return(generator.GetPathByRouteValues(routeName, values, pathBase, fragment, options));
 }
        /// <inheritdoc/>
        public override string GetPathByAddress <TAddress>(HttpContext httpContext, TAddress address, RouteValueDictionary values, RouteValueDictionary ambientValues = null, PathString?pathBase = null, FragmentString fragment = default, LinkOptions options = null)
        {
            object odataPathValue;

            if (values.TryGetValue("odataPath", out odataPathValue))
            {
                string odataPath = odataPathValue as string;
                if (odataPath != null)
                {
                    IPerRouteContainer perRouteContainer = httpContext.RequestServices.GetRequiredService <IPerRouteContainer>();
                    string             routePrefix       = perRouteContainer.GetRoutePrefix(httpContext.Request.ODataFeature().RouteName);

                    bool canGenerateDirectLink = routePrefix == null || routePrefix.IndexOf('{') == -1;
                    if (!canGenerateDirectLink)
                    {
                        routePrefix = BindPrefixTemplate(routePrefix, values, ambientValues);
                    }
                    string link = CombinePathSegments(routePrefix, odataPath);
                    link = UriEncode(link);

                    // A workaround to include the PathBase, a good solution is to use ASP.NET Core provided the APIs
                    // at https://github.com/dotnet/aspnetcore/blob/master/src/Http/Http.Extensions/src/UriHelper.cs#L48
                    // to build the absolute Uri. But, here only needs the "PathBase + Path (without OData path)",
                    HttpRequest request = httpContext.Request;
                    if (request != null && request.PathBase != null && request.PathBase.HasValue)
                    {
                        return(request.PathBase.Value + "/" + link);
                    }

                    return(link);
                }
            }

            return(_innerGenerator.GetPathByAddress(httpContext, address, values, ambientValues, pathBase, fragment, options));
        }
Exemple #21
0
 /// <summary>
 /// Generates a URI with an absolute path based on the provided values.
 /// </summary>
 /// <typeparam name="TAddress">The address type.</typeparam>
 /// <param name="address">The address value. Used to resolve endpoints.</param>
 /// <param name="values">The route values. Used to expand parameters in the route template. Optional.</param>
 /// <param name="pathBase">An optional URI path base. Prepended to the path in the resulting URI.</param>
 /// <param name="fragment">An optional URI fragment. Appended to the resulting URI.</param>
 /// <param name="options">
 /// An optional <see cref="LinkOptions"/>. Settings on provided object override the settings with matching
 /// names from <c>RouteOptions</c>.
 /// </param>
 /// <returns>A URI with an absolute path, or <c>null</c>.</returns>
 public abstract string GetPathByAddress <TAddress>(
     TAddress address,
     RouteValueDictionary values,
     PathString pathBase     = default,
     FragmentString fragment = default,
     LinkOptions options     = default);
Exemple #22
0
 public override string GetUriByAddress <TAddress>(HttpContext httpContext, TAddress address, RouteValueDictionary values, RouteValueDictionary ambientValues = null, string scheme = null, HostString?host = null, PathString?pathBase = null, FragmentString fragment = default, LinkOptions options = null)
 => Url;
Exemple #23
0
 public override string GetUriByAddress <TAddress>(HttpContext httpContext, TAddress address, RouteValueDictionary values, RouteValueDictionary ambientValues = null, string scheme = null, HostString?host = null, PathString?pathBase = null, FragmentString fragment = default, LinkOptions options = null)
 {
     TryCopyCultureTokenToExplicitValues(httpContext, address);
     return(_inner.GetUriByAddress(httpContext, address, values, ambientValues, scheme, host, pathBase, fragment, options));
 }
Exemple #24
0
        public override string GetUriByAddress <TAddress>(HttpContext httpContext, TAddress address, RouteValueDictionary values, RouteValueDictionary ambientValues = null, string scheme = null, HostString?host = null, PathString?pathBase = null, FragmentString fragment = default, LinkOptions options = null)
        {
            var promotedValues = PromoteAmbientValues(values, ambientValues);

            return(_inner.GetUriByAddress(httpContext,
                                          address,
                                          promotedValues.newValues,
                                          promotedValues.newAmbientValues,
                                          scheme,
                                          host,
                                          pathBase,
                                          fragment,
                                          options));
        }
Exemple #25
0
        public string String_Create(PathString pathBaseString, PathString pathString, QueryString queryString, FragmentString fragmentString)
        {
            var pathBase = pathBaseString.ToUriComponent();
            var path     = pathString.ToUriComponent();
            var query    = queryString.ToUriComponent();
            var fragment = fragmentString.ToUriComponent();

            var length = pathBase.Length + path.Length + query.Length + fragment.Length;

            if (string.IsNullOrEmpty(pathBase) && string.IsNullOrEmpty(path))
            {
                if (length == 0)
                {
                    return("/");
                }

                path = "/";
                length++;
            }
            else
            {
                if (!string.IsNullOrEmpty(pathBase) && pathBase.Length == length)
                {
                    return(pathBase);
                }

                if (path.Length == length)
                {
                    return(path);
                }
            }

            return(string.Create(length, (pathBase, path, query, fragment), initializeStringSpanAction));
        }
 public override string GetPathByAddress <TAddress>(TAddress address, RouteValueDictionary values, PathString pathBase = default, FragmentString fragment = default, LinkOptions options = null)
 {
     RewriteValuesDictionary(values);
     return(linkGenerator.GetPathByAddress(address, values, pathBase, fragment, options));
 }
 public override string GetPathByAddress <TAddress>(HttpContext httpContext, TAddress address, RouteValueDictionary values, RouteValueDictionary ambientValues = null, PathString?pathBase = null, FragmentString fragment = default, LinkOptions options = null) =>
 inner.GetPathByAddress(httpContext, address, values, ambientValues, pathBase, fragment, options);
 public override string GetUriByAddress <TAddress>(HttpContext httpContext, TAddress address, RouteValueDictionary values, RouteValueDictionary ambientValues = null, string scheme = null, HostString?host = null, PathString?pathBase = null, FragmentString fragment = default, LinkOptions options = null)
 {
     RewriteValuesDictionary(values);
     return(linkGenerator.GetUriByAddress(httpContext, address, values, ambientValues, scheme, host, pathBase, fragment, options));
 }
Exemple #29
0
 public override string GetPathByAddress <TAddress>(TAddress address, RouteValueDictionary values, PathString pathBase = default, FragmentString fragment = default, LinkOptions options = null)
 {
     throw new NotImplementedException();
 }
 public override string GetPathByAddress <TAddress>(TAddress address, RouteValueDictionary values, PathString pathBase = default, FragmentString fragment = default, LinkOptions options = null) =>
 inner.GetPathByAddress(address, values, pathBase, fragment, options);
Exemple #31
0
 public override string GetUriByAddress <TAddress>(TAddress address, RouteValueDictionary values, string scheme, HostString host, PathString pathBase = default, FragmentString fragment = default, LinkOptions options = null)
 => Url;
 public override string GetUriByAddress <TAddress>(HttpContext httpContext, TAddress address, RouteValueDictionary values,
                                                   RouteValueDictionary ambientValues = null, string scheme                       = null, HostString?host = null, PathString?pathBase = null,
                                                   FragmentString fragment            = new FragmentString(), LinkOptions options = null)
 {
     return("https://domain.com/some/path");
 }
Exemple #33
0
 public override string GetUriByAddress <TAddress>(TAddress address, RouteValueDictionary values, string scheme, HostString host, PathString pathBase = default, FragmentString fragment = default, LinkOptions options = null)
 {
     return(_inner.GetUriByAddress(address,
                                   values,
                                   scheme,
                                   host,
                                   pathBase,
                                   fragment,
                                   options));
 }
 public override string GetUriByAddress <TAddress>(TAddress address, RouteValueDictionary values, string scheme, HostString host,
                                                   PathString pathBase = new PathString(), FragmentString fragment = new FragmentString(), LinkOptions options = null)
 {
     throw new NotImplementedException();
 }
Exemple #35
0
 public static string Create(string scheme,
     HostString host,
     PathString pathBase = new PathString(),
     PathString path = new PathString(),
     QueryString query = new QueryString(),
     FragmentString fragment = new FragmentString())
 {
     return new UriHelper()
     {
         Scheme = scheme,
         Host = host,
         PathBase = pathBase,
         Path = path,
         Query = query,
         Fragment = fragment
     }.GetFullUri();
 }
 /// <inheritdoc />
 public override string GetUriByAddress <TAddress>(HttpContext httpContext, TAddress address, RouteValueDictionary values, RouteValueDictionary?ambientValues = null, string?scheme = null, HostString?host = null, PathString?pathBase = null, FragmentString fragment = default, LinkOptions?options = null) =>
 inner.GetUriByAddress(httpContext, address, values, ambientValues, scheme, host, pathBase, fragment, options);