| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using System; |
| | 5 | | using System.Net; |
| | 6 | | using System.Text; |
| | 7 | | using Azure.Core; |
| | 8 | | using Azure.Storage.Sas; |
| | 9 | |
|
| | 10 | | namespace Azure.Storage.Queues |
| | 11 | | { |
| | 12 | | /// <summary> |
| | 13 | | /// The <see cref="QueueUriBuilder"/> class provides a convenient way to |
| | 14 | | /// modify the contents of a <see cref="System.Uri"/> instance to point to |
| | 15 | | /// different Azure Storage resources like an account, queue, or message. |
| | 16 | | /// |
| | 17 | | /// For more information, see |
| | 18 | | /// <see href="https://docs.microsoft.com/rest/api/storageservices/addressing-queue-service-resources"> |
| | 19 | | /// Addressing Queue Service Resources</see>. |
| | 20 | | /// </summary> |
| | 21 | | public class QueueUriBuilder |
| | 22 | | { |
| | 23 | | /// <summary> |
| | 24 | | /// The Uri instance constructed by this builder. It will be reset to |
| | 25 | | /// null when changes are made and reconstructed when <see cref="System.Uri"/> |
| | 26 | | /// is accessed. |
| | 27 | | /// </summary> |
| | 28 | | private Uri _uri; |
| | 29 | |
|
| | 30 | | /// <summary> |
| | 31 | | /// Whether the Uri is an IP Uri as determined by |
| | 32 | | /// <see cref="UriExtensions.IsHostIPEndPointStyle"/>. |
| | 33 | | /// </summary> |
| | 34 | | private readonly bool _isIPStyleUri; |
| | 35 | |
|
| | 36 | | /// <summary> |
| | 37 | | /// Gets or sets the scheme name of the URI. |
| | 38 | | /// Example: "https" |
| | 39 | | /// </summary> |
| | 40 | | public string Scheme |
| | 41 | | { |
| 136 | 42 | | get => _scheme; |
| 492 | 43 | | set { ResetUri(); _scheme = value; } |
| | 44 | | } |
| | 45 | | private string _scheme; |
| | 46 | |
|
| | 47 | | /// <summary> |
| | 48 | | /// Gets or sets the Domain Name System (DNS) host name or IP address |
| | 49 | | /// of a server. |
| | 50 | | /// |
| | 51 | | /// Example: "account.queue.core.windows.net" |
| | 52 | | /// </summary> |
| | 53 | | public string Host |
| | 54 | | { |
| 136 | 55 | | get => _host; |
| 492 | 56 | | set { ResetUri(); _host = value; } |
| | 57 | | } |
| | 58 | | private string _host; |
| | 59 | |
|
| | 60 | | /// <summary> |
| | 61 | | /// Gets or sets the port number of the URI. |
| | 62 | | /// </summary> |
| | 63 | | public int Port |
| | 64 | | { |
| 136 | 65 | | get => _port; |
| 492 | 66 | | set { ResetUri(); _port = value; } |
| | 67 | | } |
| | 68 | | private int _port; |
| | 69 | |
|
| | 70 | | /// <summary> |
| | 71 | | /// Gets or sets the Azure Storage account name. |
| | 72 | | /// </summary> |
| | 73 | | public string AccountName |
| | 74 | | { |
| 196 | 75 | | get => _accountName; |
| 984 | 76 | | set { ResetUri(); _accountName = value; } |
| | 77 | | } |
| | 78 | | private string _accountName; |
| | 79 | |
|
| | 80 | | /// <summary> |
| | 81 | | /// Gets or sets the name of a Azure Storage Queue. The value defaults |
| | 82 | | /// to <see cref="string.Empty"/> if not present in the |
| | 83 | | /// <see cref="System.Uri"/>. |
| | 84 | | /// </summary> |
| | 85 | | public string QueueName |
| | 86 | | { |
| 252 | 87 | | get => _queueName; |
| 1032 | 88 | | set { ResetUri(); _queueName = value; } |
| | 89 | | } |
| | 90 | | private string _queueName; |
| | 91 | |
|
| | 92 | | /// <summary> |
| | 93 | | /// Gets or sets whether to reference a queue's messages. |
| | 94 | | /// </summary> |
| | 95 | | public bool Messages |
| | 96 | | { |
| 124 | 97 | | get => _messages; |
| 540 | 98 | | set { ResetUri(); _messages = value; } |
| | 99 | | } |
| | 100 | | private bool _messages; |
| | 101 | |
|
| | 102 | | /// <summary> |
| | 103 | | /// Gets or sets the ID of a message in a queue. The value defaults to |
| | 104 | | /// <see cref="string.Empty"/> if not present in the <see cref="System.Uri"/>. |
| | 105 | | /// </summary> |
| | 106 | | public string MessageId |
| | 107 | | { |
| 76 | 108 | | get => _messageId; |
| 516 | 109 | | set { ResetUri(); _messageId = value; } |
| | 110 | | } |
| | 111 | | private string _messageId; |
| | 112 | |
|
| | 113 | | /// <summary> |
| | 114 | | /// Gets or sets the Shared Access Signature query parameters, or null |
| | 115 | | /// if not present in the <see cref="System.Uri"/>. |
| | 116 | | /// </summary> |
| | 117 | | public SasQueryParameters Sas |
| | 118 | | { |
| 216 | 119 | | get => _sas; |
| 588 | 120 | | set { ResetUri(); _sas = value; } |
| | 121 | | } |
| | 122 | | private SasQueryParameters _sas; |
| | 123 | |
|
| | 124 | | /// <summary> |
| | 125 | | /// Gets or sets any query information included in the URI that's not |
| | 126 | | /// relevant to addressing Azure storage resources. |
| | 127 | | /// </summary> |
| | 128 | | public string Query |
| | 129 | | { |
| 136 | 130 | | get => _query; |
| 492 | 131 | | set { ResetUri(); _query = value; } |
| | 132 | | } |
| | 133 | | private string _query; |
| | 134 | |
|
| | 135 | | /// <summary> |
| | 136 | | /// Initializes a new instance of the <see cref="QueueUriBuilder"/> |
| | 137 | | /// class with the specified <see cref="System.Uri"/>. |
| | 138 | | /// </summary> |
| | 139 | | /// <param name="uri"> |
| | 140 | | /// The <see cref="System.Uri"/> to a storage resource. |
| | 141 | | /// </param> |
| 164 | 142 | | public QueueUriBuilder(Uri uri) |
| | 143 | | { |
| 164 | 144 | | Scheme = uri.Scheme; |
| 164 | 145 | | Host = uri.Host; |
| 164 | 146 | | Port = uri.Port; |
| 164 | 147 | | AccountName = ""; |
| 164 | 148 | | QueueName = ""; |
| 164 | 149 | | Messages = false; |
| 164 | 150 | | MessageId = ""; |
| 164 | 151 | | Sas = null; |
| | 152 | |
|
| | 153 | | // Find the account, queue, and message id (if any) |
| 164 | 154 | | if (!string.IsNullOrEmpty(uri.AbsolutePath)) |
| | 155 | | { |
| 164 | 156 | | var path = uri.GetPath(); |
| | 157 | |
|
| 164 | 158 | | var startIndex = 0; |
| | 159 | |
|
| 164 | 160 | | if (uri.IsHostIPEndPointStyle()) |
| | 161 | | { |
| 60 | 162 | | _isIPStyleUri = true; |
| 60 | 163 | | var accountEndIndex = path.IndexOf("/", StringComparison.InvariantCulture); |
| | 164 | |
|
| | 165 | | // Slash not found; path has account name & no queue name |
| 60 | 166 | | if (accountEndIndex == -1) |
| | 167 | | { |
| 32 | 168 | | AccountName = path; |
| 32 | 169 | | startIndex = path.Length; |
| | 170 | | } |
| | 171 | | else |
| | 172 | | { |
| 28 | 173 | | AccountName = path.Substring(0, accountEndIndex); |
| 28 | 174 | | startIndex = accountEndIndex + 1; |
| | 175 | | } |
| | 176 | | } |
| | 177 | | else |
| | 178 | | { |
| 104 | 179 | | AccountName = uri.GetAccountNameFromDomain(Constants.Queue.UriSubDomain) ?? string.Empty; |
| | 180 | | } |
| | 181 | |
|
| | 182 | | // Find the next slash (if it exists) |
| 164 | 183 | | var queueEndIndex = path.IndexOf("/", startIndex, StringComparison.InvariantCulture); |
| | 184 | |
|
| | 185 | | // Slash not found; path has queue name & no message id |
| 164 | 186 | | if (queueEndIndex == -1) |
| | 187 | | { |
| 148 | 188 | | QueueName = path.Substring(startIndex); |
| | 189 | | } |
| | 190 | | else |
| | 191 | | { |
| | 192 | | // The queue name is the part between the slashes |
| 16 | 193 | | QueueName = path.Substring(startIndex, queueEndIndex - startIndex); |
| | 194 | |
|
| | 195 | | // skip "messages" |
| 16 | 196 | | Messages = true; |
| 16 | 197 | | startIndex = startIndex + (queueEndIndex - startIndex) + 1; |
| 16 | 198 | | startIndex = path.IndexOf("/", startIndex, StringComparison.InvariantCulture) + 1; |
| | 199 | |
|
| 16 | 200 | | if (startIndex != 0) |
| | 201 | | { |
| | 202 | | // set messageId |
| 8 | 203 | | MessageId = path.Substring(startIndex, path.Length - startIndex); |
| | 204 | | } |
| | 205 | | } |
| | 206 | | } |
| | 207 | |
|
| | 208 | | // Convert the query parameters to a case-sensitive map & trim whitespace |
| 164 | 209 | | var paramsMap = new UriQueryParamsCollection(uri.Query); |
| 164 | 210 | | if (paramsMap.ContainsKey(Constants.Sas.Parameters.Version)) |
| | 211 | | { |
| 16 | 212 | | Sas = SasQueryParametersInternals.Create(paramsMap); |
| | 213 | | } |
| 164 | 214 | | Query = paramsMap.ToString(); |
| 164 | 215 | | } |
| | 216 | |
|
| | 217 | | /// <summary> |
| | 218 | | /// Returns the <see cref="System.Uri"/> constructed from the |
| | 219 | | /// <see cref="QueueUriBuilder"/>'s fields. The <see cref="Uri.Query"/> |
| | 220 | | /// property contains the SAS and additional query parameters. |
| | 221 | | /// </summary> |
| | 222 | | public Uri ToUri() |
| | 223 | | { |
| 84 | 224 | | if (_uri == null) |
| | 225 | | { |
| 84 | 226 | | _uri = BuildUri().ToUri(); |
| | 227 | | } |
| 84 | 228 | | return _uri; |
| | 229 | | } |
| | 230 | |
|
| | 231 | | /// <summary> |
| | 232 | | /// Returns the display string for the specified |
| | 233 | | /// <see cref="QueueUriBuilder"/> instance. |
| | 234 | | /// </summary> |
| | 235 | | /// <returns> |
| | 236 | | /// The display string for the specified <see cref="QueueUriBuilder"/> |
| | 237 | | /// instance. |
| | 238 | | /// </returns> |
| | 239 | | public override string ToString() => |
| 0 | 240 | | BuildUri().ToString(); |
| | 241 | |
|
| | 242 | | /// <summary> |
| | 243 | | /// Reset our cached URI. |
| | 244 | | /// </summary> |
| | 245 | | private void ResetUri() => |
| 1876 | 246 | | _uri = null; |
| | 247 | |
|
| | 248 | | /// <summary> |
| | 249 | | /// Construct a <see cref="RequestUriBuilder"/> representing the |
| | 250 | | /// <see cref="QueueUriBuilder"/>'s fields. The <see cref="Uri.Query"/> |
| | 251 | | /// property contains the SAS, snapshot, and unparsed query parameters. |
| | 252 | | /// </summary> |
| | 253 | | /// <returns>The constructed <see cref="RequestUriBuilder"/>.</returns> |
| | 254 | | private RequestUriBuilder BuildUri() |
| | 255 | | { |
| | 256 | | // Concatenate account, queue, & messageId (if they exist) |
| 84 | 257 | | var path = new StringBuilder(""); |
| | 258 | | // only append the account name to the path for Ip style Uri. |
| | 259 | | // regular style Uri will already have account name in domain |
| 84 | 260 | | if (_isIPStyleUri && !string.IsNullOrWhiteSpace(AccountName)) |
| | 261 | | { |
| 36 | 262 | | path.Append("/").Append(AccountName); |
| | 263 | | } |
| | 264 | |
|
| 84 | 265 | | if (!string.IsNullOrWhiteSpace(QueueName)) |
| | 266 | | { |
| 72 | 267 | | path.Append("/").Append(QueueName); |
| 72 | 268 | | if (Messages) |
| | 269 | | { |
| 16 | 270 | | path.Append("/messages"); |
| 16 | 271 | | if (!string.IsNullOrWhiteSpace(MessageId)) |
| | 272 | | { |
| 8 | 273 | | path.Append("/").Append(MessageId); |
| | 274 | | } |
| | 275 | | } |
| | 276 | | } |
| | 277 | |
|
| | 278 | | // Concatenate query parameters |
| 84 | 279 | | var query = new StringBuilder(Query); |
| 84 | 280 | | var sas = Sas?.ToString(); |
| 84 | 281 | | if (!string.IsNullOrWhiteSpace(sas)) |
| | 282 | | { |
| 0 | 283 | | if (query.Length > 0) { query.Append("&"); } |
| 32 | 284 | | query.Append(sas); |
| | 285 | | } |
| | 286 | |
|
| | 287 | | // Use RequestUriBuilder, which has slightly nicer formatting |
| 84 | 288 | | return new RequestUriBuilder |
| 84 | 289 | | { |
| 84 | 290 | | Scheme = Scheme, |
| 84 | 291 | | Host = Host, |
| 84 | 292 | | Port = Port, |
| 84 | 293 | | Path = path.ToString(), |
| 84 | 294 | | Query = query.Length > 0 ? "?" + query.ToString() : null |
| 84 | 295 | | }; |
| | 296 | | } |
| | 297 | | } |
| | 298 | | } |