| | 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.ServiceBus.Core |
| | 8 | | { |
| | 9 | | /// <summary> |
| | 10 | | /// Allows for parsing Service Bus connection strings. |
| | 11 | | /// </summary> |
| | 12 | | /// |
| | 13 | | internal static class ConnectionStringParser |
| | 14 | | { |
| | 15 | | /// <summary>The character used to separate a token and its value in the connection string.</summary> |
| | 16 | | private const char TokenValueSeparator = '='; |
| | 17 | |
|
| | 18 | | /// <summary>The character used to mark the beginning of a new token/value pair in the connection string.</summa |
| | 19 | | private const char TokenValuePairDelimiter = ';'; |
| | 20 | |
|
| | 21 | | /// <summary>The formatted protocol used by an Service Bus endpoint. </summary> |
| | 22 | | private const string ServiceBusEndpointScheme = "sb://"; |
| | 23 | |
|
| | 24 | | /// <summary>The token that identifies the endpoint address for the Service Bus namespace.</summary> |
| | 25 | | private const string EndpointToken = "Endpoint"; |
| | 26 | |
|
| | 27 | | /// <summary>The token that identifies the name of a specific Service Bus entity under the namespace.</summary> |
| | 28 | | private const string EntityName = "EntityPath"; |
| | 29 | |
|
| | 30 | | /// <summary>The token that identifies the name of a shared access key.</summary> |
| | 31 | | private const string SharedAccessKeyNameToken = "SharedAccessKeyName"; |
| | 32 | |
|
| | 33 | | /// <summary>The token that identifies the value of a shared access key.</summary> |
| | 34 | | private const string SharedAccessKeyValueToken = "SharedAccessKey"; |
| | 35 | |
|
| | 36 | | /// <summary> |
| | 37 | | /// Parses the specified Service Bus connection string into its component properties. |
| | 38 | | /// </summary> |
| | 39 | | /// |
| | 40 | | /// <param name="connectionString">The connection string to parse.</param> |
| | 41 | | /// |
| | 42 | | /// <returns>The component properties parsed from the connection string.</returns> |
| | 43 | | /// |
| | 44 | | /// <seealso cref="ConnectionStringProperties" /> |
| | 45 | | /// |
| | 46 | | public static ConnectionStringProperties Parse(string connectionString) |
| | 47 | | { |
| 108 | 48 | | Argument.AssertNotNullOrEmpty(connectionString, nameof(connectionString)); |
| | 49 | |
|
| 108 | 50 | | int tokenPositionModifier = (connectionString[0] == TokenValuePairDelimiter) ? 0 : 1; |
| 108 | 51 | | int lastPosition = 0; |
| 108 | 52 | | int currentPosition = 0; |
| | 53 | | int valueStart; |
| | 54 | |
|
| | 55 | | string slice; |
| | 56 | | string token; |
| | 57 | | string value; |
| | 58 | |
|
| 108 | 59 | | var parsedValues = |
| 108 | 60 | | ( |
| 108 | 61 | | EndpointToken: default(UriBuilder), |
| 108 | 62 | | EntityNameToken: default(string), |
| 108 | 63 | | SharedAccessKeyNameToken: default(string), |
| 108 | 64 | | SharedAccessKeyValueToken: default(string) |
| 108 | 65 | | ); |
| | 66 | |
|
| 458 | 67 | | while (currentPosition != -1) |
| | 68 | | { |
| | 69 | | // Slice the string into the next token/value pair. |
| | 70 | |
|
| 352 | 71 | | currentPosition = connectionString.IndexOf(TokenValuePairDelimiter, lastPosition + 1); |
| | 72 | |
|
| 352 | 73 | | if (currentPosition >= 0) |
| | 74 | | { |
| 246 | 75 | | slice = connectionString.Substring(lastPosition, (currentPosition - lastPosition)); |
| | 76 | | } |
| | 77 | | else |
| | 78 | | { |
| 106 | 79 | | slice = connectionString.Substring(lastPosition); |
| | 80 | | } |
| | 81 | |
|
| | 82 | | // Break the token and value apart, if this is a legal pair. |
| | 83 | |
|
| 352 | 84 | | valueStart = slice.IndexOf(TokenValueSeparator); |
| | 85 | |
|
| 352 | 86 | | if (valueStart >= 0) |
| | 87 | | { |
| 342 | 88 | | token = slice.Substring((1 - tokenPositionModifier), (valueStart - 1 + tokenPositionModifier)); |
| 342 | 89 | | value = slice.Substring(valueStart + 1); |
| | 90 | |
|
| | 91 | | // Guard against leading and trailing spaces, only trimming if there is a need. |
| | 92 | |
|
| 342 | 93 | | if ((!string.IsNullOrEmpty(token)) && (char.IsWhiteSpace(token[0])) || char.IsWhiteSpace(token[token |
| | 94 | | { |
| 0 | 95 | | token = token.Trim(); |
| | 96 | | } |
| | 97 | |
|
| 342 | 98 | | if ((!string.IsNullOrEmpty(value)) && (char.IsWhiteSpace(value[0]) || char.IsWhiteSpace(value[value. |
| | 99 | | { |
| 12 | 100 | | value = value.Trim(); |
| | 101 | | } |
| | 102 | |
|
| | 103 | | // If there was no value for a key, then consider the connection string to |
| | 104 | | // be malformed. |
| | 105 | |
|
| 342 | 106 | | if (string.IsNullOrEmpty(value)) |
| | 107 | | { |
| 0 | 108 | | throw new FormatException(Resources.InvalidConnectionString); |
| | 109 | | } |
| | 110 | |
|
| | 111 | | // Compare the token against the known connection string properties and capture the |
| | 112 | | // pair if they are a known attribute. |
| | 113 | |
|
| 342 | 114 | | if (string.Compare(EndpointToken, token, StringComparison.OrdinalIgnoreCase) == 0) |
| | 115 | | { |
| 106 | 116 | | parsedValues.EndpointToken = new UriBuilder(value) |
| 106 | 117 | | { |
| 106 | 118 | | Scheme = ServiceBusEndpointScheme, |
| 106 | 119 | | Port = -1 |
| 106 | 120 | | }; |
| | 121 | | } |
| 236 | 122 | | else if (string.Compare(EntityName, token, StringComparison.OrdinalIgnoreCase) == 0) |
| | 123 | | { |
| 28 | 124 | | parsedValues.EntityNameToken = value; |
| | 125 | | } |
| 208 | 126 | | else if (string.Compare(SharedAccessKeyNameToken, token, StringComparison.OrdinalIgnoreCase) == 0) |
| | 127 | | { |
| 102 | 128 | | parsedValues.SharedAccessKeyNameToken = value; |
| | 129 | | } |
| 106 | 130 | | else if (string.Compare(SharedAccessKeyValueToken, token, StringComparison.OrdinalIgnoreCase) == 0) |
| | 131 | | { |
| 102 | 132 | | parsedValues.SharedAccessKeyValueToken = value; |
| | 133 | | } |
| | 134 | | } |
| 10 | 135 | | else if ((slice.Length != 1) || (slice[0] != TokenValuePairDelimiter)) |
| | 136 | | { |
| | 137 | | // This wasn't a legal pair and it is not simply a trailing delimiter; consider |
| | 138 | | // the connection string to be malformed. |
| | 139 | |
|
| 0 | 140 | | throw new FormatException(Resources.InvalidConnectionString); |
| | 141 | | } |
| | 142 | |
|
| 350 | 143 | | tokenPositionModifier = 0; |
| 350 | 144 | | lastPosition = currentPosition; |
| | 145 | | } |
| | 146 | |
|
| 106 | 147 | | return new ConnectionStringProperties |
| 106 | 148 | | ( |
| 106 | 149 | | parsedValues.EndpointToken?.Uri, |
| 106 | 150 | | parsedValues.EntityNameToken, |
| 106 | 151 | | parsedValues.SharedAccessKeyNameToken, |
| 106 | 152 | | parsedValues.SharedAccessKeyValueToken |
| 106 | 153 | | ); |
| | 154 | | } |
| | 155 | | } |
| | 156 | | } |