QUICHE team | 82dee2f | 2019-01-18 12:35:12 -0500 | [diff] [blame] | 1 | // Copyright (c) 2012 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 | |
QUICHE team | 5be974e | 2020-12-29 18:35:24 -0500 | [diff] [blame] | 5 | #include "spdy/core/spdy_header_block.h" |
QUICHE team | 82dee2f | 2019-01-18 12:35:12 -0500 | [diff] [blame] | 6 | |
| 7 | #include <string.h> |
| 8 | |
| 9 | #include <algorithm> |
| 10 | #include <utility> |
| 11 | |
bnc | 60cef58 | 2021-03-24 10:25:37 -0700 | [diff] [blame] | 12 | #include "absl/strings/str_cat.h" |
QUICHE team | 1b5d09e | 2021-04-08 13:36:42 -0700 | [diff] [blame] | 13 | #include "common/platform/api/quiche_logging.h" |
QUICHE team | 5be974e | 2020-12-29 18:35:24 -0500 | [diff] [blame] | 14 | #include "spdy/platform/api/spdy_estimate_memory_usage.h" |
QUICHE team | 82dee2f | 2019-01-18 12:35:12 -0500 | [diff] [blame] | 15 | |
| 16 | namespace spdy { |
| 17 | namespace { |
| 18 | |
| 19 | // By default, linked_hash_map's internal map allocates space for 100 map |
| 20 | // buckets on construction, which is larger than necessary. Standard library |
| 21 | // unordered map implementations use a list of prime numbers to set the bucket |
| 22 | // count for a particular capacity. |kInitialMapBuckets| is chosen to reduce |
| 23 | // memory usage for small header blocks, at the cost of having to rehash for |
| 24 | // large header blocks. |
| 25 | const size_t kInitialMapBuckets = 11; |
| 26 | |
QUICHE team | 82dee2f | 2019-01-18 12:35:12 -0500 | [diff] [blame] | 27 | const char kCookieKey[] = "cookie"; |
| 28 | const char kNullSeparator = 0; |
| 29 | |
vasilvv | c8ccdb2 | 2020-10-12 16:42:34 -0700 | [diff] [blame] | 30 | absl::string_view SeparatorForKey(absl::string_view key) { |
QUICHE team | 82dee2f | 2019-01-18 12:35:12 -0500 | [diff] [blame] | 31 | if (key == kCookieKey) { |
vasilvv | c8ccdb2 | 2020-10-12 16:42:34 -0700 | [diff] [blame] | 32 | static absl::string_view cookie_separator = "; "; |
QUICHE team | 82dee2f | 2019-01-18 12:35:12 -0500 | [diff] [blame] | 33 | return cookie_separator; |
| 34 | } else { |
vasilvv | c8ccdb2 | 2020-10-12 16:42:34 -0700 | [diff] [blame] | 35 | return absl::string_view(&kNullSeparator, 1); |
QUICHE team | 82dee2f | 2019-01-18 12:35:12 -0500 | [diff] [blame] | 36 | } |
| 37 | } |
| 38 | |
| 39 | } // namespace |
| 40 | |
QUICHE team | 557dfbc | 2020-10-20 10:52:30 -0700 | [diff] [blame] | 41 | Http2HeaderBlock::HeaderValue::HeaderValue(SpdyHeaderStorage* storage, |
| 42 | absl::string_view key, |
| 43 | absl::string_view initial_value) |
QUICHE team | 82dee2f | 2019-01-18 12:35:12 -0500 | [diff] [blame] | 44 | : storage_(storage), |
| 45 | fragments_({initial_value}), |
| 46 | pair_({key, {}}), |
| 47 | size_(initial_value.size()), |
| 48 | separator_size_(SeparatorForKey(key).size()) {} |
| 49 | |
QUICHE team | 557dfbc | 2020-10-20 10:52:30 -0700 | [diff] [blame] | 50 | Http2HeaderBlock::HeaderValue::HeaderValue(HeaderValue&& other) |
QUICHE team | 82dee2f | 2019-01-18 12:35:12 -0500 | [diff] [blame] | 51 | : storage_(other.storage_), |
| 52 | fragments_(std::move(other.fragments_)), |
| 53 | pair_(std::move(other.pair_)), |
| 54 | size_(other.size_), |
| 55 | separator_size_(other.separator_size_) {} |
| 56 | |
QUICHE team | 557dfbc | 2020-10-20 10:52:30 -0700 | [diff] [blame] | 57 | Http2HeaderBlock::HeaderValue& Http2HeaderBlock::HeaderValue::operator=( |
QUICHE team | 82dee2f | 2019-01-18 12:35:12 -0500 | [diff] [blame] | 58 | HeaderValue&& other) { |
| 59 | storage_ = other.storage_; |
| 60 | fragments_ = std::move(other.fragments_); |
| 61 | pair_ = std::move(other.pair_); |
| 62 | size_ = other.size_; |
| 63 | separator_size_ = other.separator_size_; |
| 64 | return *this; |
| 65 | } |
| 66 | |
QUICHE team | 557dfbc | 2020-10-20 10:52:30 -0700 | [diff] [blame] | 67 | void Http2HeaderBlock::HeaderValue::set_storage(SpdyHeaderStorage* storage) { |
QUICHE team | 0b74550 | 2019-12-10 11:02:08 -0800 | [diff] [blame] | 68 | storage_ = storage; |
| 69 | } |
| 70 | |
QUICHE team | 557dfbc | 2020-10-20 10:52:30 -0700 | [diff] [blame] | 71 | Http2HeaderBlock::HeaderValue::~HeaderValue() = default; |
QUICHE team | 82dee2f | 2019-01-18 12:35:12 -0500 | [diff] [blame] | 72 | |
QUICHE team | 557dfbc | 2020-10-20 10:52:30 -0700 | [diff] [blame] | 73 | absl::string_view Http2HeaderBlock::HeaderValue::ConsolidatedValue() const { |
QUICHE team | 82dee2f | 2019-01-18 12:35:12 -0500 | [diff] [blame] | 74 | if (fragments_.empty()) { |
vasilvv | c8ccdb2 | 2020-10-12 16:42:34 -0700 | [diff] [blame] | 75 | return absl::string_view(); |
QUICHE team | 82dee2f | 2019-01-18 12:35:12 -0500 | [diff] [blame] | 76 | } |
| 77 | if (fragments_.size() > 1) { |
| 78 | fragments_ = { |
| 79 | storage_->WriteFragments(fragments_, SeparatorForKey(pair_.first))}; |
| 80 | } |
| 81 | return fragments_[0]; |
| 82 | } |
| 83 | |
QUICHE team | 557dfbc | 2020-10-20 10:52:30 -0700 | [diff] [blame] | 84 | void Http2HeaderBlock::HeaderValue::Append(absl::string_view fragment) { |
QUICHE team | 82dee2f | 2019-01-18 12:35:12 -0500 | [diff] [blame] | 85 | size_ += (fragment.size() + separator_size_); |
| 86 | fragments_.push_back(fragment); |
| 87 | } |
| 88 | |
vasilvv | c8ccdb2 | 2020-10-12 16:42:34 -0700 | [diff] [blame] | 89 | const std::pair<absl::string_view, absl::string_view>& |
QUICHE team | 557dfbc | 2020-10-20 10:52:30 -0700 | [diff] [blame] | 90 | Http2HeaderBlock::HeaderValue::as_pair() const { |
QUICHE team | 82dee2f | 2019-01-18 12:35:12 -0500 | [diff] [blame] | 91 | pair_.second = ConsolidatedValue(); |
| 92 | return pair_; |
| 93 | } |
| 94 | |
QUICHE team | 557dfbc | 2020-10-20 10:52:30 -0700 | [diff] [blame] | 95 | Http2HeaderBlock::iterator::iterator(MapType::const_iterator it) : it_(it) {} |
QUICHE team | 82dee2f | 2019-01-18 12:35:12 -0500 | [diff] [blame] | 96 | |
QUICHE team | 557dfbc | 2020-10-20 10:52:30 -0700 | [diff] [blame] | 97 | Http2HeaderBlock::iterator::iterator(const iterator& other) = default; |
QUICHE team | 82dee2f | 2019-01-18 12:35:12 -0500 | [diff] [blame] | 98 | |
QUICHE team | 557dfbc | 2020-10-20 10:52:30 -0700 | [diff] [blame] | 99 | Http2HeaderBlock::iterator::~iterator() = default; |
QUICHE team | 82dee2f | 2019-01-18 12:35:12 -0500 | [diff] [blame] | 100 | |
QUICHE team | 557dfbc | 2020-10-20 10:52:30 -0700 | [diff] [blame] | 101 | Http2HeaderBlock::ValueProxy::ValueProxy( |
| 102 | Http2HeaderBlock* block, |
| 103 | Http2HeaderBlock::MapType::iterator lookup_result, |
vasilvv | c8ccdb2 | 2020-10-12 16:42:34 -0700 | [diff] [blame] | 104 | const absl::string_view key, |
QUICHE team | 82dee2f | 2019-01-18 12:35:12 -0500 | [diff] [blame] | 105 | size_t* spdy_header_block_value_size) |
| 106 | : block_(block), |
QUICHE team | 82dee2f | 2019-01-18 12:35:12 -0500 | [diff] [blame] | 107 | lookup_result_(lookup_result), |
| 108 | key_(key), |
| 109 | spdy_header_block_value_size_(spdy_header_block_value_size), |
| 110 | valid_(true) {} |
| 111 | |
QUICHE team | 557dfbc | 2020-10-20 10:52:30 -0700 | [diff] [blame] | 112 | Http2HeaderBlock::ValueProxy::ValueProxy(ValueProxy&& other) |
QUICHE team | 82dee2f | 2019-01-18 12:35:12 -0500 | [diff] [blame] | 113 | : block_(other.block_), |
QUICHE team | 82dee2f | 2019-01-18 12:35:12 -0500 | [diff] [blame] | 114 | lookup_result_(other.lookup_result_), |
| 115 | key_(other.key_), |
| 116 | spdy_header_block_value_size_(other.spdy_header_block_value_size_), |
| 117 | valid_(true) { |
| 118 | other.valid_ = false; |
| 119 | } |
| 120 | |
QUICHE team | 557dfbc | 2020-10-20 10:52:30 -0700 | [diff] [blame] | 121 | Http2HeaderBlock::ValueProxy& Http2HeaderBlock::ValueProxy::operator=( |
| 122 | Http2HeaderBlock::ValueProxy&& other) { |
QUICHE team | 82dee2f | 2019-01-18 12:35:12 -0500 | [diff] [blame] | 123 | block_ = other.block_; |
QUICHE team | 82dee2f | 2019-01-18 12:35:12 -0500 | [diff] [blame] | 124 | lookup_result_ = other.lookup_result_; |
| 125 | key_ = other.key_; |
| 126 | valid_ = true; |
| 127 | other.valid_ = false; |
| 128 | spdy_header_block_value_size_ = other.spdy_header_block_value_size_; |
| 129 | return *this; |
| 130 | } |
| 131 | |
QUICHE team | 557dfbc | 2020-10-20 10:52:30 -0700 | [diff] [blame] | 132 | Http2HeaderBlock::ValueProxy::~ValueProxy() { |
QUICHE team | 82dee2f | 2019-01-18 12:35:12 -0500 | [diff] [blame] | 133 | // If the ValueProxy is destroyed while lookup_result_ == block_->end(), |
QUICHE team | 6b29700 | 2019-12-09 09:37:56 -0800 | [diff] [blame] | 134 | // the assignment operator was never used, and the block's SpdyHeaderStorage |
| 135 | // can reclaim the memory used by the key. This makes lookup-only access to |
QUICHE team | 557dfbc | 2020-10-20 10:52:30 -0700 | [diff] [blame] | 136 | // Http2HeaderBlock through operator[] memory-neutral. |
QUICHE team | 8e3bb9d | 2019-12-06 16:03:22 -0800 | [diff] [blame] | 137 | if (valid_ && lookup_result_ == block_->map_.end()) { |
QUICHE team | 0b74550 | 2019-12-10 11:02:08 -0800 | [diff] [blame] | 138 | block_->storage_.Rewind(key_); |
QUICHE team | 82dee2f | 2019-01-18 12:35:12 -0500 | [diff] [blame] | 139 | } |
| 140 | } |
| 141 | |
QUICHE team | 557dfbc | 2020-10-20 10:52:30 -0700 | [diff] [blame] | 142 | Http2HeaderBlock::ValueProxy& Http2HeaderBlock::ValueProxy::operator=( |
vasilvv | c8ccdb2 | 2020-10-12 16:42:34 -0700 | [diff] [blame] | 143 | absl::string_view value) { |
QUICHE team | 82dee2f | 2019-01-18 12:35:12 -0500 | [diff] [blame] | 144 | *spdy_header_block_value_size_ += value.size(); |
QUICHE team | 0b74550 | 2019-12-10 11:02:08 -0800 | [diff] [blame] | 145 | SpdyHeaderStorage* storage = &block_->storage_; |
QUICHE team | 8e3bb9d | 2019-12-06 16:03:22 -0800 | [diff] [blame] | 146 | if (lookup_result_ == block_->map_.end()) { |
QUICHE team | 1b5d09e | 2021-04-08 13:36:42 -0700 | [diff] [blame] | 147 | QUICHE_DVLOG(1) << "Inserting: (" << key_ << ", " << value << ")"; |
QUICHE team | 82dee2f | 2019-01-18 12:35:12 -0500 | [diff] [blame] | 148 | lookup_result_ = |
QUICHE team | 8e3bb9d | 2019-12-06 16:03:22 -0800 | [diff] [blame] | 149 | block_->map_ |
| 150 | .emplace(std::make_pair( |
| 151 | key_, HeaderValue(storage, key_, storage->Write(value)))) |
QUICHE team | 82dee2f | 2019-01-18 12:35:12 -0500 | [diff] [blame] | 152 | .first; |
| 153 | } else { |
QUICHE team | 1b5d09e | 2021-04-08 13:36:42 -0700 | [diff] [blame] | 154 | QUICHE_DVLOG(1) << "Updating key: " << key_ << " with value: " << value; |
QUICHE team | 82dee2f | 2019-01-18 12:35:12 -0500 | [diff] [blame] | 155 | *spdy_header_block_value_size_ -= lookup_result_->second.SizeEstimate(); |
QUICHE team | 8e3bb9d | 2019-12-06 16:03:22 -0800 | [diff] [blame] | 156 | lookup_result_->second = HeaderValue(storage, key_, storage->Write(value)); |
QUICHE team | 82dee2f | 2019-01-18 12:35:12 -0500 | [diff] [blame] | 157 | } |
| 158 | return *this; |
| 159 | } |
| 160 | |
QUICHE team | 557dfbc | 2020-10-20 10:52:30 -0700 | [diff] [blame] | 161 | bool Http2HeaderBlock::ValueProxy::operator==(absl::string_view value) const { |
QUICHE team | 3f57c75 | 2019-12-16 14:09:53 -0800 | [diff] [blame] | 162 | if (lookup_result_ == block_->map_.end()) { |
| 163 | return false; |
| 164 | } else { |
| 165 | return value == lookup_result_->second.value(); |
| 166 | } |
| 167 | } |
| 168 | |
QUICHE team | 557dfbc | 2020-10-20 10:52:30 -0700 | [diff] [blame] | 169 | std::string Http2HeaderBlock::ValueProxy::as_string() const { |
QUICHE team | 8e3bb9d | 2019-12-06 16:03:22 -0800 | [diff] [blame] | 170 | if (lookup_result_ == block_->map_.end()) { |
QUICHE team | 82dee2f | 2019-01-18 12:35:12 -0500 | [diff] [blame] | 171 | return ""; |
| 172 | } else { |
bnc | 4471291 | 2019-08-15 18:58:14 -0700 | [diff] [blame] | 173 | return std::string(lookup_result_->second.value()); |
QUICHE team | 82dee2f | 2019-01-18 12:35:12 -0500 | [diff] [blame] | 174 | } |
| 175 | } |
| 176 | |
QUICHE team | 557dfbc | 2020-10-20 10:52:30 -0700 | [diff] [blame] | 177 | Http2HeaderBlock::Http2HeaderBlock() : map_(kInitialMapBuckets) {} |
QUICHE team | 82dee2f | 2019-01-18 12:35:12 -0500 | [diff] [blame] | 178 | |
QUICHE team | 557dfbc | 2020-10-20 10:52:30 -0700 | [diff] [blame] | 179 | Http2HeaderBlock::Http2HeaderBlock(Http2HeaderBlock&& other) |
QUICHE team | 8e3bb9d | 2019-12-06 16:03:22 -0800 | [diff] [blame] | 180 | : map_(kInitialMapBuckets) { |
| 181 | map_.swap(other.map_); |
QUICHE team | 0b74550 | 2019-12-10 11:02:08 -0800 | [diff] [blame] | 182 | storage_ = std::move(other.storage_); |
| 183 | for (auto& p : map_) { |
| 184 | p.second.set_storage(&storage_); |
| 185 | } |
QUICHE team | 82dee2f | 2019-01-18 12:35:12 -0500 | [diff] [blame] | 186 | key_size_ = other.key_size_; |
| 187 | value_size_ = other.value_size_; |
| 188 | } |
| 189 | |
QUICHE team | 557dfbc | 2020-10-20 10:52:30 -0700 | [diff] [blame] | 190 | Http2HeaderBlock::~Http2HeaderBlock() = default; |
QUICHE team | 82dee2f | 2019-01-18 12:35:12 -0500 | [diff] [blame] | 191 | |
QUICHE team | 557dfbc | 2020-10-20 10:52:30 -0700 | [diff] [blame] | 192 | Http2HeaderBlock& Http2HeaderBlock::operator=(Http2HeaderBlock&& other) { |
QUICHE team | 8e3bb9d | 2019-12-06 16:03:22 -0800 | [diff] [blame] | 193 | map_.swap(other.map_); |
QUICHE team | 0b74550 | 2019-12-10 11:02:08 -0800 | [diff] [blame] | 194 | storage_ = std::move(other.storage_); |
| 195 | for (auto& p : map_) { |
| 196 | p.second.set_storage(&storage_); |
| 197 | } |
QUICHE team | 82dee2f | 2019-01-18 12:35:12 -0500 | [diff] [blame] | 198 | key_size_ = other.key_size_; |
| 199 | value_size_ = other.value_size_; |
| 200 | return *this; |
| 201 | } |
| 202 | |
QUICHE team | 557dfbc | 2020-10-20 10:52:30 -0700 | [diff] [blame] | 203 | Http2HeaderBlock Http2HeaderBlock::Clone() const { |
| 204 | Http2HeaderBlock copy; |
QUICHE team | 82dee2f | 2019-01-18 12:35:12 -0500 | [diff] [blame] | 205 | for (const auto& p : *this) { |
| 206 | copy.AppendHeader(p.first, p.second); |
| 207 | } |
| 208 | return copy; |
| 209 | } |
| 210 | |
QUICHE team | 557dfbc | 2020-10-20 10:52:30 -0700 | [diff] [blame] | 211 | bool Http2HeaderBlock::operator==(const Http2HeaderBlock& other) const { |
QUICHE team | 82dee2f | 2019-01-18 12:35:12 -0500 | [diff] [blame] | 212 | return size() == other.size() && std::equal(begin(), end(), other.begin()); |
| 213 | } |
| 214 | |
QUICHE team | 557dfbc | 2020-10-20 10:52:30 -0700 | [diff] [blame] | 215 | bool Http2HeaderBlock::operator!=(const Http2HeaderBlock& other) const { |
QUICHE team | 82dee2f | 2019-01-18 12:35:12 -0500 | [diff] [blame] | 216 | return !(operator==(other)); |
| 217 | } |
| 218 | |
QUICHE team | 557dfbc | 2020-10-20 10:52:30 -0700 | [diff] [blame] | 219 | std::string Http2HeaderBlock::DebugString() const { |
QUICHE team | 82dee2f | 2019-01-18 12:35:12 -0500 | [diff] [blame] | 220 | if (empty()) { |
| 221 | return "{}"; |
| 222 | } |
| 223 | |
bnc | 4471291 | 2019-08-15 18:58:14 -0700 | [diff] [blame] | 224 | std::string output = "\n{\n"; |
QUICHE team | 82dee2f | 2019-01-18 12:35:12 -0500 | [diff] [blame] | 225 | for (auto it = begin(); it != end(); ++it) { |
bnc | 60cef58 | 2021-03-24 10:25:37 -0700 | [diff] [blame] | 226 | absl::StrAppend(&output, " ", it->first, " ", it->second, "\n"); |
QUICHE team | 82dee2f | 2019-01-18 12:35:12 -0500 | [diff] [blame] | 227 | } |
bnc | 60cef58 | 2021-03-24 10:25:37 -0700 | [diff] [blame] | 228 | absl::StrAppend(&output, "}\n"); |
QUICHE team | 82dee2f | 2019-01-18 12:35:12 -0500 | [diff] [blame] | 229 | return output; |
| 230 | } |
| 231 | |
QUICHE team | 557dfbc | 2020-10-20 10:52:30 -0700 | [diff] [blame] | 232 | void Http2HeaderBlock::erase(absl::string_view key) { |
QUICHE team | 8e3bb9d | 2019-12-06 16:03:22 -0800 | [diff] [blame] | 233 | auto iter = map_.find(key); |
| 234 | if (iter != map_.end()) { |
QUICHE team | 1b5d09e | 2021-04-08 13:36:42 -0700 | [diff] [blame] | 235 | QUICHE_DVLOG(1) << "Erasing header with name: " << key; |
QUICHE team | 82dee2f | 2019-01-18 12:35:12 -0500 | [diff] [blame] | 236 | key_size_ -= key.size(); |
| 237 | value_size_ -= iter->second.SizeEstimate(); |
QUICHE team | 8e3bb9d | 2019-12-06 16:03:22 -0800 | [diff] [blame] | 238 | map_.erase(iter); |
QUICHE team | 82dee2f | 2019-01-18 12:35:12 -0500 | [diff] [blame] | 239 | } |
| 240 | } |
| 241 | |
QUICHE team | 557dfbc | 2020-10-20 10:52:30 -0700 | [diff] [blame] | 242 | void Http2HeaderBlock::clear() { |
QUICHE team | 82dee2f | 2019-01-18 12:35:12 -0500 | [diff] [blame] | 243 | key_size_ = 0; |
| 244 | value_size_ = 0; |
QUICHE team | 8e3bb9d | 2019-12-06 16:03:22 -0800 | [diff] [blame] | 245 | map_.clear(); |
QUICHE team | 0b74550 | 2019-12-10 11:02:08 -0800 | [diff] [blame] | 246 | storage_.Clear(); |
QUICHE team | 82dee2f | 2019-01-18 12:35:12 -0500 | [diff] [blame] | 247 | } |
| 248 | |
QUICHE team | 557dfbc | 2020-10-20 10:52:30 -0700 | [diff] [blame] | 249 | void Http2HeaderBlock::insert(const Http2HeaderBlock::value_type& value) { |
QUICHE team | 82dee2f | 2019-01-18 12:35:12 -0500 | [diff] [blame] | 250 | // TODO(birenroy): Write new value in place of old value, if it fits. |
| 251 | value_size_ += value.second.size(); |
| 252 | |
QUICHE team | 8e3bb9d | 2019-12-06 16:03:22 -0800 | [diff] [blame] | 253 | auto iter = map_.find(value.first); |
| 254 | if (iter == map_.end()) { |
QUICHE team | 1b5d09e | 2021-04-08 13:36:42 -0700 | [diff] [blame] | 255 | QUICHE_DVLOG(1) << "Inserting: (" << value.first << ", " << value.second |
| 256 | << ")"; |
QUICHE team | 82dee2f | 2019-01-18 12:35:12 -0500 | [diff] [blame] | 257 | AppendHeader(value.first, value.second); |
| 258 | } else { |
QUICHE team | 1b5d09e | 2021-04-08 13:36:42 -0700 | [diff] [blame] | 259 | QUICHE_DVLOG(1) << "Updating key: " << iter->first |
| 260 | << " with value: " << value.second; |
QUICHE team | 82dee2f | 2019-01-18 12:35:12 -0500 | [diff] [blame] | 261 | value_size_ -= iter->second.SizeEstimate(); |
QUICHE team | 82dee2f | 2019-01-18 12:35:12 -0500 | [diff] [blame] | 262 | iter->second = |
QUICHE team | 0b74550 | 2019-12-10 11:02:08 -0800 | [diff] [blame] | 263 | HeaderValue(&storage_, iter->first, storage_.Write(value.second)); |
QUICHE team | 82dee2f | 2019-01-18 12:35:12 -0500 | [diff] [blame] | 264 | } |
| 265 | } |
| 266 | |
QUICHE team | 557dfbc | 2020-10-20 10:52:30 -0700 | [diff] [blame] | 267 | Http2HeaderBlock::ValueProxy Http2HeaderBlock::operator[]( |
vasilvv | c8ccdb2 | 2020-10-12 16:42:34 -0700 | [diff] [blame] | 268 | const absl::string_view key) { |
QUICHE team | 1b5d09e | 2021-04-08 13:36:42 -0700 | [diff] [blame] | 269 | QUICHE_DVLOG(2) << "Operator[] saw key: " << key; |
vasilvv | c8ccdb2 | 2020-10-12 16:42:34 -0700 | [diff] [blame] | 270 | absl::string_view out_key; |
QUICHE team | 8e3bb9d | 2019-12-06 16:03:22 -0800 | [diff] [blame] | 271 | auto iter = map_.find(key); |
| 272 | if (iter == map_.end()) { |
QUICHE team | 82dee2f | 2019-01-18 12:35:12 -0500 | [diff] [blame] | 273 | // We write the key first, to assure that the ValueProxy has a |
vasilvv | c8ccdb2 | 2020-10-12 16:42:34 -0700 | [diff] [blame] | 274 | // reference to a valid absl::string_view in its operator=. |
QUICHE team | 82dee2f | 2019-01-18 12:35:12 -0500 | [diff] [blame] | 275 | out_key = WriteKey(key); |
QUICHE team | 1b5d09e | 2021-04-08 13:36:42 -0700 | [diff] [blame] | 276 | QUICHE_DVLOG(2) << "Key written as: " << std::hex |
| 277 | << static_cast<const void*>(key.data()) << ", " << std::dec |
| 278 | << key.size(); |
QUICHE team | 82dee2f | 2019-01-18 12:35:12 -0500 | [diff] [blame] | 279 | } else { |
| 280 | out_key = iter->first; |
| 281 | } |
QUICHE team | 8e3bb9d | 2019-12-06 16:03:22 -0800 | [diff] [blame] | 282 | return ValueProxy(this, iter, out_key, &value_size_); |
QUICHE team | 82dee2f | 2019-01-18 12:35:12 -0500 | [diff] [blame] | 283 | } |
| 284 | |
QUICHE team | 557dfbc | 2020-10-20 10:52:30 -0700 | [diff] [blame] | 285 | void Http2HeaderBlock::AppendValueOrAddHeader(const absl::string_view key, |
| 286 | const absl::string_view value) { |
QUICHE team | 82dee2f | 2019-01-18 12:35:12 -0500 | [diff] [blame] | 287 | value_size_ += value.size(); |
| 288 | |
QUICHE team | 8e3bb9d | 2019-12-06 16:03:22 -0800 | [diff] [blame] | 289 | auto iter = map_.find(key); |
| 290 | if (iter == map_.end()) { |
QUICHE team | 1b5d09e | 2021-04-08 13:36:42 -0700 | [diff] [blame] | 291 | QUICHE_DVLOG(1) << "Inserting: (" << key << ", " << value << ")"; |
QUICHE team | 82dee2f | 2019-01-18 12:35:12 -0500 | [diff] [blame] | 292 | |
| 293 | AppendHeader(key, value); |
| 294 | return; |
| 295 | } |
QUICHE team | 1b5d09e | 2021-04-08 13:36:42 -0700 | [diff] [blame] | 296 | QUICHE_DVLOG(1) << "Updating key: " << iter->first |
| 297 | << "; appending value: " << value; |
QUICHE team | 82dee2f | 2019-01-18 12:35:12 -0500 | [diff] [blame] | 298 | value_size_ += SeparatorForKey(key).size(); |
QUICHE team | 0b74550 | 2019-12-10 11:02:08 -0800 | [diff] [blame] | 299 | iter->second.Append(storage_.Write(value)); |
QUICHE team | 82dee2f | 2019-01-18 12:35:12 -0500 | [diff] [blame] | 300 | } |
| 301 | |
QUICHE team | 557dfbc | 2020-10-20 10:52:30 -0700 | [diff] [blame] | 302 | size_t Http2HeaderBlock::EstimateMemoryUsage() const { |
QUICHE team | 8e3bb9d | 2019-12-06 16:03:22 -0800 | [diff] [blame] | 303 | // TODO(xunjieli): https://crbug.com/669108. Also include |map_| when EMU() |
QUICHE team | 82dee2f | 2019-01-18 12:35:12 -0500 | [diff] [blame] | 304 | // supports linked_hash_map. |
| 305 | return SpdyEstimateMemoryUsage(storage_); |
| 306 | } |
| 307 | |
QUICHE team | 557dfbc | 2020-10-20 10:52:30 -0700 | [diff] [blame] | 308 | void Http2HeaderBlock::AppendHeader(const absl::string_view key, |
| 309 | const absl::string_view value) { |
QUICHE team | 82dee2f | 2019-01-18 12:35:12 -0500 | [diff] [blame] | 310 | auto backed_key = WriteKey(key); |
QUICHE team | 8e3bb9d | 2019-12-06 16:03:22 -0800 | [diff] [blame] | 311 | map_.emplace(std::make_pair( |
QUICHE team | 0b74550 | 2019-12-10 11:02:08 -0800 | [diff] [blame] | 312 | backed_key, HeaderValue(&storage_, backed_key, storage_.Write(value)))); |
QUICHE team | 82dee2f | 2019-01-18 12:35:12 -0500 | [diff] [blame] | 313 | } |
| 314 | |
QUICHE team | 557dfbc | 2020-10-20 10:52:30 -0700 | [diff] [blame] | 315 | absl::string_view Http2HeaderBlock::WriteKey(const absl::string_view key) { |
QUICHE team | 82dee2f | 2019-01-18 12:35:12 -0500 | [diff] [blame] | 316 | key_size_ += key.size(); |
QUICHE team | 0b74550 | 2019-12-10 11:02:08 -0800 | [diff] [blame] | 317 | return storage_.Write(key); |
QUICHE team | 82dee2f | 2019-01-18 12:35:12 -0500 | [diff] [blame] | 318 | } |
| 319 | |
QUICHE team | 557dfbc | 2020-10-20 10:52:30 -0700 | [diff] [blame] | 320 | size_t Http2HeaderBlock::bytes_allocated() const { |
QUICHE team | 0b74550 | 2019-12-10 11:02:08 -0800 | [diff] [blame] | 321 | return storage_.bytes_allocated(); |
QUICHE team | 82dee2f | 2019-01-18 12:35:12 -0500 | [diff] [blame] | 322 | } |
| 323 | |
QUICHE team | 82dee2f | 2019-01-18 12:35:12 -0500 | [diff] [blame] | 324 | } // namespace spdy |