| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using System.Collections; |
| | 5 | | using System.Collections.Generic; |
| | 6 | | using System.Diagnostics.CodeAnalysis; |
| | 7 | |
|
| | 8 | | namespace Azure.Core |
| | 9 | | { |
| | 10 | | /// <summary> |
| | 11 | | /// Headers to be sent as part of the <see cref="Request"/>. |
| | 12 | | /// </summary> |
| | 13 | | public readonly struct RequestHeaders : IEnumerable<HttpHeader> |
| | 14 | | { |
| | 15 | | private readonly Request _request; |
| | 16 | |
|
| | 17 | | internal RequestHeaders(Request request) |
| | 18 | | { |
| 11312 | 19 | | _request = request; |
| 11312 | 20 | | } |
| | 21 | |
|
| | 22 | | /// <summary> |
| | 23 | | /// Returns an enumerator that iterates through the <see cref="RequestHeaders"/>. |
| | 24 | | /// </summary> |
| | 25 | | /// <returns>A <see cref="IEnumerator{T}"/> for the <see cref="RequestHeaders"/>.</returns> |
| | 26 | | public IEnumerator<HttpHeader> GetEnumerator() |
| | 27 | | { |
| 2150 | 28 | | return _request.EnumerateHeaders().GetEnumerator(); |
| | 29 | | } |
| | 30 | |
|
| | 31 | | /// <summary> |
| | 32 | | /// Returns an enumerator that iterates through the <see cref="RequestHeaders"/>. |
| | 33 | | /// </summary> |
| | 34 | | /// <returns>A <see cref="IEnumerator"/> for the <see cref="RequestHeaders"/>.</returns> |
| | 35 | | IEnumerator IEnumerable.GetEnumerator() |
| | 36 | | { |
| 272 | 37 | | return _request.EnumerateHeaders().GetEnumerator(); |
| | 38 | | } |
| | 39 | |
|
| | 40 | | /// <summary> |
| | 41 | | /// Adds the <see cref="HttpHeader"/> instance to the collection. |
| | 42 | | /// </summary> |
| | 43 | | /// <param name="header">The header to add.</param> |
| | 44 | | public void Add(HttpHeader header) |
| | 45 | | { |
| 0 | 46 | | _request.AddHeader(header.Name, header.Value); |
| 0 | 47 | | } |
| | 48 | |
|
| | 49 | | /// <summary> |
| | 50 | | /// Adds the header to the collection. If a header with this name already exist adds an additional value to the |
| | 51 | | /// </summary> |
| | 52 | | /// <param name="name">The header name.</param> |
| | 53 | | /// <param name="value">The header value.</param> |
| | 54 | | public void Add(string name, string value) |
| | 55 | | { |
| 2284 | 56 | | _request.AddHeader(name, value); |
| 2284 | 57 | | } |
| | 58 | |
|
| | 59 | | /// <summary> |
| | 60 | | /// Returns header value if the headers is stored in the collection. If the header has multiple values they are |
| | 61 | | /// </summary> |
| | 62 | | /// <param name="name">The header name.</param> |
| | 63 | | /// <param name="value">The reference to populate with value.</param> |
| | 64 | | /// <returns><c>true</c> if the specified header is stored in the collection, otherwise <c>false</c>.</returns> |
| | 65 | | public bool TryGetValue(string name, [NotNullWhen(true)] out string? value) |
| | 66 | | { |
| 2852 | 67 | | return _request.TryGetHeader(name, out value); |
| | 68 | | } |
| | 69 | |
|
| | 70 | | /// <summary> |
| | 71 | | /// Returns header values if the header is stored in the collection. |
| | 72 | | /// </summary> |
| | 73 | | /// <param name="name">The header name.</param> |
| | 74 | | /// <param name="values">The reference to populate with values.</param> |
| | 75 | | /// <returns><c>true</c> if the specified header is stored in the collection, otherwise <c>false</c>.</returns> |
| | 76 | | public bool TryGetValues(string name, [NotNullWhen(true)] out IEnumerable<string>? values) |
| | 77 | | { |
| 152 | 78 | | return _request.TryGetHeaderValues(name, out values); |
| | 79 | | } |
| | 80 | |
|
| | 81 | |
|
| | 82 | | /// <summary> |
| | 83 | | /// Returns <c>true</c> if the headers is stored in the collection. |
| | 84 | | /// </summary> |
| | 85 | | /// <param name="name">The header name.</param> |
| | 86 | | /// <returns><c>true</c> if the specified header is stored in the collection, otherwise <c>false</c>.</returns> |
| | 87 | | public bool Contains(string name) |
| | 88 | | { |
| 136 | 89 | | return _request.ContainsHeader(name); |
| | 90 | | } |
| | 91 | |
|
| | 92 | | /// <summary> |
| | 93 | | /// Sets the header value name. If a header with this name already exist replaces it's value. |
| | 94 | | /// </summary> |
| | 95 | | /// <param name="name">The header name.</param> |
| | 96 | | /// <param name="value">The header value.</param> |
| | 97 | | public void SetValue(string name, string value) |
| | 98 | | { |
| 3354 | 99 | | _request.SetHeader(name, value); |
| 3354 | 100 | | } |
| | 101 | |
|
| | 102 | | /// <summary> |
| | 103 | | /// Removes the header from the collection. |
| | 104 | | /// </summary> |
| | 105 | | /// <param name="name">The header name.</param> |
| | 106 | | /// <returns><c>true</c> if the header existed, otherwise <c>false</c>.</returns> |
| | 107 | | public bool Remove(string name) |
| | 108 | | { |
| 112 | 109 | | return _request.RemoveHeader(name); |
| | 110 | | } |
| | 111 | | } |
| | 112 | | } |