< Summary

Class:Azure.Core.RawRequestUriBuilder
Assembly:Azure.ResourceManager.Network
File(s):C:\Git\azure-sdk-for-net\sdk\core\Azure.Core\src\Shared\AutoRest\RawRequestUriBuilder.cs
Covered lines:0
Uncovered lines:66
Coverable lines:66
Total lines:172
Line coverage:0% (0 of 66)
Covered branches:0
Total branches:42
Branch coverage:0% (0 of 42)

Metrics

MethodCyclomatic complexity Line coverage Branch coverage
.cctor()-0%100%
GetQueryParts(...)-0%0%
AppendRaw(...)-0%0%
AppendRawNextLink(...)-0%0%

File(s)

C:\Git\azure-sdk-for-net\sdk\core\Azure.Core\src\Shared\AutoRest\RawRequestUriBuilder.cs

#LineLine coverage
 1// Copyright (c) Microsoft Corporation. All rights reserved.
 2// Licensed under the MIT License.
 3
 4#nullable enable
 5
 6using System;
 7using System.Globalization;
 8
 9namespace Azure.Core
 10{
 11    internal class RawRequestUriBuilder: RequestUriBuilder
 12    {
 13        private const string SchemeSeparator = "://";
 14        private const char HostSeparator = '/';
 15        private const char PortSeparator = ':';
 016        private static readonly char[] HostOrPort = { HostSeparator, PortSeparator };
 17        private const char QueryBeginSeparator = '?';
 18        private const char QueryContinueSeparator = '&';
 19        private const char QueryValueSeparator = '=';
 20
 21        private RawWritingPosition? _position;
 22
 23        private static (string Name, string Value) GetQueryParts(string queryUnparsed)
 24        {
 025            int separatorIndex = queryUnparsed.IndexOf(QueryValueSeparator);
 026            if (separatorIndex == -1)
 27            {
 028                return (queryUnparsed, string.Empty);
 29            }
 030            return (queryUnparsed.Substring(0, separatorIndex), queryUnparsed.Substring(separatorIndex + 1));
 31        }
 32
 33        public void AppendRaw(string value, bool escape)
 34        {
 035            if (_position == null)
 36            {
 037                if (!string.IsNullOrEmpty(Query))
 38                {
 039                    _position = RawWritingPosition.Query;
 40                }
 041                else if (!string.IsNullOrEmpty(Path))
 42                {
 043                    _position = RawWritingPosition.Path;
 44                }
 045                else if (!string.IsNullOrEmpty(Host))
 46                {
 047                    _position = RawWritingPosition.Host;
 48                }
 49                else
 50                {
 051                    _position = RawWritingPosition.Scheme;
 52                }
 53            }
 054            while (!string.IsNullOrWhiteSpace(value))
 55            {
 056                if (_position == RawWritingPosition.Scheme)
 57                {
 058                    int separator = value.IndexOf(SchemeSeparator, StringComparison.InvariantCultureIgnoreCase);
 059                    if (separator == -1)
 60                    {
 061                        Scheme += value;
 062                        value = string.Empty;
 63                    }
 64                    else
 65                    {
 066                        Scheme += value.Substring(0, separator);
 67                        // TODO: Find a better way to map schemes to default ports
 068                        Port = string.Equals(Scheme, "https", StringComparison.OrdinalIgnoreCase) ? 443 : 80;
 069                        value = value.Substring(separator + SchemeSeparator.Length);
 070                        _position = RawWritingPosition.Host;
 71                    }
 72                }
 073                else if (_position == RawWritingPosition.Host)
 74                {
 075                    int separator = value.IndexOfAny(HostOrPort);
 076                    if (separator == -1)
 77                    {
 078                        if (string.IsNullOrEmpty(Path))
 79                        {
 080                            Host += value;
 081                            value = string.Empty;
 82                        }
 83                        else
 84                        {
 85                            // All Host information must be written before Path information
 86                            // If Path already has information, we transition to writing Path
 087                            _position = RawWritingPosition.Path;
 88                        }
 89                    }
 90                    else
 91                    {
 092                        Host += value.Substring(0, separator);
 093                        _position = value[separator] == HostSeparator ? RawWritingPosition.Path : RawWritingPosition.Por
 094                        value = value.Substring(separator + 1);
 95                    }
 96                }
 097                else if (_position == RawWritingPosition.Port)
 98                {
 099                    int separator = value.IndexOf(HostSeparator);
 0100                    if (separator == -1)
 101                    {
 0102                        Port = int.Parse(value, CultureInfo.InvariantCulture);
 0103                        value = string.Empty;
 104                    }
 105                    else
 106                    {
 0107                        Port = int.Parse(value.Substring(0, separator), CultureInfo.InvariantCulture);
 0108                        value = value.Substring(separator + 1);
 109                    }
 110                    // Port cannot be split (like Host), so always transition to Path when Port is parsed
 0111                    _position = RawWritingPosition.Path;
 112                }
 0113                else if (_position == RawWritingPosition.Path)
 114                {
 0115                    int separator = value.IndexOf(QueryBeginSeparator);
 0116                    if (separator == -1)
 117                    {
 0118                        AppendPath(value, escape);
 0119                        value = string.Empty;
 120                    }
 121                    else
 122                    {
 0123                        AppendPath(value.Substring(0, separator), escape);
 0124                        value = value.Substring(separator + 1);
 0125                        _position = RawWritingPosition.Query;
 126                    }
 127                }
 0128                else if (_position == RawWritingPosition.Query)
 129                {
 0130                    int separator = value.IndexOf(QueryContinueSeparator);
 0131                    if (separator == 0)
 132                    {
 0133                        value = value.Substring(1);
 134                    }
 0135                    else if (separator == -1)
 136                    {
 0137                        (string queryName, string queryValue) = GetQueryParts(value);
 0138                        AppendQuery(queryName, queryValue, escape);
 0139                        value = string.Empty;
 140                    }
 141                    else
 142                    {
 0143                        (string queryName, string queryValue) = GetQueryParts(value.Substring(0, separator));
 0144                        AppendQuery(queryName, queryValue, escape);
 0145                        value = value.Substring(separator + 1);
 146                    }
 147                }
 148            }
 0149        }
 150
 151        private enum RawWritingPosition
 152        {
 153            Scheme,
 154            Host,
 155            Port,
 156            Path,
 157            Query
 158        }
 159
 160        public void AppendRawNextLink(string nextLink, bool escape)
 161        {
 162            // If it is an absolute link, we use the nextLink as the entire url
 0163            if (nextLink.StartsWith(Uri.UriSchemeHttp, StringComparison.InvariantCultureIgnoreCase))
 164            {
 0165                Reset(new Uri(nextLink));
 0166                return;
 167            }
 168
 0169            AppendPath(nextLink, escape);
 0170        }
 171    }
 172}