Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / net / spdy / hpack / hpack_header_table.h
blob50a7acdb0545b41144fb1fe7842bfc4109d33176
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
3 // found in the LICENSE file.
5 #ifndef NET_SPDY_HPACK_HEADER_TABLE_H_
6 #define NET_SPDY_HPACK_HEADER_TABLE_H_
8 #include <cstddef>
9 #include <deque>
10 #include <set>
12 #include "base/basictypes.h"
13 #include "base/macros.h"
14 #include "net/base/net_export.h"
15 #include "net/spdy/hpack/hpack_entry.h"
17 // All section references below are to http://tools.ietf.org/html/rfc7541.
19 namespace net {
21 using base::StringPiece;
23 namespace test {
24 class HpackHeaderTablePeer;
25 } // namespace test
27 // A data structure for the static table (2.3.1) and the dynamic table (2.3.2).
28 class NET_EXPORT_PRIVATE HpackHeaderTable {
29 public:
30 friend class test::HpackHeaderTablePeer;
32 // HpackHeaderTable takes advantage of the deque property that references
33 // remain valid, so long as insertions & deletions are at the head & tail.
34 // If this changes (eg we start to drop entries from the middle of the table),
35 // this needs to be a std::list, in which case |*_index_| can be trivially
36 // extended to map to list iterators.
37 typedef std::deque<HpackEntry> EntryTable;
39 // Implements a total ordering of HpackEntry on name(), value(), then index
40 // ascending. Note that index may change over the lifetime of an HpackEntry,
41 // but the relative index order of two entries will not. This comparator is
42 // composed with the 'lookup' HpackEntry constructor to allow for efficient
43 // lower-bounding of matching entries.
44 struct NET_EXPORT_PRIVATE EntryComparator {
45 bool operator()(const HpackEntry* lhs, const HpackEntry* rhs) const;
47 typedef std::set<HpackEntry*, EntryComparator> OrderedEntrySet;
49 HpackHeaderTable();
51 ~HpackHeaderTable();
53 // Last-acknowledged value of SETTINGS_HEADER_TABLE_SIZE.
54 size_t settings_size_bound() { return settings_size_bound_; }
56 // Current and maximum estimated byte size of the table, as described in
57 // 4.1. Notably, this is /not/ the number of entries in the table.
58 size_t size() const { return size_; }
59 size_t max_size() const { return max_size_; }
61 // Returns the entry matching the index, or NULL.
62 const HpackEntry* GetByIndex(size_t index);
64 // Returns the lowest-value entry having |name|, or NULL.
65 const HpackEntry* GetByName(StringPiece name);
67 // Returns the lowest-index matching entry, or NULL.
68 const HpackEntry* GetByNameAndValue(StringPiece name, StringPiece value);
70 // Returns the index of an entry within this header table.
71 size_t IndexOf(const HpackEntry* entry) const;
73 // Sets the maximum size of the header table, evicting entries if
74 // necessary as described in 5.2.
75 void SetMaxSize(size_t max_size);
77 // Sets the SETTINGS_HEADER_TABLE_SIZE bound of the table. Will call
78 // SetMaxSize() as needed to preserve max_size() <= settings_size_bound().
79 void SetSettingsHeaderTableSize(size_t settings_size);
81 // Determine the set of entries which would be evicted by the insertion
82 // of |name| & |value| into the table, as per section 4.4. No eviction
83 // actually occurs. The set is returned via range [begin_out, end_out).
84 void EvictionSet(StringPiece name,
85 StringPiece value,
86 EntryTable::iterator* begin_out,
87 EntryTable::iterator* end_out);
89 // Adds an entry for the representation, evicting entries as needed. |name|
90 // and |value| must not be owned by an entry which could be evicted. The
91 // added HpackEntry is returned, or NULL is returned if all entries were
92 // evicted and the empty table is of insufficent size for the representation.
93 const HpackEntry* TryAddEntry(StringPiece name, StringPiece value);
95 void DebugLogTableState() const;
97 private:
98 // Returns number of evictions required to enter |name| & |value|.
99 size_t EvictionCountForEntry(StringPiece name, StringPiece value) const;
101 // Returns number of evictions required to reclaim |reclaim_size| table size.
102 size_t EvictionCountToReclaim(size_t reclaim_size) const;
104 // Evicts |count| oldest entries from the table.
105 void Evict(size_t count);
107 // |static_entries_| and |static_index_| are owned by HpackStaticTable
108 // singleton.
109 const EntryTable& static_entries_;
110 EntryTable dynamic_entries_;
112 const OrderedEntrySet& static_index_;
113 OrderedEntrySet dynamic_index_;
115 // Last acknowledged value for SETTINGS_HEADER_TABLE_SIZE.
116 size_t settings_size_bound_;
118 // Estimated current and maximum byte size of the table.
119 // |max_size_| <= |settings_size_bound_|
120 size_t size_;
121 size_t max_size_;
123 // Total number of table insertions which have occurred. Referenced by
124 // IndexOf() for determination of an HpackEntry's table index.
125 size_t total_insertions_;
127 DISALLOW_COPY_AND_ASSIGN(HpackHeaderTable);
130 } // namespace net
132 #endif // NET_SPDY_HPACK_HEADER_TABLE_H_