blob: 8961b497e82e6296865b3aad40b52af176251b72 [file] [log] [blame]
QUICHE team82dee2f2019-01-18 12:35:12 -05001// 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 teambbce6032020-01-03 07:42:10 -080010#include <functional>
QUICHE team82dee2f2019-01-18 12:35:12 -050011#include <list>
bnc44712912019-08-15 18:58:14 -070012#include <string>
QUICHE team82dee2f2019-01-18 12:35:12 -050013#include <utility>
14#include <vector>
15
vasilvvc8ccdb22020-10-12 16:42:34 -070016#include "absl/strings/string_view.h"
bnc45ccf4b2020-01-21 19:05:27 -080017#include "net/third_party/quiche/src/common/platform/api/quiche_export.h"
QUICHE team6b297002019-12-09 09:37:56 -080018#include "net/third_party/quiche/src/spdy/core/spdy_header_storage.h"
QUICHE team82dee2f2019-01-18 12:35:12 -050019#include "net/third_party/quiche/src/spdy/platform/api/spdy_containers.h"
QUICHE team82dee2f2019-01-18 12:35:12 -050020#include "net/third_party/quiche/src/spdy/platform/api/spdy_macros.h"
QUICHE teama0795f02020-01-03 13:01:12 -080021#include "net/third_party/quiche/src/spdy/platform/api/spdy_string_utils.h"
QUICHE team82dee2f2019-01-18 12:35:12 -050022
23namespace spdy {
24
25namespace test {
QUICHE team1bd18d12020-10-21 08:36:48 -070026class Http2HeaderBlockPeer;
QUICHE team82dee2f2019-01-18 12:35:12 -050027class ValueProxyPeer;
28} // namespace test
29
dschinazib2b26152020-07-01 10:17:25 -070030#ifndef SPDY_HEADER_DEBUG
31#if !defined(NDEBUG) || defined(ADDRESS_SANITIZER)
32#define SPDY_HEADER_DEBUG 1
33#else // !defined(NDEBUG) || defined(ADDRESS_SANITIZER)
34#define SPDY_HEADER_DEBUG 0
35#endif // !defined(NDEBUG) || defined(ADDRESS_SANITIZER)
36#endif // SPDY_HEADER_DEBUG
37
QUICHE team82dee2f2019-01-18 12:35:12 -050038// This class provides a key-value map that can be used to store SPDY header
39// names and values. This data structure preserves insertion order.
40//
41// Under the hood, this data structure uses large, contiguous blocks of memory
vasilvvc8ccdb22020-10-12 16:42:34 -070042// to store names and values. Lookups may be performed with absl::string_view
43// keys, and values are returned as absl::string_views (via ValueProxy, below).
QUICHE team557dfbc2020-10-20 10:52:30 -070044// Value absl::string_views are valid as long as the Http2HeaderBlock exists;
45// allocated memory is never freed until Http2HeaderBlock's destruction.
QUICHE team82dee2f2019-01-18 12:35:12 -050046//
47// This implementation does not make much of an effort to minimize wasted space.
QUICHE team557dfbc2020-10-20 10:52:30 -070048// It's expected that keys are rarely deleted from a Http2HeaderBlock.
49class QUICHE_EXPORT_PRIVATE Http2HeaderBlock {
QUICHE team82dee2f2019-01-18 12:35:12 -050050 private:
QUICHE team82dee2f2019-01-18 12:35:12 -050051 // Stores a list of value fragments that can be joined later with a
52 // key-dependent separator.
bnc45ccf4b2020-01-21 19:05:27 -080053 class QUICHE_EXPORT_PRIVATE HeaderValue {
QUICHE team82dee2f2019-01-18 12:35:12 -050054 public:
QUICHE team6b297002019-12-09 09:37:56 -080055 HeaderValue(SpdyHeaderStorage* storage,
vasilvvc8ccdb22020-10-12 16:42:34 -070056 absl::string_view key,
57 absl::string_view initial_value);
QUICHE team82dee2f2019-01-18 12:35:12 -050058
59 // Moves are allowed.
60 HeaderValue(HeaderValue&& other);
61 HeaderValue& operator=(HeaderValue&& other);
62
QUICHE team0b745502019-12-10 11:02:08 -080063 void set_storage(SpdyHeaderStorage* storage);
64
QUICHE team82dee2f2019-01-18 12:35:12 -050065 // Copies are not.
66 HeaderValue(const HeaderValue& other) = delete;
67 HeaderValue& operator=(const HeaderValue& other) = delete;
68
69 ~HeaderValue();
70
71 // Consumes at most |fragment.size()| bytes of memory.
vasilvvc8ccdb22020-10-12 16:42:34 -070072 void Append(absl::string_view fragment);
QUICHE team82dee2f2019-01-18 12:35:12 -050073
vasilvvc8ccdb22020-10-12 16:42:34 -070074 absl::string_view value() const { return as_pair().second; }
75 const std::pair<absl::string_view, absl::string_view>& as_pair() const;
QUICHE team82dee2f2019-01-18 12:35:12 -050076
77 // Size estimate including separators. Used when keys are erased from
QUICHE team557dfbc2020-10-20 10:52:30 -070078 // Http2HeaderBlock.
QUICHE team82dee2f2019-01-18 12:35:12 -050079 size_t SizeEstimate() const { return size_; }
80
81 private:
82 // May allocate a large contiguous region of memory to hold the concatenated
83 // fragments and separators.
vasilvvc8ccdb22020-10-12 16:42:34 -070084 absl::string_view ConsolidatedValue() const;
QUICHE team82dee2f2019-01-18 12:35:12 -050085
QUICHE team6b297002019-12-09 09:37:56 -080086 mutable SpdyHeaderStorage* storage_;
vasilvvc8ccdb22020-10-12 16:42:34 -070087 mutable std::vector<absl::string_view> fragments_;
QUICHE team82dee2f2019-01-18 12:35:12 -050088 // The first element is the key; the second is the consolidated value.
vasilvvc8ccdb22020-10-12 16:42:34 -070089 mutable std::pair<absl::string_view, absl::string_view> pair_;
QUICHE team82dee2f2019-01-18 12:35:12 -050090 size_t size_ = 0;
91 size_t separator_size_ = 0;
92 };
93
vasilvvc8ccdb22020-10-12 16:42:34 -070094 typedef SpdyLinkedHashMap<absl::string_view,
QUICHE teambbce6032020-01-03 07:42:10 -080095 HeaderValue,
QUICHE teama0795f02020-01-03 13:01:12 -080096 SpdyStringPieceCaseHash,
97 SpdyStringPieceCaseEq>
QUICHE team82dee2f2019-01-18 12:35:12 -050098 MapType;
99
100 public:
vasilvvc8ccdb22020-10-12 16:42:34 -0700101 typedef std::pair<absl::string_view, absl::string_view> value_type;
QUICHE team82dee2f2019-01-18 12:35:12 -0500102
vasilvvc8ccdb22020-10-12 16:42:34 -0700103 // Provides iteration over a sequence of std::pair<absl::string_view,
104 // absl::string_view>, even though the underlying MapType::value_type is
QUICHE team82dee2f2019-01-18 12:35:12 -0500105 // different. Dereferencing the iterator will result in memory allocation for
106 // multi-value headers.
bnc45ccf4b2020-01-21 19:05:27 -0800107 class QUICHE_EXPORT_PRIVATE iterator {
QUICHE team82dee2f2019-01-18 12:35:12 -0500108 public:
109 // The following type definitions fulfill the requirements for iterator
110 // implementations.
vasilvvc8ccdb22020-10-12 16:42:34 -0700111 typedef std::pair<absl::string_view, absl::string_view> value_type;
QUICHE team82dee2f2019-01-18 12:35:12 -0500112 typedef value_type& reference;
113 typedef value_type* pointer;
114 typedef std::forward_iterator_tag iterator_category;
115 typedef MapType::iterator::difference_type difference_type;
116
117 // In practice, this iterator only offers access to const value_type.
118 typedef const value_type& const_reference;
119 typedef const value_type* const_pointer;
120
121 explicit iterator(MapType::const_iterator it);
122 iterator(const iterator& other);
123 ~iterator();
124
125 // This will result in memory allocation if the value consists of multiple
126 // fragments.
dschinazib2b26152020-07-01 10:17:25 -0700127 const_reference operator*() const {
128#if SPDY_HEADER_DEBUG
129 CHECK(!dereference_forbidden_);
130#endif // SPDY_HEADER_DEBUG
131 return it_->second.as_pair();
132 }
QUICHE team82dee2f2019-01-18 12:35:12 -0500133
134 const_pointer operator->() const { return &(this->operator*()); }
135 bool operator==(const iterator& it) const { return it_ == it.it_; }
136 bool operator!=(const iterator& it) const { return !(*this == it); }
137
138 iterator& operator++() {
139 it_++;
140 return *this;
141 }
142
143 iterator operator++(int) {
144 auto ret = *this;
145 this->operator++();
146 return ret;
147 }
148
dschinazib2b26152020-07-01 10:17:25 -0700149#if SPDY_HEADER_DEBUG
150 void forbid_dereference() { dereference_forbidden_ = true; }
151#endif // SPDY_HEADER_DEBUG
152
QUICHE team82dee2f2019-01-18 12:35:12 -0500153 private:
154 MapType::const_iterator it_;
dschinazib2b26152020-07-01 10:17:25 -0700155#if SPDY_HEADER_DEBUG
156 bool dereference_forbidden_ = false;
157#endif // SPDY_HEADER_DEBUG
QUICHE team82dee2f2019-01-18 12:35:12 -0500158 };
159 typedef iterator const_iterator;
160
QUICHE team557dfbc2020-10-20 10:52:30 -0700161 Http2HeaderBlock();
162 Http2HeaderBlock(const Http2HeaderBlock& other) = delete;
163 Http2HeaderBlock(Http2HeaderBlock&& other);
164 ~Http2HeaderBlock();
QUICHE team82dee2f2019-01-18 12:35:12 -0500165
QUICHE team557dfbc2020-10-20 10:52:30 -0700166 Http2HeaderBlock& operator=(const Http2HeaderBlock& other) = delete;
167 Http2HeaderBlock& operator=(Http2HeaderBlock&& other);
168 Http2HeaderBlock Clone() const;
QUICHE team82dee2f2019-01-18 12:35:12 -0500169
QUICHE team557dfbc2020-10-20 10:52:30 -0700170 bool operator==(const Http2HeaderBlock& other) const;
171 bool operator!=(const Http2HeaderBlock& other) const;
QUICHE team82dee2f2019-01-18 12:35:12 -0500172
173 // Provides a human readable multi-line representation of the stored header
174 // keys and values.
bnc44712912019-08-15 18:58:14 -0700175 std::string DebugString() const;
QUICHE team82dee2f2019-01-18 12:35:12 -0500176
dschinazib2b26152020-07-01 10:17:25 -0700177 iterator begin() { return wrap_iterator(map_.begin()); }
178 iterator end() { return wrap_iterator(map_.end()); }
179 const_iterator begin() const { return wrap_const_iterator(map_.begin()); }
180 const_iterator end() const { return wrap_const_iterator(map_.end()); }
QUICHE team8e3bb9d2019-12-06 16:03:22 -0800181 bool empty() const { return map_.empty(); }
182 size_t size() const { return map_.size(); }
vasilvvc8ccdb22020-10-12 16:42:34 -0700183 iterator find(absl::string_view key) { return wrap_iterator(map_.find(key)); }
184 const_iterator find(absl::string_view key) const {
dschinazib2b26152020-07-01 10:17:25 -0700185 return wrap_const_iterator(map_.find(key));
QUICHE team82dee2f2019-01-18 12:35:12 -0500186 }
vasilvvc8ccdb22020-10-12 16:42:34 -0700187 void erase(absl::string_view key);
QUICHE team82dee2f2019-01-18 12:35:12 -0500188
189 // Clears both our MapType member and the memory used to hold headers.
190 void clear();
191
192 // The next few methods copy data into our backing storage.
193
194 // If key already exists in the block, replaces the value of that key. Else
195 // adds a new header to the end of the block.
196 void insert(const value_type& value);
197
198 // If a header with the key is already present, then append the value to the
199 // existing header value, NUL ("\0") separated unless the key is cookie, in
200 // which case the separator is "; ".
201 // If there is no such key, a new header with the key and value is added.
vasilvvc8ccdb22020-10-12 16:42:34 -0700202 void AppendValueOrAddHeader(const absl::string_view key,
203 const absl::string_view value);
QUICHE team82dee2f2019-01-18 12:35:12 -0500204
QUICHE team557dfbc2020-10-20 10:52:30 -0700205 // This object provides automatic conversions that allow Http2HeaderBlock to
206 // be nearly a drop-in replacement for
bnc44712912019-08-15 18:58:14 -0700207 // SpdyLinkedHashMap<std::string, std::string>.
QUICHE team6b297002019-12-09 09:37:56 -0800208 // It reads data from or writes data to a SpdyHeaderStorage.
bnc45ccf4b2020-01-21 19:05:27 -0800209 class QUICHE_EXPORT_PRIVATE ValueProxy {
QUICHE team82dee2f2019-01-18 12:35:12 -0500210 public:
211 ~ValueProxy();
212
213 // Moves are allowed.
214 ValueProxy(ValueProxy&& other);
215 ValueProxy& operator=(ValueProxy&& other);
216
217 // Copies are not.
218 ValueProxy(const ValueProxy& other) = delete;
219 ValueProxy& operator=(const ValueProxy& other) = delete;
220
QUICHE team557dfbc2020-10-20 10:52:30 -0700221 // Assignment modifies the underlying Http2HeaderBlock.
vasilvvc8ccdb22020-10-12 16:42:34 -0700222 ValueProxy& operator=(absl::string_view value);
QUICHE team3f57c752019-12-16 14:09:53 -0800223
vasilvvc8ccdb22020-10-12 16:42:34 -0700224 // Provides easy comparison against absl::string_view.
225 bool operator==(absl::string_view value) const;
QUICHE team82dee2f2019-01-18 12:35:12 -0500226
bnc44712912019-08-15 18:58:14 -0700227 std::string as_string() const;
QUICHE team82dee2f2019-01-18 12:35:12 -0500228
229 private:
QUICHE team557dfbc2020-10-20 10:52:30 -0700230 friend class Http2HeaderBlock;
QUICHE team82dee2f2019-01-18 12:35:12 -0500231 friend class test::ValueProxyPeer;
232
QUICHE team557dfbc2020-10-20 10:52:30 -0700233 ValueProxy(Http2HeaderBlock* block,
234 Http2HeaderBlock::MapType::iterator lookup_result,
vasilvvc8ccdb22020-10-12 16:42:34 -0700235 const absl::string_view key,
QUICHE team82dee2f2019-01-18 12:35:12 -0500236 size_t* spdy_header_block_value_size);
237
QUICHE team557dfbc2020-10-20 10:52:30 -0700238 Http2HeaderBlock* block_;
239 Http2HeaderBlock::MapType::iterator lookup_result_;
vasilvvc8ccdb22020-10-12 16:42:34 -0700240 absl::string_view key_;
QUICHE team82dee2f2019-01-18 12:35:12 -0500241 size_t* spdy_header_block_value_size_;
242 bool valid_;
243 };
244
QUICHE team8e3bb9d2019-12-06 16:03:22 -0800245 // Allows either lookup or mutation of the value associated with a key.
vasilvvc8ccdb22020-10-12 16:42:34 -0700246 SPDY_MUST_USE_RESULT ValueProxy operator[](const absl::string_view key);
QUICHE team8e3bb9d2019-12-06 16:03:22 -0800247
QUICHE team82dee2f2019-01-18 12:35:12 -0500248 // Returns the estimate of dynamically allocated memory in bytes.
249 size_t EstimateMemoryUsage() const;
250
251 size_t TotalBytesUsed() const { return key_size_ + value_size_; }
252
253 private:
QUICHE team1bd18d12020-10-21 08:36:48 -0700254 friend class test::Http2HeaderBlockPeer;
QUICHE team82dee2f2019-01-18 12:35:12 -0500255
dschinazib2b26152020-07-01 10:17:25 -0700256 inline iterator wrap_iterator(MapType::const_iterator inner_iterator) const {
257#if SPDY_HEADER_DEBUG
258 iterator outer_iterator(inner_iterator);
259 if (inner_iterator == map_.end()) {
260 outer_iterator.forbid_dereference();
261 }
262 return outer_iterator;
263#else // SPDY_HEADER_DEBUG
264 return iterator(inner_iterator);
265#endif // SPDY_HEADER_DEBUG
266 }
267
268 inline const_iterator wrap_const_iterator(
269 MapType::const_iterator inner_iterator) const {
270#if SPDY_HEADER_DEBUG
271 const_iterator outer_iterator(inner_iterator);
272 if (inner_iterator == map_.end()) {
273 outer_iterator.forbid_dereference();
274 }
275 return outer_iterator;
276#else // SPDY_HEADER_DEBUG
277 return iterator(inner_iterator);
278#endif // SPDY_HEADER_DEBUG
279 }
280
vasilvvc8ccdb22020-10-12 16:42:34 -0700281 void AppendHeader(const absl::string_view key, const absl::string_view value);
282 absl::string_view WriteKey(const absl::string_view key);
QUICHE team82dee2f2019-01-18 12:35:12 -0500283 size_t bytes_allocated() const;
284
vasilvvc8ccdb22020-10-12 16:42:34 -0700285 // absl::string_views held by |map_| point to memory owned by |storage_|.
QUICHE team8e3bb9d2019-12-06 16:03:22 -0800286 MapType map_;
QUICHE team0b745502019-12-10 11:02:08 -0800287 SpdyHeaderStorage storage_;
QUICHE team82dee2f2019-01-18 12:35:12 -0500288
289 size_t key_size_ = 0;
290 size_t value_size_ = 0;
291};
292
QUICHE team557dfbc2020-10-20 10:52:30 -0700293// TODO(b/156770486): Remove this alias when the rename is complete.
294using SpdyHeaderBlock = Http2HeaderBlock;
295
QUICHE team82dee2f2019-01-18 12:35:12 -0500296} // namespace spdy
297
298#endif // QUICHE_SPDY_CORE_SPDY_HEADER_BLOCK_H_