1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package io.netty.handler.codec.http3;
17
18 import io.netty.buffer.ByteBuf;
19 import io.netty.buffer.ByteBufAllocator;
20 import io.netty.handler.codec.quic.QuicStreamChannel;
21 import io.netty.util.ReferenceCountUtil;
22 import io.netty.util.collection.LongObjectHashMap;
23
24 import java.util.ArrayDeque;
25 import java.util.Arrays;
26 import java.util.Map;
27 import java.util.Queue;
28
29 import static io.netty.handler.codec.http3.Http3CodecUtils.closeOnFailure;
30 import static io.netty.handler.codec.http3.QpackHeaderField.sizeOf;
31 import static io.netty.handler.codec.http3.QpackUtil.encodePrefixedInteger;
32
33
34
35
36 final class QpackEncoder {
37 private static final QpackException INVALID_SECTION_ACKNOWLEDGMENT =
38 QpackException.newStatic(QpackDecoder.class, "sectionAcknowledgment(...)",
39 "QPACK - section acknowledgment received for unknown stream.");
40 private static final int DYNAMIC_TABLE_ENCODE_NOT_DONE = -1;
41 private static final int DYNAMIC_TABLE_ENCODE_NOT_POSSIBLE = -2;
42
43 private final QpackHuffmanEncoder huffmanEncoder;
44 private final QpackEncoderDynamicTable dynamicTable;
45 private int maxBlockedStreams;
46 private int blockedStreams;
47 private LongObjectHashMap<Queue<Indices>> streamSectionTrackers;
48
49 QpackEncoder() {
50 this(new QpackEncoderDynamicTable());
51 }
52
53 QpackEncoder(QpackEncoderDynamicTable dynamicTable) {
54 huffmanEncoder = new QpackHuffmanEncoder();
55 this.dynamicTable = dynamicTable;
56 }
57
58
59
60
61
62
63 void encodeHeaders(QpackAttributes qpackAttributes, ByteBuf out, ByteBufAllocator allocator, long streamId,
64 Http3Headers headers) {
65 final int base = dynamicTable.insertCount();
66
67
68 ByteBuf tmp = allocator.buffer();
69 try {
70 int maxDynamicTblIdx = -1;
71 int requiredInsertCount = 0;
72 Indices dynamicTableIndices = null;
73 for (Map.Entry<CharSequence, CharSequence> header : headers) {
74 CharSequence name = header.getKey();
75 CharSequence value = header.getValue();
76 int dynamicTblIdx = encodeHeader(qpackAttributes, tmp, base, name, value);
77 if (dynamicTblIdx >= 0) {
78 int req = dynamicTable.addReferenceToEntry(name, value, dynamicTblIdx);
79 if (dynamicTblIdx > maxDynamicTblIdx) {
80 maxDynamicTblIdx = dynamicTblIdx;
81 requiredInsertCount = req;
82 }
83 if (dynamicTableIndices == null) {
84 dynamicTableIndices = new Indices();
85 }
86 dynamicTableIndices.add(dynamicTblIdx);
87 }
88 }
89
90
91 if (dynamicTableIndices != null) {
92 assert streamSectionTrackers != null;
93 streamSectionTrackers.computeIfAbsent(streamId, __ -> new ArrayDeque<>())
94 .add(dynamicTableIndices);
95 }
96
97
98
99
100
101
102
103
104 encodePrefixedInteger(out, (byte) 0b0, 8, dynamicTable.encodedRequiredInsertCount(requiredInsertCount));
105 if (base >= requiredInsertCount) {
106 encodePrefixedInteger(out, (byte) 0b0, 7, base - requiredInsertCount);
107 } else {
108 encodePrefixedInteger(out, (byte) 0b1000_0000, 7, requiredInsertCount - base - 1);
109 }
110 out.writeBytes(tmp);
111 } finally {
112 tmp.release();
113 }
114 }
115
116 void configureDynamicTable(QpackAttributes attributes, long maxTableCapacity, int blockedStreams)
117 throws QpackException {
118 if (maxTableCapacity > 0) {
119 assert attributes.encoderStreamAvailable();
120 final QuicStreamChannel encoderStream = attributes.encoderStream();
121 dynamicTable.maxTableCapacity(maxTableCapacity);
122 final ByteBuf tableCapacity = encoderStream.alloc().buffer(8);
123
124
125
126
127
128 encodePrefixedInteger(tableCapacity, (byte) 0b0010_0000, 5, maxTableCapacity);
129 closeOnFailure(encoderStream.writeAndFlush(tableCapacity));
130
131 streamSectionTrackers = new LongObjectHashMap<>();
132 maxBlockedStreams = blockedStreams;
133 }
134 }
135
136
137
138
139
140
141
142 void sectionAcknowledgment(long streamId) throws QpackException {
143 assert streamSectionTrackers != null;
144 final Queue<Indices> tracker = streamSectionTrackers.get(streamId);
145 if (tracker == null) {
146 throw INVALID_SECTION_ACKNOWLEDGMENT;
147 }
148
149 Indices dynamicTableIndices = tracker.poll();
150
151 if (tracker.isEmpty()) {
152 streamSectionTrackers.remove(streamId);
153 }
154
155 if (dynamicTableIndices == null) {
156 throw INVALID_SECTION_ACKNOWLEDGMENT;
157 }
158
159 dynamicTableIndices.forEach(dynamicTable::acknowledgeInsertCountOnAck);
160 }
161
162
163
164
165
166
167
168 void streamCancellation(long streamId) throws QpackException {
169
170
171
172 if (streamSectionTrackers == null) {
173 return;
174 }
175 final Queue<Indices> tracker = streamSectionTrackers.remove(streamId);
176 if (tracker != null) {
177 for (;;) {
178 Indices dynamicTableIndices = tracker.poll();
179 if (dynamicTableIndices == null) {
180 break;
181 }
182 dynamicTableIndices.forEach(dynamicTable::acknowledgeInsertCountOnCancellation);
183 }
184 }
185 }
186
187
188
189
190
191
192
193 void insertCountIncrement(int increment) throws QpackException {
194 dynamicTable.incrementKnownReceivedCount(increment);
195 }
196
197
198
199
200
201
202
203
204
205
206
207 private int encodeHeader(QpackAttributes qpackAttributes, ByteBuf out, int base, CharSequence name,
208 CharSequence value) {
209 int index = QpackStaticTable.findFieldIndex(name, value);
210 if (index == QpackStaticTable.NOT_FOUND) {
211 if (qpackAttributes.dynamicTableDisabled()) {
212 encodeLiteral(out, name, value);
213 return DYNAMIC_TABLE_ENCODE_NOT_POSSIBLE;
214 }
215 return encodeWithDynamicTable(qpackAttributes, out, base, name, value);
216 } else if ((index & QpackStaticTable.MASK_NAME_REF) == QpackStaticTable.MASK_NAME_REF) {
217 int dynamicTblIdx = tryEncodeWithDynamicTable(qpackAttributes, out, base, name, value);
218 if (dynamicTblIdx >= 0) {
219 return dynamicTblIdx;
220 }
221 final int nameIdx = index ^ QpackStaticTable.MASK_NAME_REF;
222 dynamicTblIdx = tryAddToDynamicTable(qpackAttributes, true, nameIdx, name, value);
223 if (dynamicTblIdx >= 0) {
224 if (dynamicTblIdx >= base) {
225 encodePostBaseIndexed(out, base, dynamicTblIdx);
226 } else {
227 encodeIndexedDynamicTable(out, base, dynamicTblIdx);
228 }
229 return dynamicTblIdx;
230 }
231 encodeLiteralWithNameRefStaticTable(out, nameIdx, value);
232 } else {
233 encodeIndexedStaticTable(out, index);
234 }
235 return qpackAttributes.dynamicTableDisabled() ? DYNAMIC_TABLE_ENCODE_NOT_POSSIBLE :
236 DYNAMIC_TABLE_ENCODE_NOT_DONE;
237 }
238
239
240
241
242
243
244
245
246
247
248
249
250 private int encodeWithDynamicTable(QpackAttributes qpackAttributes, ByteBuf out, int base, CharSequence name,
251 CharSequence value) {
252 int idx = tryEncodeWithDynamicTable(qpackAttributes, out, base, name, value);
253 if (idx >= 0) {
254 return idx;
255 }
256
257 if (idx == DYNAMIC_TABLE_ENCODE_NOT_DONE) {
258 idx = tryAddToDynamicTable(qpackAttributes, false, -1, name, value);
259 if (idx >= 0) {
260 if (idx >= base) {
261 encodePostBaseIndexed(out, base, idx);
262 } else {
263 encodeIndexedDynamicTable(out, base, idx);
264 }
265 return idx;
266 }
267 }
268 encodeLiteral(out, name, value);
269 return idx;
270 }
271
272
273
274
275
276
277
278
279
280
281
282
283
284 private int tryEncodeWithDynamicTable(QpackAttributes qpackAttributes, ByteBuf out, int base, CharSequence name,
285 CharSequence value) {
286 if (qpackAttributes.dynamicTableDisabled()) {
287 return DYNAMIC_TABLE_ENCODE_NOT_POSSIBLE;
288 }
289 assert qpackAttributes.encoderStreamAvailable();
290 final QuicStreamChannel encoderStream = qpackAttributes.encoderStream();
291
292 int idx = dynamicTable.getEntryIndex(name, value);
293 if (idx == QpackEncoderDynamicTable.NOT_FOUND) {
294 return DYNAMIC_TABLE_ENCODE_NOT_DONE;
295 }
296 if (idx >= 0) {
297 if (dynamicTable.requiresDuplication(idx, sizeOf(name, value))) {
298 idx = dynamicTable.add(name, value, sizeOf(name, value));
299 assert idx >= 0;
300
301
302
303
304
305 ByteBuf duplicate = encoderStream.alloc().buffer(8);
306 encodePrefixedInteger(duplicate, (byte) 0b0000_0000, 5,
307 dynamicTable.relativeIndexForEncoderInstructions(idx));
308 closeOnFailure(encoderStream.writeAndFlush(duplicate));
309 if (mayNotBlockStream()) {
310
311 return DYNAMIC_TABLE_ENCODE_NOT_POSSIBLE;
312 }
313 }
314 if (idx >= base) {
315 encodePostBaseIndexed(out, base, idx);
316 } else {
317 encodeIndexedDynamicTable(out, base, idx);
318 }
319 } else {
320 idx = -(idx + 1);
321 int addIdx = tryAddToDynamicTable(qpackAttributes, false,
322 dynamicTable.relativeIndexForEncoderInstructions(idx), name, value);
323 if (addIdx < 0) {
324 return DYNAMIC_TABLE_ENCODE_NOT_POSSIBLE;
325 }
326 idx = addIdx;
327
328 if (idx >= base) {
329 encodeLiteralWithPostBaseNameRef(out, base, idx, value);
330 } else {
331 encodeLiteralWithNameRefDynamicTable(out, base, idx, value);
332 }
333 }
334 return idx;
335 }
336
337
338
339
340
341
342
343
344
345
346
347
348 private int tryAddToDynamicTable(QpackAttributes qpackAttributes, boolean staticTableNameRef, int nameIdx,
349 CharSequence name, CharSequence value) {
350 if (qpackAttributes.dynamicTableDisabled()) {
351 return DYNAMIC_TABLE_ENCODE_NOT_POSSIBLE;
352 }
353 assert qpackAttributes.encoderStreamAvailable();
354 final QuicStreamChannel encoderStream = qpackAttributes.encoderStream();
355
356 int idx = dynamicTable.add(name, value, sizeOf(name, value));
357 if (idx >= 0) {
358 ByteBuf insert = null;
359 try {
360 if (nameIdx >= 0) {
361
362 insert = encoderStream.alloc().buffer(value.length() + 16);
363
364
365
366
367
368 encodePrefixedInteger(insert, (byte) (staticTableNameRef ? 0b1100_0000 : 0b1000_0000), 6, nameIdx);
369 } else {
370
371 insert = encoderStream.alloc().buffer(name.length() + value.length() + 16);
372
373
374
375
376
377
378
379
380 encodeLengthPrefixedHuffmanEncodedLiteral(insert, (byte) 0b0110_0000, 5, name);
381 }
382
383
384
385
386
387
388 encodeStringLiteral(insert, value);
389 } catch (Exception e) {
390 ReferenceCountUtil.release(insert);
391 return DYNAMIC_TABLE_ENCODE_NOT_DONE;
392 }
393 closeOnFailure(encoderStream.writeAndFlush(insert));
394 if (mayNotBlockStream()) {
395
396 return DYNAMIC_TABLE_ENCODE_NOT_DONE;
397 }
398 blockedStreams++;
399 }
400 return idx;
401 }
402
403 private void encodeIndexedStaticTable(ByteBuf out, int index) {
404
405
406
407
408
409 encodePrefixedInteger(out, (byte) 0b1100_0000, 6, index);
410 }
411
412 private void encodeIndexedDynamicTable(ByteBuf out, int base, int index) {
413
414
415
416
417
418 encodePrefixedInteger(out, (byte) 0b1000_0000, 6, base - index - 1);
419 }
420
421 private void encodePostBaseIndexed(ByteBuf out, int base, int index) {
422
423
424
425
426
427 encodePrefixedInteger(out, (byte) 0b0001_0000, 4, index - base);
428 }
429
430 private void encodeLiteralWithNameRefStaticTable(ByteBuf out, int nameIndex, CharSequence value) {
431
432
433
434
435
436
437
438
439
440
441 encodePrefixedInteger(out, (byte) 0b0101_0000, 4, nameIndex);
442 encodeStringLiteral(out, value);
443 }
444
445 private void encodeLiteralWithNameRefDynamicTable(ByteBuf out, int base, int nameIndex, CharSequence value) {
446
447
448
449
450
451
452
453
454
455
456 encodePrefixedInteger(out, (byte) 0b0101_0000, 4, base - nameIndex - 1);
457 encodeStringLiteral(out, value);
458 }
459
460 private void encodeLiteralWithPostBaseNameRef(ByteBuf out, int base, int nameIndex, CharSequence value) {
461
462
463
464
465
466
467
468
469
470
471 encodePrefixedInteger(out, (byte) 0b0000_0000, 4, nameIndex - base);
472 encodeStringLiteral(out, value);
473 }
474
475 private void encodeLiteral(ByteBuf out, CharSequence name, CharSequence value) {
476
477
478
479
480
481
482
483
484
485
486
487
488 encodeLengthPrefixedHuffmanEncodedLiteral(out, (byte) 0b0010_1000, 3, name);
489 encodeStringLiteral(out, value);
490 }
491
492
493
494
495
496 private void encodeStringLiteral(ByteBuf out, CharSequence value) {
497
498
499
500
501
502
503
504 encodeLengthPrefixedHuffmanEncodedLiteral(out, (byte) 0b1000_0000, 7, value);
505 }
506
507
508
509
510 private void encodeLengthPrefixedHuffmanEncodedLiteral(ByteBuf out, byte mask, int prefix, CharSequence value) {
511 int huffmanLength = huffmanEncoder.getEncodedLength(value);
512 encodePrefixedInteger(out, mask, prefix, huffmanLength);
513 huffmanEncoder.encode(out, value);
514 }
515
516 private boolean mayNotBlockStream() {
517 return blockedStreams >= maxBlockedStreams - 1;
518 }
519
520 private static final class Indices {
521 private int idx;
522
523 private int[] array = new int[4];
524
525 void add(int index) {
526 if (idx == array.length) {
527
528 array = Arrays.copyOf(array, array.length << 1);
529 }
530 array[idx++] = index;
531 }
532
533 void forEach(IndexConsumer consumer) throws QpackException {
534 for (int i = 0; i < idx; i++) {
535 consumer.accept(array[i]);
536 }
537 }
538
539 @FunctionalInterface
540 interface IndexConsumer {
541 void accept(int idx) throws QpackException;
542 }
543 }
544 }