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