OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 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/http2/hpack/decoder/hpack_decoder_tables.h" |
| 6 |
| 7 #include "base/logging.h" |
| 8 |
| 9 namespace net { |
| 10 namespace { |
| 11 |
| 12 std::vector<HpackStringPair>* MakeStaticTable() { |
| 13 auto ptr = new std::vector<HpackStringPair>(); |
| 14 ptr->reserve(kFirstDynamicTableIndex); |
| 15 ptr->emplace_back("", ""); |
| 16 |
| 17 #define STATIC_TABLE_ENTRY(name, value, index) \ |
| 18 DCHECK_EQ(ptr->size(), index); \ |
| 19 ptr->emplace_back(name, value) |
| 20 |
| 21 #include "net/http2/hpack/hpack_static_table_entries.inc" |
| 22 |
| 23 #undef STATIC_TABLE_ENTRY |
| 24 |
| 25 return ptr; |
| 26 } |
| 27 |
| 28 const std::vector<HpackStringPair>* GetStaticTable() { |
| 29 static const std::vector<HpackStringPair>* const g_static_table = |
| 30 MakeStaticTable(); |
| 31 return g_static_table; |
| 32 } |
| 33 |
| 34 } // namespace |
| 35 |
| 36 HpackDecoderStaticTable::HpackDecoderStaticTable( |
| 37 const std::vector<HpackStringPair>* table) |
| 38 : table_(table) {} |
| 39 |
| 40 HpackDecoderStaticTable::HpackDecoderStaticTable() : table_(GetStaticTable()) {} |
| 41 |
| 42 const HpackStringPair* HpackDecoderStaticTable::Lookup(size_t index) const { |
| 43 if (0 < index && index < kFirstDynamicTableIndex) { |
| 44 return &((*table_)[index]); |
| 45 } |
| 46 return nullptr; |
| 47 } |
| 48 |
| 49 HpackDecoderDynamicTable::HpackDecoderDynamicTable() {} |
| 50 HpackDecoderDynamicTable::~HpackDecoderDynamicTable() {} |
| 51 |
| 52 void HpackDecoderDynamicTable::DynamicTableSizeUpdate(size_t size_limit) { |
| 53 EnsureSizeNoMoreThan(size_limit); |
| 54 DCHECK_LE(current_size_, size_limit); |
| 55 size_limit_ = size_limit; |
| 56 } |
| 57 |
| 58 // TODO(jamessynge): Check somewhere before here that names received from the |
| 59 // peer are valid (e.g. are lower-case, no whitespace, etc.). |
| 60 bool HpackDecoderDynamicTable::Insert(const HpackString& name, |
| 61 const HpackString& value) { |
| 62 HpackStringPair p(name, value); |
| 63 size_t entry_size = p.size(); |
| 64 DVLOG(2) << "InsertEntry of size=" << entry_size << "\n name: " << name |
| 65 << "\n value: " << value; |
| 66 if (entry_size > size_limit_) { |
| 67 DVLOG(2) << "InsertEntry: entry larger than table, removing " |
| 68 << table_.size() << " entries, of total size " << current_size_ |
| 69 << " bytes."; |
| 70 table_.clear(); |
| 71 current_size_ = 0; |
| 72 return false; // Not inserted because too large. |
| 73 } |
| 74 size_t insert_limit = size_limit_ - entry_size; |
| 75 EnsureSizeNoMoreThan(insert_limit); |
| 76 table_.push_front(p); |
| 77 current_size_ += entry_size; |
| 78 DVLOG(2) << "InsertEntry: current_size_=" << current_size_; |
| 79 DCHECK_GE(current_size_, entry_size); |
| 80 DCHECK_LE(current_size_, size_limit_); |
| 81 return true; |
| 82 } |
| 83 |
| 84 const HpackStringPair* HpackDecoderDynamicTable::Lookup(size_t index) const { |
| 85 if (index < table_.size()) { |
| 86 return &(table_[index]); |
| 87 } |
| 88 return nullptr; |
| 89 } |
| 90 |
| 91 void HpackDecoderDynamicTable::EnsureSizeNoMoreThan(size_t limit) { |
| 92 DVLOG(2) << "EnsureSizeNoMoreThan limit=" << limit |
| 93 << ", current_size_=" << current_size_; |
| 94 // Not the most efficient choice, but any easy way to start. |
| 95 while (current_size_ > limit) { |
| 96 RemoveLastEntry(); |
| 97 } |
| 98 DCHECK_LE(current_size_, limit); |
| 99 } |
| 100 |
| 101 void HpackDecoderDynamicTable::RemoveLastEntry() { |
| 102 DCHECK(!table_.empty()); |
| 103 if (!table_.empty()) { |
| 104 DVLOG(2) << "RemoveLastEntry current_size_=" << current_size_ |
| 105 << ", last entry size=" << table_.back().size(); |
| 106 DCHECK_GE(current_size_, table_.back().size()); |
| 107 current_size_ -= table_.back().size(); |
| 108 table_.pop_back(); |
| 109 // Empty IFF current_size_ == 0. |
| 110 DCHECK_EQ(table_.empty(), current_size_ == 0); |
| 111 } |
| 112 } |
| 113 |
| 114 HpackDecoderTables::HpackDecoderTables() {} |
| 115 HpackDecoderTables::~HpackDecoderTables() {} |
| 116 |
| 117 } // namespace net |
OLD | NEW |