| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using System; |
| | 5 | | using System.Collections; |
| | 6 | | using System.Collections.Generic; |
| | 7 | | using System.Diagnostics.CodeAnalysis; |
| | 8 | | using System.Globalization; |
| | 9 | |
|
| | 10 | | namespace Azure.Core |
| | 11 | | { |
| | 12 | | /// <summary> |
| | 13 | | /// Headers received as part of the <see cref="Response"/>. |
| | 14 | | /// </summary> |
| | 15 | | public readonly struct ResponseHeaders : IEnumerable<HttpHeader> |
| | 16 | | { |
| | 17 | | private readonly Response _response; |
| | 18 | |
|
| | 19 | | internal ResponseHeaders(Response response) |
| | 20 | | { |
| 4952 | 21 | | _response = response; |
| 4952 | 22 | | } |
| | 23 | |
|
| | 24 | | /// <summary> |
| | 25 | | /// Gets the parsed value of "Date" or "x-ms-date" header. |
| | 26 | | /// </summary> |
| | 27 | | public DateTimeOffset? Date => |
| 8 | 28 | | TryGetValue(HttpHeader.Names.Date, out var value) || |
| 8 | 29 | | TryGetValue(HttpHeader.Names.XMsDate, out value) ? |
| 8 | 30 | | (DateTimeOffset?)DateTimeOffset.Parse(value, CultureInfo.InvariantCulture) : |
| 8 | 31 | | null; |
| | 32 | |
|
| | 33 | | /// <summary> |
| | 34 | | /// Gets the value of "Content-Type" header. |
| | 35 | | /// </summary> |
| 2118 | 36 | | public string? ContentType => TryGetValue(HttpHeader.Names.ContentType, out string? value) ? value : null; |
| | 37 | |
|
| | 38 | | /// <summary> |
| | 39 | | /// Gets the parsed value of "Content-Length" header. |
| | 40 | | /// </summary> |
| 2 | 41 | | public int? ContentLength => TryGetValue(HttpHeader.Names.ContentLength, out string? stringValue) ? int.Parse(st |
| | 42 | |
|
| | 43 | | /// <summary> |
| | 44 | | /// Gets the parsed value of "ETag" header. |
| | 45 | | /// </summary> |
| 2 | 46 | | public ETag? ETag => TryGetValue(HttpHeader.Names.ETag, out string? stringValue) ? Azure.ETag.Parse(stringValue) |
| | 47 | |
|
| | 48 | | /// <summary> |
| | 49 | | /// Gets the value of "x-ms-request-id" header. |
| | 50 | | /// </summary> |
| 16 | 51 | | public string? RequestId => TryGetValue(HttpHeader.Names.XMsRequestId, out string? value) ? value : null; |
| | 52 | |
|
| | 53 | | /// <summary> |
| | 54 | | /// Returns an enumerator that iterates through the <see cref="ResponseHeaders"/>. |
| | 55 | | /// </summary> |
| | 56 | | /// <returns>A <see cref="IEnumerator{T}"/> for the <see cref="ResponseHeaders"/>.</returns> |
| | 57 | | public IEnumerator<HttpHeader> GetEnumerator() |
| | 58 | | { |
| 2134 | 59 | | return _response.EnumerateHeaders().GetEnumerator(); |
| | 60 | | } |
| | 61 | |
|
| | 62 | | /// <summary> |
| | 63 | | /// Returns an enumerator that iterates through the <see cref="ResponseHeaders"/>. |
| | 64 | | /// </summary> |
| | 65 | | /// <returns>A <see cref="IEnumerator"/> for the <see cref="ResponseHeaders"/>.</returns> |
| | 66 | | IEnumerator IEnumerable.GetEnumerator() |
| | 67 | | { |
| 164 | 68 | | return _response.EnumerateHeaders().GetEnumerator(); |
| | 69 | | } |
| | 70 | |
|
| | 71 | | /// <summary> |
| | 72 | | /// Returns header value if the header is stored in the collection. If header has multiple values they are going |
| | 73 | | /// </summary> |
| | 74 | | /// <param name="name">The header name.</param> |
| | 75 | | /// <param name="value">The reference to populate with value.</param> |
| | 76 | | /// <returns><c>true</c> if the specified header is stored in the collection, otherwise <c>false</c>.</returns> |
| | 77 | | public bool TryGetValue(string name, [NotNullWhen(true)] out string? value) |
| | 78 | | { |
| 2322 | 79 | | return _response.TryGetHeader(name, out value); |
| | 80 | | } |
| | 81 | |
|
| | 82 | | /// <summary> |
| | 83 | | /// Returns header values if the header is stored in the collection. |
| | 84 | | /// </summary> |
| | 85 | | /// <param name="name">The header name.</param> |
| | 86 | | /// <param name="values">The reference to populate with values.</param> |
| | 87 | | /// <returns><c>true</c> if the specified header is stored in the collection, otherwise <c>false</c>.</returns> |
| | 88 | | public bool TryGetValues(string name, [NotNullWhen(true)] out IEnumerable<string>? values) |
| | 89 | | { |
| 172 | 90 | | return _response.TryGetHeaderValues(name, out values); |
| | 91 | | } |
| | 92 | |
|
| | 93 | | /// <summary> |
| | 94 | | /// Returns <c>true</c> if the header is stored in the collection. |
| | 95 | | /// </summary> |
| | 96 | | /// <param name="name">The header name.</param> |
| | 97 | | /// <returns><c>true</c> if the specified header is stored in the collection, otherwise <c>false</c>.</returns> |
| | 98 | | public bool Contains(string name) |
| | 99 | | { |
| 164 | 100 | | return _response.ContainsHeader(name); |
| | 101 | | } |
| | 102 | | } |
| | 103 | | } |