xref: /aosp_15_r20/external/cronet/net/third_party/quiche/src/quiche/spdy/core/hpack/hpack_static_table.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2014 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 #include "quiche/spdy/core/hpack/hpack_static_table.h"
6 
7 #include <cstddef>
8 #include <string>
9 #include <utility>
10 
11 #include "quiche/common/platform/api/quiche_logging.h"
12 #include "quiche/spdy/core/hpack/hpack_constants.h"
13 #include "quiche/spdy/core/hpack/hpack_entry.h"
14 
15 namespace spdy {
16 
17 HpackStaticTable::HpackStaticTable() = default;
18 
19 HpackStaticTable::~HpackStaticTable() = default;
20 
Initialize(const HpackStaticEntry * static_entry_table,size_t static_entry_count)21 void HpackStaticTable::Initialize(const HpackStaticEntry* static_entry_table,
22                                   size_t static_entry_count) {
23   QUICHE_CHECK(!IsInitialized());
24 
25   static_entries_.reserve(static_entry_count);
26 
27   for (const HpackStaticEntry* it = static_entry_table;
28        it != static_entry_table + static_entry_count; ++it) {
29     std::string name(it->name, it->name_len);
30     std::string value(it->value, it->value_len);
31     static_entries_.push_back(HpackEntry(std::move(name), std::move(value)));
32   }
33 
34   // |static_entries_| will not be mutated any more.  Therefore its entries will
35   // remain stable even if the container does not have iterator stability.
36   int insertion_count = 0;
37   for (const auto& entry : static_entries_) {
38     auto result = static_index_.insert(std::make_pair(
39         HpackLookupEntry{entry.name(), entry.value()}, insertion_count));
40     QUICHE_CHECK(result.second);
41 
42     // Multiple static entries may have the same name, so inserts may fail.
43     static_name_index_.insert(std::make_pair(entry.name(), insertion_count));
44 
45     ++insertion_count;
46   }
47 }
48 
IsInitialized() const49 bool HpackStaticTable::IsInitialized() const {
50   return !static_entries_.empty();
51 }
52 
53 }  // namespace spdy
54