| | | 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 | | #nullable enable |
| | | 5 | | using System.Buffers; |
| | | 6 | | using System.Collections.Generic; |
| | | 7 | | using System.Diagnostics; |
| | | 8 | | using System.Net.Http.HPack; |
| | | 9 | | using System.Text; |
| | | 10 | | |
| | | 11 | | namespace System.Net.Http.QPack |
| | | 12 | | { |
| | | 13 | | internal static class QPackEncoder |
| | | 14 | | { |
| | | 15 | | // https://tools.ietf.org/html/draft-ietf-quic-qpack-11#section-4.5.2 |
| | | 16 | | // 0 1 2 3 4 5 6 7 |
| | | 17 | | // +---+---+---+---+---+---+---+---+ |
| | | 18 | | // | 1 | T | Index (6+) | |
| | | 19 | | // +---+---+-----------------------+ |
| | | 20 | | // |
| | | 21 | | // Note for this method's implementation of above: |
| | | 22 | | // - T is constant 1 here, indicating a static table reference. |
| | | 23 | | public static bool EncodeStaticIndexedHeaderField(int index, Span<byte> destination, out int bytesWritten) |
| | 0 | 24 | | { |
| | 0 | 25 | | if (!destination.IsEmpty) |
| | 0 | 26 | | { |
| | 0 | 27 | | destination[0] = 0b11000000; |
| | 0 | 28 | | return IntegerEncoder.Encode(index, 6, destination, out bytesWritten); |
| | | 29 | | } |
| | | 30 | | else |
| | 0 | 31 | | { |
| | 0 | 32 | | bytesWritten = 0; |
| | 0 | 33 | | return false; |
| | | 34 | | } |
| | 0 | 35 | | } |
| | | 36 | | |
| | | 37 | | public static byte[] EncodeStaticIndexedHeaderFieldToArray(int index) |
| | 0 | 38 | | { |
| | 0 | 39 | | Span<byte> buffer = stackalloc byte[IntegerEncoder.MaxInt32EncodedLength]; |
| | | 40 | | |
| | 0 | 41 | | bool res = EncodeStaticIndexedHeaderField(index, buffer, out int bytesWritten); |
| | 0 | 42 | | Debug.Assert(res); |
| | | 43 | | |
| | 0 | 44 | | return buffer.Slice(0, bytesWritten).ToArray(); |
| | 0 | 45 | | } |
| | | 46 | | |
| | | 47 | | // https://tools.ietf.org/html/draft-ietf-quic-qpack-11#section-4.5.4 |
| | | 48 | | // 0 1 2 3 4 5 6 7 |
| | | 49 | | // +---+---+---+---+---+---+---+---+ |
| | | 50 | | // | 0 | 1 | N | T |Name Index (4+)| |
| | | 51 | | // +---+---+---+---+---------------+ |
| | | 52 | | // | H | Value Length (7+) | |
| | | 53 | | // +---+---------------------------+ |
| | | 54 | | // | Value String (Length bytes) | |
| | | 55 | | // +-------------------------------+ |
| | | 56 | | // |
| | | 57 | | // Note for this method's implementation of above: |
| | | 58 | | // - N is constant 0 here, indicating intermediates (proxies) can compress the header when fordwarding. |
| | | 59 | | // - T is constant 1 here, indicating a static table reference. |
| | | 60 | | // - H is constant 0 here, as we do not yet perform Huffman coding. |
| | | 61 | | public static bool EncodeLiteralHeaderFieldWithStaticNameReference(int index, string value, Span<byte> destinati |
| | 0 | 62 | | { |
| | 0 | 63 | | return EncodeLiteralHeaderFieldWithStaticNameReference(index, value, valueEncoding: null, destination, out b |
| | 0 | 64 | | } |
| | | 65 | | |
| | | 66 | | public static bool EncodeLiteralHeaderFieldWithStaticNameReference(int index, string value, Encoding? valueEncod |
| | 0 | 67 | | { |
| | | 68 | | // Requires at least two bytes (one for name reference header, one for value length) |
| | 0 | 69 | | if (destination.Length >= 2) |
| | 0 | 70 | | { |
| | 0 | 71 | | destination[0] = 0b01010000; |
| | 0 | 72 | | if (IntegerEncoder.Encode(index, 4, destination, out int headerBytesWritten)) |
| | 0 | 73 | | { |
| | 0 | 74 | | destination = destination.Slice(headerBytesWritten); |
| | | 75 | | |
| | 0 | 76 | | if (EncodeValueString(value, valueEncoding, destination, out int valueBytesWritten)) |
| | 0 | 77 | | { |
| | 0 | 78 | | bytesWritten = headerBytesWritten + valueBytesWritten; |
| | 0 | 79 | | return true; |
| | | 80 | | } |
| | 0 | 81 | | } |
| | 0 | 82 | | } |
| | | 83 | | |
| | 0 | 84 | | bytesWritten = 0; |
| | 0 | 85 | | return false; |
| | 0 | 86 | | } |
| | | 87 | | |
| | | 88 | | /// <summary> |
| | | 89 | | /// Encodes just the name part of a Literal Header Field With Static Name Reference. Must call <see cref="Encode |
| | | 90 | | /// </summary> |
| | | 91 | | public static byte[] EncodeLiteralHeaderFieldWithStaticNameReferenceToArray(int index) |
| | 37 | 92 | | { |
| | 37 | 93 | | Span<byte> temp = stackalloc byte[IntegerEncoder.MaxInt32EncodedLength]; |
| | | 94 | | |
| | 37 | 95 | | temp[0] = 0b01110000; |
| | 37 | 96 | | bool res = IntegerEncoder.Encode(index, 4, temp, out int headerBytesWritten); |
| | 37 | 97 | | Debug.Assert(res); |
| | | 98 | | |
| | 37 | 99 | | return temp.Slice(0, headerBytesWritten).ToArray(); |
| | 37 | 100 | | } |
| | | 101 | | |
| | | 102 | | public static byte[] EncodeLiteralHeaderFieldWithStaticNameReferenceToArray(int index, string value) |
| | 0 | 103 | | { |
| | 0 | 104 | | Span<byte> temp = value.Length < 256 ? stackalloc byte[256 + IntegerEncoder.MaxInt32EncodedLength * 2] : new |
| | 0 | 105 | | bool res = EncodeLiteralHeaderFieldWithStaticNameReference(index, value, temp, out int bytesWritten); |
| | 0 | 106 | | Debug.Assert(res); |
| | 0 | 107 | | return temp.Slice(0, bytesWritten).ToArray(); |
| | 0 | 108 | | } |
| | | 109 | | |
| | | 110 | | // https://tools.ietf.org/html/draft-ietf-quic-qpack-11#section-4.5.6 |
| | | 111 | | // 0 1 2 3 4 5 6 7 |
| | | 112 | | // +---+---+---+---+---+---+---+---+ |
| | | 113 | | // | 0 | 0 | 1 | N | H |NameLen(3+)| |
| | | 114 | | // +---+---+---+---+---+-----------+ |
| | | 115 | | // | Name String (Length bytes) | |
| | | 116 | | // +---+---------------------------+ |
| | | 117 | | // | H | Value Length (7+) | |
| | | 118 | | // +---+---------------------------+ |
| | | 119 | | // | Value String (Length bytes) | |
| | | 120 | | // +-------------------------------+ |
| | | 121 | | // |
| | | 122 | | // Note for this method's implementation of above: |
| | | 123 | | // - N is constant 0 here, indicating intermediates (proxies) can compress the header when fordwarding. |
| | | 124 | | // - H is constant 0 here, as we do not yet perform Huffman coding. |
| | | 125 | | public static bool EncodeLiteralHeaderFieldWithoutNameReference(string name, string value, Span<byte> destinatio |
| | 0 | 126 | | { |
| | 0 | 127 | | return EncodeLiteralHeaderFieldWithoutNameReference(name, value, valueEncoding: null, destination, out bytes |
| | 0 | 128 | | } |
| | | 129 | | |
| | | 130 | | public static bool EncodeLiteralHeaderFieldWithoutNameReference(string name, string value, Encoding? valueEncodi |
| | 0 | 131 | | { |
| | 0 | 132 | | if (EncodeNameString(name, destination, out int nameLength) && EncodeValueString(value, valueEncoding, desti |
| | 0 | 133 | | { |
| | 0 | 134 | | bytesWritten = nameLength + valueLength; |
| | 0 | 135 | | return true; |
| | | 136 | | } |
| | | 137 | | else |
| | 0 | 138 | | { |
| | 0 | 139 | | bytesWritten = 0; |
| | 0 | 140 | | return false; |
| | | 141 | | } |
| | 0 | 142 | | } |
| | | 143 | | |
| | | 144 | | /// <summary> |
| | | 145 | | /// Encodes a Literal Header Field Without Name Reference, building the value by concatenating a collection of s |
| | | 146 | | /// </summary> |
| | | 147 | | public static bool EncodeLiteralHeaderFieldWithoutNameReference(string name, ReadOnlySpan<string> values, byte[] |
| | 0 | 148 | | { |
| | 0 | 149 | | if (EncodeNameString(name, destination, out int nameLength) && EncodeValueString(values, separator, valueEnc |
| | 0 | 150 | | { |
| | 0 | 151 | | bytesWritten = nameLength + valueLength; |
| | 0 | 152 | | return true; |
| | | 153 | | } |
| | | 154 | | |
| | 0 | 155 | | bytesWritten = 0; |
| | 0 | 156 | | return false; |
| | 0 | 157 | | } |
| | | 158 | | |
| | | 159 | | /// <summary> |
| | | 160 | | /// Encodes just the value part of a Literawl Header Field Without Static Name Reference. Must call <see cref="E |
| | | 161 | | /// </summary> |
| | | 162 | | public static byte[] EncodeLiteralHeaderFieldWithoutNameReferenceToArray(string name) |
| | 61 | 163 | | { |
| | 61 | 164 | | Span<byte> temp = name.Length < 256 ? stackalloc byte[256 + IntegerEncoder.MaxInt32EncodedLength] : new byte |
| | | 165 | | |
| | 61 | 166 | | bool res = EncodeNameString(name, temp, out int nameLength); |
| | 61 | 167 | | Debug.Assert(res); |
| | | 168 | | |
| | 61 | 169 | | return temp.Slice(0, nameLength).ToArray(); |
| | 61 | 170 | | } |
| | | 171 | | |
| | | 172 | | public static byte[] EncodeLiteralHeaderFieldWithoutNameReferenceToArray(string name, string value) |
| | 0 | 173 | | { |
| | 0 | 174 | | Span<byte> temp = (name.Length + value.Length) < 256 ? stackalloc byte[256 + IntegerEncoder.MaxInt32EncodedL |
| | | 175 | | |
| | 0 | 176 | | bool res = EncodeLiteralHeaderFieldWithoutNameReference(name, value, temp, out int bytesWritten); |
| | 0 | 177 | | Debug.Assert(res); |
| | | 178 | | |
| | 0 | 179 | | return temp.Slice(0, bytesWritten).ToArray(); |
| | 0 | 180 | | } |
| | | 181 | | |
| | | 182 | | private static bool EncodeValueString(string s, Encoding? valueEncoding, Span<byte> buffer, out int length) |
| | 0 | 183 | | { |
| | 0 | 184 | | if (buffer.Length != 0) |
| | 0 | 185 | | { |
| | 0 | 186 | | buffer[0] = 0; |
| | | 187 | | |
| | 0 | 188 | | int encodedStringLength = valueEncoding is null || ReferenceEquals(valueEncoding, Encoding.Latin1) |
| | 0 | 189 | | ? s.Length |
| | 0 | 190 | | : valueEncoding.GetByteCount(s); |
| | | 191 | | |
| | 0 | 192 | | if (IntegerEncoder.Encode(encodedStringLength, 7, buffer, out int nameLength)) |
| | 0 | 193 | | { |
| | 0 | 194 | | buffer = buffer.Slice(nameLength); |
| | 0 | 195 | | if (buffer.Length >= encodedStringLength) |
| | 0 | 196 | | { |
| | 0 | 197 | | if (valueEncoding is null) |
| | 0 | 198 | | { |
| | 0 | 199 | | EncodeValueStringPart(s, buffer); |
| | 0 | 200 | | } |
| | | 201 | | else |
| | 0 | 202 | | { |
| | 0 | 203 | | int written = valueEncoding.GetBytes(s, buffer); |
| | 0 | 204 | | Debug.Assert(written == encodedStringLength); |
| | 0 | 205 | | } |
| | | 206 | | |
| | 0 | 207 | | length = nameLength + encodedStringLength; |
| | 0 | 208 | | return true; |
| | | 209 | | } |
| | 0 | 210 | | } |
| | 0 | 211 | | } |
| | | 212 | | |
| | 0 | 213 | | length = 0; |
| | 0 | 214 | | return false; |
| | 0 | 215 | | } |
| | | 216 | | |
| | | 217 | | /// <summary> |
| | | 218 | | /// Encodes a value by concatenating a collection of strings, separated by a separator string. |
| | | 219 | | /// </summary> |
| | | 220 | | public static bool EncodeValueString(ReadOnlySpan<string> values, byte[]? separator, Encoding? valueEncoding, Sp |
| | 0 | 221 | | { |
| | 0 | 222 | | if (values.Length == 1) |
| | 0 | 223 | | { |
| | 0 | 224 | | return EncodeValueString(values[0], valueEncoding, buffer, out length); |
| | | 225 | | } |
| | | 226 | | |
| | 0 | 227 | | if (values.Length == 0) |
| | 0 | 228 | | { |
| | | 229 | | // TODO: this will be called with a string array from HttpHeaderCollection. Can we ever get a 0-length a |
| | 0 | 230 | | return EncodeValueString(string.Empty, valueEncoding: null, buffer, out length); |
| | | 231 | | } |
| | | 232 | | |
| | 0 | 233 | | if (buffer.Length > 0) |
| | 0 | 234 | | { |
| | 0 | 235 | | Debug.Assert(separator != null); |
| | 0 | 236 | | Debug.Assert(Ascii.IsValid(separator)); |
| | 0 | 237 | | int valueLength = separator.Length * (values.Length - 1); |
| | | 238 | | |
| | 0 | 239 | | if (valueEncoding is null || ReferenceEquals(valueEncoding, Encoding.Latin1)) |
| | 0 | 240 | | { |
| | 0 | 241 | | foreach (string part in values) |
| | 0 | 242 | | { |
| | 0 | 243 | | valueLength += part.Length; |
| | 0 | 244 | | } |
| | 0 | 245 | | } |
| | | 246 | | else |
| | 0 | 247 | | { |
| | 0 | 248 | | foreach (string part in values) |
| | 0 | 249 | | { |
| | 0 | 250 | | valueLength += valueEncoding.GetByteCount(part); |
| | 0 | 251 | | } |
| | 0 | 252 | | } |
| | | 253 | | |
| | 0 | 254 | | buffer[0] = 0; |
| | 0 | 255 | | if (IntegerEncoder.Encode(valueLength, 7, buffer, out int nameLength)) |
| | 0 | 256 | | { |
| | 0 | 257 | | buffer = buffer.Slice(nameLength); |
| | 0 | 258 | | if (buffer.Length >= valueLength) |
| | 0 | 259 | | { |
| | 0 | 260 | | if (valueEncoding is null) |
| | 0 | 261 | | { |
| | 0 | 262 | | string value = values[0]; |
| | 0 | 263 | | EncodeValueStringPart(value, buffer); |
| | 0 | 264 | | buffer = buffer.Slice(value.Length); |
| | | 265 | | |
| | 0 | 266 | | for (int i = 1; i < values.Length; i++) |
| | 0 | 267 | | { |
| | 0 | 268 | | separator.CopyTo(buffer); |
| | 0 | 269 | | buffer = buffer.Slice(separator.Length); |
| | | 270 | | |
| | 0 | 271 | | value = values[i]; |
| | 0 | 272 | | EncodeValueStringPart(value, buffer); |
| | 0 | 273 | | buffer = buffer.Slice(value.Length); |
| | 0 | 274 | | } |
| | 0 | 275 | | } |
| | | 276 | | else |
| | 0 | 277 | | { |
| | 0 | 278 | | int written = valueEncoding.GetBytes(values[0], buffer); |
| | 0 | 279 | | buffer = buffer.Slice(written); |
| | | 280 | | |
| | 0 | 281 | | for (int i = 1; i < values.Length; i++) |
| | 0 | 282 | | { |
| | 0 | 283 | | separator.CopyTo(buffer); |
| | 0 | 284 | | buffer = buffer.Slice(separator.Length); |
| | | 285 | | |
| | 0 | 286 | | written = valueEncoding.GetBytes(values[i], buffer); |
| | 0 | 287 | | buffer = buffer.Slice(written); |
| | 0 | 288 | | } |
| | 0 | 289 | | } |
| | | 290 | | |
| | 0 | 291 | | length = nameLength + valueLength; |
| | 0 | 292 | | return true; |
| | | 293 | | } |
| | 0 | 294 | | } |
| | 0 | 295 | | } |
| | | 296 | | |
| | 0 | 297 | | length = 0; |
| | 0 | 298 | | return false; |
| | 0 | 299 | | } |
| | | 300 | | |
| | | 301 | | private static void EncodeValueStringPart(string s, Span<byte> buffer) |
| | 0 | 302 | | { |
| | 0 | 303 | | Debug.Assert(buffer.Length >= s.Length); |
| | | 304 | | |
| | 0 | 305 | | OperationStatus status = Ascii.FromUtf16(s, buffer, out int bytesWritten); |
| | | 306 | | |
| | 0 | 307 | | if (status == OperationStatus.InvalidData) |
| | 0 | 308 | | { |
| | 0 | 309 | | throw new QPackEncodingException(SR.net_http_request_invalid_char_encoding); |
| | | 310 | | } |
| | | 311 | | |
| | 0 | 312 | | Debug.Assert(status == OperationStatus.Done); |
| | 0 | 313 | | Debug.Assert(bytesWritten == s.Length); |
| | 0 | 314 | | } |
| | | 315 | | |
| | | 316 | | private static bool EncodeNameString(string s, Span<byte> buffer, out int length) |
| | 61 | 317 | | { |
| | 61 | 318 | | Debug.Assert(Ascii.IsValid(s)); |
| | | 319 | | |
| | 61 | 320 | | if (buffer.Length != 0) |
| | 61 | 321 | | { |
| | 61 | 322 | | buffer[0] = 0x30; |
| | | 323 | | |
| | 61 | 324 | | if (IntegerEncoder.Encode(s.Length, 3, buffer, out int nameLength)) |
| | 61 | 325 | | { |
| | 61 | 326 | | buffer = buffer.Slice(nameLength); |
| | | 327 | | |
| | 61 | 328 | | if (buffer.Length >= s.Length) |
| | 61 | 329 | | { |
| | 61 | 330 | | OperationStatus status = Ascii.ToLower(s, buffer, out int valueBytesWritten); |
| | 61 | 331 | | Debug.Assert(status == OperationStatus.Done); |
| | 61 | 332 | | Debug.Assert(valueBytesWritten == s.Length); |
| | | 333 | | |
| | 61 | 334 | | length = nameLength + s.Length; |
| | 61 | 335 | | return true; |
| | | 336 | | } |
| | 0 | 337 | | } |
| | 0 | 338 | | } |
| | | 339 | | |
| | 0 | 340 | | length = 0; |
| | 0 | 341 | | return false; |
| | 61 | 342 | | } |
| | | 343 | | |
| | | 344 | | /* |
| | | 345 | | * 0 1 2 3 4 5 6 7 |
| | | 346 | | +---+---+---+---+---+---+---+---+ |
| | | 347 | | | Required Insert Count (8+) | |
| | | 348 | | +---+---------------------------+ |
| | | 349 | | | S | Delta Base (7+) | |
| | | 350 | | +---+---------------------------+ |
| | | 351 | | | Compressed Headers ... |
| | | 352 | | +-------------------------------+ |
| | | 353 | | * |
| | | 354 | | */ |
| | | 355 | | private static bool EncodeHeaderBlockPrefix(Span<byte> destination, out int bytesWritten) |
| | | 356 | | { |
| | | 357 | | int length; |
| | | 358 | | bytesWritten = 0; |
| | | 359 | | // Required insert count as first int |
| | | 360 | | if (!IntegerEncoder.Encode(0, 8, destination, out length)) |
| | | 361 | | { |
| | | 362 | | return false; |
| | | 363 | | } |
| | | 364 | | |
| | | 365 | | bytesWritten += length; |
| | | 366 | | destination = destination.Slice(length); |
| | | 367 | | |
| | | 368 | | // Delta base |
| | | 369 | | if (destination.IsEmpty) |
| | | 370 | | { |
| | | 371 | | return false; |
| | | 372 | | } |
| | | 373 | | |
| | | 374 | | destination[0] = 0x00; |
| | | 375 | | if (!IntegerEncoder.Encode(0, 7, destination, out length)) |
| | | 376 | | { |
| | | 377 | | return false; |
| | | 378 | | } |
| | | 379 | | |
| | | 380 | | bytesWritten += length; |
| | | 381 | | |
| | | 382 | | return true; |
| | | 383 | | } |
| | | 384 | | } |
| | | 385 | | } |