QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1 | // Copyright (c) 2018 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
QUICHE team | 5be974e | 2020-12-29 18:35:24 -0500 | [diff] [blame] | 5 | #include "quic/core/qpack/qpack_decoder.h" |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 6 | |
bnc | 463f235 | 2019-10-10 04:49:34 -0700 | [diff] [blame] | 7 | #include <utility> |
| 8 | |
vasilvv | 7cac7b0 | 2020-10-08 12:32:10 -0700 | [diff] [blame] | 9 | #include "absl/strings/string_view.h" |
QUICHE team | 5be974e | 2020-12-29 18:35:24 -0500 | [diff] [blame] | 10 | #include "quic/core/qpack/qpack_index_conversions.h" |
| 11 | #include "quic/platform/api/quic_flag_utils.h" |
| 12 | #include "quic/platform/api/quic_flags.h" |
| 13 | #include "quic/platform/api/quic_logging.h" |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 14 | |
| 15 | namespace quic { |
| 16 | |
| 17 | QpackDecoder::QpackDecoder( |
bnc | 4c664c5 | 2019-08-04 18:14:12 -0700 | [diff] [blame] | 18 | uint64_t maximum_dynamic_table_capacity, |
bnc | 57b5f62 | 2019-08-21 14:07:44 -0700 | [diff] [blame] | 19 | uint64_t maximum_blocked_streams, |
renjietang | 8a2df8f | 2019-08-07 10:43:52 -0700 | [diff] [blame] | 20 | EncoderStreamErrorDelegate* encoder_stream_error_delegate) |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 21 | : encoder_stream_error_delegate_(encoder_stream_error_delegate), |
bnc | 57b5f62 | 2019-08-21 14:07:44 -0700 | [diff] [blame] | 22 | encoder_stream_receiver_(this), |
bnc | 45af751 | 2019-10-08 06:59:58 -0700 | [diff] [blame] | 23 | maximum_blocked_streams_(maximum_blocked_streams), |
| 24 | known_received_count_(0) { |
vasilvv | f803516 | 2021-02-01 14:49:14 -0800 | [diff] [blame] | 25 | QUICHE_DCHECK(encoder_stream_error_delegate_); |
bnc | 4c664c5 | 2019-08-04 18:14:12 -0700 | [diff] [blame] | 26 | |
| 27 | header_table_.SetMaximumDynamicTableCapacity(maximum_dynamic_table_capacity); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 28 | } |
| 29 | |
| 30 | QpackDecoder::~QpackDecoder() {} |
| 31 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 32 | void QpackDecoder::OnStreamReset(QuicStreamId stream_id) { |
bnc | 6f18a82 | 2019-11-27 17:50:38 -0800 | [diff] [blame] | 33 | if (header_table_.maximum_dynamic_table_capacity() > 0) { |
| 34 | decoder_stream_sender_.SendStreamCancellation(stream_id); |
| 35 | decoder_stream_sender_.Flush(); |
| 36 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 37 | } |
| 38 | |
bnc | 57b5f62 | 2019-08-21 14:07:44 -0700 | [diff] [blame] | 39 | bool QpackDecoder::OnStreamBlocked(QuicStreamId stream_id) { |
| 40 | auto result = blocked_streams_.insert(stream_id); |
vasilvv | f803516 | 2021-02-01 14:49:14 -0800 | [diff] [blame] | 41 | QUICHE_DCHECK(result.second); |
bnc | 57b5f62 | 2019-08-21 14:07:44 -0700 | [diff] [blame] | 42 | return blocked_streams_.size() <= maximum_blocked_streams_; |
| 43 | } |
| 44 | |
| 45 | void QpackDecoder::OnStreamUnblocked(QuicStreamId stream_id) { |
| 46 | size_t result = blocked_streams_.erase(stream_id); |
vasilvv | f803516 | 2021-02-01 14:49:14 -0800 | [diff] [blame] | 47 | QUICHE_DCHECK_EQ(1u, result); |
bnc | 57b5f62 | 2019-08-21 14:07:44 -0700 | [diff] [blame] | 48 | } |
| 49 | |
bnc | 45af751 | 2019-10-08 06:59:58 -0700 | [diff] [blame] | 50 | void QpackDecoder::OnDecodingCompleted(QuicStreamId stream_id, |
| 51 | uint64_t required_insert_count) { |
| 52 | if (required_insert_count > 0) { |
| 53 | decoder_stream_sender_.SendHeaderAcknowledgement(stream_id); |
| 54 | |
| 55 | if (known_received_count_ < required_insert_count) { |
| 56 | known_received_count_ = required_insert_count; |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | // Send an Insert Count Increment instruction if not all dynamic table entries |
| 61 | // have been acknowledged yet. This is necessary for efficient compression in |
| 62 | // case the encoder chooses not to reference unacknowledged dynamic table |
| 63 | // entries, otherwise inserted entries would never be acknowledged. |
| 64 | if (known_received_count_ < header_table_.inserted_entry_count()) { |
| 65 | decoder_stream_sender_.SendInsertCountIncrement( |
| 66 | header_table_.inserted_entry_count() - known_received_count_); |
| 67 | known_received_count_ = header_table_.inserted_entry_count(); |
| 68 | } |
bnc | 6b1fc8a | 2019-10-11 17:17:14 -0700 | [diff] [blame] | 69 | |
| 70 | decoder_stream_sender_.Flush(); |
bnc | 45af751 | 2019-10-08 06:59:58 -0700 | [diff] [blame] | 71 | } |
| 72 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 73 | void QpackDecoder::OnInsertWithNameReference(bool is_static, |
| 74 | uint64_t name_index, |
vasilvv | 7cac7b0 | 2020-10-08 12:32:10 -0700 | [diff] [blame] | 75 | absl::string_view value) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 76 | if (is_static) { |
| 77 | auto entry = header_table_.LookupEntry(/* is_static = */ true, name_index); |
| 78 | if (!entry) { |
bnc | 4e44010 | 2020-10-20 17:32:29 -0700 | [diff] [blame] | 79 | OnErrorDetected(QUIC_QPACK_ENCODER_STREAM_INVALID_STATIC_ENTRY, |
bnc | 34a2680 | 2020-10-17 05:09:54 -0700 | [diff] [blame] | 80 | "Invalid static table entry."); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 81 | return; |
| 82 | } |
| 83 | |
bnc | c45c976 | 2021-03-15 12:17:35 -0700 | [diff] [blame] | 84 | if (!header_table_.EntryFitsDynamicTableCapacity(entry->name(), value)) { |
bnc | 4e44010 | 2020-10-20 17:32:29 -0700 | [diff] [blame] | 85 | OnErrorDetected(QUIC_QPACK_ENCODER_STREAM_ERROR_INSERTING_STATIC, |
bnc | 34a2680 | 2020-10-17 05:09:54 -0700 | [diff] [blame] | 86 | "Error inserting entry with name reference."); |
bnc | c45c976 | 2021-03-15 12:17:35 -0700 | [diff] [blame] | 87 | return; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 88 | } |
bnc | c45c976 | 2021-03-15 12:17:35 -0700 | [diff] [blame] | 89 | header_table_.InsertEntry(entry->name(), value); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 90 | return; |
| 91 | } |
| 92 | |
| 93 | uint64_t absolute_index; |
bnc | cd5ec3c | 2019-08-14 13:50:46 -0700 | [diff] [blame] | 94 | if (!QpackEncoderStreamRelativeIndexToAbsoluteIndex( |
| 95 | name_index, header_table_.inserted_entry_count(), &absolute_index)) { |
bnc | 4e44010 | 2020-10-20 17:32:29 -0700 | [diff] [blame] | 96 | OnErrorDetected(QUIC_QPACK_ENCODER_STREAM_INSERTION_INVALID_RELATIVE_INDEX, |
| 97 | "Invalid relative index."); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 98 | return; |
| 99 | } |
| 100 | |
| 101 | const QpackEntry* entry = |
| 102 | header_table_.LookupEntry(/* is_static = */ false, absolute_index); |
| 103 | if (!entry) { |
bnc | 4e44010 | 2020-10-20 17:32:29 -0700 | [diff] [blame] | 104 | OnErrorDetected(QUIC_QPACK_ENCODER_STREAM_INSERTION_DYNAMIC_ENTRY_NOT_FOUND, |
bnc | 34a2680 | 2020-10-17 05:09:54 -0700 | [diff] [blame] | 105 | "Dynamic table entry not found."); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 106 | return; |
| 107 | } |
bnc | c45c976 | 2021-03-15 12:17:35 -0700 | [diff] [blame] | 108 | if (!header_table_.EntryFitsDynamicTableCapacity(entry->name(), value)) { |
bnc | 4e44010 | 2020-10-20 17:32:29 -0700 | [diff] [blame] | 109 | OnErrorDetected(QUIC_QPACK_ENCODER_STREAM_ERROR_INSERTING_DYNAMIC, |
bnc | 34a2680 | 2020-10-17 05:09:54 -0700 | [diff] [blame] | 110 | "Error inserting entry with name reference."); |
bnc | c45c976 | 2021-03-15 12:17:35 -0700 | [diff] [blame] | 111 | return; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 112 | } |
bnc | c45c976 | 2021-03-15 12:17:35 -0700 | [diff] [blame] | 113 | header_table_.InsertEntry(entry->name(), value); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 114 | } |
| 115 | |
vasilvv | 7cac7b0 | 2020-10-08 12:32:10 -0700 | [diff] [blame] | 116 | void QpackDecoder::OnInsertWithoutNameReference(absl::string_view name, |
| 117 | absl::string_view value) { |
bnc | c45c976 | 2021-03-15 12:17:35 -0700 | [diff] [blame] | 118 | if (!header_table_.EntryFitsDynamicTableCapacity(name, value)) { |
bnc | 4e44010 | 2020-10-20 17:32:29 -0700 | [diff] [blame] | 119 | OnErrorDetected(QUIC_QPACK_ENCODER_STREAM_ERROR_INSERTING_LITERAL, |
bnc | 34a2680 | 2020-10-17 05:09:54 -0700 | [diff] [blame] | 120 | "Error inserting literal entry."); |
bnc | c45c976 | 2021-03-15 12:17:35 -0700 | [diff] [blame] | 121 | return; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 122 | } |
bnc | c45c976 | 2021-03-15 12:17:35 -0700 | [diff] [blame] | 123 | header_table_.InsertEntry(name, value); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | void QpackDecoder::OnDuplicate(uint64_t index) { |
| 127 | uint64_t absolute_index; |
bnc | cd5ec3c | 2019-08-14 13:50:46 -0700 | [diff] [blame] | 128 | if (!QpackEncoderStreamRelativeIndexToAbsoluteIndex( |
| 129 | index, header_table_.inserted_entry_count(), &absolute_index)) { |
bnc | 4e44010 | 2020-10-20 17:32:29 -0700 | [diff] [blame] | 130 | OnErrorDetected(QUIC_QPACK_ENCODER_STREAM_DUPLICATE_INVALID_RELATIVE_INDEX, |
| 131 | "Invalid relative index."); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 132 | return; |
| 133 | } |
| 134 | |
| 135 | const QpackEntry* entry = |
| 136 | header_table_.LookupEntry(/* is_static = */ false, absolute_index); |
| 137 | if (!entry) { |
bnc | 4e44010 | 2020-10-20 17:32:29 -0700 | [diff] [blame] | 138 | OnErrorDetected(QUIC_QPACK_ENCODER_STREAM_DUPLICATE_DYNAMIC_ENTRY_NOT_FOUND, |
bnc | 34a2680 | 2020-10-17 05:09:54 -0700 | [diff] [blame] | 139 | "Dynamic table entry not found."); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 140 | return; |
| 141 | } |
bnc | c45c976 | 2021-03-15 12:17:35 -0700 | [diff] [blame] | 142 | if (!header_table_.EntryFitsDynamicTableCapacity(entry->name(), |
| 143 | entry->value())) { |
| 144 | // This is impossible since entry was retrieved from the dynamic table. |
bnc | 4e44010 | 2020-10-20 17:32:29 -0700 | [diff] [blame] | 145 | OnErrorDetected(QUIC_INTERNAL_ERROR, "Error inserting duplicate entry."); |
bnc | c45c976 | 2021-03-15 12:17:35 -0700 | [diff] [blame] | 146 | return; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 147 | } |
bnc | c45c976 | 2021-03-15 12:17:35 -0700 | [diff] [blame] | 148 | header_table_.InsertEntry(entry->name(), entry->value()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | void QpackDecoder::OnSetDynamicTableCapacity(uint64_t capacity) { |
| 152 | if (!header_table_.SetDynamicTableCapacity(capacity)) { |
bnc | 4e44010 | 2020-10-20 17:32:29 -0700 | [diff] [blame] | 153 | OnErrorDetected(QUIC_QPACK_ENCODER_STREAM_SET_DYNAMIC_TABLE_CAPACITY, |
bnc | 34a2680 | 2020-10-17 05:09:54 -0700 | [diff] [blame] | 154 | "Error updating dynamic table capacity."); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 155 | } |
| 156 | } |
| 157 | |
bnc | 4e44010 | 2020-10-20 17:32:29 -0700 | [diff] [blame] | 158 | void QpackDecoder::OnErrorDetected(QuicErrorCode error_code, |
bnc | 34a2680 | 2020-10-17 05:09:54 -0700 | [diff] [blame] | 159 | absl::string_view error_message) { |
bnc | 1fe972f | 2021-01-28 09:24:49 -0800 | [diff] [blame] | 160 | encoder_stream_error_delegate_->OnEncoderStreamError(error_code, |
| 161 | error_message); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 162 | } |
| 163 | |
bnc | a904b05 | 2019-06-11 11:34:38 -0700 | [diff] [blame] | 164 | std::unique_ptr<QpackProgressiveDecoder> QpackDecoder::CreateProgressiveDecoder( |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 165 | QuicStreamId stream_id, |
| 166 | QpackProgressiveDecoder::HeadersHandlerInterface* handler) { |
bnc | 45af751 | 2019-10-08 06:59:58 -0700 | [diff] [blame] | 167 | return std::make_unique<QpackProgressiveDecoder>(stream_id, this, this, |
| 168 | &header_table_, handler); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | } // namespace quic |