Ejemplo n.º 1
0
        /// <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())
        {
            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());
        }
Ejemplo n.º 2
0
        private string BuildAbsolute(
            string scheme,
            HostString host,
            PathString pathBase     = new PathString(),
            PathString path         = new PathString(),
            QueryString query       = new QueryString(),
            FragmentString fragment = new FragmentString())
        {
            if (scheme == null)
            {
                throw new ArgumentNullException(nameof(scheme));
            }

            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());
        }
Ejemplo n.º 3
0
        /// <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();
        }
Ejemplo n.º 4
0
        public MongoUrl Resolve(HostString host)
        {
            var connectionString = _configuration.GetConnectionString(host.ToString());

            if (connectionString == null)
            {
                return(null);
            }

            return(new MongoUrl(connectionString));
        }
Ejemplo n.º 5
0
 public override void WriteJson(JsonWriter writer, HostString value, JsonSerializer serializer)
 {
     writer.WriteValue(value.ToString());
 }
        public void ApplyRule(RewriteContext context)
        {
            HttpRequest request = context.HttpContext.Request;
            PathString  path    = context.HttpContext.Request.Path;
            HostString  host    = context.HttpContext.Request.Host;

            if (path.HasValue && path.Value.Any(char.IsUpper) || host.HasValue && host.Value.Any(char.IsUpper))
            {
                var newUrl   = UriHelper.BuildAbsolute(request.Scheme.ToLower(), new HostString(host.ToString().ToLower()), request.PathBase.ToString().ToLower(), request.Path.ToString().ToLower(), request.QueryString);
                var response = context.HttpContext.Response;
                response.StatusCode = (int)statusCode;
                response.Headers[HeaderNames.Location] = newUrl;
                context.Result = RuleResult.EndResponse;
            }
            else
            {
                context.Result = RuleResult.ContinueRules;
            }
        }