| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef NET_SPDY_HPACK_HEADER_TABLE_H_ | 5 #ifndef NET_SPDY_HPACK_HEADER_TABLE_H_ |
| 6 #define NET_SPDY_HPACK_HEADER_TABLE_H_ | 6 #define NET_SPDY_HPACK_HEADER_TABLE_H_ |
| 7 | 7 |
| 8 #include <cstddef> | 8 #include <cstddef> |
| 9 #include <deque> | 9 #include <deque> |
| 10 #include <set> | 10 #include <set> |
| 11 | 11 |
| 12 #include "base/basictypes.h" | 12 #include "base/basictypes.h" |
| 13 #include "base/macros.h" | 13 #include "base/macros.h" |
| 14 #include "net/base/net_export.h" | 14 #include "net/base/net_export.h" |
| 15 #include "net/spdy/hpack_entry.h" | 15 #include "net/spdy/hpack_entry.h" |
| 16 | 16 |
| 17 // All section references below are to | 17 // All section references below are to |
| 18 // http://tools.ietf.org/html/draft-ietf-httpbis-header-compression-08 | 18 // http://tools.ietf.org/html/draft-ietf-httpbis-header-compression-08 |
| 19 | 19 |
| 20 namespace net { | 20 namespace net { |
| 21 | 21 |
| 22 using base::StringPiece; |
| 23 |
| 22 namespace test { | 24 namespace test { |
| 23 class HpackHeaderTablePeer; | 25 class HpackHeaderTablePeer; |
| 24 } // namespace test | 26 } // namespace test |
| 25 | 27 |
| 26 // A data structure for the static table (3.3.1) and the header table (3.3.2). | 28 // A data structure for the static table (3.3.1) and the header table (3.3.2). |
| 27 class NET_EXPORT_PRIVATE HpackHeaderTable { | 29 class NET_EXPORT_PRIVATE HpackHeaderTable { |
| 28 public: | 30 public: |
| 29 friend class test::HpackHeaderTablePeer; | 31 friend class test::HpackHeaderTablePeer; |
| 30 | 32 |
| 31 // HpackHeaderTable takes advantage of the deque property that references | 33 // HpackHeaderTable takes advantage of the deque property that references |
| 32 // remain valid, so long as insertions & deletions are at the head & tail. | 34 // remain valid, so long as insertions & deletions are at the head & tail. |
| 33 // If this changes (eg we start to drop entries from the middle of the table), | 35 // If this changes (eg we start to drop entries from the middle of the table), |
| 34 // this needs to be a std::list, in which case |index_| can be trivially | 36 // this needs to be a std::list, in which case |*_index_| can be trivially |
| 35 // extended to map to list iterators. | 37 // extended to map to list iterators. |
| 36 typedef std::deque<HpackEntry> EntryTable; | 38 typedef std::deque<HpackEntry> EntryTable; |
| 37 | 39 |
| 38 // Implements a total ordering of HpackEntry on name(), value(), then index | 40 // Implements a total ordering of HpackEntry on name(), value(), then index |
| 39 // ascending. Note that index may change over the lifetime of an HpackEntry, | 41 // ascending. Note that index may change over the lifetime of an HpackEntry, |
| 40 // but the relative index order of two entries will not. This comparator is | 42 // but the relative index order of two entries will not. This comparator is |
| 41 // composed with the 'lookup' HpackEntry constructor to allow for efficient | 43 // composed with the 'lookup' HpackEntry constructor to allow for efficient |
| 42 // lower-bounding of matching entries. | 44 // lower-bounding of matching entries. |
| 43 struct NET_EXPORT_PRIVATE EntryComparator { | 45 struct NET_EXPORT_PRIVATE EntryComparator { |
| 44 explicit EntryComparator(HpackHeaderTable* table) : table_(table) {} | |
| 45 | |
| 46 bool operator() (const HpackEntry* lhs, const HpackEntry* rhs) const; | 46 bool operator() (const HpackEntry* lhs, const HpackEntry* rhs) const; |
| 47 | |
| 48 private: | |
| 49 HpackHeaderTable* table_; | |
| 50 }; | 47 }; |
| 51 typedef std::set<HpackEntry*, EntryComparator> OrderedEntrySet; | 48 typedef std::set<HpackEntry*, EntryComparator> OrderedEntrySet; |
| 52 | 49 |
| 53 HpackHeaderTable(); | 50 HpackHeaderTable(); |
| 54 | 51 |
| 55 ~HpackHeaderTable(); | 52 ~HpackHeaderTable(); |
| 56 | 53 |
| 57 // Last-aknowledged value of SETTINGS_HEADER_TABLE_SIZE. | 54 // Last-aknowledged value of SETTINGS_HEADER_TABLE_SIZE. |
| 58 size_t settings_size_bound() { return settings_size_bound_; } | 55 size_t settings_size_bound() { return settings_size_bound_; } |
| 59 | 56 |
| 60 // Current and maximum estimated byte size of the table, as described in | 57 // Current and maximum estimated byte size of the table, as described in |
| 61 // 5.1. Notably, this is /not/ the number of entries in the table. | 58 // 5.1. Notably, this is /not/ the number of entries in the table. |
| 62 size_t size() const { return size_; } | 59 size_t size() const { return size_; } |
| 63 size_t max_size() const { return max_size_; } | 60 size_t max_size() const { return max_size_; } |
| 64 | 61 |
| 65 // Returns the entry matching the index, or NULL. | 62 // Returns the entry matching the index, or NULL. |
| 66 HpackEntry* GetByIndex(size_t index); | 63 const HpackEntry* GetByIndex(size_t index); |
| 67 | 64 |
| 68 // Returns the lowest-value entry having |name|, or NULL. | 65 // Returns the lowest-value entry having |name|, or NULL. |
| 69 HpackEntry* GetByName(base::StringPiece name); | 66 const HpackEntry* GetByName(StringPiece name); |
| 70 | 67 |
| 71 // Returns the lowest-index matching entry, or NULL. | 68 // Returns the lowest-index matching entry, or NULL. |
| 72 HpackEntry* GetByNameAndValue(base::StringPiece name, | 69 const HpackEntry* GetByNameAndValue(StringPiece name, StringPiece value); |
| 73 base::StringPiece value); | |
| 74 | 70 |
| 75 // Returns the index of an entry within this header table. | 71 // Returns the index of an entry within this header table. |
| 76 size_t IndexOf(const HpackEntry* entry) const; | 72 size_t IndexOf(const HpackEntry* entry) const; |
| 77 | 73 |
| 78 // Sets the maximum size of the header table, evicting entries if | 74 // Sets the maximum size of the header table, evicting entries if |
| 79 // necessary as described in 5.2. | 75 // necessary as described in 5.2. |
| 80 void SetMaxSize(size_t max_size); | 76 void SetMaxSize(size_t max_size); |
| 81 | 77 |
| 82 // Sets the SETTINGS_HEADER_TABLE_SIZE bound of the table. Will call | 78 // Sets the SETTINGS_HEADER_TABLE_SIZE bound of the table. Will call |
| 83 // SetMaxSize() as needed to preserve max_size() <= settings_size_bound(). | 79 // SetMaxSize() as needed to preserve max_size() <= settings_size_bound(). |
| 84 void SetSettingsHeaderTableSize(size_t settings_size); | 80 void SetSettingsHeaderTableSize(size_t settings_size); |
| 85 | 81 |
| 86 // Determine the set of entries which would be evicted by the insertion | 82 // Determine the set of entries which would be evicted by the insertion |
| 87 // of |name| & |value| into the table, as per section 3.3.3. No eviction | 83 // of |name| & |value| into the table, as per section 3.3.3. No eviction |
| 88 // actually occurs. The set is returned via range [begin_out, end_out). | 84 // actually occurs. The set is returned via range [begin_out, end_out). |
| 89 void EvictionSet(base::StringPiece name, base::StringPiece value, | 85 void EvictionSet(StringPiece name, |
| 86 StringPiece value, |
| 90 EntryTable::iterator* begin_out, | 87 EntryTable::iterator* begin_out, |
| 91 EntryTable::iterator* end_out); | 88 EntryTable::iterator* end_out); |
| 92 | 89 |
| 93 // Adds an entry for the representation, evicting entries as needed. |name| | 90 // Adds an entry for the representation, evicting entries as needed. |name| |
| 94 // and |value| must not be owned by an entry which could be evicted. The | 91 // and |value| must not be owned by an entry which could be evicted. The |
| 95 // added HpackEntry is returned, or NULL is returned if all entries were | 92 // added HpackEntry is returned, or NULL is returned if all entries were |
| 96 // evicted and the empty table is of insufficent size for the representation. | 93 // evicted and the empty table is of insufficent size for the representation. |
| 97 HpackEntry* TryAddEntry(base::StringPiece name, base::StringPiece value); | 94 const HpackEntry* TryAddEntry(StringPiece name, StringPiece value); |
| 98 | 95 |
| 99 void DebugLogTableState() const; | 96 void DebugLogTableState() const; |
| 100 | 97 |
| 101 private: | 98 private: |
| 102 // Returns number of evictions required to enter |name| & |value|. | 99 // Returns number of evictions required to enter |name| & |value|. |
| 103 size_t EvictionCountForEntry(base::StringPiece name, | 100 size_t EvictionCountForEntry(StringPiece name, StringPiece value) const; |
| 104 base::StringPiece value) const; | |
| 105 | 101 |
| 106 // Returns number of evictions required to reclaim |reclaim_size| table size. | 102 // Returns number of evictions required to reclaim |reclaim_size| table size. |
| 107 size_t EvictionCountToReclaim(size_t reclaim_size) const; | 103 size_t EvictionCountToReclaim(size_t reclaim_size) const; |
| 108 | 104 |
| 109 // Evicts |count| oldest entries from the table. | 105 // Evicts |count| oldest entries from the table. |
| 110 void Evict(size_t count); | 106 void Evict(size_t count); |
| 111 | 107 |
| 108 // |static_entries_| and |static_index_| are owned by HpackStaticTable |
| 109 // singleton. |
| 110 const EntryTable& static_entries_; |
| 112 EntryTable dynamic_entries_; | 111 EntryTable dynamic_entries_; |
| 113 EntryTable static_entries_; | |
| 114 | 112 |
| 115 // Full table index, over |dynamic_entries_| and |static_entries_|. | 113 const OrderedEntrySet& static_index_; |
| 116 OrderedEntrySet index_; | 114 OrderedEntrySet dynamic_index_; |
| 117 | 115 |
| 118 // Last acknowledged value for SETTINGS_HEADER_TABLE_SIZE. | 116 // Last acknowledged value for SETTINGS_HEADER_TABLE_SIZE. |
| 119 size_t settings_size_bound_; | 117 size_t settings_size_bound_; |
| 120 | 118 |
| 121 // Estimated current and maximum byte size of the table. | 119 // Estimated current and maximum byte size of the table. |
| 122 // |max_size_| <= |settings_header_table_size_| | 120 // |max_size_| <= |settings_header_table_size_| |
| 123 size_t size_; | 121 size_t size_; |
| 124 size_t max_size_; | 122 size_t max_size_; |
| 125 | 123 |
| 126 // Total number of table insertions which have occurred. Referenced by | 124 // Total number of table insertions which have occurred. Referenced by |
| 127 // IndexOf() for determination of an HpackEntry's table index. | 125 // IndexOf() for determination of an HpackEntry's table index. |
| 128 size_t total_insertions_; | 126 size_t total_insertions_; |
| 129 | 127 |
| 130 DISALLOW_COPY_AND_ASSIGN(HpackHeaderTable); | 128 DISALLOW_COPY_AND_ASSIGN(HpackHeaderTable); |
| 131 }; | 129 }; |
| 132 | 130 |
| 133 } // namespace net | 131 } // namespace net |
| 134 | 132 |
| 135 #endif // NET_SPDY_HPACK_HEADER_TABLE_H_ | 133 #endif // NET_SPDY_HPACK_HEADER_TABLE_H_ |
| OLD | NEW |