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 #include "net/spdy/hpack_header_table.h" | 5 #include "net/spdy/hpack_header_table.h" |
6 | 6 |
| 7 #include <algorithm> |
| 8 |
7 #include "base/logging.h" | 9 #include "base/logging.h" |
8 #include "net/spdy/hpack_constants.h" | 10 #include "net/spdy/hpack_constants.h" |
9 #include "net/spdy/hpack_string_util.h" | 11 #include "net/spdy/hpack_string_util.h" |
10 | 12 |
11 namespace net { | 13 namespace net { |
12 | 14 |
| 15 using base::StringPiece; |
| 16 |
| 17 namespace { |
| 18 |
| 19 // An entry in the static table. Must be a POD in order to avoid |
| 20 // static initializers, i.e. no user-defined constructors or |
| 21 // destructors. |
| 22 struct StaticEntry { |
| 23 const char* const name; |
| 24 const size_t name_len; |
| 25 const char* const value; |
| 26 const size_t value_len; |
| 27 }; |
| 28 |
| 29 // The "constructor" for a StaticEntry that computes the lengths at |
| 30 // compile time. |
| 31 #define STATIC_ENTRY(name, value) \ |
| 32 { name, arraysize(name) - 1, value, arraysize(value) - 1 } |
| 33 |
| 34 const StaticEntry kStaticTable[] = { |
| 35 STATIC_ENTRY(":authority" , ""), // 1 |
| 36 STATIC_ENTRY(":method" , "GET"), // 2 |
| 37 STATIC_ENTRY(":method" , "POST"), // 3 |
| 38 STATIC_ENTRY(":path" , "/"), // 4 |
| 39 STATIC_ENTRY(":path" , "/index.html"), // 5 |
| 40 STATIC_ENTRY(":scheme" , "http"), // 6 |
| 41 STATIC_ENTRY(":scheme" , "https"), // 7 |
| 42 STATIC_ENTRY(":status" , "200"), // 8 |
| 43 STATIC_ENTRY(":status" , "500"), // 9 |
| 44 STATIC_ENTRY(":status" , "404"), // 10 |
| 45 STATIC_ENTRY(":status" , "403"), // 11 |
| 46 STATIC_ENTRY(":status" , "400"), // 12 |
| 47 STATIC_ENTRY(":status" , "401"), // 13 |
| 48 STATIC_ENTRY("accept-charset" , ""), // 14 |
| 49 STATIC_ENTRY("accept-encoding" , ""), // 15 |
| 50 STATIC_ENTRY("accept-language" , ""), // 16 |
| 51 STATIC_ENTRY("accept-ranges" , ""), // 17 |
| 52 STATIC_ENTRY("accept" , ""), // 18 |
| 53 STATIC_ENTRY("access-control-allow-origin" , ""), // 19 |
| 54 STATIC_ENTRY("age" , ""), // 20 |
| 55 STATIC_ENTRY("allow" , ""), // 21 |
| 56 STATIC_ENTRY("authorization" , ""), // 22 |
| 57 STATIC_ENTRY("cache-control" , ""), // 23 |
| 58 STATIC_ENTRY("content-disposition" , ""), // 24 |
| 59 STATIC_ENTRY("content-encoding" , ""), // 25 |
| 60 STATIC_ENTRY("content-language" , ""), // 26 |
| 61 STATIC_ENTRY("content-length" , ""), // 27 |
| 62 STATIC_ENTRY("content-location" , ""), // 28 |
| 63 STATIC_ENTRY("content-range" , ""), // 29 |
| 64 STATIC_ENTRY("content-type" , ""), // 30 |
| 65 STATIC_ENTRY("cookie" , ""), // 31 |
| 66 STATIC_ENTRY("date" , ""), // 32 |
| 67 STATIC_ENTRY("etag" , ""), // 33 |
| 68 STATIC_ENTRY("expect" , ""), // 34 |
| 69 STATIC_ENTRY("expires" , ""), // 35 |
| 70 STATIC_ENTRY("from" , ""), // 36 |
| 71 STATIC_ENTRY("host" , ""), // 37 |
| 72 STATIC_ENTRY("if-match" , ""), // 38 |
| 73 STATIC_ENTRY("if-modified-since" , ""), // 39 |
| 74 STATIC_ENTRY("if-none-match" , ""), // 40 |
| 75 STATIC_ENTRY("if-range" , ""), // 41 |
| 76 STATIC_ENTRY("if-unmodified-since" , ""), // 42 |
| 77 STATIC_ENTRY("last-modified" , ""), // 43 |
| 78 STATIC_ENTRY("link" , ""), // 44 |
| 79 STATIC_ENTRY("location" , ""), // 45 |
| 80 STATIC_ENTRY("max-forwards" , ""), // 46 |
| 81 STATIC_ENTRY("proxy-authenticate" , ""), // 47 |
| 82 STATIC_ENTRY("proxy-authorization" , ""), // 48 |
| 83 STATIC_ENTRY("range" , ""), // 49 |
| 84 STATIC_ENTRY("referer" , ""), // 50 |
| 85 STATIC_ENTRY("refresh" , ""), // 51 |
| 86 STATIC_ENTRY("retry-after" , ""), // 52 |
| 87 STATIC_ENTRY("server" , ""), // 53 |
| 88 STATIC_ENTRY("set-cookie" , ""), // 54 |
| 89 STATIC_ENTRY("strict-transport-security" , ""), // 55 |
| 90 STATIC_ENTRY("transfer-encoding" , ""), // 56 |
| 91 STATIC_ENTRY("user-agent" , ""), // 57 |
| 92 STATIC_ENTRY("vary" , ""), // 58 |
| 93 STATIC_ENTRY("via" , ""), // 59 |
| 94 STATIC_ENTRY("www-authenticate" , ""), // 60 |
| 95 }; |
| 96 |
| 97 #undef STATIC_ENTRY |
| 98 |
| 99 } // namespace |
| 100 |
13 HpackHeaderTable::HpackHeaderTable() | 101 HpackHeaderTable::HpackHeaderTable() |
14 : size_(0), | 102 : settings_size_bound_(kDefaultHeaderTableSizeSetting), |
15 max_size_(kDefaultHeaderTableSizeSetting) {} | 103 size_(0), |
| 104 max_size_(kDefaultHeaderTableSizeSetting), |
| 105 total_insertions_(0), |
| 106 dynamic_entries_count_(0) { |
| 107 for (const StaticEntry* it = kStaticTable; |
| 108 it != kStaticTable + arraysize(kStaticTable); ++it) { |
| 109 static_entries_.push_back( |
| 110 HpackEntry(StringPiece(it->name, it->name_len), |
| 111 StringPiece(it->value, it->value_len), |
| 112 true, // is_static |
| 113 total_insertions_, |
| 114 &dynamic_entries_count_)); |
| 115 CHECK(index_.insert(&static_entries_.back()).second); |
| 116 |
| 117 ++total_insertions_; |
| 118 } |
| 119 } |
16 | 120 |
17 HpackHeaderTable::~HpackHeaderTable() {} | 121 HpackHeaderTable::~HpackHeaderTable() {} |
18 | 122 |
19 uint32 HpackHeaderTable::GetEntryCount() const { | 123 HpackEntry* HpackHeaderTable::GetByIndex(size_t index) { |
20 size_t size = entries_.size(); | 124 if (index == 0) { |
21 DCHECK_LE(size, kuint32max); | 125 return NULL; |
22 return static_cast<uint32>(size); | 126 } |
23 } | 127 index -= 1; |
24 | 128 if (index < dynamic_entries_.size()) { |
25 const HpackEntry& HpackHeaderTable::GetEntry(uint32 index) const { | 129 return &dynamic_entries_[index]; |
26 CHECK_GE(index, 1u); | 130 } |
27 CHECK_LE(index, GetEntryCount()); | 131 index -= dynamic_entries_.size(); |
28 return entries_[index-1]; | 132 if (index < static_entries_.size()) { |
29 } | 133 return &static_entries_[index]; |
30 | 134 } |
31 HpackEntry* HpackHeaderTable::GetMutableEntry(uint32 index) { | 135 return NULL; |
32 CHECK_GE(index, 1u); | 136 } |
33 CHECK_LE(index, GetEntryCount()); | 137 |
34 return &entries_[index-1]; | 138 HpackEntry* HpackHeaderTable::GetByName(StringPiece name) { |
35 } | 139 HpackEntry query(name, ""); |
36 | 140 HpackEntry::OrderedSet::const_iterator it = index_.lower_bound(&query); |
37 void HpackHeaderTable::SetMaxSize(uint32 max_size) { | 141 if (it != index_.end() && (*it)->name() == name) { |
| 142 return *it; |
| 143 } |
| 144 return NULL; |
| 145 } |
| 146 |
| 147 HpackEntry* HpackHeaderTable::GetByNameAndValue(StringPiece name, |
| 148 StringPiece value) { |
| 149 HpackEntry query(name, value); |
| 150 HpackEntry::OrderedSet::const_iterator it = index_.lower_bound(&query); |
| 151 if (it != index_.end() && (*it)->name() == name && (*it)->value() == value) { |
| 152 return *it; |
| 153 } |
| 154 return NULL; |
| 155 } |
| 156 |
| 157 void HpackHeaderTable::SetMaxSize(size_t max_size) { |
| 158 CHECK_LE(max_size, settings_size_bound_); |
| 159 |
38 max_size_ = max_size; | 160 max_size_ = max_size; |
39 while (size_ > max_size_) { | 161 if (size_ > max_size_) { |
40 CHECK(!entries_.empty()); | 162 Evict(EvictionCountToReclaim(size_ - max_size_)); |
41 size_ -= entries_.back().Size(); | 163 CHECK_LE(size_, max_size_); |
42 entries_.pop_back(); | 164 } |
43 } | 165 } |
44 } | 166 |
45 | 167 void HpackHeaderTable::SetSettingsHeaderTableSize(size_t settings_size) { |
46 void HpackHeaderTable::TryAddEntry( | 168 settings_size_bound_ = settings_size; |
47 const HpackEntry& entry, | 169 if (settings_size_bound_ < max_size_) { |
48 uint32* index, | 170 SetMaxSize(settings_size_bound_); |
49 std::vector<uint32>* removed_referenced_indices) { | 171 } |
50 *index = 0; | 172 } |
51 removed_referenced_indices->clear(); | 173 |
52 | 174 void HpackHeaderTable::EvictionSet(StringPiece name, |
53 // The algorithm used here is described in 3.3.3. We're assuming | 175 StringPiece value, |
54 // that the given entry is caching the name/value. | 176 EntryTable::iterator* begin_out, |
55 size_t target_size = 0; | 177 EntryTable::iterator* end_out) { |
56 size_t size_t_max_size = static_cast<size_t>(max_size_); | 178 size_t eviction_count = EvictionCountForEntry(name, value); |
57 if (entry.Size() <= size_t_max_size) { | 179 *begin_out = dynamic_entries_.end() - eviction_count; |
58 // The conditional implies the difference can fit in 32 bits. | 180 *end_out = dynamic_entries_.end(); |
59 target_size = size_t_max_size - entry.Size(); | 181 } |
60 } | 182 |
61 while (static_cast<size_t>(size_) > target_size) { | 183 size_t HpackHeaderTable::EvictionCountForEntry(StringPiece name, |
62 DCHECK(!entries_.empty()); | 184 StringPiece value) const { |
63 if (entries_.back().IsReferenced()) { | 185 size_t available_size = max_size_ - size_; |
64 removed_referenced_indices->push_back(entries_.size()); | 186 size_t entry_size = HpackEntry::Size(name, value); |
65 } | 187 |
66 size_ -= entries_.back().Size(); | 188 if (entry_size <= available_size) { |
67 entries_.pop_back(); | 189 // No evictions are required. |
68 } | 190 return 0; |
69 | 191 } |
70 if (entry.Size() <= size_t_max_size) { | 192 return EvictionCountToReclaim(entry_size - available_size); |
71 // Implied by the exit condition of the while loop above and the | 193 } |
72 // condition of the if. | 194 |
73 DCHECK_LE(static_cast<size_t>(size_) + entry.Size(), size_t_max_size); | 195 size_t HpackHeaderTable::EvictionCountToReclaim(size_t reclaim_size) const { |
74 size_ += entry.Size(); | 196 size_t count = 0; |
75 *index = 1; | 197 for (EntryTable::const_reverse_iterator it = dynamic_entries_.rbegin(); |
76 entries_.push_front(entry); | 198 it != dynamic_entries_.rend() && reclaim_size != 0; ++it, ++count) { |
77 } | 199 reclaim_size -= std::min(reclaim_size, it->Size()); |
78 } | 200 } |
79 | 201 return count; |
| 202 } |
| 203 |
| 204 void HpackHeaderTable::Evict(size_t count) { |
| 205 for (size_t i = 0; i != count; ++i) { |
| 206 CHECK(!dynamic_entries_.empty()); |
| 207 HpackEntry* entry = &dynamic_entries_.back(); |
| 208 |
| 209 size_ -= entry->Size(); |
| 210 CHECK_EQ(1u, index_.erase(entry)); |
| 211 reference_set_.erase(entry); |
| 212 dynamic_entries_.pop_back(); |
| 213 |
| 214 --dynamic_entries_count_; |
| 215 DCHECK_EQ(dynamic_entries_count_, dynamic_entries_.size()); |
| 216 } |
| 217 } |
| 218 |
| 219 HpackEntry* HpackHeaderTable::TryAddEntry(StringPiece name, StringPiece value) { |
| 220 Evict(EvictionCountForEntry(name, value)); |
| 221 |
| 222 size_t entry_size = HpackEntry::Size(name, value); |
| 223 if (entry_size > (max_size_ - size_)) { |
| 224 // Entire table has been emptied, but there's still insufficient room. |
| 225 DCHECK(dynamic_entries_.empty()); |
| 226 DCHECK_EQ(0u, size_); |
| 227 return NULL; |
| 228 } |
| 229 dynamic_entries_.push_front(HpackEntry(name, |
| 230 value, |
| 231 false, // is_static |
| 232 total_insertions_, |
| 233 &total_insertions_)); |
| 234 CHECK(index_.insert(&dynamic_entries_.front()).second); |
| 235 |
| 236 size_ += entry_size; |
| 237 ++dynamic_entries_count_; |
| 238 ++total_insertions_; |
| 239 |
| 240 DCHECK_EQ(dynamic_entries_count_, dynamic_entries_.size()); |
| 241 return &dynamic_entries_.front(); |
| 242 } |
| 243 |
| 244 void HpackHeaderTable::ClearReferenceSet() { |
| 245 for (HpackEntry::OrderedSet::iterator it = reference_set_.begin(); |
| 246 it != reference_set_.end(); ++it) { |
| 247 (*it)->set_state(0); |
| 248 } |
| 249 reference_set_.clear(); |
| 250 } |
| 251 |
| 252 bool HpackHeaderTable::Toggle(HpackEntry* entry) { |
| 253 CHECK(!entry->IsStatic()); |
| 254 CHECK_EQ(0u, entry->state()); |
| 255 |
| 256 std::pair<HpackEntry::OrderedSet::iterator, bool> insert_result = |
| 257 reference_set_.insert(entry); |
| 258 if (insert_result.second) { |
| 259 return true; |
| 260 } else { |
| 261 reference_set_.erase(insert_result.first); |
| 262 return false; |
| 263 } |
| 264 } |
| 265 |
| 266 void HpackHeaderTable::DebugLogTableState() const { |
| 267 DVLOG(2) << "Reference Set:"; |
| 268 for (HpackEntry::OrderedSet::const_iterator it = reference_set_.begin(); |
| 269 it != reference_set_.end(); ++it) { |
| 270 DVLOG(2) << " " << (*it)->GetDebugString(); |
| 271 } |
| 272 DVLOG(2) << "Dynamic table:"; |
| 273 for (EntryTable::const_iterator it = dynamic_entries_.begin(); |
| 274 it != dynamic_entries_.end(); ++it) { |
| 275 DVLOG(2) << " " << it->GetDebugString(); |
| 276 } |
| 277 DVLOG(2) << "Full Index:"; |
| 278 for (HpackEntry::OrderedSet::const_iterator it = index_.begin(); |
| 279 it != index_.end(); ++it) { |
| 280 DVLOG(2) << " " << (*it)->GetDebugString(); |
| 281 } |
| 282 } |
| 283 |
80 } // namespace net | 284 } // namespace net |
OLD | NEW |