View Javadoc
1   /*
2    * Copyright 2020 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   https://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
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  import io.netty.util.internal.ObjectUtil;
24  
25  import javax.annotation.Nullable;
26  import java.util.ArrayDeque;
27  import java.util.Arrays;
28  import java.util.Map;
29  import java.util.Queue;
30  
31  import static io.netty.handler.codec.http3.Http3CodecUtils.closeOnFailure;
32  import static io.netty.handler.codec.http3.QpackHeaderField.sizeOf;
33  import static io.netty.handler.codec.http3.QpackUtil.encodePrefixedInteger;
34  
35  /**
36   * A QPACK encoder.
37   */
38  final class QpackEncoder {
39      private static final QpackException INVALID_SECTION_ACKNOWLEDGMENT =
40              QpackException.newStatic(QpackDecoder.class, "sectionAcknowledgment(...)",
41                      "QPACK - section acknowledgment received for unknown stream.");
42      private static final int DYNAMIC_TABLE_ENCODE_NOT_DONE = -1;
43      private static final int DYNAMIC_TABLE_ENCODE_NOT_POSSIBLE = -2;
44      /**
45       * Maximum number of field sections for which we will track dynamic-table references while waiting for the
46       * peer's Section Acknowledgment or Stream Cancellation instruction. Both instructions are optional per
47       * <a href="https://www.rfc-editor.org/rfc/rfc9204.html#section-2.2.2.2">RFC 9204, section 2.2.2.2</a>, so a
48       * remote peer that never sends them (while still acknowledging dynamic-table insertions) must not be able to
49       * grow this per-connection state without bound. Once the limit is reached we stop referencing the dynamic
50       * table for new field sections (falling back to literal encoding, which is always legal, see
51       * <a href="https://www.rfc-editor.org/rfc/rfc9204.html#section-4.5.4">section 4.5.4</a>) until enough
52       * outstanding sections are acknowledged or cancelled to free up tracking capacity again.
53       */
54      // Visible for tests
55      static final int MAX_OUTSTANDING_SECTIONS = 10_000;
56  
57      private final QpackHuffmanEncoder huffmanEncoder;
58      private final QpackEncoderDynamicTable dynamicTable;
59      private final QpackSensitivityDetector sensitivityDetector;
60      private int maxBlockedStreams;
61      private int blockedStreams;
62      private LongObjectHashMap<Queue<Indices>> streamSectionTrackers;
63      private int outstandingSections;
64  
65      QpackEncoder(@Nullable QpackSensitivityDetector sensitivityDetector) {
66          this(new QpackEncoderDynamicTable(), sensitivityDetector);
67      }
68  
69      QpackEncoder(QpackEncoderDynamicTable dynamicTable, @Nullable QpackSensitivityDetector sensitivityDetector) {
70          huffmanEncoder = new QpackHuffmanEncoder();
71          this.dynamicTable = ObjectUtil.checkNotNull(dynamicTable, "dynamicTable");
72          this.sensitivityDetector = sensitivityDetector == null ?
73                  QpackSensitivityDetector.NEVER_SENSITIVE : sensitivityDetector;
74      }
75  
76      /**
77       * Encode the header field into the header block.
78       *
79       * <p>Fields for which {@link QpackSensitivityDetector#isSensitive(CharSequence, CharSequence)}
80       * returns {@code true} are encoded as literals with the
81       * <a href="https://www.rfc-editor.org/rfc/rfc9204.html#section-4.5.4">"Never Indexed"</a>
82       * ({@code N=1}) flag set, and are never inserted into the dynamic table.</p>
83       */
84      void encodeHeaders(QpackAttributes qpackAttributes, ByteBuf out, ByteBufAllocator allocator, long streamId,
85                         Http3Headers headers) {
86          final int base = dynamicTable.insertCount();
87          // Allocate a new buffer as we have to go back and write a variable length base and required insert count
88          // later.
89          ByteBuf tmp = allocator.buffer();
90          try {
91              int maxDynamicTblIdx = -1;
92              int requiredInsertCount = 0;
93              Indices dynamicTableIndices = null;
94              for (Map.Entry<CharSequence, CharSequence> header : headers) {
95                  CharSequence name = header.getKey();
96                  CharSequence value = header.getValue();
97                  int dynamicTblIdx;
98                  if (sensitivityDetector.isSensitive(name, value)) {
99                      encodeSensitiveHeader(tmp, name, value);
100                     dynamicTblIdx = DYNAMIC_TABLE_ENCODE_NOT_POSSIBLE;
101                 } else {
102                     dynamicTblIdx = encodeHeader(qpackAttributes, tmp, base, name, value);
103                 }
104                 if (dynamicTblIdx >= 0) {
105                     int req = dynamicTable.addReferenceToEntry(name, value, dynamicTblIdx);
106                     if (dynamicTblIdx > maxDynamicTblIdx) {
107                         maxDynamicTblIdx = dynamicTblIdx;
108                         requiredInsertCount = req;
109                     }
110                     if (dynamicTableIndices == null) {
111                         dynamicTableIndices = new Indices();
112                     }
113                     dynamicTableIndices.add(dynamicTblIdx);
114                 }
115             }
116 
117             // Track all the indices that we need to ack later.
118             if (dynamicTableIndices != null) {
119                 assert streamSectionTrackers != null;
120                 streamSectionTrackers.computeIfAbsent(streamId, __ -> new ArrayDeque<>())
121                         .add(dynamicTableIndices);
122                 outstandingSections++;
123             }
124 
125             // https://www.rfc-editor.org/rfc/rfc9204.html#name-encoded-field-section-prefi
126             //   0   1   2   3   4   5   6   7
127             // +---+---+---+---+---+---+---+---+
128             // |   Required Insert Count (8+)  |
129             // +---+---------------------------+
130             // | S |      Delta Base (7+)      |
131             // +---+---------------------------+
132             encodePrefixedInteger(out, (byte) 0b0, 8, dynamicTable.encodedRequiredInsertCount(requiredInsertCount));
133             if (base >= requiredInsertCount) {
134                 encodePrefixedInteger(out, (byte) 0b0, 7, base - requiredInsertCount);
135             } else {
136                 encodePrefixedInteger(out, (byte) 0b1000_0000, 7, requiredInsertCount - base - 1);
137             }
138             out.writeBytes(tmp);
139         } finally {
140             tmp.release();
141         }
142     }
143 
144     void configureDynamicTable(QpackAttributes attributes, long maxTableCapacity, int blockedStreams)
145             throws QpackException {
146         if (maxTableCapacity > 0) {
147             assert attributes.encoderStreamAvailable();
148             final QuicStreamChannel encoderStream = attributes.encoderStream();
149             dynamicTable.maxTableCapacity(maxTableCapacity);
150             final ByteBuf tableCapacity = encoderStream.alloc().buffer(8);
151             // https://www.rfc-editor.org/rfc/rfc9204.html#name-set-dynamic-table-capacity
152             //  0   1   2   3   4   5   6   7
153             // +---+---+---+---+---+---+---+---+
154             // | 0 | 0 | 1 |   Capacity (5+)   |
155             // +---+---+---+-------------------+
156             encodePrefixedInteger(tableCapacity, (byte) 0b0010_0000, 5, maxTableCapacity);
157             closeOnFailure(encoderStream.writeAndFlush(tableCapacity));
158 
159             streamSectionTrackers = new LongObjectHashMap<>();
160             maxBlockedStreams = blockedStreams;
161         }
162     }
163 
164     /**
165      * <a href="https://quicwg.org/base-drafts/draft-ietf-quic-qpack.html#name-section-acknowledgment">
166      *     Section acknowledgment</a> for the passed {@code streamId}.
167      *
168      * @param streamId For which the header fields section is acknowledged.
169      */
170     void sectionAcknowledgment(long streamId) throws QpackException {
171         assert streamSectionTrackers != null;
172         final Queue<Indices> tracker = streamSectionTrackers.get(streamId);
173         if (tracker == null) {
174             throw INVALID_SECTION_ACKNOWLEDGMENT;
175         }
176 
177         Indices dynamicTableIndices = tracker.poll();
178 
179         if (tracker.isEmpty()) {
180             streamSectionTrackers.remove(streamId);
181         }
182 
183         if (dynamicTableIndices == null) {
184             throw INVALID_SECTION_ACKNOWLEDGMENT;
185         }
186         outstandingSections--;
187 
188         dynamicTableIndices.forEach(dynamicTable::acknowledgeInsertCountOnAck);
189     }
190 
191     /**
192      * <a href="https://quicwg.org/base-drafts/draft-ietf-quic-qpack.html#name-stream-cancellation">
193      *     Stream cancellation</a> for the passed {@code streamId}.
194      *
195      * @param streamId which is cancelled.
196      */
197     void streamCancellation(long streamId) throws QpackException {
198         // If a configureDynamicTable(...) was called with a maxTableCapacity of 0 we will have not instanced
199         // streamSectionTrackers. The remote peer might still send a stream cancellation for a stream, while it
200         // is optional. See https://www.rfc-editor.org/rfc/rfc9204.html#section-2.2.2.2
201         if (streamSectionTrackers == null) {
202             return;
203         }
204         final Queue<Indices> tracker = streamSectionTrackers.remove(streamId);
205         if (tracker != null) {
206             for (;;) {
207                 Indices dynamicTableIndices = tracker.poll();
208                 if (dynamicTableIndices == null) {
209                     break;
210                 }
211                 outstandingSections--;
212                 dynamicTableIndices.forEach(dynamicTable::acknowledgeInsertCountOnCancellation);
213             }
214         }
215     }
216 
217     /**
218      * <a href="https://quicwg.org/base-drafts/draft-ietf-quic-qpack.html#name-insert-count-increment">
219      *     Insert count increment</a>.
220      *
221      * @param increment for the known received count.
222      */
223     void insertCountIncrement(int increment) throws QpackException {
224         dynamicTable.incrementKnownReceivedCount(increment);
225     }
226 
227     /**
228      * Encode a header field that the {@link QpackSensitivityDetector} flagged as sensitive.
229      *
230      * <p>Sensitive fields are never inserted into the dynamic table and are encoded
231      * with the {@code "Never Indexed"} flag ({@code N=1}) so that intermediaries
232      * also avoid indexing them — see
233      * <a href="https://www.rfc-editor.org/rfc/rfc9204.html#section-7.1">RFC 9204 7.1</a>.
234      * </p>
235      */
236     private void encodeSensitiveHeader(ByteBuf out, CharSequence name, CharSequence value) {
237         final int index = QpackStaticTable.findFieldIndex(name, value);
238         if (index == QpackStaticTable.NOT_FOUND) {
239             encodeLiteral(out, name, value, true);
240         } else if ((index & QpackStaticTable.MASK_NAME_REF) == QpackStaticTable.MASK_NAME_REF) {
241             // Name-only match, reuse the cached lookup instead of calling getIndex(name) again.
242             encodeLiteralWithNameRefStaticTable(out, index ^ QpackStaticTable.MASK_NAME_REF, value, true);
243         } else {
244             // Exact (name, value) match in the static table, an indexed static reference
245             // does not leak more information than what is already public, and the
246             // intermediary cannot gain any compression benefit by inserting a copy into
247             // its own dynamic table (the entry is already there as part of the static table).
248             encodeIndexedStaticTable(out, index);
249         }
250     }
251 
252     /**
253      * Encode the header field into the header block.
254      * @param qpackAttributes {@link QpackAttributes} for the channel.
255      * @param out {@link ByteBuf} to which encoded header field is to be written.
256      * @param base Base for the dynamic table index.
257      * @param name for the header field.
258      * @param value for the header field.
259      * @return Index in the dynamic table if the header field was encoded as a reference to the dynamic table,
260      * {@link #DYNAMIC_TABLE_ENCODE_NOT_DONE } otherwise.
261      */
262     private int encodeHeader(QpackAttributes qpackAttributes, ByteBuf out, int base, CharSequence name,
263                              CharSequence value) {
264         int index = QpackStaticTable.findFieldIndex(name, value);
265         if (index == QpackStaticTable.NOT_FOUND) {
266             if (isDynamicTableUnavailable(qpackAttributes)) {
267                 encodeLiteral(out, name, value, false);
268                 return DYNAMIC_TABLE_ENCODE_NOT_POSSIBLE;
269             }
270             return encodeWithDynamicTable(qpackAttributes, out, base, name, value);
271         } else if ((index & QpackStaticTable.MASK_NAME_REF) == QpackStaticTable.MASK_NAME_REF) {
272             int dynamicTblIdx = tryEncodeWithDynamicTable(qpackAttributes, out, base, name, value);
273             if (dynamicTblIdx >= 0) {
274                 return dynamicTblIdx;
275             }
276             final int nameIdx = index ^ QpackStaticTable.MASK_NAME_REF;
277             dynamicTblIdx = tryAddToDynamicTable(qpackAttributes, true, nameIdx, name, value);
278             if (dynamicTblIdx >= 0) {
279                 if (dynamicTblIdx >= base) {
280                     encodePostBaseIndexed(out, base, dynamicTblIdx);
281                 } else {
282                     encodeIndexedDynamicTable(out, base, dynamicTblIdx);
283                 }
284                 return dynamicTblIdx;
285             }
286             encodeLiteralWithNameRefStaticTable(out, nameIdx, value, false);
287         } else {
288             encodeIndexedStaticTable(out, index);
289         }
290         return isDynamicTableUnavailable(qpackAttributes) ? DYNAMIC_TABLE_ENCODE_NOT_POSSIBLE :
291                 DYNAMIC_TABLE_ENCODE_NOT_DONE;
292     }
293 
294     /**
295      * Encode the header field using dynamic table, if possible.
296      *
297      * @param qpackAttributes {@link QpackAttributes} for the channel.
298      * @param out {@link ByteBuf} to which encoded header field is to be written.
299      * @param base Base for the dynamic table index.
300      * @param name for the header field.
301      * @param value for the header field.
302      * @return Index in the dynamic table if the header field was encoded as a reference to the dynamic table,
303      * {@link #DYNAMIC_TABLE_ENCODE_NOT_DONE } otherwise.
304      */
305     private int encodeWithDynamicTable(QpackAttributes qpackAttributes, ByteBuf out, int base, CharSequence name,
306                                        CharSequence value) {
307         int idx = tryEncodeWithDynamicTable(qpackAttributes, out, base, name, value);
308         if (idx >= 0) {
309             return idx;
310         }
311 
312         if (idx == DYNAMIC_TABLE_ENCODE_NOT_DONE) {
313             idx = tryAddToDynamicTable(qpackAttributes, false, -1, name, value);
314             if (idx >= 0) {
315                 if (idx >= base) {
316                     encodePostBaseIndexed(out, base, idx);
317                 } else {
318                     encodeIndexedDynamicTable(out, base, idx);
319                 }
320                 return idx;
321             }
322         }
323         encodeLiteral(out, name, value, false);
324         return idx;
325     }
326 
327     /**
328      * Try to encode the header field using dynamic table, otherwise do not encode.
329      *
330      * @param qpackAttributes {@link QpackAttributes} for the channel.
331      * @param out {@link ByteBuf} to which encoded header field is to be written.
332      * @param base Base for the dynamic table index.
333      * @param name for the header field.
334      * @param value for the header field.
335      * @return Index in the dynamic table if the header field was encoded as a reference to the dynamic table.
336      * {@link #DYNAMIC_TABLE_ENCODE_NOT_DONE } if encoding was not done. {@link #DYNAMIC_TABLE_ENCODE_NOT_POSSIBLE }
337      * if dynamic table encoding is not possible (size constraint) and hence should not be tried for this header.
338      */
339     private int tryEncodeWithDynamicTable(QpackAttributes qpackAttributes, ByteBuf out, int base, CharSequence name,
340                                           CharSequence value) {
341         if (isDynamicTableUnavailable(qpackAttributes)) {
342             return DYNAMIC_TABLE_ENCODE_NOT_POSSIBLE;
343         }
344         assert qpackAttributes.encoderStreamAvailable();
345         final QuicStreamChannel encoderStream = qpackAttributes.encoderStream();
346 
347         int idx = dynamicTable.getEntryIndex(name, value);
348         if (idx == QpackEncoderDynamicTable.NOT_FOUND) {
349             return DYNAMIC_TABLE_ENCODE_NOT_DONE;
350         }
351         if (idx >= 0) {
352             if (dynamicTable.requiresDuplication(idx, sizeOf(name, value))) {
353                 idx = dynamicTable.add(name, value, sizeOf(name, value));
354                 assert idx >= 0;
355                 // https://www.rfc-editor.org/rfc/rfc9204.html#section-4.3.4
356                 //  0   1   2   3   4   5   6   7
357                 // +---+---+---+---+---+---+---+---+
358                 // | 0 | 0 | 0 |    Index (5+)     |
359                 // +---+---+---+-------------------+
360                 ByteBuf duplicate = encoderStream.alloc().buffer(8);
361                 encodePrefixedInteger(duplicate, (byte) 0b0000_0000, 5,
362                         dynamicTable.relativeIndexForEncoderInstructions(idx));
363                 closeOnFailure(encoderStream.writeAndFlush(duplicate));
364                 if (mayNotBlockStream()) {
365                     // Add to the table but do not use the entry in the header block to avoid blocking.
366                     return DYNAMIC_TABLE_ENCODE_NOT_POSSIBLE;
367                 }
368             }
369             if (idx >= base) {
370                 encodePostBaseIndexed(out, base, idx);
371             } else {
372                 encodeIndexedDynamicTable(out, base, idx);
373             }
374         } else { // name match
375             idx = -(idx + 1);
376             int addIdx = tryAddToDynamicTable(qpackAttributes, false,
377                     dynamicTable.relativeIndexForEncoderInstructions(idx), name, value);
378             if (addIdx < 0) {
379                 return DYNAMIC_TABLE_ENCODE_NOT_POSSIBLE;
380             }
381             idx = addIdx;
382 
383             if (idx >= base) {
384                 encodeLiteralWithPostBaseNameRef(out, base, idx, value);
385             } else {
386                 encodeLiteralWithNameRefDynamicTable(out, base, idx, value);
387             }
388         }
389         return idx;
390     }
391 
392     /**
393      * Try adding the header field to the dynamic table.
394      *
395      * @param qpackAttributes {@link QpackAttributes} for the channel.
396      * @param staticTableNameRef if {@code nameIdx} is an index in the static table.
397      * @param nameIdx Index of the name if {@code > 0}.
398      * @param name for the header field.
399      * @param value for the header field.
400      * @return Index in the dynamic table if the header field was encoded as a reference to the dynamic table,
401      * {@link #DYNAMIC_TABLE_ENCODE_NOT_DONE} otherwise.
402      */
403     private int tryAddToDynamicTable(QpackAttributes qpackAttributes, boolean staticTableNameRef, int nameIdx,
404                                      CharSequence name, CharSequence value) {
405         if (isDynamicTableUnavailable(qpackAttributes)) {
406             return DYNAMIC_TABLE_ENCODE_NOT_POSSIBLE;
407         }
408         assert qpackAttributes.encoderStreamAvailable();
409         final QuicStreamChannel encoderStream = qpackAttributes.encoderStream();
410 
411         int idx = dynamicTable.add(name, value, sizeOf(name, value));
412         if (idx >= 0) {
413             ByteBuf insert = null;
414             try {
415                 if (nameIdx >= 0) {
416                     // 2 prefixed integers (name index and value length) each requires a maximum of 8 bytes
417                     insert = encoderStream.alloc().buffer(value.length() + 16);
418                     // https://www.rfc-editor.org/rfc/rfc9204.html#name-insert-with-name-reference
419                     //    0   1   2   3   4   5   6   7
420                     // +---+---+---+---+---+---+---+---+
421                     // | 1 | T |    Name Index (6+)    |
422                     // +---+---+-----------------------+
423                     encodePrefixedInteger(insert, (byte) (staticTableNameRef ? 0b1100_0000 : 0b1000_0000), 6, nameIdx);
424                 } else {
425                     // 2 prefixed integers (name and value length) each requires a maximum of 8 bytes
426                     insert = encoderStream.alloc().buffer(name.length() + value.length() + 16);
427                     // https://www.rfc-editor.org/rfc/rfc9204.html#name-insert-with-literal-name
428                     //     0   1   2   3   4   5   6   7
429                     //   +---+---+---+---+---+---+---+---+
430                     //   | 0 | 1 | H | Name Length (5+)  |
431                     //   +---+---+---+-------------------+
432                     //   |  Name String (Length bytes)   |
433                     //   +---+---------------------------+
434                     // Names are always Huffman-encoded (H = 1). RFC 9204 makes
435                     // Huffman a pure size/CPU tradeoff and does not tie it to the
436                     // sensitivity of the field; whether intermediaries may index
437                     // the field is controlled separately by the N bit on the
438                     // matching literal field line.
439                     encodeLengthPrefixedHuffmanEncodedLiteral(insert, (byte) 0b0110_0000, 5, name);
440                 }
441                 //    0   1   2   3   4   5   6   7
442                 // +---+---+-----------------------+
443                 // | H |     Value Length (7+)     |
444                 // +---+---------------------------+
445                 // |  Value String (Length bytes)  |
446                 // +-------------------------------+
447                 encodeStringLiteral(insert, value);
448             } catch (Exception e) {
449                 ReferenceCountUtil.release(insert);
450                 return DYNAMIC_TABLE_ENCODE_NOT_DONE;
451             }
452             closeOnFailure(encoderStream.writeAndFlush(insert));
453             if (mayNotBlockStream()) {
454                 // Add to the table but do not use the entry in the header block to avoid blocking.
455                 return DYNAMIC_TABLE_ENCODE_NOT_DONE;
456             }
457             blockedStreams++;
458         }
459         return idx;
460     }
461 
462     private void encodeIndexedStaticTable(ByteBuf out, int index) {
463         // https://www.rfc-editor.org/rfc/rfc9204.html#name-indexed-field-line
464         //   0   1   2   3   4   5   6   7
465         // +---+---+---+---+---+---+---+---+
466         // | 1 | T |      Index (6+)       |
467         // +---+---+-----------------------+
468         encodePrefixedInteger(out, (byte) 0b1100_0000, 6, index);
469     }
470 
471     private void encodeIndexedDynamicTable(ByteBuf out, int base, int index) {
472         // https://www.rfc-editor.org/rfc/rfc9204.html#name-indexed-field-line
473         //   0   1   2   3   4   5   6   7
474         // +---+---+---+---+---+---+---+---+
475         // | 1 | T |      Index (6+)       |
476         // +---+---+-----------------------+
477         encodePrefixedInteger(out, (byte) 0b1000_0000, 6, base - index - 1);
478     }
479 
480     private void encodePostBaseIndexed(ByteBuf out, int base, int index) {
481         // https://www.rfc-editor.org/rfc/rfc9204.html#name-indexed-field-line-with-pos
482         //   0   1   2   3   4   5   6   7
483         // +---+---+---+---+---+---+---+---+
484         // | 0 | 0 | 0 | 1 |  Index (4+)   |
485         // +---+---+---+---+---------------+
486         encodePrefixedInteger(out, (byte) 0b0001_0000, 4, index - base);
487     }
488 
489     private void encodeLiteralWithNameRefStaticTable(ByteBuf out, int nameIndex, CharSequence value,
490                                                      boolean neverIndex) {
491         // https://www.rfc-editor.org/rfc/rfc9204.html#name-literal-field-line-with-nam
492         //     0   1   2   3   4   5   6   7
493         //   +---+---+---+---+---+---+---+---+
494         //   | 0 | 1 | N | T |Name Index (4+)|
495         //   +---+---+---+---+---------------+
496         //   | H |     Value Length (7+)     |
497         //   +---+---------------------------+
498         //   |  Value String (Length bytes)  |
499         //   +-------------------------------+
500         //
501         // T = 1 (static table). N is driven by the sensitivity detector.
502         final byte prefix = (byte) (0b0101_0000 | (neverIndex ? 0b0010_0000 : 0));
503         encodePrefixedInteger(out, prefix, 4, nameIndex);
504         encodeStringLiteral(out, value);
505     }
506 
507     private void encodeLiteralWithNameRefDynamicTable(ByteBuf out, int base, int nameIndex, CharSequence value) {
508         // https://www.rfc-editor.org/rfc/rfc9204.html#name-literal-field-line-with-nam
509         //     0   1   2   3   4   5   6   7
510         //   +---+---+---+---+---+---+---+---+
511         //   | 0 | 1 | N | T |Name Index (4+)|
512         //   +---+---+---+---+---------------+
513         //   | H |     Value Length (7+)     |
514         //   +---+---------------------------+
515         //   |  Value String (Length bytes)  |
516         //   +-------------------------------+
517         //
518         // T = 0 (dynamic table). Sensitive headers are routed through
519         // encodeSensitiveHeader() which bypasses the dynamic table entirely, so
520         // anything reaching this method is non-sensitive and N is always 0.
521         encodePrefixedInteger(out, (byte) 0b0100_0000, 4, base - nameIndex - 1);
522         encodeStringLiteral(out, value);
523     }
524 
525     private void encodeLiteralWithPostBaseNameRef(ByteBuf out, int base, int nameIndex, CharSequence value) {
526         // https://www.rfc-editor.org/rfc/rfc9204.html#name-literal-field-line-with-pos
527         //    0   1   2   3   4   5   6   7
528         //   +---+---+---+---+---+---+---+---+
529         //   | 0 | 0 | 0 | 0 | N |NameIdx(3+)|
530         //   +---+---+---+---+---+-----------+
531         //   | H |     Value Length (7+)     |
532         //   +---+---------------------------+
533         //   |  Value String (Length bytes)  |
534         //   +-------------------------------+
535         //
536         // Same as above post-base references only exist for entries in the
537         // encoder's dynamic table, so sensitive headers never reach here and
538         // N is always 0.
539         encodePrefixedInteger(out, (byte) 0, 3, nameIndex - base);
540         encodeStringLiteral(out, value);
541     }
542 
543     private void encodeLiteral(ByteBuf out, CharSequence name, CharSequence value, boolean neverIndex) {
544         // https://www.rfc-editor.org/rfc/rfc9204.html#name-literal-field-line-with-lit
545         //   0   1   2   3   4   5   6   7
546         //   +---+---+---+---+---+---+---+---+
547         //   | 0 | 0 | 1 | N | H |NameLen(3+)|
548         //   +---+---+---+---+---+-----------+
549         //   |  Name String (Length bytes)   |
550         //   +---+---------------------------+
551         //   | H |     Value Length (7+)     |
552         //   +---+---------------------------+
553         //   |  Value String (Length bytes)  |
554         //   +-------------------------------+
555         //
556         // H = 1 (Huffman) is always set for the name length prefix.
557         final byte prefix = (byte) (0b0010_1000 | (neverIndex ? 0b0001_0000 : 0));
558         encodeLengthPrefixedHuffmanEncodedLiteral(out, prefix, 3, name);
559         encodeStringLiteral(out, value);
560     }
561 
562     /**
563      * Encode string literal according to Section 5.2.
564      * <a href="https://tools.ietf.org/html/rfc7541#section-5.2">Section 5.2</a>.
565      */
566     private void encodeStringLiteral(ByteBuf out, CharSequence value) {
567         //    0   1   2   3   4   5   6   7
568         // +---+---+---+---+---+---+---+---+
569         // | H |    String Length (7+)     |
570         // +---+---------------------------+
571         // |  String Data (Length octets)  |
572         // +-------------------------------+
573         // String values are always Huffman-encoded (H = 1). RFC 9204 treats the
574         // H bit as a pure size/CPU choice; whether intermediaries may index the
575         // field is controlled separately by the N bit on the literal field line.
576         encodeLengthPrefixedHuffmanEncodedLiteral(out, (byte) 0b1000_0000, 7, value);
577     }
578 
579     /**
580      * Encode a string literal.
581      */
582     private void encodeLengthPrefixedHuffmanEncodedLiteral(ByteBuf out, byte mask, int prefix, CharSequence value) {
583         int huffmanLength = huffmanEncoder.getEncodedLength(value);
584         encodePrefixedInteger(out, mask, prefix, huffmanLength);
585         huffmanEncoder.encode(out, value);
586     }
587 
588     private boolean mayNotBlockStream() {
589         return blockedStreams >= maxBlockedStreams - 1;
590     }
591 
592     /**
593      * Whether the dynamic table must not be used to encode the next header field: either because it was disabled
594      * for this connection, or because we are already tracking {@link #MAX_OUTSTANDING_SECTIONS} field sections
595      * that are awaiting a Section Acknowledgment or Stream Cancellation instruction from the peer.
596      *
597      * @param qpackAttributes  the attributes used.
598      * @return {@code true} if the dynamic table can't be used, {@code false} otherwise.
599      */
600     private boolean isDynamicTableUnavailable(QpackAttributes qpackAttributes) {
601         return qpackAttributes.dynamicTableDisabled() || outstandingSections >= MAX_OUTSTANDING_SECTIONS;
602     }
603 
604     // Visible for tests
605     int outstandingSectionCount() {
606         return outstandingSections;
607     }
608 
609     private static final class Indices {
610         private int idx;
611         // Let's just assume 4 indices for now that we will store here as max.
612         private int[] array = new int[4];
613 
614         void add(int index) {
615             if (idx == array.length) {
616                 // Double it if needed.
617                 array = Arrays.copyOf(array, array.length << 1);
618             }
619             array[idx++] = index;
620         }
621 
622         void forEach(IndexConsumer consumer) throws QpackException {
623             for (int i = 0; i < idx; i++) {
624                 consumer.accept(array[i]);
625             }
626         }
627 
628         @FunctionalInterface
629         interface IndexConsumer {
630             void accept(int idx) throws QpackException;
631         }
632     }
633 }