QUICHE team | 82dee2f | 2019-01-18 12:35:12 -0500 | [diff] [blame] | 1 | // Copyright 2017 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/spdy/core/hpack/hpack_decoder_adapter.h" |
| 6 | |
QUICHE team | 82dee2f | 2019-01-18 12:35:12 -0500 | [diff] [blame] | 7 | #include "net/third_party/quiche/src/http2/decoder/decode_buffer.h" |
| 8 | #include "net/third_party/quiche/src/http2/decoder/decode_status.h" |
| 9 | #include "net/third_party/quiche/src/spdy/platform/api/spdy_estimate_memory_usage.h" |
renjietang | 6932faf | 2020-01-22 13:03:14 -0800 | [diff] [blame] | 10 | #include "net/third_party/quiche/src/spdy/platform/api/spdy_flags.h" |
QUICHE team | ded0351 | 2019-03-07 14:45:11 -0800 | [diff] [blame] | 11 | #include "net/third_party/quiche/src/spdy/platform/api/spdy_logging.h" |
QUICHE team | 82dee2f | 2019-01-18 12:35:12 -0500 | [diff] [blame] | 12 | |
| 13 | using ::http2::DecodeBuffer; |
QUICHE team | 82dee2f | 2019-01-18 12:35:12 -0500 | [diff] [blame] | 14 | using ::http2::HpackString; |
| 15 | |
| 16 | namespace spdy { |
| 17 | namespace { |
| 18 | const size_t kMaxDecodeBufferSizeBytes = 32 * 1024; // 32 KB |
| 19 | } // namespace |
| 20 | |
| 21 | HpackDecoderAdapter::HpackDecoderAdapter() |
| 22 | : hpack_decoder_(&listener_adapter_, kMaxDecodeBufferSizeBytes), |
| 23 | max_decode_buffer_size_bytes_(kMaxDecodeBufferSizeBytes), |
QUICHE team | 6275484 | 2019-07-23 14:06:29 -0700 | [diff] [blame] | 24 | max_header_block_bytes_(0), |
bnc | 9b56111 | 2020-02-07 07:57:05 -0800 | [diff] [blame] | 25 | header_block_started_(false), |
| 26 | error_(http2::HpackDecodingError::kOk) {} |
QUICHE team | 82dee2f | 2019-01-18 12:35:12 -0500 | [diff] [blame] | 27 | |
| 28 | HpackDecoderAdapter::~HpackDecoderAdapter() = default; |
| 29 | |
| 30 | void HpackDecoderAdapter::ApplyHeaderTableSizeSetting(size_t size_setting) { |
QUICHE team | ded0351 | 2019-03-07 14:45:11 -0800 | [diff] [blame] | 31 | SPDY_DVLOG(2) << "HpackDecoderAdapter::ApplyHeaderTableSizeSetting"; |
QUICHE team | 82dee2f | 2019-01-18 12:35:12 -0500 | [diff] [blame] | 32 | hpack_decoder_.ApplyHeaderTableSizeSetting(size_setting); |
| 33 | } |
| 34 | |
| 35 | void HpackDecoderAdapter::HandleControlFrameHeadersStart( |
| 36 | SpdyHeadersHandlerInterface* handler) { |
QUICHE team | ded0351 | 2019-03-07 14:45:11 -0800 | [diff] [blame] | 37 | SPDY_DVLOG(2) << "HpackDecoderAdapter::HandleControlFrameHeadersStart"; |
QUICHE team | 82dee2f | 2019-01-18 12:35:12 -0500 | [diff] [blame] | 38 | DCHECK(!header_block_started_); |
| 39 | listener_adapter_.set_handler(handler); |
| 40 | } |
| 41 | |
| 42 | bool HpackDecoderAdapter::HandleControlFrameHeadersData( |
| 43 | const char* headers_data, |
| 44 | size_t headers_data_length) { |
QUICHE team | ded0351 | 2019-03-07 14:45:11 -0800 | [diff] [blame] | 45 | SPDY_DVLOG(2) << "HpackDecoderAdapter::HandleControlFrameHeadersData: len=" |
| 46 | << headers_data_length; |
QUICHE team | 82dee2f | 2019-01-18 12:35:12 -0500 | [diff] [blame] | 47 | if (!header_block_started_) { |
| 48 | // Initialize the decoding process here rather than in |
| 49 | // HandleControlFrameHeadersStart because that method is not always called. |
| 50 | header_block_started_ = true; |
| 51 | if (!hpack_decoder_.StartDecodingBlock()) { |
| 52 | header_block_started_ = false; |
renjietang | 439c769 | 2020-01-22 18:36:28 -0800 | [diff] [blame] | 53 | SPDY_CODE_COUNT_N(decompress_failure_2, 1, 5); |
bnc | 9b56111 | 2020-02-07 07:57:05 -0800 | [diff] [blame] | 54 | error_ = hpack_decoder_.error(); |
QUICHE team | 82dee2f | 2019-01-18 12:35:12 -0500 | [diff] [blame] | 55 | return false; |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | // Sometimes we get a call with headers_data==nullptr and |
| 60 | // headers_data_length==0, in which case we need to avoid creating |
| 61 | // a DecodeBuffer, which would otherwise complain. |
| 62 | if (headers_data_length > 0) { |
| 63 | DCHECK_NE(headers_data, nullptr); |
| 64 | if (headers_data_length > max_decode_buffer_size_bytes_) { |
QUICHE team | ded0351 | 2019-03-07 14:45:11 -0800 | [diff] [blame] | 65 | SPDY_DVLOG(1) << "max_decode_buffer_size_bytes_ < headers_data_length: " |
| 66 | << max_decode_buffer_size_bytes_ << " < " |
| 67 | << headers_data_length; |
renjietang | 439c769 | 2020-01-22 18:36:28 -0800 | [diff] [blame] | 68 | SPDY_CODE_COUNT_N(decompress_failure_2, 2, 5); |
bnc | 9b56111 | 2020-02-07 07:57:05 -0800 | [diff] [blame] | 69 | error_ = http2::HpackDecodingError::kFragmentTooLong; |
QUICHE team | 82dee2f | 2019-01-18 12:35:12 -0500 | [diff] [blame] | 70 | return false; |
| 71 | } |
| 72 | listener_adapter_.AddToTotalHpackBytes(headers_data_length); |
QUICHE team | 6275484 | 2019-07-23 14:06:29 -0700 | [diff] [blame] | 73 | if (max_header_block_bytes_ != 0 && |
| 74 | listener_adapter_.total_hpack_bytes() > max_header_block_bytes_) { |
renjietang | 439c769 | 2020-01-22 18:36:28 -0800 | [diff] [blame] | 75 | SPDY_CODE_COUNT_N(decompress_failure, 3, 5); |
bnc | 9b56111 | 2020-02-07 07:57:05 -0800 | [diff] [blame] | 76 | error_ = http2::HpackDecodingError::kCompressedHeaderSizeExceedsLimit; |
QUICHE team | 6275484 | 2019-07-23 14:06:29 -0700 | [diff] [blame] | 77 | return false; |
| 78 | } |
QUICHE team | 82dee2f | 2019-01-18 12:35:12 -0500 | [diff] [blame] | 79 | http2::DecodeBuffer db(headers_data, headers_data_length); |
| 80 | bool ok = hpack_decoder_.DecodeFragment(&db); |
| 81 | DCHECK(!ok || db.Empty()) << "Remaining=" << db.Remaining(); |
renjietang | 439c769 | 2020-01-22 18:36:28 -0800 | [diff] [blame] | 82 | if (!ok) { |
| 83 | SPDY_CODE_COUNT_N(decompress_failure_2, 4, 5); |
bnc | 9b56111 | 2020-02-07 07:57:05 -0800 | [diff] [blame] | 84 | error_ = hpack_decoder_.error(); |
renjietang | 439c769 | 2020-01-22 18:36:28 -0800 | [diff] [blame] | 85 | } |
QUICHE team | 82dee2f | 2019-01-18 12:35:12 -0500 | [diff] [blame] | 86 | return ok; |
| 87 | } |
| 88 | return true; |
| 89 | } |
| 90 | |
| 91 | bool HpackDecoderAdapter::HandleControlFrameHeadersComplete( |
| 92 | size_t* compressed_len) { |
QUICHE team | ded0351 | 2019-03-07 14:45:11 -0800 | [diff] [blame] | 93 | SPDY_DVLOG(2) << "HpackDecoderAdapter::HandleControlFrameHeadersComplete"; |
QUICHE team | 82dee2f | 2019-01-18 12:35:12 -0500 | [diff] [blame] | 94 | if (compressed_len != nullptr) { |
| 95 | *compressed_len = listener_adapter_.total_hpack_bytes(); |
| 96 | } |
| 97 | if (!hpack_decoder_.EndDecodingBlock()) { |
QUICHE team | ded0351 | 2019-03-07 14:45:11 -0800 | [diff] [blame] | 98 | SPDY_DVLOG(3) << "EndDecodingBlock returned false"; |
renjietang | 439c769 | 2020-01-22 18:36:28 -0800 | [diff] [blame] | 99 | SPDY_CODE_COUNT_N(decompress_failure_2, 5, 5); |
bnc | 9b56111 | 2020-02-07 07:57:05 -0800 | [diff] [blame] | 100 | error_ = hpack_decoder_.error(); |
QUICHE team | 82dee2f | 2019-01-18 12:35:12 -0500 | [diff] [blame] | 101 | return false; |
| 102 | } |
| 103 | header_block_started_ = false; |
| 104 | return true; |
| 105 | } |
| 106 | |
| 107 | const SpdyHeaderBlock& HpackDecoderAdapter::decoded_block() const { |
| 108 | return listener_adapter_.decoded_block(); |
| 109 | } |
| 110 | |
| 111 | void HpackDecoderAdapter::SetHeaderTableDebugVisitor( |
| 112 | std::unique_ptr<HpackHeaderTable::DebugVisitorInterface> visitor) { |
QUICHE team | ded0351 | 2019-03-07 14:45:11 -0800 | [diff] [blame] | 113 | SPDY_DVLOG(2) << "HpackDecoderAdapter::SetHeaderTableDebugVisitor"; |
QUICHE team | 82dee2f | 2019-01-18 12:35:12 -0500 | [diff] [blame] | 114 | if (visitor != nullptr) { |
| 115 | listener_adapter_.SetHeaderTableDebugVisitor(std::move(visitor)); |
| 116 | hpack_decoder_.set_tables_debug_listener(&listener_adapter_); |
| 117 | } else { |
| 118 | hpack_decoder_.set_tables_debug_listener(nullptr); |
| 119 | listener_adapter_.SetHeaderTableDebugVisitor(nullptr); |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | void HpackDecoderAdapter::set_max_decode_buffer_size_bytes( |
| 124 | size_t max_decode_buffer_size_bytes) { |
QUICHE team | ded0351 | 2019-03-07 14:45:11 -0800 | [diff] [blame] | 125 | SPDY_DVLOG(2) << "HpackDecoderAdapter::set_max_decode_buffer_size_bytes"; |
QUICHE team | 82dee2f | 2019-01-18 12:35:12 -0500 | [diff] [blame] | 126 | max_decode_buffer_size_bytes_ = max_decode_buffer_size_bytes; |
| 127 | hpack_decoder_.set_max_string_size_bytes(max_decode_buffer_size_bytes); |
| 128 | } |
| 129 | |
QUICHE team | 6275484 | 2019-07-23 14:06:29 -0700 | [diff] [blame] | 130 | void HpackDecoderAdapter::set_max_header_block_bytes( |
| 131 | size_t max_header_block_bytes) { |
| 132 | max_header_block_bytes_ = max_header_block_bytes; |
| 133 | } |
| 134 | |
QUICHE team | 82dee2f | 2019-01-18 12:35:12 -0500 | [diff] [blame] | 135 | size_t HpackDecoderAdapter::EstimateMemoryUsage() const { |
| 136 | return SpdyEstimateMemoryUsage(hpack_decoder_); |
| 137 | } |
| 138 | |
| 139 | HpackDecoderAdapter::ListenerAdapter::ListenerAdapter() : handler_(nullptr) {} |
| 140 | HpackDecoderAdapter::ListenerAdapter::~ListenerAdapter() = default; |
| 141 | |
| 142 | void HpackDecoderAdapter::ListenerAdapter::set_handler( |
| 143 | SpdyHeadersHandlerInterface* handler) { |
| 144 | handler_ = handler; |
| 145 | } |
| 146 | |
| 147 | void HpackDecoderAdapter::ListenerAdapter::SetHeaderTableDebugVisitor( |
| 148 | std::unique_ptr<HpackHeaderTable::DebugVisitorInterface> visitor) { |
| 149 | visitor_ = std::move(visitor); |
| 150 | } |
| 151 | |
| 152 | void HpackDecoderAdapter::ListenerAdapter::OnHeaderListStart() { |
QUICHE team | ded0351 | 2019-03-07 14:45:11 -0800 | [diff] [blame] | 153 | SPDY_DVLOG(2) << "HpackDecoderAdapter::ListenerAdapter::OnHeaderListStart"; |
QUICHE team | 82dee2f | 2019-01-18 12:35:12 -0500 | [diff] [blame] | 154 | total_hpack_bytes_ = 0; |
| 155 | total_uncompressed_bytes_ = 0; |
| 156 | decoded_block_.clear(); |
| 157 | if (handler_ != nullptr) { |
| 158 | handler_->OnHeaderBlockStart(); |
| 159 | } |
| 160 | } |
| 161 | |
zhongyi | eff02bb | 2019-12-13 14:38:48 -0800 | [diff] [blame] | 162 | void HpackDecoderAdapter::ListenerAdapter::OnHeader(const HpackString& name, |
QUICHE team | 82dee2f | 2019-01-18 12:35:12 -0500 | [diff] [blame] | 163 | const HpackString& value) { |
QUICHE team | ded0351 | 2019-03-07 14:45:11 -0800 | [diff] [blame] | 164 | SPDY_DVLOG(2) << "HpackDecoderAdapter::ListenerAdapter::OnHeader:\n name: " |
| 165 | << name << "\n value: " << value; |
QUICHE team | 82dee2f | 2019-01-18 12:35:12 -0500 | [diff] [blame] | 166 | total_uncompressed_bytes_ += name.size() + value.size(); |
| 167 | if (handler_ == nullptr) { |
QUICHE team | ded0351 | 2019-03-07 14:45:11 -0800 | [diff] [blame] | 168 | SPDY_DVLOG(3) << "Adding to decoded_block"; |
QUICHE team | 82dee2f | 2019-01-18 12:35:12 -0500 | [diff] [blame] | 169 | decoded_block_.AppendValueOrAddHeader(name.ToStringPiece(), |
| 170 | value.ToStringPiece()); |
| 171 | } else { |
QUICHE team | ded0351 | 2019-03-07 14:45:11 -0800 | [diff] [blame] | 172 | SPDY_DVLOG(3) << "Passing to handler"; |
QUICHE team | 82dee2f | 2019-01-18 12:35:12 -0500 | [diff] [blame] | 173 | handler_->OnHeader(name.ToStringPiece(), value.ToStringPiece()); |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | void HpackDecoderAdapter::ListenerAdapter::OnHeaderListEnd() { |
QUICHE team | ded0351 | 2019-03-07 14:45:11 -0800 | [diff] [blame] | 178 | SPDY_DVLOG(2) << "HpackDecoderAdapter::ListenerAdapter::OnHeaderListEnd"; |
QUICHE team | 82dee2f | 2019-01-18 12:35:12 -0500 | [diff] [blame] | 179 | // We don't clear the SpdyHeaderBlock here to allow access to it until the |
| 180 | // next HPACK block is decoded. |
| 181 | if (handler_ != nullptr) { |
| 182 | handler_->OnHeaderBlockEnd(total_uncompressed_bytes_, total_hpack_bytes_); |
| 183 | handler_ = nullptr; |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | void HpackDecoderAdapter::ListenerAdapter::OnHeaderErrorDetected( |
bnc | 7f82d04 | 2020-01-03 12:18:53 -0800 | [diff] [blame] | 188 | quiche::QuicheStringPiece error_message) { |
QUICHE team | ded0351 | 2019-03-07 14:45:11 -0800 | [diff] [blame] | 189 | SPDY_VLOG(1) << error_message; |
QUICHE team | 82dee2f | 2019-01-18 12:35:12 -0500 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | int64_t HpackDecoderAdapter::ListenerAdapter::OnEntryInserted( |
zhongyi | eff02bb | 2019-12-13 14:38:48 -0800 | [diff] [blame] | 193 | const http2::HpackStringPair& entry, |
QUICHE team | 82dee2f | 2019-01-18 12:35:12 -0500 | [diff] [blame] | 194 | size_t insert_count) { |
QUICHE team | ded0351 | 2019-03-07 14:45:11 -0800 | [diff] [blame] | 195 | SPDY_DVLOG(2) << "HpackDecoderAdapter::ListenerAdapter::OnEntryInserted: " |
zhongyi | eff02bb | 2019-12-13 14:38:48 -0800 | [diff] [blame] | 196 | << entry << ", insert_count=" << insert_count; |
QUICHE team | 82dee2f | 2019-01-18 12:35:12 -0500 | [diff] [blame] | 197 | if (visitor_ == nullptr) { |
| 198 | return 0; |
| 199 | } |
zhongyi | eff02bb | 2019-12-13 14:38:48 -0800 | [diff] [blame] | 200 | HpackEntry hpack_entry(entry.name.ToStringPiece(), |
| 201 | entry.value.ToStringPiece(), |
| 202 | /*is_static*/ false, insert_count); |
| 203 | int64_t time_added = visitor_->OnNewEntry(hpack_entry); |
QUICHE team | ded0351 | 2019-03-07 14:45:11 -0800 | [diff] [blame] | 204 | SPDY_DVLOG(2) |
QUICHE team | 82dee2f | 2019-01-18 12:35:12 -0500 | [diff] [blame] | 205 | << "HpackDecoderAdapter::ListenerAdapter::OnEntryInserted: time_added=" |
| 206 | << time_added; |
| 207 | return time_added; |
| 208 | } |
| 209 | |
| 210 | void HpackDecoderAdapter::ListenerAdapter::OnUseEntry( |
zhongyi | eff02bb | 2019-12-13 14:38:48 -0800 | [diff] [blame] | 211 | const http2::HpackStringPair& entry, |
QUICHE team | 82dee2f | 2019-01-18 12:35:12 -0500 | [diff] [blame] | 212 | size_t insert_count, |
| 213 | int64_t time_added) { |
zhongyi | eff02bb | 2019-12-13 14:38:48 -0800 | [diff] [blame] | 214 | SPDY_DVLOG(2) << "HpackDecoderAdapter::ListenerAdapter::OnUseEntry: " << entry |
QUICHE team | ded0351 | 2019-03-07 14:45:11 -0800 | [diff] [blame] | 215 | << ", insert_count=" << insert_count |
| 216 | << ", time_added=" << time_added; |
QUICHE team | 82dee2f | 2019-01-18 12:35:12 -0500 | [diff] [blame] | 217 | if (visitor_ != nullptr) { |
zhongyi | eff02bb | 2019-12-13 14:38:48 -0800 | [diff] [blame] | 218 | HpackEntry hpack_entry(entry.name.ToStringPiece(), |
| 219 | entry.value.ToStringPiece(), /*is_static*/ false, |
| 220 | insert_count); |
| 221 | hpack_entry.set_time_added(time_added); |
| 222 | visitor_->OnUseEntry(hpack_entry); |
QUICHE team | 82dee2f | 2019-01-18 12:35:12 -0500 | [diff] [blame] | 223 | } |
| 224 | } |
| 225 | |
| 226 | } // namespace spdy |