| | | 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.Diagnostics; |
| | | 5 | | using System.Text; |
| | | 6 | | |
| | | 7 | | namespace System.Net.Http.HPack |
| | | 8 | | { |
| | | 9 | | internal readonly struct HeaderField |
| | | 10 | | { |
| | | 11 | | // http://httpwg.org/specs/rfc7541.html#rfc.section.4.1 |
| | | 12 | | public const int RfcOverhead = 32; |
| | | 13 | | |
| | | 14 | | public HeaderField(int? staticTableIndex, ReadOnlySpan<byte> name, ReadOnlySpan<byte> value) |
| | 0 | 15 | | { |
| | | 16 | | // Store the static table index (if there is one) for the header field. |
| | | 17 | | // ASP.NET Core has a fast path that sets a header value using the static table index instead of the name. |
| | 0 | 18 | | StaticTableIndex = staticTableIndex; |
| | | 19 | | |
| | 0 | 20 | | Debug.Assert(name.Length > 0); |
| | | 21 | | |
| | | 22 | | // TODO: We're allocating here on every new table entry. |
| | | 23 | | // That means a poorly-behaved server could cause us to allocate repeatedly. |
| | | 24 | | // We should revisit our allocation strategy here so we don't need to allocate per entry |
| | | 25 | | // and we have a cap to how much allocation can happen per dynamic table |
| | | 26 | | // (without limiting the number of table entries a server can provide within the table size limit). |
| | 0 | 27 | | Name = name.ToArray(); |
| | 0 | 28 | | Value = value.ToArray(); |
| | 0 | 29 | | } |
| | | 30 | | |
| | 0 | 31 | | public int? StaticTableIndex { get; } |
| | | 32 | | |
| | 0 | 33 | | public byte[] Name { get; } |
| | | 34 | | |
| | 0 | 35 | | public byte[] Value { get; } |
| | | 36 | | |
| | 0 | 37 | | public int Length => GetLength(Name.Length, Value.Length); |
| | | 38 | | |
| | 0 | 39 | | public static int GetLength(int nameLength, int valueLength) => nameLength + valueLength + RfcOverhead; |
| | | 40 | | |
| | | 41 | | public override string ToString() |
| | 0 | 42 | | { |
| | 0 | 43 | | if (Name != null) |
| | 0 | 44 | | { |
| | 0 | 45 | | return Encoding.Latin1.GetString(Name) + ": " + Encoding.Latin1.GetString(Value); |
| | | 46 | | } |
| | | 47 | | else |
| | 0 | 48 | | { |
| | 0 | 49 | | return "<empty>"; |
| | | 50 | | } |
| | 0 | 51 | | } |
| | | 52 | | } |
| | | 53 | | } |