|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | +// This has been copied from https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/core/Azure.Core/src/Shared/ConnectionString.cs |
| 4 | + |
| 5 | +#nullable enable |
| 6 | + |
| 7 | +namespace Microsoft.ApplicationInsights.Internal |
| 8 | +{ |
| 9 | + using System; |
| 10 | + using System.Collections.Generic; |
| 11 | + using System.Text; |
| 12 | + |
| 13 | + internal sealed class ConnectionString |
| 14 | + { |
| 15 | + private readonly Dictionary<string, string> pairs; |
| 16 | + private readonly string pairSeparator; |
| 17 | + private readonly string keywordValueSeparator; |
| 18 | + |
| 19 | + private ConnectionString(Dictionary<string, string> pairs, string pairSeparator, string keywordValueSeparator) |
| 20 | + { |
| 21 | + this.pairs = pairs; |
| 22 | + this.pairSeparator = pairSeparator; |
| 23 | + this.keywordValueSeparator = keywordValueSeparator; |
| 24 | + } |
| 25 | + |
| 26 | + public static ConnectionString Parse(string connectionString, string segmentSeparator = ";", string keywordValueSeparator = "=", bool allowEmptyValues = false) |
| 27 | + { |
| 28 | + Validate(connectionString, segmentSeparator, keywordValueSeparator, allowEmptyValues); |
| 29 | + return new ConnectionString(ParseSegments(connectionString, segmentSeparator, keywordValueSeparator), segmentSeparator, keywordValueSeparator); |
| 30 | + } |
| 31 | + |
| 32 | + public static ConnectionString Empty(string segmentSeparator = ";", string keywordValueSeparator = "=") => |
| 33 | + new (new Dictionary<string, string>(), segmentSeparator, keywordValueSeparator); |
| 34 | + |
| 35 | + public string GetRequired(string keyword) => |
| 36 | + this.pairs.TryGetValue(keyword, out var value) ? value : throw new InvalidOperationException($"Required keyword '{keyword}' is missing in connection string."); |
| 37 | + |
| 38 | + public string? GetNonRequired(string keyword) => |
| 39 | + this.pairs.TryGetValue(keyword, out var value) ? value : null; |
| 40 | + |
| 41 | + public bool TryGetSegmentValue(string keyword, out string? value) => |
| 42 | + this.pairs.TryGetValue(keyword, out value); |
| 43 | + |
| 44 | + public string? GetSegmentValueOrDefault(string keyword, string defaultValue) => |
| 45 | + this.pairs.TryGetValue(keyword, out var value) switch { |
| 46 | + false => defaultValue, |
| 47 | + true => value |
| 48 | + }; |
| 49 | + |
| 50 | + public bool ContainsSegmentKey(string keyword) => |
| 51 | + this.pairs.ContainsKey(keyword); |
| 52 | + |
| 53 | + public void Replace(string keyword, string value) |
| 54 | + { |
| 55 | + if (this.pairs.ContainsKey(keyword)) |
| 56 | + { |
| 57 | + this.pairs[keyword] = value; |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + public void Add(string keyword, string value) => |
| 62 | + this.pairs.Add(keyword, value); |
| 63 | + |
| 64 | + public override string ToString() |
| 65 | + { |
| 66 | + if (this.pairs.Count == 0) |
| 67 | + { |
| 68 | + return string.Empty; |
| 69 | + } |
| 70 | + |
| 71 | + var stringBuilder = new StringBuilder(); |
| 72 | + var isFirst = true; |
| 73 | + foreach (KeyValuePair<string, string> pair in this.pairs) |
| 74 | + { |
| 75 | + if (isFirst) |
| 76 | + { |
| 77 | + isFirst = false; |
| 78 | + } |
| 79 | + else |
| 80 | + { |
| 81 | + stringBuilder.Append(this.pairSeparator); |
| 82 | + } |
| 83 | + |
| 84 | + stringBuilder.Append(pair.Key); |
| 85 | + if (pair.Value != null) |
| 86 | + { |
| 87 | + stringBuilder.Append(this.keywordValueSeparator).Append(pair.Value); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + return stringBuilder.ToString(); |
| 92 | + } |
| 93 | + |
| 94 | + private static Dictionary<string, string> ParseSegments(in string connectionString, in string separator, in string keywordValueSeparator) |
| 95 | + { |
| 96 | + var pairs = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase); |
| 97 | + |
| 98 | + var segmentStart = -1; |
| 99 | + var segmentEnd = 0; |
| 100 | + |
| 101 | + while (TryGetNextSegment(connectionString, separator, ref segmentStart, ref segmentEnd)) |
| 102 | + { |
| 103 | + var kvSeparatorIndex = connectionString.IndexOf(keywordValueSeparator, segmentStart, segmentEnd - segmentStart, StringComparison.Ordinal); |
| 104 | + int keywordStart = GetStart(connectionString, segmentStart); |
| 105 | + int keyLength = GetLength(connectionString, keywordStart, kvSeparatorIndex); |
| 106 | + |
| 107 | + var keyword = connectionString.Substring(keywordStart, keyLength); |
| 108 | + if (pairs.ContainsKey(keyword)) |
| 109 | + { |
| 110 | + throw new InvalidOperationException($"Duplicated keyword '{keyword}'"); |
| 111 | + } |
| 112 | + |
| 113 | + var valueStart = GetStart(connectionString, kvSeparatorIndex + keywordValueSeparator.Length); |
| 114 | + var valueLength = GetLength(connectionString, valueStart, segmentEnd); |
| 115 | + pairs.Add(keyword, connectionString.Substring(valueStart, valueLength)); |
| 116 | + } |
| 117 | + |
| 118 | + return pairs; |
| 119 | + |
| 120 | + static int GetStart(in string str, int start) |
| 121 | + { |
| 122 | + while (start < str.Length && char.IsWhiteSpace(str[start])) |
| 123 | + { |
| 124 | + start++; |
| 125 | + } |
| 126 | + |
| 127 | + return start; |
| 128 | + } |
| 129 | + |
| 130 | + static int GetLength(in string str, in int start, int end) |
| 131 | + { |
| 132 | + while (end > start && char.IsWhiteSpace(str[end - 1])) |
| 133 | + { |
| 134 | + end--; |
| 135 | + } |
| 136 | + |
| 137 | + return end - start; |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + private static bool TryGetNextSegment(in string str, in string separator, ref int start, ref int end) |
| 142 | + { |
| 143 | + if (start == -1) |
| 144 | + { |
| 145 | + start = 0; |
| 146 | + } |
| 147 | + else |
| 148 | + { |
| 149 | + start = end + separator.Length; |
| 150 | + if (start >= str.Length) |
| 151 | + { |
| 152 | + return false; |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + end = str.IndexOf(separator, start, StringComparison.Ordinal); |
| 157 | + if (end == -1) |
| 158 | + { |
| 159 | + end = str.Length; |
| 160 | + } |
| 161 | + |
| 162 | + return true; |
| 163 | + } |
| 164 | + |
| 165 | + private static void Validate(string connectionString, string segmentSeparator, string keywordValueSeparator, bool allowEmptyValues) |
| 166 | + { |
| 167 | + var segmentStart = -1; |
| 168 | + var segmentEnd = 0; |
| 169 | + |
| 170 | + while (TryGetNextSegment(connectionString, segmentSeparator, ref segmentStart, ref segmentEnd)) |
| 171 | + { |
| 172 | + if (segmentStart == segmentEnd) |
| 173 | + { |
| 174 | + if (segmentStart == 0) |
| 175 | + { |
| 176 | + throw new InvalidOperationException($"Connection string starts with separator '{segmentSeparator}'."); |
| 177 | + } |
| 178 | + |
| 179 | + throw new InvalidOperationException($"Connection string contains two following separators '{segmentSeparator}'."); |
| 180 | + } |
| 181 | + |
| 182 | + var kvSeparatorIndex = connectionString.IndexOf(keywordValueSeparator, segmentStart, segmentEnd - segmentStart, StringComparison.Ordinal); |
| 183 | + if (kvSeparatorIndex == -1) |
| 184 | + { |
| 185 | + throw new InvalidOperationException($"Connection string doesn't have value for keyword '{connectionString.Substring(segmentStart, segmentEnd - segmentStart)}'."); |
| 186 | + } |
| 187 | + |
| 188 | + if (segmentStart == kvSeparatorIndex) |
| 189 | + { |
| 190 | + throw new InvalidOperationException($"Connection string has value '{connectionString.Substring(segmentStart, kvSeparatorIndex - segmentStart)}' with no keyword."); |
| 191 | + } |
| 192 | + |
| 193 | + if (!allowEmptyValues && kvSeparatorIndex + 1 == segmentEnd) |
| 194 | + { |
| 195 | + throw new InvalidOperationException($"Connection string has keyword '{connectionString.Substring(segmentStart, kvSeparatorIndex - segmentStart)}' with empty value."); |
| 196 | + } |
| 197 | + } |
| 198 | + } |
| 199 | + } |
| 200 | +} |
0 commit comments