< Summary

Class:Azure.Messaging.EventHubs.Core.ConnectionStringParser
Assembly:Azure.Messaging.EventHubs.Processor
File(s):C:\Git\azure-sdk-for-net\sdk\eventhub\Azure.Messaging.EventHubs.Shared\src\Core\ConnectionStringParser.cs
Covered lines:0
Uncovered lines:53
Coverable lines:53
Total lines:165
Line coverage:0% (0 of 53)
Covered branches:0
Total branches:40
Branch coverage:0% (0 of 40)

Metrics

MethodCyclomatic complexity Line coverage Branch coverage
.cctor()-0%100%
Parse(...)-0%0%

File(s)

C:\Git\azure-sdk-for-net\sdk\eventhub\Azure.Messaging.EventHubs.Shared\src\Core\ConnectionStringParser.cs

#LineLine coverage
 1// Copyright (c) Microsoft Corporation. All rights reserved.
 2// Licensed under the MIT License.
 3
 4using System;
 5using Azure.Core;
 6
 7namespace Azure.Messaging.EventHubs.Core
 8{
 9    /// <summary>
 10    ///   Allows for parsing Event Hubs connection strings.
 11    /// </summary>
 12    ///
 13    internal static class ConnectionStringParser
 14    {
 15        /// <summary>The token that identifies the endpoint address for the Event Hubs namespace.</summary>
 16        private const string EndpointToken = "Endpoint";
 17
 18        /// <summary>The token that identifies the name of a specific Event Hub under the namespace.</summary>
 19        private const string EventHubNameToken = "EntityPath";
 20
 21        /// <summary>The token that identifies the name of a shared access key.</summary>
 22        private const string SharedAccessKeyNameToken = "SharedAccessKeyName";
 23
 24        /// <summary>The token that identifies the value of a shared access key.</summary>
 25        private const string SharedAccessKeyValueToken = "SharedAccessKey";
 26
 27        /// <summary>The character used to separate a token and its value in the connection string.</summary>
 28        private const char TokenValueSeparator = '=';
 29
 30        /// <summary>The character used to mark the beginning of a new token/value pair in the connection string.</summa
 31        private const char TokenValuePairDelimiter = ';';
 32
 33        /// <summary>The name of the protocol used by an Event Hubs endpoint. </summary>
 34        private const string EventHubsEndpointSchemeName = "sb";
 35
 36        /// <summary>The formatted protocol used by an Event Hubs endpoint. </summary>
 037        private static readonly string EventHubsEndpointScheme = $"{ EventHubsEndpointSchemeName }{ Uri.SchemeDelimiter 
 38
 39        /// <summary>
 40        ///   Parses the specified Event Hubs connection string into its component properties.
 41        /// </summary>
 42        ///
 43        /// <param name="connectionString">The connection string to parse.</param>
 44        ///
 45        /// <returns>The component properties parsed from the connection string.</returns>
 46        ///
 47        /// <seealso cref="ConnectionStringProperties" />
 48        ///
 49        public static ConnectionStringProperties Parse(string connectionString)
 50        {
 051            Argument.AssertNotNullOrEmpty(connectionString, nameof(connectionString));
 52
 053            int tokenPositionModifier = (connectionString[0] == TokenValuePairDelimiter) ? 0 : 1;
 054            int lastPosition = 0;
 055            int currentPosition = 0;
 56            int valueStart;
 57
 58            string slice;
 59            string token;
 60            string value;
 61
 062            var parsedValues =
 063            (
 064                EndpointToken: default(UriBuilder),
 065                EventHubNameToken: default(string),
 066                SharedAccessKeyNameToken: default(string),
 067                SharedAccessKeyValueToken: default(string)
 068            );
 69
 070            while (currentPosition != -1)
 71            {
 72                // Slice the string into the next token/value pair.
 73
 074                currentPosition = connectionString.IndexOf(TokenValuePairDelimiter, lastPosition + 1);
 75
 076                if (currentPosition >= 0)
 77                {
 078                    slice = connectionString.Substring(lastPosition, (currentPosition - lastPosition));
 79                }
 80                else
 81                {
 082                    slice = connectionString.Substring(lastPosition);
 83                }
 84
 85                // Break the token and value apart, if this is a legal pair.
 86
 087                valueStart = slice.IndexOf(TokenValueSeparator);
 88
 089                if (valueStart >= 0)
 90                {
 091                    token = slice.Substring((1 - tokenPositionModifier), (valueStart - 1 + tokenPositionModifier));
 092                    value = slice.Substring(valueStart + 1);
 93
 94                    // Guard against leading and trailing spaces, only trimming if there is a need.
 95
 096                    if ((!string.IsNullOrEmpty(token)) && (char.IsWhiteSpace(token[0])) || char.IsWhiteSpace(token[token
 97                    {
 098                        token = token.Trim();
 99                    }
 100
 0101                    if ((!string.IsNullOrEmpty(value)) && (char.IsWhiteSpace(value[0]) || char.IsWhiteSpace(value[value.
 102                    {
 0103                        value = value.Trim();
 104                    }
 105
 106                    // If there was no value for a key, then consider the connection string to
 107                    // be malformed.
 108
 0109                    if (string.IsNullOrEmpty(value))
 110                    {
 0111                        throw new FormatException(Resources.InvalidConnectionString);
 112                    }
 113
 114                    // Compare the token against the known connection string properties and capture the
 115                    // pair if they are a known attribute.
 116
 0117                    if (string.Compare(EndpointToken, token, StringComparison.OrdinalIgnoreCase) == 0)
 118                    {
 0119                        parsedValues.EndpointToken = new UriBuilder(value)
 0120                        {
 0121                            Scheme = EventHubsEndpointScheme,
 0122                            Port = -1
 0123                        };
 124
 0125                        if ((string.Compare(parsedValues.EndpointToken.Scheme, EventHubsEndpointSchemeName, StringCompar
 0126                            || (Uri.CheckHostName(parsedValues.EndpointToken.Host) == UriHostNameType.Unknown))
 127                        {
 0128                            throw new FormatException(Resources.InvalidConnectionString);
 129                        }
 130                    }
 0131                    else if (string.Compare(EventHubNameToken, token, StringComparison.OrdinalIgnoreCase) == 0)
 132                    {
 0133                        parsedValues.EventHubNameToken = value;
 134                    }
 0135                    else if (string.Compare(SharedAccessKeyNameToken, token, StringComparison.OrdinalIgnoreCase) == 0)
 136                    {
 0137                        parsedValues.SharedAccessKeyNameToken = value;
 138                    }
 0139                    else if (string.Compare(SharedAccessKeyValueToken, token, StringComparison.OrdinalIgnoreCase) == 0)
 140                    {
 0141                        parsedValues.SharedAccessKeyValueToken = value;
 142                    }
 143                }
 0144                else if ((slice.Length != 1) || (slice[0] != TokenValuePairDelimiter))
 145                {
 146                    // This wasn't a legal pair and it is not simply a trailing delimiter; consider
 147                    // the connection string to be malformed.
 148
 0149                    throw new FormatException(Resources.InvalidConnectionString);
 150                }
 151
 0152                tokenPositionModifier = 0;
 0153                lastPosition = currentPosition;
 154            }
 155
 0156            return new ConnectionStringProperties
 0157            (
 0158                parsedValues.EndpointToken?.Uri,
 0159                parsedValues.EventHubNameToken,
 0160                parsedValues.SharedAccessKeyNameToken,
 0161                parsedValues.SharedAccessKeyValueToken
 0162            );
 163        }
 164    }
 165}

Methods/Properties

.cctor()
Parse(...)