blob: a625393326c354c01c41ea2b5415e5442c27229b [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
bnc7f82d042020-01-03 12:18:53 -080016#include "net/third_party/quiche/src/common/platform/api/quiche_string_piece.h"
QUICHE team6b297002019-12-09 09:37:56 -080017#include "net/third_party/quiche/src/spdy/core/spdy_header_storage.h"
QUICHE team82dee2f2019-01-18 12:35:12 -050018#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 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
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
bnc7f82d042020-01-03 12:18:53 -080034// 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 team82dee2f2019-01-18 12:35:12 -050037// 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.
41class SPDY_EXPORT_PRIVATE SpdyHeaderBlock {
42 private:
QUICHE team82dee2f2019-01-18 12:35:12 -050043 // 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 team6b297002019-12-09 09:37:56 -080047 HeaderValue(SpdyHeaderStorage* storage,
bnc7f82d042020-01-03 12:18:53 -080048 quiche::QuicheStringPiece key,
49 quiche::QuicheStringPiece initial_value);
QUICHE team82dee2f2019-01-18 12:35:12 -050050
51 // Moves are allowed.
52 HeaderValue(HeaderValue&& other);
53 HeaderValue& operator=(HeaderValue&& other);
54
QUICHE team0b745502019-12-10 11:02:08 -080055 void set_storage(SpdyHeaderStorage* storage);
56
QUICHE team82dee2f2019-01-18 12:35:12 -050057 // 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.
bnc7f82d042020-01-03 12:18:53 -080064 void Append(quiche::QuicheStringPiece fragment);
QUICHE team82dee2f2019-01-18 12:35:12 -050065
bnc7f82d042020-01-03 12:18:53 -080066 quiche::QuicheStringPiece value() const { return as_pair().second; }
67 const std::pair<quiche::QuicheStringPiece, quiche::QuicheStringPiece>&
68 as_pair() const;
QUICHE team82dee2f2019-01-18 12:35:12 -050069
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.
bnc7f82d042020-01-03 12:18:53 -080077 quiche::QuicheStringPiece ConsolidatedValue() const;
QUICHE team82dee2f2019-01-18 12:35:12 -050078
QUICHE team6b297002019-12-09 09:37:56 -080079 mutable SpdyHeaderStorage* storage_;
bnc7f82d042020-01-03 12:18:53 -080080 mutable std::vector<quiche::QuicheStringPiece> fragments_;
QUICHE team82dee2f2019-01-18 12:35:12 -050081 // The first element is the key; the second is the consolidated value.
bnc7f82d042020-01-03 12:18:53 -080082 mutable std::pair<quiche::QuicheStringPiece, quiche::QuicheStringPiece>
83 pair_;
QUICHE team82dee2f2019-01-18 12:35:12 -050084 size_t size_ = 0;
85 size_t separator_size_ = 0;
86 };
87
bnc7f82d042020-01-03 12:18:53 -080088 typedef SpdyLinkedHashMap<quiche::QuicheStringPiece,
QUICHE teambbce6032020-01-03 07:42:10 -080089 HeaderValue,
QUICHE teama0795f02020-01-03 13:01:12 -080090 SpdyStringPieceCaseHash,
91 SpdyStringPieceCaseEq>
QUICHE team82dee2f2019-01-18 12:35:12 -050092 MapType;
93
94 public:
bnc7f82d042020-01-03 12:18:53 -080095 typedef std::pair<quiche::QuicheStringPiece, quiche::QuicheStringPiece>
96 value_type;
QUICHE team82dee2f2019-01-18 12:35:12 -050097
bnc7f82d042020-01-03 12:18:53 -080098 // Provides iteration over a sequence of std::pair<QuicheStringPiece,
99 // QuicheStringPiece>, even though the underlying MapType::value_type is
QUICHE team82dee2f2019-01-18 12:35:12 -0500100 // 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.
bnc7f82d042020-01-03 12:18:53 -0800106 typedef std::pair<quiche::QuicheStringPiece, quiche::QuicheStringPiece>
107 value_type;
QUICHE team82dee2f2019-01-18 12:35:12 -0500108 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 team82dee2f2019-01-18 12:35:12 -0500145 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.
bnc44712912019-08-15 18:58:14 -0700159 std::string DebugString() const;
QUICHE team82dee2f2019-01-18 12:35:12 -0500160
QUICHE team8e3bb9d2019-12-06 16:03:22 -0800161 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(); }
bnc7f82d042020-01-03 12:18:53 -0800167 iterator find(quiche::QuicheStringPiece key) {
168 return iterator(map_.find(key));
169 }
170 const_iterator find(quiche::QuicheStringPiece key) const {
QUICHE team8e3bb9d2019-12-06 16:03:22 -0800171 return const_iterator(map_.find(key));
QUICHE team82dee2f2019-01-18 12:35:12 -0500172 }
bnc7f82d042020-01-03 12:18:53 -0800173 void erase(quiche::QuicheStringPiece key);
QUICHE team82dee2f2019-01-18 12:35:12 -0500174
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.
bnc7f82d042020-01-03 12:18:53 -0800188 void AppendValueOrAddHeader(const quiche::QuicheStringPiece key,
189 const quiche::QuicheStringPiece value);
QUICHE team82dee2f2019-01-18 12:35:12 -0500190
QUICHE team82dee2f2019-01-18 12:35:12 -0500191 // This object provides automatic conversions that allow SpdyHeaderBlock to be
bnc44712912019-08-15 18:58:14 -0700192 // nearly a drop-in replacement for
193 // SpdyLinkedHashMap<std::string, std::string>.
QUICHE team6b297002019-12-09 09:37:56 -0800194 // It reads data from or writes data to a SpdyHeaderStorage.
QUICHE team82dee2f2019-01-18 12:35:12 -0500195 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.
bnc7f82d042020-01-03 12:18:53 -0800208 ValueProxy& operator=(quiche::QuicheStringPiece value);
QUICHE team3f57c752019-12-16 14:09:53 -0800209
bnc7f82d042020-01-03 12:18:53 -0800210 // Provides easy comparison against QuicheStringPiece.
211 bool operator==(quiche::QuicheStringPiece value) const;
QUICHE team82dee2f2019-01-18 12:35:12 -0500212
bnc44712912019-08-15 18:58:14 -0700213 std::string as_string() const;
QUICHE team82dee2f2019-01-18 12:35:12 -0500214
215 private:
216 friend class SpdyHeaderBlock;
217 friend class test::ValueProxyPeer;
218
QUICHE team8e3bb9d2019-12-06 16:03:22 -0800219 ValueProxy(SpdyHeaderBlock* block,
QUICHE team82dee2f2019-01-18 12:35:12 -0500220 SpdyHeaderBlock::MapType::iterator lookup_result,
bnc7f82d042020-01-03 12:18:53 -0800221 const quiche::QuicheStringPiece key,
QUICHE team82dee2f2019-01-18 12:35:12 -0500222 size_t* spdy_header_block_value_size);
223
QUICHE team8e3bb9d2019-12-06 16:03:22 -0800224 SpdyHeaderBlock* block_;
QUICHE team82dee2f2019-01-18 12:35:12 -0500225 SpdyHeaderBlock::MapType::iterator lookup_result_;
bnc7f82d042020-01-03 12:18:53 -0800226 quiche::QuicheStringPiece key_;
QUICHE team82dee2f2019-01-18 12:35:12 -0500227 size_t* spdy_header_block_value_size_;
228 bool valid_;
229 };
230
QUICHE team8e3bb9d2019-12-06 16:03:22 -0800231 // Allows either lookup or mutation of the value associated with a key.
bnc7f82d042020-01-03 12:18:53 -0800232 SPDY_MUST_USE_RESULT ValueProxy
233 operator[](const quiche::QuicheStringPiece key);
QUICHE team8e3bb9d2019-12-06 16:03:22 -0800234
QUICHE team82dee2f2019-01-18 12:35:12 -0500235 // 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
bnc7f82d042020-01-03 12:18:53 -0800243 void AppendHeader(const quiche::QuicheStringPiece key,
244 const quiche::QuicheStringPiece value);
245 quiche::QuicheStringPiece WriteKey(const quiche::QuicheStringPiece key);
QUICHE team82dee2f2019-01-18 12:35:12 -0500246 size_t bytes_allocated() const;
247
bnc7f82d042020-01-03 12:18:53 -0800248 // QuicheStringPieces held by |map_| point to memory owned by |storage_|.
QUICHE team8e3bb9d2019-12-06 16:03:22 -0800249 MapType map_;
QUICHE team0b745502019-12-10 11:02:08 -0800250 SpdyHeaderStorage storage_;
QUICHE team82dee2f2019-01-18 12:35:12 -0500251
252 size_t key_size_ = 0;
253 size_t value_size_ = 0;
254};
255
QUICHE team82dee2f2019-01-18 12:35:12 -0500256} // namespace spdy
257
258#endif // QUICHE_SPDY_CORE_SPDY_HEADER_BLOCK_H_