| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System.Buffers; |
| | | 5 | | using System.Diagnostics; |
| | | 6 | | using System.Diagnostics.CodeAnalysis; |
| | | 7 | | using System.Text; |
| | | 8 | | using System.Text.Unicode; |
| | | 9 | | |
| | | 10 | | namespace System.Net.Http.Headers |
| | | 11 | | { |
| | | 12 | | // This struct represents a particular named header -- |
| | | 13 | | // if the header is one of our known headers, then it contains a reference to the KnownHeader object; |
| | | 14 | | // otherwise, for custom headers, it just contains a string for the header name. |
| | | 15 | | // Use HeaderDescriptor.TryGet to resolve an arbitrary header name to a HeaderDescriptor. |
| | | 16 | | internal readonly struct HeaderDescriptor : IEquatable<HeaderDescriptor> |
| | | 17 | | { |
| | 0 | 18 | | private static readonly SearchValues<byte> s_dangerousCharacterBytes = SearchValues.Create((byte)'\0', (byte)'\r |
| | | 19 | | |
| | | 20 | | /// <summary> |
| | | 21 | | /// Either a <see cref="KnownHeader"/> or <see cref="string"/>. |
| | | 22 | | /// </summary> |
| | | 23 | | private readonly object _descriptor; |
| | | 24 | | |
| | | 25 | | public HeaderDescriptor(KnownHeader knownHeader) |
| | 76631 | 26 | | { |
| | 76631 | 27 | | _descriptor = knownHeader; |
| | 76631 | 28 | | } |
| | | 29 | | |
| | | 30 | | // This should not be used directly; use static TryGet below |
| | | 31 | | internal HeaderDescriptor(string headerName, bool customHeader = false) |
| | 20881 | 32 | | { |
| | 20881 | 33 | | Debug.Assert(customHeader || KnownHeaders.TryGetKnownHeader(headerName) is null, $"The {nameof(KnownHeader)} |
| | 20881 | 34 | | _descriptor = headerName; |
| | 20881 | 35 | | } |
| | | 36 | | |
| | 73895 | 37 | | public string Name => _descriptor is KnownHeader header ? header.Name : (_descriptor as string)!; |
| | 1004922 | 38 | | public HttpHeaderParser? Parser => (_descriptor as KnownHeader)?.Parser; |
| | 110362 | 39 | | public HttpHeaderType HeaderType => _descriptor is KnownHeader knownHeader ? knownHeader.HeaderType : HttpHeader |
| | 0 | 40 | | public KnownHeader? KnownHeader => _descriptor as KnownHeader; |
| | | 41 | | |
| | 0 | 42 | | public bool Equals(KnownHeader other) => ReferenceEquals(_descriptor, other); |
| | | 43 | | |
| | | 44 | | public bool Equals(HeaderDescriptor other) |
| | 13744 | 45 | | { |
| | 13744 | 46 | | if (_descriptor is string headerName) |
| | 4819 | 47 | | { |
| | 4819 | 48 | | return string.Equals(headerName, other._descriptor as string, StringComparison.OrdinalIgnoreCase); |
| | | 49 | | } |
| | | 50 | | else |
| | 8925 | 51 | | { |
| | 8925 | 52 | | return ReferenceEquals(_descriptor, other._descriptor); |
| | | 53 | | } |
| | 13744 | 54 | | } |
| | | 55 | | |
| | 0 | 56 | | public override int GetHashCode() => _descriptor is KnownHeader knownHeader ? knownHeader.GetHashCode() : String |
| | | 57 | | |
| | 0 | 58 | | public override bool Equals(object? obj) => throw new InvalidOperationException(); // Ensure this is never cal |
| | | 59 | | |
| | | 60 | | // Returns false for invalid header name. |
| | | 61 | | public static bool TryGet(string headerName, out HeaderDescriptor descriptor) |
| | 76631 | 62 | | { |
| | 76631 | 63 | | Debug.Assert(!string.IsNullOrEmpty(headerName)); |
| | | 64 | | |
| | 76631 | 65 | | KnownHeader? knownHeader = KnownHeaders.TryGetKnownHeader(headerName); |
| | 76631 | 66 | | if (knownHeader != null) |
| | 76631 | 67 | | { |
| | 76631 | 68 | | descriptor = new HeaderDescriptor(knownHeader); |
| | 76631 | 69 | | return true; |
| | | 70 | | } |
| | | 71 | | |
| | 0 | 72 | | if (!HttpRuleParser.IsToken(headerName)) |
| | 0 | 73 | | { |
| | 0 | 74 | | descriptor = default(HeaderDescriptor); |
| | 0 | 75 | | return false; |
| | | 76 | | } |
| | | 77 | | |
| | 0 | 78 | | descriptor = new HeaderDescriptor(headerName); |
| | 0 | 79 | | return true; |
| | 76631 | 80 | | } |
| | | 81 | | |
| | | 82 | | // Returns false for invalid header name. |
| | | 83 | | public static bool TryGet(ReadOnlySpan<byte> headerName, out HeaderDescriptor descriptor) |
| | 0 | 84 | | { |
| | 0 | 85 | | Debug.Assert(headerName.Length > 0); |
| | | 86 | | |
| | 0 | 87 | | KnownHeader? knownHeader = KnownHeaders.TryGetKnownHeader(headerName); |
| | 0 | 88 | | if (knownHeader != null) |
| | 0 | 89 | | { |
| | 0 | 90 | | descriptor = new HeaderDescriptor(knownHeader); |
| | 0 | 91 | | return true; |
| | | 92 | | } |
| | | 93 | | |
| | 0 | 94 | | if (!HttpRuleParser.IsToken(headerName)) |
| | 0 | 95 | | { |
| | 0 | 96 | | descriptor = default(HeaderDescriptor); |
| | 0 | 97 | | return false; |
| | | 98 | | } |
| | | 99 | | |
| | 0 | 100 | | descriptor = new HeaderDescriptor(HttpRuleParser.GetTokenString(headerName)); |
| | 0 | 101 | | return true; |
| | 0 | 102 | | } |
| | | 103 | | |
| | | 104 | | internal static bool TryGetStaticQPackHeader(int index, out HeaderDescriptor descriptor, [NotNullWhen(true)] out |
| | 0 | 105 | | { |
| | 0 | 106 | | Debug.Assert(index >= 0); |
| | | 107 | | |
| | | 108 | | // Micro-opt: store field to variable to prevent Length re-read and use unsigned to avoid bounds check. |
| | 0 | 109 | | (HeaderDescriptor descriptor, string value)[] qpackStaticTable = QPackStaticTable.HeaderLookup; |
| | 0 | 110 | | Debug.Assert(qpackStaticTable.Length == 99); |
| | | 111 | | |
| | 0 | 112 | | uint uindex = (uint)index; |
| | | 113 | | |
| | 0 | 114 | | if (uindex < (uint)qpackStaticTable.Length) |
| | 0 | 115 | | { |
| | 0 | 116 | | (descriptor, knownValue) = qpackStaticTable[uindex]; |
| | 0 | 117 | | return true; |
| | | 118 | | } |
| | | 119 | | else |
| | 0 | 120 | | { |
| | 0 | 121 | | descriptor = default; |
| | 0 | 122 | | knownValue = null; |
| | 0 | 123 | | return false; |
| | | 124 | | } |
| | 0 | 125 | | } |
| | | 126 | | |
| | | 127 | | public HeaderDescriptor AsCustomHeader() |
| | 20881 | 128 | | { |
| | 20881 | 129 | | Debug.Assert(_descriptor is KnownHeader); |
| | 20881 | 130 | | Debug.Assert(HeaderType != HttpHeaderType.Custom); |
| | 20881 | 131 | | return new HeaderDescriptor(Name, customHeader: true); |
| | 20881 | 132 | | } |
| | | 133 | | |
| | | 134 | | public string GetHeaderValue(ReadOnlySpan<byte> headerValue, Encoding? valueEncoding) |
| | 0 | 135 | | { |
| | 0 | 136 | | if (headerValue.Length == 0) |
| | 0 | 137 | | { |
| | 0 | 138 | | return string.Empty; |
| | | 139 | | } |
| | | 140 | | |
| | | 141 | | // If it's a known header value, use the known value instead of allocating a new string. |
| | 0 | 142 | | if (_descriptor is KnownHeader knownHeader) |
| | 0 | 143 | | { |
| | 0 | 144 | | if (knownHeader.KnownValues is string[] knownValues) |
| | 0 | 145 | | { |
| | 0 | 146 | | for (int i = 0; i < knownValues.Length; i++) |
| | 0 | 147 | | { |
| | 0 | 148 | | if (Ascii.Equals(headerValue, knownValues[i])) |
| | 0 | 149 | | { |
| | 0 | 150 | | return knownValues[i]; |
| | | 151 | | } |
| | 0 | 152 | | } |
| | 0 | 153 | | } |
| | | 154 | | |
| | 0 | 155 | | if (knownHeader == KnownHeaders.ContentType) |
| | 0 | 156 | | { |
| | 0 | 157 | | string? contentType = GetKnownContentType(headerValue); |
| | 0 | 158 | | if (contentType != null) |
| | 0 | 159 | | { |
| | 0 | 160 | | return contentType; |
| | | 161 | | } |
| | 0 | 162 | | } |
| | 0 | 163 | | else if (knownHeader == KnownHeaders.Location) |
| | 0 | 164 | | { |
| | | 165 | | // Normally Location should be in ISO-8859-1 but occasionally some servers respond with UTF-8. |
| | | 166 | | // If the user set the ResponseHeaderEncodingSelector, we give that priority instead. |
| | 0 | 167 | | if (valueEncoding is null && TryDecodeUtf8(headerValue, out string? decoded)) |
| | 0 | 168 | | { |
| | 0 | 169 | | return decoded; |
| | | 170 | | } |
| | 0 | 171 | | } |
| | 0 | 172 | | } |
| | | 173 | | |
| | 0 | 174 | | string value = (valueEncoding ?? HttpRuleParser.DefaultHttpEncoding).GetString(headerValue); |
| | 0 | 175 | | if (headerValue.ContainsAny(s_dangerousCharacterBytes)) |
| | 0 | 176 | | { |
| | | 177 | | // Depending on the encoding, 'value' may contain a dangerous character. |
| | | 178 | | // We are replacing them with SP to conform with https://www.rfc-editor.org/rfc/rfc9110.html#section-5.5 |
| | | 179 | | // This is a low-occurrence corner case, so we don't care about the cost of Replace() and the extra allo |
| | 0 | 180 | | value = value.Replace('\0', ' ').Replace('\r', ' ').Replace('\n', ' '); |
| | 0 | 181 | | } |
| | | 182 | | |
| | 0 | 183 | | return value; |
| | 0 | 184 | | } |
| | | 185 | | |
| | | 186 | | internal static string? GetKnownContentType(ReadOnlySpan<byte> contentTypeValue) |
| | 0 | 187 | | { |
| | 0 | 188 | | string? candidate = null; |
| | 0 | 189 | | switch (contentTypeValue.Length) |
| | | 190 | | { |
| | | 191 | | case 8: |
| | 0 | 192 | | switch (contentTypeValue[7]) |
| | | 193 | | { |
| | 0 | 194 | | case (byte)'l': candidate = "text/xml"; break; // text/xm[l] |
| | 0 | 195 | | case (byte)'s': candidate = "text/css"; break; // text/cs[s] |
| | 0 | 196 | | case (byte)'v': candidate = "text/csv"; break; // text/cs[v] |
| | | 197 | | } |
| | 0 | 198 | | break; |
| | | 199 | | |
| | | 200 | | case 9: |
| | 0 | 201 | | switch (contentTypeValue[6]) |
| | | 202 | | { |
| | 0 | 203 | | case (byte)'g': candidate = "image/gif"; break; // image/[g]if |
| | 0 | 204 | | case (byte)'p': candidate = "image/png"; break; // image/[p]ng |
| | 0 | 205 | | case (byte)'t': candidate = "text/html"; break; // text/h[t]ml |
| | | 206 | | } |
| | 0 | 207 | | break; |
| | | 208 | | |
| | | 209 | | case 10: |
| | 0 | 210 | | switch (contentTypeValue[6]) |
| | | 211 | | { |
| | 0 | 212 | | case (byte)'l': candidate = "text/plain"; break; // text/p[l]ain |
| | 0 | 213 | | case (byte)'j': candidate = "image/jpeg"; break; // image/[j]peg |
| | 0 | 214 | | case (byte)'w': candidate = "image/webp"; break; // image/[w]ebp |
| | | 215 | | } |
| | 0 | 216 | | break; |
| | | 217 | | |
| | | 218 | | case 13: |
| | 0 | 219 | | candidate = "image/svg+xml"; // image/svg+xml |
| | 0 | 220 | | break; |
| | | 221 | | |
| | | 222 | | case 15: |
| | 0 | 223 | | switch (contentTypeValue[12]) |
| | | 224 | | { |
| | 0 | 225 | | case (byte)'p': candidate = "application/pdf"; break; // application/[p]df |
| | 0 | 226 | | case (byte)'x': candidate = "application/xml"; break; // application/[x]ml |
| | 0 | 227 | | case (byte)'z': candidate = "application/zip"; break; // application/[z]ip |
| | 0 | 228 | | case (byte)'i': candidate = "text/javascript"; break; // text/javascr[i]pt |
| | | 229 | | } |
| | 0 | 230 | | break; |
| | | 231 | | |
| | | 232 | | case 16: |
| | 0 | 233 | | switch (contentTypeValue[12]) |
| | | 234 | | { |
| | 0 | 235 | | case (byte)'g': candidate = "application/grpc"; break; // application/[g]rpc |
| | 0 | 236 | | case (byte)'j': candidate = "application/json"; break; // application/[j]son |
| | | 237 | | } |
| | 0 | 238 | | break; |
| | | 239 | | |
| | | 240 | | case 17: |
| | 0 | 241 | | candidate = "text/event-stream"; // text/event-stream |
| | 0 | 242 | | break; |
| | | 243 | | |
| | | 244 | | case 19: |
| | 0 | 245 | | candidate = "multipart/form-data"; // multipart/form-data |
| | 0 | 246 | | break; |
| | | 247 | | |
| | | 248 | | case 22: |
| | 0 | 249 | | candidate = "application/javascript"; // application/javascript |
| | 0 | 250 | | break; |
| | | 251 | | |
| | | 252 | | case 23: |
| | 0 | 253 | | switch (contentTypeValue[18]) |
| | | 254 | | { |
| | 0 | 255 | | case (byte)'u': candidate = "text/html;charset=utf-8"; break; // text/html;charset=[u]tf-8 |
| | 0 | 256 | | case (byte)'U': candidate = "text/html;charset=UTF-8"; break; // text/html;charset=[U]TF-8 |
| | | 257 | | } |
| | 0 | 258 | | break; |
| | | 259 | | |
| | | 260 | | case 24: |
| | 0 | 261 | | switch (contentTypeValue[10] ^ contentTypeValue[19]) |
| | | 262 | | { |
| | 0 | 263 | | case 'n' ^ 't': candidate = "application/octet-stream"; break; // applicatio[n]/octet-s[t]ream |
| | 0 | 264 | | case ' ' ^ 'u': candidate = "text/html; charset=utf-8"; break; // text/html;[ ]charset=[u]tf-8 |
| | 0 | 265 | | case ' ' ^ 'U': candidate = "text/html; charset=UTF-8"; break; // text/html;[ ]charset=[U]TF-8 |
| | 0 | 266 | | case ';' ^ 'u': candidate = "text/plain;charset=utf-8"; break; // text/plain[;]charset=[u]tf-8 |
| | 0 | 267 | | case ';' ^ 'U': candidate = "text/plain;charset=UTF-8"; break; // text/plain[;]charset=[U]TF-8 |
| | | 268 | | } |
| | 0 | 269 | | break; |
| | | 270 | | |
| | | 271 | | case 25: |
| | 0 | 272 | | switch (contentTypeValue[20]) |
| | | 273 | | { |
| | 0 | 274 | | case (byte)'u': candidate = "text/plain; charset=utf-8"; break; // text/plain; charset=[u]tf-8 |
| | 0 | 275 | | case (byte)'U': candidate = "text/plain; charset=UTF-8"; break; // text/plain; charset=[U]TF-8 |
| | | 276 | | } |
| | 0 | 277 | | break; |
| | | 278 | | |
| | | 279 | | case 29: |
| | 0 | 280 | | switch (contentTypeValue[19]) |
| | | 281 | | { |
| | 0 | 282 | | case (byte)'I': candidate = "text/html; charset=ISO-8859-1"; break; // text/html; charset=[I]SO- |
| | 0 | 283 | | case (byte)'i': candidate = "text/html; charset=iso-8859-1"; break; // text/html; charset=[i]so- |
| | | 284 | | } |
| | 0 | 285 | | break; |
| | | 286 | | |
| | | 287 | | case 30: |
| | 0 | 288 | | switch (contentTypeValue[25]) |
| | | 289 | | { |
| | 0 | 290 | | case (byte)'u': candidate = "text/javascript; charset=utf-8"; break; // text/javascript; charset |
| | 0 | 291 | | case (byte)'U': candidate = "text/javascript; charset=UTF-8"; break; // text/javascript; charset |
| | | 292 | | } |
| | 0 | 293 | | break; |
| | | 294 | | |
| | | 295 | | case 31: |
| | 0 | 296 | | candidate = "application/json; charset=utf-8"; // application/json; charset=utf-8 |
| | 0 | 297 | | break; |
| | | 298 | | |
| | | 299 | | case 33: |
| | 0 | 300 | | candidate = "application/x-www-form-urlencoded"; // application/x-www-form-urlencoded |
| | 0 | 301 | | break; |
| | | 302 | | } |
| | | 303 | | |
| | 0 | 304 | | Debug.Assert(candidate is null || candidate.Length == contentTypeValue.Length); |
| | | 305 | | |
| | 0 | 306 | | return candidate != null && Ascii.Equals(contentTypeValue, candidate) ? |
| | 0 | 307 | | candidate : |
| | 0 | 308 | | null; |
| | 0 | 309 | | } |
| | | 310 | | |
| | | 311 | | private static bool TryDecodeUtf8(ReadOnlySpan<byte> input, [NotNullWhen(true)] out string? decoded) |
| | 0 | 312 | | { |
| | 0 | 313 | | char[] rented = ArrayPool<char>.Shared.Rent(input.Length); |
| | | 314 | | |
| | | 315 | | try |
| | 0 | 316 | | { |
| | 0 | 317 | | if (Utf8.ToUtf16(input, rented, out _, out int charsWritten, replaceInvalidSequences: false) == Operatio |
| | 0 | 318 | | { |
| | 0 | 319 | | decoded = new string(rented, 0, charsWritten); |
| | 0 | 320 | | return true; |
| | | 321 | | } |
| | 0 | 322 | | } |
| | | 323 | | finally |
| | 0 | 324 | | { |
| | 0 | 325 | | ArrayPool<char>.Shared.Return(rented); |
| | 0 | 326 | | } |
| | | 327 | | |
| | 0 | 328 | | decoded = null; |
| | 0 | 329 | | return false; |
| | 0 | 330 | | } |
| | | 331 | | |
| | 0 | 332 | | public string Separator => Parser is { } parser ? parser.Separator : HttpHeaderParser.DefaultSeparator; |
| | | 333 | | |
| | 0 | 334 | | public byte[] SeparatorBytes => Parser is { } parser ? parser.SeparatorBytes : HttpHeaderParser.DefaultSeparator |
| | | 335 | | } |
| | | 336 | | } |