| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using System; |
| | 5 | |
|
| | 6 | | namespace Azure.Core |
| | 7 | | { |
| | 8 | | /// <summary> |
| | 9 | | /// Represents an HTTP header. |
| | 10 | | /// </summary> |
| | 11 | | public readonly struct HttpHeader : IEquatable<HttpHeader> |
| | 12 | | { |
| | 13 | | /// <summary> |
| | 14 | | /// Creates a new instance of <see cref="HttpHeader"/> with provided name and value. |
| | 15 | | /// </summary> |
| | 16 | | /// <param name="name">The header name.</param> |
| | 17 | | /// <param name="value">The header value.</param> |
| | 18 | | public HttpHeader(string name, string value) |
| | 19 | | { |
| 13796 | 20 | | if (string.IsNullOrEmpty(name)) |
| | 21 | | { |
| 0 | 22 | | throw new ArgumentException($"{nameof(name)} shouldn't be null or empty", nameof(name)); |
| | 23 | | } |
| | 24 | |
|
| 13796 | 25 | | Name = name; |
| 13796 | 26 | | Value = value; |
| 13796 | 27 | | } |
| | 28 | |
|
| | 29 | | /// <summary> |
| | 30 | | /// Gets header name. |
| | 31 | | /// </summary> |
| 26718 | 32 | | public string Name { get; } |
| | 33 | |
|
| | 34 | | /// <summary> |
| | 35 | | /// Gets header value. If the header has multiple values they would be joined with a comma. To get separate valu |
| | 36 | | /// </summary> |
| 13464 | 37 | | public string Value { get; } |
| | 38 | |
|
| | 39 | | /// <inheritdoc/> |
| | 40 | | public override int GetHashCode() |
| | 41 | | { |
| 4 | 42 | | var hashCode = new HashCodeBuilder(); |
| 4 | 43 | | hashCode.Add(Name, StringComparer.OrdinalIgnoreCase); |
| 4 | 44 | | hashCode.Add(Value); |
| 4 | 45 | | return hashCode.ToHashCode(); |
| | 46 | | } |
| | 47 | |
|
| | 48 | | /// <inheritdoc/> |
| | 49 | | public override bool Equals(object? obj) |
| | 50 | | { |
| 0 | 51 | | if (obj is HttpHeader header) |
| | 52 | | { |
| 0 | 53 | | return Equals(header); |
| | 54 | | } |
| 0 | 55 | | return false; |
| | 56 | | } |
| | 57 | |
|
| | 58 | | /// <inheritdoc/> |
| 0 | 59 | | public override string ToString() => $"{Name}:{Value}"; |
| | 60 | |
|
| | 61 | | /// <inheritdoc/> |
| | 62 | | public bool Equals(HttpHeader other) |
| | 63 | | { |
| 734 | 64 | | return string.Equals(Name, other.Name, StringComparison.OrdinalIgnoreCase) && Value.Equals(other.Value, Stri |
| | 65 | | } |
| | 66 | |
|
| | 67 | | #pragma warning disable CA1034 // Nested types should not be visible |
| | 68 | | /// <summary> |
| | 69 | | /// Contains names of commonly used headers. |
| | 70 | | /// </summary> |
| | 71 | | public static class Names |
| | 72 | | #pragma warning restore CA1034 // Nested types should not be visible |
| | 73 | | { |
| | 74 | | /// <summary> |
| | 75 | | /// Returns. <code>"Date"</code> |
| | 76 | | /// </summary> |
| 8 | 77 | | public static string Date => "Date"; |
| | 78 | | /// <summary> |
| | 79 | | /// Returns. <code>"x-ms-date"</code> |
| | 80 | | /// </summary> |
| 4 | 81 | | public static string XMsDate => "x-ms-date"; |
| | 82 | | /// <summary> |
| | 83 | | /// Returns. <code>"Content-Type"</code> |
| | 84 | | /// </summary> |
| 4248 | 85 | | public static string ContentType => "Content-Type"; |
| | 86 | | /// <summary> |
| | 87 | | /// Returns. <code>"Content-Length"</code> |
| | 88 | | /// </summary> |
| 2 | 89 | | public static string ContentLength => "Content-Length"; |
| | 90 | | /// <summary> |
| | 91 | | /// Returns. <code>"ETag"</code> |
| | 92 | | /// </summary> |
| 2 | 93 | | public static string ETag => "ETag"; |
| | 94 | | /// <summary> |
| | 95 | | /// Returns. <code>"x-ms-request-id"</code> |
| | 96 | | /// </summary> |
| 16 | 97 | | public static string XMsRequestId => "x-ms-request-id"; |
| | 98 | | /// <summary> |
| | 99 | | /// Returns. <code>"User-Agent"</code> |
| | 100 | | /// </summary> |
| 1642 | 101 | | public static string UserAgent => "User-Agent"; |
| | 102 | | /// <summary> |
| | 103 | | /// Returns. <code>"Accept"</code> |
| | 104 | | /// </summary> |
| 0 | 105 | | public static string Accept => "Accept"; |
| | 106 | | /// <summary> |
| | 107 | | /// Returns. <code>"Authorization"</code> |
| | 108 | | /// </summary> |
| 520 | 109 | | public static string Authorization => "Authorization"; |
| | 110 | | /// <summary> |
| | 111 | | /// Returns. <code>"Range"</code> |
| | 112 | | /// </summary> |
| 0 | 113 | | public static string Range => "Range"; |
| | 114 | | /// <summary> |
| | 115 | | /// Returns. <code>"x-ms-range"</code> |
| | 116 | | /// </summary> |
| 0 | 117 | | public static string XMsRange => "x-ms-range"; |
| | 118 | | /// <summary> |
| | 119 | | /// Returns. <code>"If-Match"</code> |
| | 120 | | /// </summary> |
| 0 | 121 | | public static string IfMatch => "If-Match"; |
| | 122 | | /// <summary> |
| | 123 | | /// Returns. <code>"If-None-Match"</code> |
| | 124 | | /// </summary> |
| 0 | 125 | | public static string IfNoneMatch => "If-None-Match"; |
| | 126 | | /// <summary> |
| | 127 | | /// Returns. <code>"If-Modified-Since"</code> |
| | 128 | | /// </summary> |
| 0 | 129 | | public static string IfModifiedSince => "If-Modified-Since"; |
| | 130 | | /// <summary> |
| | 131 | | /// Returns. <code>"If-Unmodified-Since"</code> |
| | 132 | | /// </summary> |
| 0 | 133 | | public static string IfUnmodifiedSince => "If-Unmodified-Since"; |
| | 134 | |
|
| 0 | 135 | | internal static string Referer => "Referer"; |
| 0 | 136 | | internal static string Host => "Host"; |
| | 137 | | } |
| | 138 | |
|
| | 139 | | #pragma warning disable CA1034 // Nested types should not be visible |
| | 140 | | #pragma warning disable CA1724 // Type name conflicts with standard namespace |
| | 141 | | /// <summary> |
| | 142 | | /// Commonly defined header values. |
| | 143 | | /// </summary> |
| | 144 | | public static class Common |
| | 145 | | #pragma warning restore CA1034 // Nested types should not be visible |
| | 146 | | #pragma warning restore CA1724 // Type name conflicts with standard namespace |
| | 147 | | { |
| | 148 | | private const string ApplicationJson = "application/json"; |
| | 149 | | private const string ApplicationOctetStream = "application/octet-stream"; |
| | 150 | | private const string ApplicationFormUrlEncoded = "application/x-www-form-urlencoded"; |
| | 151 | |
|
| | 152 | | /// <summary> |
| | 153 | | /// Returns header with name "ContentType" and value "application/json". |
| | 154 | | /// </summary> |
| 0 | 155 | | public static readonly HttpHeader JsonContentType = new HttpHeader(Names.ContentType, ApplicationJson); |
| | 156 | | /// <summary> |
| | 157 | | /// Returns header with name "Accept" and value "application/json". |
| | 158 | | /// </summary> |
| 0 | 159 | | public static readonly HttpHeader JsonAccept = new HttpHeader(Names.Accept, ApplicationJson); |
| | 160 | | /// <summary> |
| | 161 | | /// Returns header with name "ContentType" and value "application/octet-stream". |
| | 162 | | /// </summary> |
| 0 | 163 | | public static readonly HttpHeader OctetStreamContentType = new HttpHeader(Names.ContentType, ApplicationOcte |
| | 164 | | /// <summary> |
| | 165 | | /// Returns header with name "ContentType" and value "application/x-www-form-urlencoded". |
| | 166 | | /// </summary> |
| 0 | 167 | | public static readonly HttpHeader FormUrlEncodedContentType = new HttpHeader(Names.ContentType, ApplicationF |
| | 168 | | } |
| | 169 | | } |
| | 170 | | } |