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 | |
| 5 | #ifndef QUICHE_SPDY_CORE_SPDY_HEADER_BLOCK_H_ |
| 6 | #define QUICHE_SPDY_CORE_SPDY_HEADER_BLOCK_H_ |
| 7 | |
| 8 | #include <stddef.h> |
| 9 | |
QUICHE team | bbce603 | 2020-01-03 07:42:10 -0800 | [diff] [blame] | 10 | #include <functional> |
QUICHE team | 82dee2f | 2019-01-18 12:35:12 -0500 | [diff] [blame] | 11 | #include <list> |
bnc | 4471291 | 2019-08-15 18:58:14 -0700 | [diff] [blame] | 12 | #include <string> |
QUICHE team | 82dee2f | 2019-01-18 12:35:12 -0500 | [diff] [blame] | 13 | #include <utility> |
| 14 | #include <vector> |
| 15 | |
bnc | 7f82d04 | 2020-01-03 12:18:53 -0800 | [diff] [blame] | 16 | #include "net/third_party/quiche/src/common/platform/api/quiche_string_piece.h" |
QUICHE team | 6b29700 | 2019-12-09 09:37:56 -0800 | [diff] [blame] | 17 | #include "net/third_party/quiche/src/spdy/core/spdy_header_storage.h" |
QUICHE team | 82dee2f | 2019-01-18 12:35:12 -0500 | [diff] [blame] | 18 | #include "net/third_party/quiche/src/spdy/platform/api/spdy_containers.h" |
| 19 | #include "net/third_party/quiche/src/spdy/platform/api/spdy_export.h" |
| 20 | #include "net/third_party/quiche/src/spdy/platform/api/spdy_macros.h" |
QUICHE team | a0795f0 | 2020-01-03 13:01:12 -0800 | [diff] [blame] | 21 | #include "net/third_party/quiche/src/spdy/platform/api/spdy_string_utils.h" |
QUICHE team | 82dee2f | 2019-01-18 12:35:12 -0500 | [diff] [blame] | 22 | |
| 23 | namespace spdy { |
| 24 | |
| 25 | namespace test { |
| 26 | class SpdyHeaderBlockPeer; |
| 27 | class ValueProxyPeer; |
| 28 | } // namespace test |
| 29 | |
| 30 | // This class provides a key-value map that can be used to store SPDY header |
| 31 | // names and values. This data structure preserves insertion order. |
| 32 | // |
| 33 | // Under the hood, this data structure uses large, contiguous blocks of memory |
bnc | 7f82d04 | 2020-01-03 12:18:53 -0800 | [diff] [blame] | 34 | // to store names and values. Lookups may be performed with QuicheStringPiece |
| 35 | // keys, and values are returned as QuicheStringPieces (via ValueProxy, below). |
| 36 | // Value QuicheStringPieces are valid as long as the SpdyHeaderBlock exists; |
QUICHE team | 82dee2f | 2019-01-18 12:35:12 -0500 | [diff] [blame] | 37 | // allocated memory is never freed until SpdyHeaderBlock's destruction. |
| 38 | // |
| 39 | // This implementation does not make much of an effort to minimize wasted space. |
| 40 | // It's expected that keys are rarely deleted from a SpdyHeaderBlock. |
| 41 | class SPDY_EXPORT_PRIVATE SpdyHeaderBlock { |
| 42 | private: |
QUICHE team | 82dee2f | 2019-01-18 12:35:12 -0500 | [diff] [blame] | 43 | // Stores a list of value fragments that can be joined later with a |
| 44 | // key-dependent separator. |
| 45 | class SPDY_EXPORT_PRIVATE HeaderValue { |
| 46 | public: |
QUICHE team | 6b29700 | 2019-12-09 09:37:56 -0800 | [diff] [blame] | 47 | HeaderValue(SpdyHeaderStorage* storage, |
bnc | 7f82d04 | 2020-01-03 12:18:53 -0800 | [diff] [blame] | 48 | quiche::QuicheStringPiece key, |
| 49 | quiche::QuicheStringPiece initial_value); |
QUICHE team | 82dee2f | 2019-01-18 12:35:12 -0500 | [diff] [blame] | 50 | |
| 51 | // Moves are allowed. |
| 52 | HeaderValue(HeaderValue&& other); |
| 53 | HeaderValue& operator=(HeaderValue&& other); |
| 54 | |
QUICHE team | 0b74550 | 2019-12-10 11:02:08 -0800 | [diff] [blame] | 55 | void set_storage(SpdyHeaderStorage* storage); |
| 56 | |
QUICHE team | 82dee2f | 2019-01-18 12:35:12 -0500 | [diff] [blame] | 57 | // Copies are not. |
| 58 | HeaderValue(const HeaderValue& other) = delete; |
| 59 | HeaderValue& operator=(const HeaderValue& other) = delete; |
| 60 | |
| 61 | ~HeaderValue(); |
| 62 | |
| 63 | // Consumes at most |fragment.size()| bytes of memory. |
bnc | 7f82d04 | 2020-01-03 12:18:53 -0800 | [diff] [blame] | 64 | void Append(quiche::QuicheStringPiece fragment); |
QUICHE team | 82dee2f | 2019-01-18 12:35:12 -0500 | [diff] [blame] | 65 | |
bnc | 7f82d04 | 2020-01-03 12:18:53 -0800 | [diff] [blame] | 66 | quiche::QuicheStringPiece value() const { return as_pair().second; } |
| 67 | const std::pair<quiche::QuicheStringPiece, quiche::QuicheStringPiece>& |
| 68 | as_pair() const; |
QUICHE team | 82dee2f | 2019-01-18 12:35:12 -0500 | [diff] [blame] | 69 | |
| 70 | // Size estimate including separators. Used when keys are erased from |
| 71 | // SpdyHeaderBlock. |
| 72 | size_t SizeEstimate() const { return size_; } |
| 73 | |
| 74 | private: |
| 75 | // May allocate a large contiguous region of memory to hold the concatenated |
| 76 | // fragments and separators. |
bnc | 7f82d04 | 2020-01-03 12:18:53 -0800 | [diff] [blame] | 77 | quiche::QuicheStringPiece ConsolidatedValue() const; |
QUICHE team | 82dee2f | 2019-01-18 12:35:12 -0500 | [diff] [blame] | 78 | |
QUICHE team | 6b29700 | 2019-12-09 09:37:56 -0800 | [diff] [blame] | 79 | mutable SpdyHeaderStorage* storage_; |
bnc | 7f82d04 | 2020-01-03 12:18:53 -0800 | [diff] [blame] | 80 | mutable std::vector<quiche::QuicheStringPiece> fragments_; |
QUICHE team | 82dee2f | 2019-01-18 12:35:12 -0500 | [diff] [blame] | 81 | // The first element is the key; the second is the consolidated value. |
bnc | 7f82d04 | 2020-01-03 12:18:53 -0800 | [diff] [blame] | 82 | mutable std::pair<quiche::QuicheStringPiece, quiche::QuicheStringPiece> |
| 83 | pair_; |
QUICHE team | 82dee2f | 2019-01-18 12:35:12 -0500 | [diff] [blame] | 84 | size_t size_ = 0; |
| 85 | size_t separator_size_ = 0; |
| 86 | }; |
| 87 | |
bnc | 7f82d04 | 2020-01-03 12:18:53 -0800 | [diff] [blame] | 88 | typedef SpdyLinkedHashMap<quiche::QuicheStringPiece, |
QUICHE team | bbce603 | 2020-01-03 07:42:10 -0800 | [diff] [blame] | 89 | HeaderValue, |
QUICHE team | a0795f0 | 2020-01-03 13:01:12 -0800 | [diff] [blame] | 90 | SpdyStringPieceCaseHash, |
| 91 | SpdyStringPieceCaseEq> |
QUICHE team | 82dee2f | 2019-01-18 12:35:12 -0500 | [diff] [blame] | 92 | MapType; |
| 93 | |
| 94 | public: |
bnc | 7f82d04 | 2020-01-03 12:18:53 -0800 | [diff] [blame] | 95 | typedef std::pair<quiche::QuicheStringPiece, quiche::QuicheStringPiece> |
| 96 | value_type; |
QUICHE team | 82dee2f | 2019-01-18 12:35:12 -0500 | [diff] [blame] | 97 | |
bnc | 7f82d04 | 2020-01-03 12:18:53 -0800 | [diff] [blame] | 98 | // Provides iteration over a sequence of std::pair<QuicheStringPiece, |
| 99 | // QuicheStringPiece>, even though the underlying MapType::value_type is |
QUICHE team | 82dee2f | 2019-01-18 12:35:12 -0500 | [diff] [blame] | 100 | // different. Dereferencing the iterator will result in memory allocation for |
| 101 | // multi-value headers. |
| 102 | class SPDY_EXPORT_PRIVATE iterator { |
| 103 | public: |
| 104 | // The following type definitions fulfill the requirements for iterator |
| 105 | // implementations. |
bnc | 7f82d04 | 2020-01-03 12:18:53 -0800 | [diff] [blame] | 106 | typedef std::pair<quiche::QuicheStringPiece, quiche::QuicheStringPiece> |
| 107 | value_type; |
QUICHE team | 82dee2f | 2019-01-18 12:35:12 -0500 | [diff] [blame] | 108 | typedef value_type& reference; |
| 109 | typedef value_type* pointer; |
| 110 | typedef std::forward_iterator_tag iterator_category; |
| 111 | typedef MapType::iterator::difference_type difference_type; |
| 112 | |
| 113 | // In practice, this iterator only offers access to const value_type. |
| 114 | typedef const value_type& const_reference; |
| 115 | typedef const value_type* const_pointer; |
| 116 | |
| 117 | explicit iterator(MapType::const_iterator it); |
| 118 | iterator(const iterator& other); |
| 119 | ~iterator(); |
| 120 | |
| 121 | // This will result in memory allocation if the value consists of multiple |
| 122 | // fragments. |
| 123 | const_reference operator*() const { return it_->second.as_pair(); } |
| 124 | |
| 125 | const_pointer operator->() const { return &(this->operator*()); } |
| 126 | bool operator==(const iterator& it) const { return it_ == it.it_; } |
| 127 | bool operator!=(const iterator& it) const { return !(*this == it); } |
| 128 | |
| 129 | iterator& operator++() { |
| 130 | it_++; |
| 131 | return *this; |
| 132 | } |
| 133 | |
| 134 | iterator operator++(int) { |
| 135 | auto ret = *this; |
| 136 | this->operator++(); |
| 137 | return ret; |
| 138 | } |
| 139 | |
| 140 | private: |
| 141 | MapType::const_iterator it_; |
| 142 | }; |
| 143 | typedef iterator const_iterator; |
| 144 | |
QUICHE team | 82dee2f | 2019-01-18 12:35:12 -0500 | [diff] [blame] | 145 | SpdyHeaderBlock(); |
| 146 | SpdyHeaderBlock(const SpdyHeaderBlock& other) = delete; |
| 147 | SpdyHeaderBlock(SpdyHeaderBlock&& other); |
| 148 | ~SpdyHeaderBlock(); |
| 149 | |
| 150 | SpdyHeaderBlock& operator=(const SpdyHeaderBlock& other) = delete; |
| 151 | SpdyHeaderBlock& operator=(SpdyHeaderBlock&& other); |
| 152 | SpdyHeaderBlock Clone() const; |
| 153 | |
| 154 | bool operator==(const SpdyHeaderBlock& other) const; |
| 155 | bool operator!=(const SpdyHeaderBlock& other) const; |
| 156 | |
| 157 | // Provides a human readable multi-line representation of the stored header |
| 158 | // keys and values. |
bnc | 4471291 | 2019-08-15 18:58:14 -0700 | [diff] [blame] | 159 | std::string DebugString() const; |
QUICHE team | 82dee2f | 2019-01-18 12:35:12 -0500 | [diff] [blame] | 160 | |
QUICHE team | 8e3bb9d | 2019-12-06 16:03:22 -0800 | [diff] [blame] | 161 | iterator begin() { return iterator(map_.begin()); } |
| 162 | iterator end() { return iterator(map_.end()); } |
| 163 | const_iterator begin() const { return const_iterator(map_.begin()); } |
| 164 | const_iterator end() const { return const_iterator(map_.end()); } |
| 165 | bool empty() const { return map_.empty(); } |
| 166 | size_t size() const { return map_.size(); } |
bnc | 7f82d04 | 2020-01-03 12:18:53 -0800 | [diff] [blame] | 167 | iterator find(quiche::QuicheStringPiece key) { |
| 168 | return iterator(map_.find(key)); |
| 169 | } |
| 170 | const_iterator find(quiche::QuicheStringPiece key) const { |
QUICHE team | 8e3bb9d | 2019-12-06 16:03:22 -0800 | [diff] [blame] | 171 | return const_iterator(map_.find(key)); |
QUICHE team | 82dee2f | 2019-01-18 12:35:12 -0500 | [diff] [blame] | 172 | } |
bnc | 7f82d04 | 2020-01-03 12:18:53 -0800 | [diff] [blame] | 173 | void erase(quiche::QuicheStringPiece key); |
QUICHE team | 82dee2f | 2019-01-18 12:35:12 -0500 | [diff] [blame] | 174 | |
| 175 | // Clears both our MapType member and the memory used to hold headers. |
| 176 | void clear(); |
| 177 | |
| 178 | // The next few methods copy data into our backing storage. |
| 179 | |
| 180 | // If key already exists in the block, replaces the value of that key. Else |
| 181 | // adds a new header to the end of the block. |
| 182 | void insert(const value_type& value); |
| 183 | |
| 184 | // If a header with the key is already present, then append the value to the |
| 185 | // existing header value, NUL ("\0") separated unless the key is cookie, in |
| 186 | // which case the separator is "; ". |
| 187 | // If there is no such key, a new header with the key and value is added. |
bnc | 7f82d04 | 2020-01-03 12:18:53 -0800 | [diff] [blame] | 188 | void AppendValueOrAddHeader(const quiche::QuicheStringPiece key, |
| 189 | const quiche::QuicheStringPiece value); |
QUICHE team | 82dee2f | 2019-01-18 12:35:12 -0500 | [diff] [blame] | 190 | |
QUICHE team | 82dee2f | 2019-01-18 12:35:12 -0500 | [diff] [blame] | 191 | // This object provides automatic conversions that allow SpdyHeaderBlock to be |
bnc | 4471291 | 2019-08-15 18:58:14 -0700 | [diff] [blame] | 192 | // nearly a drop-in replacement for |
| 193 | // SpdyLinkedHashMap<std::string, std::string>. |
QUICHE team | 6b29700 | 2019-12-09 09:37:56 -0800 | [diff] [blame] | 194 | // It reads data from or writes data to a SpdyHeaderStorage. |
QUICHE team | 82dee2f | 2019-01-18 12:35:12 -0500 | [diff] [blame] | 195 | class SPDY_EXPORT_PRIVATE ValueProxy { |
| 196 | public: |
| 197 | ~ValueProxy(); |
| 198 | |
| 199 | // Moves are allowed. |
| 200 | ValueProxy(ValueProxy&& other); |
| 201 | ValueProxy& operator=(ValueProxy&& other); |
| 202 | |
| 203 | // Copies are not. |
| 204 | ValueProxy(const ValueProxy& other) = delete; |
| 205 | ValueProxy& operator=(const ValueProxy& other) = delete; |
| 206 | |
| 207 | // Assignment modifies the underlying SpdyHeaderBlock. |
bnc | 7f82d04 | 2020-01-03 12:18:53 -0800 | [diff] [blame] | 208 | ValueProxy& operator=(quiche::QuicheStringPiece value); |
QUICHE team | 3f57c75 | 2019-12-16 14:09:53 -0800 | [diff] [blame] | 209 | |
bnc | 7f82d04 | 2020-01-03 12:18:53 -0800 | [diff] [blame] | 210 | // Provides easy comparison against QuicheStringPiece. |
| 211 | bool operator==(quiche::QuicheStringPiece value) const; |
QUICHE team | 82dee2f | 2019-01-18 12:35:12 -0500 | [diff] [blame] | 212 | |
bnc | 4471291 | 2019-08-15 18:58:14 -0700 | [diff] [blame] | 213 | std::string as_string() const; |
QUICHE team | 82dee2f | 2019-01-18 12:35:12 -0500 | [diff] [blame] | 214 | |
| 215 | private: |
| 216 | friend class SpdyHeaderBlock; |
| 217 | friend class test::ValueProxyPeer; |
| 218 | |
QUICHE team | 8e3bb9d | 2019-12-06 16:03:22 -0800 | [diff] [blame] | 219 | ValueProxy(SpdyHeaderBlock* block, |
QUICHE team | 82dee2f | 2019-01-18 12:35:12 -0500 | [diff] [blame] | 220 | SpdyHeaderBlock::MapType::iterator lookup_result, |
bnc | 7f82d04 | 2020-01-03 12:18:53 -0800 | [diff] [blame] | 221 | const quiche::QuicheStringPiece key, |
QUICHE team | 82dee2f | 2019-01-18 12:35:12 -0500 | [diff] [blame] | 222 | size_t* spdy_header_block_value_size); |
| 223 | |
QUICHE team | 8e3bb9d | 2019-12-06 16:03:22 -0800 | [diff] [blame] | 224 | SpdyHeaderBlock* block_; |
QUICHE team | 82dee2f | 2019-01-18 12:35:12 -0500 | [diff] [blame] | 225 | SpdyHeaderBlock::MapType::iterator lookup_result_; |
bnc | 7f82d04 | 2020-01-03 12:18:53 -0800 | [diff] [blame] | 226 | quiche::QuicheStringPiece key_; |
QUICHE team | 82dee2f | 2019-01-18 12:35:12 -0500 | [diff] [blame] | 227 | size_t* spdy_header_block_value_size_; |
| 228 | bool valid_; |
| 229 | }; |
| 230 | |
QUICHE team | 8e3bb9d | 2019-12-06 16:03:22 -0800 | [diff] [blame] | 231 | // Allows either lookup or mutation of the value associated with a key. |
bnc | 7f82d04 | 2020-01-03 12:18:53 -0800 | [diff] [blame] | 232 | SPDY_MUST_USE_RESULT ValueProxy |
| 233 | operator[](const quiche::QuicheStringPiece key); |
QUICHE team | 8e3bb9d | 2019-12-06 16:03:22 -0800 | [diff] [blame] | 234 | |
QUICHE team | 82dee2f | 2019-01-18 12:35:12 -0500 | [diff] [blame] | 235 | // Returns the estimate of dynamically allocated memory in bytes. |
| 236 | size_t EstimateMemoryUsage() const; |
| 237 | |
| 238 | size_t TotalBytesUsed() const { return key_size_ + value_size_; } |
| 239 | |
| 240 | private: |
| 241 | friend class test::SpdyHeaderBlockPeer; |
| 242 | |
bnc | 7f82d04 | 2020-01-03 12:18:53 -0800 | [diff] [blame] | 243 | void AppendHeader(const quiche::QuicheStringPiece key, |
| 244 | const quiche::QuicheStringPiece value); |
| 245 | quiche::QuicheStringPiece WriteKey(const quiche::QuicheStringPiece key); |
QUICHE team | 82dee2f | 2019-01-18 12:35:12 -0500 | [diff] [blame] | 246 | size_t bytes_allocated() const; |
| 247 | |
bnc | 7f82d04 | 2020-01-03 12:18:53 -0800 | [diff] [blame] | 248 | // QuicheStringPieces held by |map_| point to memory owned by |storage_|. |
QUICHE team | 8e3bb9d | 2019-12-06 16:03:22 -0800 | [diff] [blame] | 249 | MapType map_; |
QUICHE team | 0b74550 | 2019-12-10 11:02:08 -0800 | [diff] [blame] | 250 | SpdyHeaderStorage storage_; |
QUICHE team | 82dee2f | 2019-01-18 12:35:12 -0500 | [diff] [blame] | 251 | |
| 252 | size_t key_size_ = 0; |
| 253 | size_t value_size_ = 0; |
| 254 | }; |
| 255 | |
QUICHE team | 82dee2f | 2019-01-18 12:35:12 -0500 | [diff] [blame] | 256 | } // namespace spdy |
| 257 | |
| 258 | #endif // QUICHE_SPDY_CORE_SPDY_HEADER_BLOCK_H_ |