| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using System; |
| | 5 | | using System.Diagnostics; |
| | 6 | | using System.Text; |
| | 7 | |
|
| | 8 | | namespace Azure.Core |
| | 9 | | { |
| | 10 | | /// <summary> |
| | 11 | | /// Provides a custom constructor for uniform resource identifiers (URIs) and modifies URIs for the <see cref="Syste |
| | 12 | | /// </summary> |
| | 13 | | public class RequestUriBuilder |
| | 14 | | { |
| | 15 | | private const char QuerySeparator = '?'; |
| | 16 | |
|
| | 17 | | private const char PathSeparator = '/'; |
| | 18 | |
|
| 3966 | 19 | | private readonly StringBuilder _pathAndQuery = new StringBuilder(); |
| | 20 | |
|
| 3966 | 21 | | private int _queryIndex = -1; |
| | 22 | |
|
| | 23 | | private Uri? _uri; |
| | 24 | |
|
| | 25 | | private int _port; |
| | 26 | |
|
| | 27 | | private string? _host; |
| | 28 | |
|
| | 29 | | private string? _scheme; |
| | 30 | |
|
| | 31 | | /// <summary> |
| | 32 | | /// Gets or sets the scheme name of the URI. |
| | 33 | | /// </summary> |
| | 34 | | public string? Scheme |
| | 35 | | { |
| 3656 | 36 | | get => _scheme; |
| | 37 | | set |
| | 38 | | { |
| 3864 | 39 | | ResetUri(); |
| 3864 | 40 | | _scheme = value; |
| 3864 | 41 | | } |
| | 42 | | } |
| | 43 | |
|
| | 44 | | /// <summary> |
| | 45 | | /// Gets or sets the Domain Name System (DNS) host name or IP address of a server. |
| | 46 | | /// </summary> |
| | 47 | | public string? Host |
| | 48 | | { |
| 2382 | 49 | | get => _host; |
| | 50 | | set |
| | 51 | | { |
| 3864 | 52 | | ResetUri(); |
| 3864 | 53 | | _host = value; |
| 3864 | 54 | | } |
| | 55 | | } |
| | 56 | |
|
| | 57 | | /// <summary> |
| | 58 | | /// Gets or sets the port number of the URI. |
| | 59 | | /// </summary> |
| | 60 | | public int Port |
| | 61 | | { |
| 6548 | 62 | | get => _port; |
| | 63 | | set |
| | 64 | | { |
| 3864 | 65 | | ResetUri(); |
| 3864 | 66 | | _port = value; |
| 3864 | 67 | | } |
| | 68 | | } |
| | 69 | |
|
| | 70 | | /// <summary> |
| | 71 | | /// Gets or sets any query information included in the URI. |
| | 72 | | /// </summary> |
| | 73 | | public string Query |
| | 74 | | { |
| 24 | 75 | | get => HasQuery ? _pathAndQuery.ToString(_queryIndex, _pathAndQuery.Length - _queryIndex) : string.Empty; |
| | 76 | | set |
| | 77 | | { |
| 3916 | 78 | | ResetUri(); |
| 3916 | 79 | | if (HasQuery) |
| | 80 | | { |
| 72 | 81 | | _pathAndQuery.Remove(_queryIndex, _pathAndQuery.Length - _queryIndex); |
| 72 | 82 | | _queryIndex = -1; |
| | 83 | | } |
| | 84 | |
|
| 3916 | 85 | | if (!string.IsNullOrEmpty(value)) |
| | 86 | | { |
| 100 | 87 | | _queryIndex = _pathAndQuery.Length; |
| 100 | 88 | | if (value[0] != QuerySeparator) |
| | 89 | | { |
| 10 | 90 | | _pathAndQuery.Append(QuerySeparator); |
| | 91 | | } |
| 100 | 92 | | _pathAndQuery.Append(value); |
| | 93 | | } |
| 3916 | 94 | | } |
| | 95 | | } |
| | 96 | |
|
| | 97 | | /// <summary> |
| | 98 | | /// Gets or sets the password associated with the user that accesses the URI and the query information. |
| | 99 | | /// </summary> |
| | 100 | | public string Path |
| | 101 | | { |
| 24 | 102 | | get => HasQuery ? _pathAndQuery.ToString(0, _queryIndex) : _pathAndQuery.ToString(); |
| | 103 | | set |
| | 104 | | { |
| 3828 | 105 | | if (HasQuery) |
| | 106 | | { |
| 0 | 107 | | _pathAndQuery.Remove(0, _queryIndex); |
| 0 | 108 | | _pathAndQuery.Insert(0, value); |
| 0 | 109 | | _queryIndex = value.Length; |
| | 110 | | } |
| | 111 | | else |
| | 112 | | { |
| 3828 | 113 | | _pathAndQuery.Remove(0, _pathAndQuery.Length); |
| 3828 | 114 | | _pathAndQuery.Append(value); |
| | 115 | | } |
| 3828 | 116 | | } |
| | 117 | | } |
| | 118 | |
|
| 10512 | 119 | | private bool HasQuery => _queryIndex != -1; |
| | 120 | |
|
| 102 | 121 | | private int QueryLength => HasQuery ? _pathAndQuery.Length - _queryIndex : 0; |
| | 122 | |
|
| 38 | 123 | | private int PathLength => HasQuery ? _queryIndex : _pathAndQuery.Length; |
| | 124 | |
|
| | 125 | | /// <summary> |
| | 126 | | /// Gets the path to the resource referenced by the URI. |
| | 127 | | /// </summary> |
| 0 | 128 | | public string PathAndQuery => _pathAndQuery.ToString(); |
| | 129 | |
|
| | 130 | | /// <summary> |
| | 131 | | /// Replaces values inside this instance with values provided in <paramref name="value"/> parameter. |
| | 132 | | /// </summary> |
| | 133 | | /// <param name="value">The <see cref="Uri"/> instance to get values from.</param> |
| | 134 | | public void Reset(Uri value) |
| | 135 | | { |
| 3792 | 136 | | Scheme = value.Scheme; |
| 3792 | 137 | | Host = value.Host; |
| 3792 | 138 | | Port = value.Port; |
| 3792 | 139 | | Path = value.AbsolutePath; |
| 3792 | 140 | | Query = value.Query; |
| 3792 | 141 | | _uri = value; |
| 3792 | 142 | | } |
| | 143 | |
|
| | 144 | | /// <summary> |
| | 145 | | /// Gets the <see cref="System.Uri"></see> instance constructed by the specified <see cref="RequestUriBuilder"/> |
| | 146 | | /// </summary> |
| | 147 | | /// <returns> |
| | 148 | | /// A <see cref="System.Uri"></see> that contains the URI constructed by the <see cref="RequestUriBuilder"/>. |
| | 149 | | /// </returns> |
| | 150 | | public Uri ToUri() |
| | 151 | | { |
| 3366 | 152 | | if (_uri == null) |
| | 153 | | { |
| 182 | 154 | | _uri = new Uri(ToString()); |
| | 155 | | } |
| | 156 | |
|
| 3366 | 157 | | return _uri; |
| | 158 | | } |
| | 159 | |
|
| | 160 | | /// <summary> |
| | 161 | | /// Appends a query parameter adding separator if required. Escapes the value. |
| | 162 | | /// </summary> |
| | 163 | | /// <param name="name">The name of parameter.</param> |
| | 164 | | /// <param name="value">The value of parameter.</param> |
| | 165 | | public void AppendQuery(string name, string value) |
| | 166 | | { |
| 184 | 167 | | AppendQuery(name, value, true); |
| 184 | 168 | | } |
| | 169 | |
|
| | 170 | | /// <summary> |
| | 171 | | /// Appends a query parameter adding separator if required. |
| | 172 | | /// </summary> |
| | 173 | | /// <param name="name">The name of parameter.</param> |
| | 174 | | /// <param name="value">The value of parameter.</param> |
| | 175 | | /// <param name="escapeValue">Whether value should be escaped.</param> |
| | 176 | | public void AppendQuery(string name, string value, bool escapeValue) |
| | 177 | | { |
| 184 | 178 | | ResetUri(); |
| 184 | 179 | | if (!HasQuery) |
| | 180 | | { |
| 82 | 181 | | _queryIndex = _pathAndQuery.Length; |
| 82 | 182 | | _pathAndQuery.Append(QuerySeparator); |
| | 183 | | } |
| 102 | 184 | | else if (!(QueryLength == 1 && _pathAndQuery[_queryIndex] == QuerySeparator)) |
| | 185 | | { |
| 100 | 186 | | _pathAndQuery.Append('&'); |
| | 187 | | } |
| | 188 | |
|
| 184 | 189 | | _pathAndQuery.Append(name); |
| 184 | 190 | | _pathAndQuery.Append('='); |
| 184 | 191 | | if (escapeValue && !string.IsNullOrEmpty(value)) |
| | 192 | | { |
| 182 | 193 | | value = Uri.EscapeDataString(value); |
| | 194 | | } |
| 184 | 195 | | _pathAndQuery.Append(value); |
| | 196 | |
|
| | 197 | | Debug.Assert(_pathAndQuery[_queryIndex] == QuerySeparator); |
| 184 | 198 | | } |
| | 199 | |
|
| | 200 | | /// <summary> |
| | 201 | | /// Appends escaped <paramref name="value"/> to <see cref="Path"/> without adding path separator. |
| | 202 | | /// </summary> |
| | 203 | | /// <param name="value">The value to append.</param> |
| | 204 | | public void AppendPath(string value) |
| | 205 | | { |
| 26 | 206 | | AppendPath(value, escape: true); |
| 26 | 207 | | } |
| | 208 | |
|
| | 209 | | /// <summary> |
| | 210 | | /// Appends optionally escaped <paramref name="value"/> to <see cref="Path"/> without adding path separator. |
| | 211 | | /// </summary> |
| | 212 | | /// <param name="value">The value to append.</param> |
| | 213 | | /// <param name="escape">Whether value should be escaped.</param> |
| | 214 | | public void AppendPath(string value, bool escape) |
| | 215 | | { |
| 40 | 216 | | if (string.IsNullOrEmpty(value)) |
| | 217 | | { |
| 2 | 218 | | return; |
| | 219 | | } |
| | 220 | |
|
| 38 | 221 | | ResetUri(); |
| 38 | 222 | | int startIndex = 0; |
| 38 | 223 | | if (PathLength == 1 && _pathAndQuery[0] == PathSeparator && value[0] == PathSeparator) |
| | 224 | | { |
| 6 | 225 | | startIndex = 1; |
| | 226 | | } |
| 38 | 227 | | if (HasQuery) |
| | 228 | | { |
| 4 | 229 | | string substring = value.Substring(startIndex, value.Length - startIndex); |
| 4 | 230 | | if (escape) |
| | 231 | | { |
| 4 | 232 | | substring = Uri.EscapeDataString(substring); |
| | 233 | | } |
| 4 | 234 | | _pathAndQuery.Insert(_queryIndex, substring); |
| 4 | 235 | | _queryIndex += value.Length; |
| | 236 | | } |
| | 237 | | else |
| | 238 | | { |
| 34 | 239 | | if (escape) |
| | 240 | | { |
| 22 | 241 | | string substring = value.Substring(startIndex, value.Length - startIndex); |
| 22 | 242 | | substring = Uri.EscapeDataString(substring); |
| 22 | 243 | | _pathAndQuery.Append(substring); |
| | 244 | | } |
| | 245 | | else |
| | 246 | | { |
| 12 | 247 | | _pathAndQuery.Append(value, startIndex, value.Length - startIndex); |
| | 248 | | } |
| | 249 | | } |
| 12 | 250 | | } |
| | 251 | |
|
| | 252 | | /// <summary> |
| | 253 | | /// Returns a string representation of this <see cref="RequestUriBuilder"/> i. |
| | 254 | | /// </summary> |
| | 255 | | /// <returns></returns> |
| | 256 | | public override string ToString() |
| | 257 | | { |
| 2358 | 258 | | var stringBuilder = new StringBuilder(); |
| 2358 | 259 | | stringBuilder.Append(Scheme); |
| 2358 | 260 | | stringBuilder.Append("://"); |
| 2358 | 261 | | stringBuilder.Append(Host); |
| 2358 | 262 | | if (!HasDefaultPortForScheme) |
| | 263 | | { |
| 2040 | 264 | | stringBuilder.Append(':'); |
| 2040 | 265 | | stringBuilder.Append(Port); |
| | 266 | | } |
| | 267 | |
|
| 2358 | 268 | | if (_pathAndQuery.Length == 0 || _pathAndQuery[0] != PathSeparator) |
| | 269 | | { |
| 70 | 270 | | stringBuilder.Append(PathSeparator); |
| | 271 | | } |
| | 272 | |
|
| | 273 | | // TODO: Escaping can be done in-place |
| 2358 | 274 | | if (!HasQuery) |
| | 275 | | { |
| 2248 | 276 | | stringBuilder.Append(_pathAndQuery); |
| | 277 | | } |
| | 278 | | else |
| | 279 | | { |
| 110 | 280 | | stringBuilder.Append(_pathAndQuery.ToString(0, _queryIndex)); |
| 110 | 281 | | stringBuilder.Append(_pathAndQuery.ToString(_queryIndex, _pathAndQuery.Length - _queryIndex)); |
| | 282 | | } |
| | 283 | |
|
| 2358 | 284 | | return stringBuilder.ToString(); |
| | 285 | | } |
| | 286 | |
|
| | 287 | | private bool HasDefaultPortForScheme => |
| 2358 | 288 | | (Port == 80 && string.Equals(Scheme, "http", StringComparison.InvariantCultureIgnoreCase)) || |
| 2358 | 289 | | (Port == 443 && string.Equals(Scheme, "https", StringComparison.InvariantCultureIgnoreCase)); |
| | 290 | |
|
| | 291 | | private void ResetUri() |
| | 292 | | { |
| 15730 | 293 | | _uri = null; |
| 15730 | 294 | | } |
| | 295 | | } |
| | 296 | | } |