blob: 2348551f5671e8d64e93d7bc00f104d881d88e4c [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
bnc45ccf4b2020-01-21 19:05:27 -080016#include "net/third_party/quiche/src/common/platform/api/quiche_export.h"
bnc7f82d042020-01-03 12:18:53 -080017#include "net/third_party/quiche/src/common/platform/api/quiche_string_piece.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 {
26class SpdyHeaderBlockPeer;
27class 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
bnc7f82d042020-01-03 12:18:53 -080042// to store names and values. Lookups may be performed with QuicheStringPiece
43// keys, and values are returned as QuicheStringPieces (via ValueProxy, below).
44// Value QuicheStringPieces are valid as long as the SpdyHeaderBlock exists;
QUICHE team82dee2f2019-01-18 12:35:12 -050045// allocated memory is never freed until SpdyHeaderBlock's destruction.
46//
47// This implementation does not make much of an effort to minimize wasted space.
48// It's expected that keys are rarely deleted from a SpdyHeaderBlock.
bnc45ccf4b2020-01-21 19:05:27 -080049class QUICHE_EXPORT_PRIVATE SpdyHeaderBlock {
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,
bnc7f82d042020-01-03 12:18:53 -080056 quiche::QuicheStringPiece key,
57 quiche::QuicheStringPiece 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.
bnc7f82d042020-01-03 12:18:53 -080072 void Append(quiche::QuicheStringPiece fragment);
QUICHE team82dee2f2019-01-18 12:35:12 -050073
bnc7f82d042020-01-03 12:18:53 -080074 quiche::QuicheStringPiece value() const { return as_pair().second; }
75 const std::pair<quiche::QuicheStringPiece, quiche::QuicheStringPiece>&
76 as_pair() const;
QUICHE team82dee2f2019-01-18 12:35:12 -050077
78 // Size estimate including separators. Used when keys are erased from
79 // SpdyHeaderBlock.
80 size_t SizeEstimate() const { return size_; }
81
82 private:
83 // May allocate a large contiguous region of memory to hold the concatenated
84 // fragments and separators.
bnc7f82d042020-01-03 12:18:53 -080085 quiche::QuicheStringPiece ConsolidatedValue() const;
QUICHE team82dee2f2019-01-18 12:35:12 -050086
QUICHE team6b297002019-12-09 09:37:56 -080087 mutable SpdyHeaderStorage* storage_;
bnc7f82d042020-01-03 12:18:53 -080088 mutable std::vector<quiche::QuicheStringPiece> fragments_;
QUICHE team82dee2f2019-01-18 12:35:12 -050089 // The first element is the key; the second is the consolidated value.
bnc7f82d042020-01-03 12:18:53 -080090 mutable std::pair<quiche::QuicheStringPiece, quiche::QuicheStringPiece>
91 pair_;
QUICHE team82dee2f2019-01-18 12:35:12 -050092 size_t size_ = 0;
93 size_t separator_size_ = 0;
94 };
95
bnc7f82d042020-01-03 12:18:53 -080096 typedef SpdyLinkedHashMap<quiche::QuicheStringPiece,
QUICHE teambbce6032020-01-03 07:42:10 -080097 HeaderValue,
QUICHE teama0795f02020-01-03 13:01:12 -080098 SpdyStringPieceCaseHash,
99 SpdyStringPieceCaseEq>
QUICHE team82dee2f2019-01-18 12:35:12 -0500100 MapType;
101
102 public:
bnc7f82d042020-01-03 12:18:53 -0800103 typedef std::pair<quiche::QuicheStringPiece, quiche::QuicheStringPiece>
104 value_type;
QUICHE team82dee2f2019-01-18 12:35:12 -0500105
bnc7f82d042020-01-03 12:18:53 -0800106 // Provides iteration over a sequence of std::pair<QuicheStringPiece,
107 // QuicheStringPiece>, even though the underlying MapType::value_type is
QUICHE team82dee2f2019-01-18 12:35:12 -0500108 // different. Dereferencing the iterator will result in memory allocation for
109 // multi-value headers.
bnc45ccf4b2020-01-21 19:05:27 -0800110 class QUICHE_EXPORT_PRIVATE iterator {
QUICHE team82dee2f2019-01-18 12:35:12 -0500111 public:
112 // The following type definitions fulfill the requirements for iterator
113 // implementations.
bnc7f82d042020-01-03 12:18:53 -0800114 typedef std::pair<quiche::QuicheStringPiece, quiche::QuicheStringPiece>
115 value_type;
QUICHE team82dee2f2019-01-18 12:35:12 -0500116 typedef value_type& reference;
117 typedef value_type* pointer;
118 typedef std::forward_iterator_tag iterator_category;
119 typedef MapType::iterator::difference_type difference_type;
120
121 // In practice, this iterator only offers access to const value_type.
122 typedef const value_type& const_reference;
123 typedef const value_type* const_pointer;
124
125 explicit iterator(MapType::const_iterator it);
126 iterator(const iterator& other);
127 ~iterator();
128
129 // This will result in memory allocation if the value consists of multiple
130 // fragments.
dschinazib2b26152020-07-01 10:17:25 -0700131 const_reference operator*() const {
132#if SPDY_HEADER_DEBUG
133 CHECK(!dereference_forbidden_);
134#endif // SPDY_HEADER_DEBUG
135 return it_->second.as_pair();
136 }
QUICHE team82dee2f2019-01-18 12:35:12 -0500137
138 const_pointer operator->() const { return &(this->operator*()); }
139 bool operator==(const iterator& it) const { return it_ == it.it_; }
140 bool operator!=(const iterator& it) const { return !(*this == it); }
141
142 iterator& operator++() {
143 it_++;
144 return *this;
145 }
146
147 iterator operator++(int) {
148 auto ret = *this;
149 this->operator++();
150 return ret;
151 }
152
dschinazib2b26152020-07-01 10:17:25 -0700153#if SPDY_HEADER_DEBUG
154 void forbid_dereference() { dereference_forbidden_ = true; }
155#endif // SPDY_HEADER_DEBUG
156
QUICHE team82dee2f2019-01-18 12:35:12 -0500157 private:
158 MapType::const_iterator it_;
dschinazib2b26152020-07-01 10:17:25 -0700159#if SPDY_HEADER_DEBUG
160 bool dereference_forbidden_ = false;
161#endif // SPDY_HEADER_DEBUG
QUICHE team82dee2f2019-01-18 12:35:12 -0500162 };
163 typedef iterator const_iterator;
164
QUICHE team82dee2f2019-01-18 12:35:12 -0500165 SpdyHeaderBlock();
166 SpdyHeaderBlock(const SpdyHeaderBlock& other) = delete;
167 SpdyHeaderBlock(SpdyHeaderBlock&& other);
168 ~SpdyHeaderBlock();
169
170 SpdyHeaderBlock& operator=(const SpdyHeaderBlock& other) = delete;
171 SpdyHeaderBlock& operator=(SpdyHeaderBlock&& other);
172 SpdyHeaderBlock Clone() const;
173
174 bool operator==(const SpdyHeaderBlock& other) const;
175 bool operator!=(const SpdyHeaderBlock& other) const;
176
177 // Provides a human readable multi-line representation of the stored header
178 // keys and values.
bnc44712912019-08-15 18:58:14 -0700179 std::string DebugString() const;
QUICHE team82dee2f2019-01-18 12:35:12 -0500180
dschinazib2b26152020-07-01 10:17:25 -0700181 iterator begin() { return wrap_iterator(map_.begin()); }
182 iterator end() { return wrap_iterator(map_.end()); }
183 const_iterator begin() const { return wrap_const_iterator(map_.begin()); }
184 const_iterator end() const { return wrap_const_iterator(map_.end()); }
QUICHE team8e3bb9d2019-12-06 16:03:22 -0800185 bool empty() const { return map_.empty(); }
186 size_t size() const { return map_.size(); }
bnc7f82d042020-01-03 12:18:53 -0800187 iterator find(quiche::QuicheStringPiece key) {
dschinazib2b26152020-07-01 10:17:25 -0700188 return wrap_iterator(map_.find(key));
bnc7f82d042020-01-03 12:18:53 -0800189 }
190 const_iterator find(quiche::QuicheStringPiece key) const {
dschinazib2b26152020-07-01 10:17:25 -0700191 return wrap_const_iterator(map_.find(key));
QUICHE team82dee2f2019-01-18 12:35:12 -0500192 }
bnc7f82d042020-01-03 12:18:53 -0800193 void erase(quiche::QuicheStringPiece key);
QUICHE team82dee2f2019-01-18 12:35:12 -0500194
195 // Clears both our MapType member and the memory used to hold headers.
196 void clear();
197
198 // The next few methods copy data into our backing storage.
199
200 // If key already exists in the block, replaces the value of that key. Else
201 // adds a new header to the end of the block.
202 void insert(const value_type& value);
203
204 // If a header with the key is already present, then append the value to the
205 // existing header value, NUL ("\0") separated unless the key is cookie, in
206 // which case the separator is "; ".
207 // If there is no such key, a new header with the key and value is added.
bnc7f82d042020-01-03 12:18:53 -0800208 void AppendValueOrAddHeader(const quiche::QuicheStringPiece key,
209 const quiche::QuicheStringPiece value);
QUICHE team82dee2f2019-01-18 12:35:12 -0500210
QUICHE team82dee2f2019-01-18 12:35:12 -0500211 // This object provides automatic conversions that allow SpdyHeaderBlock to be
bnc44712912019-08-15 18:58:14 -0700212 // nearly a drop-in replacement for
213 // SpdyLinkedHashMap<std::string, std::string>.
QUICHE team6b297002019-12-09 09:37:56 -0800214 // It reads data from or writes data to a SpdyHeaderStorage.
bnc45ccf4b2020-01-21 19:05:27 -0800215 class QUICHE_EXPORT_PRIVATE ValueProxy {
QUICHE team82dee2f2019-01-18 12:35:12 -0500216 public:
217 ~ValueProxy();
218
219 // Moves are allowed.
220 ValueProxy(ValueProxy&& other);
221 ValueProxy& operator=(ValueProxy&& other);
222
223 // Copies are not.
224 ValueProxy(const ValueProxy& other) = delete;
225 ValueProxy& operator=(const ValueProxy& other) = delete;
226
227 // Assignment modifies the underlying SpdyHeaderBlock.
bnc7f82d042020-01-03 12:18:53 -0800228 ValueProxy& operator=(quiche::QuicheStringPiece value);
QUICHE team3f57c752019-12-16 14:09:53 -0800229
bnc7f82d042020-01-03 12:18:53 -0800230 // Provides easy comparison against QuicheStringPiece.
231 bool operator==(quiche::QuicheStringPiece value) const;
QUICHE team82dee2f2019-01-18 12:35:12 -0500232
bnc44712912019-08-15 18:58:14 -0700233 std::string as_string() const;
QUICHE team82dee2f2019-01-18 12:35:12 -0500234
235 private:
236 friend class SpdyHeaderBlock;
237 friend class test::ValueProxyPeer;
238
QUICHE team8e3bb9d2019-12-06 16:03:22 -0800239 ValueProxy(SpdyHeaderBlock* block,
QUICHE team82dee2f2019-01-18 12:35:12 -0500240 SpdyHeaderBlock::MapType::iterator lookup_result,
bnc7f82d042020-01-03 12:18:53 -0800241 const quiche::QuicheStringPiece key,
QUICHE team82dee2f2019-01-18 12:35:12 -0500242 size_t* spdy_header_block_value_size);
243
QUICHE team8e3bb9d2019-12-06 16:03:22 -0800244 SpdyHeaderBlock* block_;
QUICHE team82dee2f2019-01-18 12:35:12 -0500245 SpdyHeaderBlock::MapType::iterator lookup_result_;
bnc7f82d042020-01-03 12:18:53 -0800246 quiche::QuicheStringPiece key_;
QUICHE team82dee2f2019-01-18 12:35:12 -0500247 size_t* spdy_header_block_value_size_;
248 bool valid_;
249 };
250
QUICHE team8e3bb9d2019-12-06 16:03:22 -0800251 // Allows either lookup or mutation of the value associated with a key.
bnc7f82d042020-01-03 12:18:53 -0800252 SPDY_MUST_USE_RESULT ValueProxy
253 operator[](const quiche::QuicheStringPiece key);
QUICHE team8e3bb9d2019-12-06 16:03:22 -0800254
QUICHE team82dee2f2019-01-18 12:35:12 -0500255 // Returns the estimate of dynamically allocated memory in bytes.
256 size_t EstimateMemoryUsage() const;
257
258 size_t TotalBytesUsed() const { return key_size_ + value_size_; }
259
260 private:
261 friend class test::SpdyHeaderBlockPeer;
262
dschinazib2b26152020-07-01 10:17:25 -0700263 inline iterator wrap_iterator(MapType::const_iterator inner_iterator) const {
264#if SPDY_HEADER_DEBUG
265 iterator outer_iterator(inner_iterator);
266 if (inner_iterator == map_.end()) {
267 outer_iterator.forbid_dereference();
268 }
269 return outer_iterator;
270#else // SPDY_HEADER_DEBUG
271 return iterator(inner_iterator);
272#endif // SPDY_HEADER_DEBUG
273 }
274
275 inline const_iterator wrap_const_iterator(
276 MapType::const_iterator inner_iterator) const {
277#if SPDY_HEADER_DEBUG
278 const_iterator outer_iterator(inner_iterator);
279 if (inner_iterator == map_.end()) {
280 outer_iterator.forbid_dereference();
281 }
282 return outer_iterator;
283#else // SPDY_HEADER_DEBUG
284 return iterator(inner_iterator);
285#endif // SPDY_HEADER_DEBUG
286 }
287
bnc7f82d042020-01-03 12:18:53 -0800288 void AppendHeader(const quiche::QuicheStringPiece key,
289 const quiche::QuicheStringPiece value);
290 quiche::QuicheStringPiece WriteKey(const quiche::QuicheStringPiece key);
QUICHE team82dee2f2019-01-18 12:35:12 -0500291 size_t bytes_allocated() const;
292
bnc7f82d042020-01-03 12:18:53 -0800293 // QuicheStringPieces held by |map_| point to memory owned by |storage_|.
QUICHE team8e3bb9d2019-12-06 16:03:22 -0800294 MapType map_;
QUICHE team0b745502019-12-10 11:02:08 -0800295 SpdyHeaderStorage storage_;
QUICHE team82dee2f2019-01-18 12:35:12 -0500296
297 size_t key_size_ = 0;
298 size_t value_size_ = 0;
299};
300
QUICHE team82dee2f2019-01-18 12:35:12 -0500301} // namespace spdy
302
303#endif // QUICHE_SPDY_CORE_SPDY_HEADER_BLOCK_H_