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 #include "net/spdy/hpack/hpack_header_table.h"
9 #include "base/logging.h"
10 #include "net/spdy/hpack/hpack_constants.h"
11 #include "net/spdy/hpack/hpack_static_table.h"
15 using base::StringPiece
;
17 bool HpackHeaderTable::EntryComparator::operator()(
18 const HpackEntry
* lhs
,
19 const HpackEntry
* rhs
) const {
20 int result
= lhs
->name().compare(rhs
->name());
24 result
= lhs
->value().compare(rhs
->value());
28 const size_t lhs_index
= lhs
->IsLookup() ? 0 : 1 + lhs
->InsertionIndex();
29 const size_t rhs_index
= rhs
->IsLookup() ? 0 : 1 + rhs
->InsertionIndex();
30 DCHECK(lhs
== rhs
|| lhs_index
!= rhs_index
)
31 << "lhs: (" << lhs
->name() << ", " << rhs
->value() << ") rhs: ("
32 << rhs
->name() << ", " << rhs
->value() << ")"
33 << " lhs index: " << lhs_index
<< " rhs index: " << rhs_index
;
34 return lhs_index
< rhs_index
;
37 HpackHeaderTable::HpackHeaderTable()
38 : static_entries_(ObtainHpackStaticTable().GetStaticEntries()),
39 static_index_(ObtainHpackStaticTable().GetStaticIndex()),
40 settings_size_bound_(kDefaultHeaderTableSizeSetting
),
42 max_size_(kDefaultHeaderTableSizeSetting
),
43 total_insertions_(static_entries_
.size()) {}
45 HpackHeaderTable::~HpackHeaderTable() {}
47 const HpackEntry
* HpackHeaderTable::GetByIndex(size_t index
) {
52 if (index
< static_entries_
.size()) {
53 return &static_entries_
[index
];
55 index
-= static_entries_
.size();
56 if (index
< dynamic_entries_
.size()) {
57 return &dynamic_entries_
[index
];
62 const HpackEntry
* HpackHeaderTable::GetByName(StringPiece name
) {
63 HpackEntry
query(name
, "");
65 OrderedEntrySet::const_iterator it
= static_index_
.lower_bound(&query
);
66 if (it
!= static_index_
.end() && (*it
)->name() == name
) {
71 OrderedEntrySet::const_iterator it
= dynamic_index_
.lower_bound(&query
);
72 if (it
!= dynamic_index_
.end() && (*it
)->name() == name
) {
79 const HpackEntry
* HpackHeaderTable::GetByNameAndValue(StringPiece name
,
81 HpackEntry
query(name
, value
);
83 OrderedEntrySet::const_iterator it
= static_index_
.lower_bound(&query
);
84 if (it
!= static_index_
.end() && (*it
)->name() == name
&&
85 (*it
)->value() == value
) {
90 OrderedEntrySet::const_iterator it
= dynamic_index_
.lower_bound(&query
);
91 if (it
!= dynamic_index_
.end() && (*it
)->name() == name
&&
92 (*it
)->value() == value
) {
99 size_t HpackHeaderTable::IndexOf(const HpackEntry
* entry
) const {
100 if (entry
->IsLookup()) {
102 } else if (entry
->IsStatic()) {
103 return 1 + entry
->InsertionIndex();
105 return total_insertions_
- entry
->InsertionIndex() + static_entries_
.size();
109 void HpackHeaderTable::SetMaxSize(size_t max_size
) {
110 CHECK_LE(max_size
, settings_size_bound_
);
112 max_size_
= max_size
;
113 if (size_
> max_size_
) {
114 Evict(EvictionCountToReclaim(size_
- max_size_
));
115 CHECK_LE(size_
, max_size_
);
119 void HpackHeaderTable::SetSettingsHeaderTableSize(size_t settings_size
) {
120 settings_size_bound_
= settings_size
;
121 if (settings_size_bound_
< max_size_
) {
122 SetMaxSize(settings_size_bound_
);
126 void HpackHeaderTable::EvictionSet(StringPiece name
,
128 EntryTable::iterator
* begin_out
,
129 EntryTable::iterator
* end_out
) {
130 size_t eviction_count
= EvictionCountForEntry(name
, value
);
131 *begin_out
= dynamic_entries_
.end() - eviction_count
;
132 *end_out
= dynamic_entries_
.end();
135 size_t HpackHeaderTable::EvictionCountForEntry(StringPiece name
,
136 StringPiece value
) const {
137 size_t available_size
= max_size_
- size_
;
138 size_t entry_size
= HpackEntry::Size(name
, value
);
140 if (entry_size
<= available_size
) {
141 // No evictions are required.
144 return EvictionCountToReclaim(entry_size
- available_size
);
147 size_t HpackHeaderTable::EvictionCountToReclaim(size_t reclaim_size
) const {
149 for (EntryTable::const_reverse_iterator it
= dynamic_entries_
.rbegin();
150 it
!= dynamic_entries_
.rend() && reclaim_size
!= 0; ++it
, ++count
) {
151 reclaim_size
-= std::min(reclaim_size
, it
->Size());
156 void HpackHeaderTable::Evict(size_t count
) {
157 for (size_t i
= 0; i
!= count
; ++i
) {
158 CHECK(!dynamic_entries_
.empty());
159 HpackEntry
* entry
= &dynamic_entries_
.back();
161 size_
-= entry
->Size();
162 CHECK_EQ(1u, dynamic_index_
.erase(entry
));
163 dynamic_entries_
.pop_back();
167 const HpackEntry
* HpackHeaderTable::TryAddEntry(StringPiece name
,
169 Evict(EvictionCountForEntry(name
, value
));
171 size_t entry_size
= HpackEntry::Size(name
, value
);
172 if (entry_size
> (max_size_
- size_
)) {
173 // Entire table has been emptied, but there's still insufficient room.
174 DCHECK(dynamic_entries_
.empty());
175 DCHECK_EQ(0u, size_
);
178 dynamic_entries_
.push_front(HpackEntry(name
, value
,
181 CHECK(dynamic_index_
.insert(&dynamic_entries_
.front()).second
);
186 return &dynamic_entries_
.front();
189 void HpackHeaderTable::DebugLogTableState() const {
190 DVLOG(2) << "Dynamic table:";
191 for (EntryTable::const_iterator it
= dynamic_entries_
.begin();
192 it
!= dynamic_entries_
.end(); ++it
) {
193 DVLOG(2) << " " << it
->GetDebugString();
195 DVLOG(2) << "Full Static Index:";
196 for (OrderedEntrySet::const_iterator it
= static_index_
.begin();
197 it
!= static_index_
.end(); ++it
) {
198 DVLOG(2) << " " << (*it
)->GetDebugString();
200 DVLOG(2) << "Full Dynamic Index:";
201 for (OrderedEntrySet::const_iterator it
= dynamic_index_
.begin();
202 it
!= dynamic_index_
.end(); ++it
) {
203 DVLOG(2) << " " << (*it
)->GetDebugString();