| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using System; |
| | 5 | | using Azure.Core; |
| | 6 | |
|
| | 7 | | namespace 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> |
| 0 | 37 | | 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 | | { |
| 0 | 51 | | Argument.AssertNotNullOrEmpty(connectionString, nameof(connectionString)); |
| | 52 | |
|
| 0 | 53 | | int tokenPositionModifier = (connectionString[0] == TokenValuePairDelimiter) ? 0 : 1; |
| 0 | 54 | | int lastPosition = 0; |
| 0 | 55 | | int currentPosition = 0; |
| | 56 | | int valueStart; |
| | 57 | |
|
| | 58 | | string slice; |
| | 59 | | string token; |
| | 60 | | string value; |
| | 61 | |
|
| 0 | 62 | | var parsedValues = |
| 0 | 63 | | ( |
| 0 | 64 | | EndpointToken: default(UriBuilder), |
| 0 | 65 | | EventHubNameToken: default(string), |
| 0 | 66 | | SharedAccessKeyNameToken: default(string), |
| 0 | 67 | | SharedAccessKeyValueToken: default(string) |
| 0 | 68 | | ); |
| | 69 | |
|
| 0 | 70 | | while (currentPosition != -1) |
| | 71 | | { |
| | 72 | | // Slice the string into the next token/value pair. |
| | 73 | |
|
| 0 | 74 | | currentPosition = connectionString.IndexOf(TokenValuePairDelimiter, lastPosition + 1); |
| | 75 | |
|
| 0 | 76 | | if (currentPosition >= 0) |
| | 77 | | { |
| 0 | 78 | | slice = connectionString.Substring(lastPosition, (currentPosition - lastPosition)); |
| | 79 | | } |
| | 80 | | else |
| | 81 | | { |
| 0 | 82 | | slice = connectionString.Substring(lastPosition); |
| | 83 | | } |
| | 84 | |
|
| | 85 | | // Break the token and value apart, if this is a legal pair. |
| | 86 | |
|
| 0 | 87 | | valueStart = slice.IndexOf(TokenValueSeparator); |
| | 88 | |
|
| 0 | 89 | | if (valueStart >= 0) |
| | 90 | | { |
| 0 | 91 | | token = slice.Substring((1 - tokenPositionModifier), (valueStart - 1 + tokenPositionModifier)); |
| 0 | 92 | | value = slice.Substring(valueStart + 1); |
| | 93 | |
|
| | 94 | | // Guard against leading and trailing spaces, only trimming if there is a need. |
| | 95 | |
|
| 0 | 96 | | if ((!string.IsNullOrEmpty(token)) && (char.IsWhiteSpace(token[0])) || char.IsWhiteSpace(token[token |
| | 97 | | { |
| 0 | 98 | | token = token.Trim(); |
| | 99 | | } |
| | 100 | |
|
| 0 | 101 | | if ((!string.IsNullOrEmpty(value)) && (char.IsWhiteSpace(value[0]) || char.IsWhiteSpace(value[value. |
| | 102 | | { |
| 0 | 103 | | value = value.Trim(); |
| | 104 | | } |
| | 105 | |
|
| | 106 | | // If there was no value for a key, then consider the connection string to |
| | 107 | | // be malformed. |
| | 108 | |
|
| 0 | 109 | | if (string.IsNullOrEmpty(value)) |
| | 110 | | { |
| 0 | 111 | | 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 | |
|
| 0 | 117 | | if (string.Compare(EndpointToken, token, StringComparison.OrdinalIgnoreCase) == 0) |
| | 118 | | { |
| 0 | 119 | | parsedValues.EndpointToken = new UriBuilder(value) |
| 0 | 120 | | { |
| 0 | 121 | | Scheme = EventHubsEndpointScheme, |
| 0 | 122 | | Port = -1 |
| 0 | 123 | | }; |
| | 124 | |
|
| 0 | 125 | | if ((string.Compare(parsedValues.EndpointToken.Scheme, EventHubsEndpointSchemeName, StringCompar |
| 0 | 126 | | || (Uri.CheckHostName(parsedValues.EndpointToken.Host) == UriHostNameType.Unknown)) |
| | 127 | | { |
| 0 | 128 | | throw new FormatException(Resources.InvalidConnectionString); |
| | 129 | | } |
| | 130 | | } |
| 0 | 131 | | else if (string.Compare(EventHubNameToken, token, StringComparison.OrdinalIgnoreCase) == 0) |
| | 132 | | { |
| 0 | 133 | | parsedValues.EventHubNameToken = value; |
| | 134 | | } |
| 0 | 135 | | else if (string.Compare(SharedAccessKeyNameToken, token, StringComparison.OrdinalIgnoreCase) == 0) |
| | 136 | | { |
| 0 | 137 | | parsedValues.SharedAccessKeyNameToken = value; |
| | 138 | | } |
| 0 | 139 | | else if (string.Compare(SharedAccessKeyValueToken, token, StringComparison.OrdinalIgnoreCase) == 0) |
| | 140 | | { |
| 0 | 141 | | parsedValues.SharedAccessKeyValueToken = value; |
| | 142 | | } |
| | 143 | | } |
| 0 | 144 | | 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 | |
|
| 0 | 149 | | throw new FormatException(Resources.InvalidConnectionString); |
| | 150 | | } |
| | 151 | |
|
| 0 | 152 | | tokenPositionModifier = 0; |
| 0 | 153 | | lastPosition = currentPosition; |
| | 154 | | } |
| | 155 | |
|
| 0 | 156 | | return new ConnectionStringProperties |
| 0 | 157 | | ( |
| 0 | 158 | | parsedValues.EndpointToken?.Uri, |
| 0 | 159 | | parsedValues.EventHubNameToken, |
| 0 | 160 | | parsedValues.SharedAccessKeyNameToken, |
| 0 | 161 | | parsedValues.SharedAccessKeyValueToken |
| 0 | 162 | | ); |
| | 163 | | } |
| | 164 | | } |
| | 165 | | } |